diff --git a/ml/thing.py b/ml/thing.py index c22becf722c5bc915dd1c5d127b44278c3c509ec..53b2961d76211abcc6ec778b37f54a7abdca8866 100644 --- a/ml/thing.py +++ b/ml/thing.py @@ -57,6 +57,7 @@ def __init__( self.repo_json = dict() self.dir_json = dict() + self.dt_json = dict() attributes = model.get("attributes", None) self.__name = "" @@ -114,6 +115,7 @@ def run_forever(self): print("[S³I]: Launch {}{}{}".format(BColors.OKGREEN, self.name, BColors.ENDC)) self.__connect_with_idp() + threading.Thread(target=self.__json_syn).start() threading.Thread(target=self.__dir_syn).start() if self.__is_repo: threading.Thread(target=self.__repo_syn).start() @@ -122,10 +124,18 @@ def run_forever(self): def add_user_def(func): threading.Thread(target=func).start() - def __dir_syn(self): + def __json_syn(self, freq=0.1): while True: try: - time.sleep(0.1) + time.sleep(freq) + self.to_json() + except: + continue + + def __dir_syn(self, freq=0.1): + while True: + try: + time.sleep(freq) old_dir_json = self.dir_json self.to_dir_json() if self.dir_json == old_dir_json: @@ -135,10 +145,10 @@ def __dir_syn(self): except: continue - def __repo_syn(self): + def __repo_syn(self, freq=0.1): while self.__is_repo: try: - time.sleep(0.1) + time.sleep(freq) old_repo_json = self.repo_json self.to_repo_json() if self.repo_json == old_repo_json: @@ -304,17 +314,17 @@ def on_get_value_request(self, msg): def _uriToData(self, uri): if uri == "": - return self.repo_json + return self.dt_json else: uri_list = uri.split("/") if uri_list[0] == "features": try: - return self.repo_json[uri] + return self.dt_json[uri] except KeyError: return "invalid attribute path" try: - self._getValue(self.repo_json, uri_list) + self._getValue(self.dt_json, uri_list) except: return "invalid attribute path" if self.__resGetValue.__len__() == 0: @@ -334,7 +344,7 @@ def _getValue(self, source, uri_list): try: stringValue_split = value.split(":") if stringValue_split[0] == "ditto-feature": - value = self.repo_json["features"][stringValue_split[1]]["properties"][uri_list[0]] + value = self.dt_json["features"][stringValue_split[1]]["properties"][uri_list[0]] except: pass self.__resGetValue.append(value) @@ -368,7 +378,7 @@ def _getValue(self, source, uri_list): try: stringValue_split = value.split(":") if stringValue_split[0] == "ditto-feature": - value = self.repo_json["features"][stringValue_split[1]][ + value = self.dt_json["features"][stringValue_split[1]][ "properties" ][uri_list[0]] except: @@ -496,26 +506,29 @@ def to_dir_json(self): return self.dir_json - def to_repo_json(self, path=None, value=None): - if path is None and value is None: - self.repo_json = { - "thingId": self.thing_id, - "policyId": self.policy_id, - "attributes": { - "class": "ml40::Thing", - "name": self.name, - } - } - if self.roles: - self.repo_json["attributes"]["roles"] = list() - if self.features: - self.repo_json["attributes"]["features"] = list() - for key in self.roles.keys(): - self.repo_json["attributes"]["roles"].append(self.roles[key].to_json()) - for key in self.features: - self.repo_json["attributes"]["features"].append(self.features[key].to_json()) + def to_repo_json(self): + self.repo_json = self.dt_json return self.repo_json + def to_json(self): + self.dt_json = { + "thingId": self.thing_id, + "policyId": self.policy_id, + "attributes": { + "class": "ml40::Thing", + "name": self.name, + } + } + if self.roles: + self.dt_json["attributes"]["roles"] = list() + if self.features: + self.dt_json["attributes"]["features"] = list() + for key in self.roles.keys(): + self.dt_json["attributes"]["roles"].append(self.roles[key].to_json()) + for key in self.features: + self.dt_json["attributes"]["features"].append(self.features[key].to_json()) + return self.dt_json + def to_subthing_json(self): json_out = { "class": "ml40::Thing",