import network import json class DeviceInfo: wlan = network.WLAN(network.STA_IF) app_version = "1.0.0" def get_macaddress(self): return self._format_mac(self.wlan.config('mac').hex()) def get_ipaddress(self): return self.wlan.ifconfig()[0] def get_all_device_infos(self): return { 'name': self.get_name(), 'mac_address': self.get_macaddress(), 'ip_address': self.get_ipaddress(), 'token': self.get_token()} def config(self, filepath=None): return self._loadJsonConfig(filepath) def app_config(self): import gs.config.app as app_config return app_config def server_url(self): return self.app_config().api_url def get_token(self): return self.config()['token'] if self.config() and self.config()['token'] else '' def get_name(self): return self.config()['name'] if self.config() and self.config()['name'] else '' def get_device_id(self): return self.config()['device_id'] if self.config() and self.config()['device_id'] else '' def _format_mac(self, mac): # Split the MAC address into pairs of two characters each pairs = [mac[i:i+2] for i in range(0, len(mac), 2)] # Join the pairs with colons to create the formatted MAC address formatted_mac = ":".join(pairs) return formatted_mac def _loadJsonConfig(self, filepath=None): filepath = filepath if filepath else '/gs/config/device_config.json' try: file = open(filepath, "r") json_content = file.read() return json.loads(json_content) except OSError: # open failed print("File not found: ", filepath) return None #with open(filepath, 'r') as file: # json_content = file.read() #return json.loads(json_content)