def protocol(self): try: key = OPTION["protocol"] if self._data_json != None and self.has_key( self._data_json, key) and self._data_json[key] != None: return self._data_json[key] else: return None except: raise (IoTConnectSDKException("04", "protocol"))
def __exit__(self, exc_type, exc_value, exc_tb): try: for attr in self.attributes: if self.has_key(attr, "evaluation"): attr["evaluation"].destroyed() del attr["evaluation"] if self._client and hasattr(self._client, 'Disconnect'): self._client.Disconnect() except: raise (IoTConnectSDKException("00", "Exit"))
def rules(self): try: key = OPTION["rule"] if self._data_json != None and self.has_key( self._data_json, key) and self._data_json[key] != None: return self._data_json[key] else: return [] except: raise (IoTConnectSDKException("04", "rules"))
def get_config(self): try: self._config = None _path = os.path.abspath(os.path.dirname(__file__)) _config_path = os.path.join(_path, "assets/config.json") with open(_config_path) as config_file: self._config = json.loads(config_file.read()) self.get_properties() except: raise (IoTConnectSDKException("01", "Config file"))
def get_properties(self): try: _properties = None _path = os.path.join(sys.path[0], 'properties.json') if _path != None and _path != "" and os.path.isfile(_path): with open(_path) as properties_file: _properties = json.loads(properties_file.read()) if _properties == None: raise (IoTConnectSDKException("01", "Properties file")) if _properties != None: for prop in _properties: self._config[prop] = _properties[prop] except Exception as ex: raise (ex)
def _command_template(self): try: data = { "cpId": "", "guid": "", "uniqueId": "", "command": "", "ack": True, "ackId": None, "cmdType": CMDTYPE["DCOMM"] } if self._data_json != None: data["cpId"] = self._data_json["cpId"] for d in self.devices: data["guid"] = "" data["uniqueId"] = d["id"] return data except: raise (IoTConnectSDKException("07", "command"))
def _data_template(self): try: data = { "cpId": "", "t": "", "mt": "", "d": [], "dtg": "", "sdk": { "l": self._config["sdk_lang"], "v": self._config["sdk_version"], "e": self._config["env"] } } data["t"] = self._timestamp if self._data_json != None: data["cpId"] = self._data_json["cpId"] data["dtg"] = self._data_json["dtg"] return data except: raise (IoTConnectSDKException("07", "telementry"))
def __init__(self, cpId, uniqueId, scopeId, listner, listner_twin, env="PROD"): self._lock = threading.Lock() if sys.platform == 'win32': self.win_user() elif sys.platform == 'linux2': self.linux_user() #ByPass SSL Verification if (not os.environ.get('PYTHONHTTPSVERIFY', '') and getattr(ssl, '_create_unverified_context', None)): ssl._create_default_https_context = ssl._create_unverified_context self.get_config() if self._config == None: raise (IoTConnectSDKException("01", "Config settings")) if not self.is_not_blank(cpId): raise (IoTConnectSDKException("01", "CPID")) if not self.is_not_blank(uniqueId): raise (IoTConnectSDKException("01", "Unique Id")) if not self.is_not_blank(scopeId): raise (IoTConnectSDKException("01", "Scope Id")) self._cpId = cpId self._uniqueId = uniqueId self._scope_id = scopeId self._registration_id = str(cpId + "-" + uniqueId) self._global_prov_url = str(self._config["api_global_prov_url"]) self._iot_hub_url = None self._reg_result = None self._config["env"] = env self._offlineClient = offlineclient(self._config, self.send_offline_msg_to_broker) self._ruleEval = rule_evaluation(self.send_rule_data, self.command_sender) if listner != None: self._listner_callback = listner if listner_twin != None: self._listner_twin_callback = listner_twin self._base_url = self.get_base_url(self._cpId) if self._base_url != None: self.process_sync("all") try: while self._process_start == False: time.sleep(0.5) except KeyboardInterrupt: sys.exit(0) else: raise (IoTConnectSDKException("02"))
def process_sync(self, option): try: url = self._base_url req_json = {} req_json["uniqueId"] = self._uniqueId req_json["cpId"] = self._cpId req_json["option"] = {} if option == "all": req_json["option"]["attribute"] = True req_json["option"]["setting"] = True req_json["option"]["protocol"] = True req_json["option"]["device"] = True req_json["option"]["sdkConfig"] = True req_json["option"]["rule"] = True else: req_json["option"][option] = True body = json.dumps(req_json) response = self.post_call(url + 'sync', body) isReChecking = False if option == "all": if response == None: raise (IoTConnectSDKException("01", "Sync response")) elif response != None and self.has_key(response, "status"): raise (IoTConnectSDKException("03", response["message"])) else: response = response["d"] if response["at"] == 4: if response["ds"] == RCCode["DEV_NOT_REG"]: is_registered = self.init_provisioning_client() if is_registered == True: print("\nDevice registered successfully!") isReChecking = True else: if response["rc"] != RCCode["OK"]: isReChecking = True else: if response == None: isReChecking = True elif response != None and self.has_key(response, "status"): isReChecking = True else: response = response["d"] if response["rc"] != RCCode["OK"]: isReChecking = True if isReChecking: print("\nRe-Checking...") _tProcess = threading.Thread(target=self.reset_process_sync, args=[option]) _tProcess.setName("PSYNC") _tProcess.daemon = True _tProcess.start() return else: self._process_start = False # Pre Process self.clear_object(option) #print(json.dumps(response)) # -------------------------------- if option == "all": self._data_json = response else: key = OPTION[option] if self._data_json and self.has_key(self._data_json, key): self._data_json[key] = response[key] print("\n" + option + " updated sucessfully...") if option == "all" or option == "attribute": for attr in self.attributes: attr["evaluation"] = data_evaluation( self.isEdge, attr, self.send_edge_data) if option == "all" or option == "protocol": self.init_protocol() self._process_start = True except Exception as ex: raise (ex)