Commit fcf0aa04 authored by Jiahang Chen's avatar Jiahang Chen
Browse files

add sync for the original thing json model

parent d96ffe74
Pipeline #349758 passed with stage
in 13 seconds
...@@ -57,6 +57,7 @@ def __init__( ...@@ -57,6 +57,7 @@ def __init__(
self.repo_json = dict() self.repo_json = dict()
self.dir_json = dict() self.dir_json = dict()
self.dt_json = dict()
attributes = model.get("attributes", None) attributes = model.get("attributes", None)
self.__name = "" self.__name = ""
...@@ -114,6 +115,7 @@ def run_forever(self): ...@@ -114,6 +115,7 @@ def run_forever(self):
print("[S³I]: Launch {}{}{}".format(BColors.OKGREEN, self.name, BColors.ENDC)) print("[S³I]: Launch {}{}{}".format(BColors.OKGREEN, self.name, BColors.ENDC))
self.__connect_with_idp() self.__connect_with_idp()
threading.Thread(target=self.__json_syn).start()
threading.Thread(target=self.__dir_syn).start() threading.Thread(target=self.__dir_syn).start()
if self.__is_repo: if self.__is_repo:
threading.Thread(target=self.__repo_syn).start() threading.Thread(target=self.__repo_syn).start()
...@@ -122,10 +124,18 @@ def run_forever(self): ...@@ -122,10 +124,18 @@ def run_forever(self):
def add_user_def(func): def add_user_def(func):
threading.Thread(target=func).start() threading.Thread(target=func).start()
def __dir_syn(self): def __json_syn(self, freq=0.1):
while True: while True:
try: 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 old_dir_json = self.dir_json
self.to_dir_json() self.to_dir_json()
if self.dir_json == old_dir_json: if self.dir_json == old_dir_json:
...@@ -135,10 +145,10 @@ def __dir_syn(self): ...@@ -135,10 +145,10 @@ def __dir_syn(self):
except: except:
continue continue
def __repo_syn(self): def __repo_syn(self, freq=0.1):
while self.__is_repo: while self.__is_repo:
try: try:
time.sleep(0.1) time.sleep(freq)
old_repo_json = self.repo_json old_repo_json = self.repo_json
self.to_repo_json() self.to_repo_json()
if self.repo_json == old_repo_json: if self.repo_json == old_repo_json:
...@@ -304,17 +314,17 @@ def on_get_value_request(self, msg): ...@@ -304,17 +314,17 @@ def on_get_value_request(self, msg):
def _uriToData(self, uri): def _uriToData(self, uri):
if uri == "": if uri == "":
return self.repo_json return self.dt_json
else: else:
uri_list = uri.split("/") uri_list = uri.split("/")
if uri_list[0] == "features": if uri_list[0] == "features":
try: try:
return self.repo_json[uri] return self.dt_json[uri]
except KeyError: except KeyError:
return "invalid attribute path" return "invalid attribute path"
try: try:
self._getValue(self.repo_json, uri_list) self._getValue(self.dt_json, uri_list)
except: except:
return "invalid attribute path" return "invalid attribute path"
if self.__resGetValue.__len__() == 0: if self.__resGetValue.__len__() == 0:
...@@ -334,7 +344,7 @@ def _getValue(self, source, uri_list): ...@@ -334,7 +344,7 @@ def _getValue(self, source, uri_list):
try: try:
stringValue_split = value.split(":") stringValue_split = value.split(":")
if stringValue_split[0] == "ditto-feature": 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: except:
pass pass
self.__resGetValue.append(value) self.__resGetValue.append(value)
...@@ -368,7 +378,7 @@ def _getValue(self, source, uri_list): ...@@ -368,7 +378,7 @@ def _getValue(self, source, uri_list):
try: try:
stringValue_split = value.split(":") stringValue_split = value.split(":")
if stringValue_split[0] == "ditto-feature": if stringValue_split[0] == "ditto-feature":
value = self.repo_json["features"][stringValue_split[1]][ value = self.dt_json["features"][stringValue_split[1]][
"properties" "properties"
][uri_list[0]] ][uri_list[0]]
except: except:
...@@ -496,26 +506,29 @@ def to_dir_json(self): ...@@ -496,26 +506,29 @@ def to_dir_json(self):
return self.dir_json return self.dir_json
def to_repo_json(self, path=None, value=None): def to_repo_json(self):
if path is None and value is None: self.repo_json = self.dt_json
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())
return self.repo_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): def to_subthing_json(self):
json_out = { json_out = {
"class": "ml40::Thing", "class": "ml40::Thing",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment