Commit 9a57aa06 authored by Jiahang Chen's avatar Jiahang Chen
Browse files

fix dir json problem

parent c20d6bcc
{"thingId": "s3i:b6d1cc6d-896c-40fe-9403-b5b7682b1d03", "policyId": "s3i:b6d1cc6d-896c-40fe-9403-b5b7682b1d03", "attributes": {"class": "ml40::Thing", "name": "my_dt_harvester", "roles": [{"class": "fml40::Harvester"}], "features": [{"class": "fml40::ProvidesProductionData"}, {"class": "fml40::AcceptsFellingJobs"}, {"class": "ml40::Composite", "targets": [{"class": "ml40::Thing", "name": "my_engine", "roles": [{"class": "ml40::Engine"}], "features": [{"class": "ml40::RotationalSpeed", "rpm": 2001}]}, {"class": "ml40::Thing", "name": "my_bord_computer", "roles": [{"class": "ml40::MachineUI"}], "features": []}]}]}}
\ No newline at end of file
......@@ -5,6 +5,7 @@
from s3i import IdentityProvider, TokenType, GetValueReply, Directory, Repository
from s3i.broker import Broker, BrokerREST
from s3i.messages import ServiceReply
from ml.identifier import ID
from ml.tools import BColors
from ml.tools import find_broker_endpoint
from ml.app_logger import APP_LOGGER
......@@ -35,6 +36,8 @@ def __init__(
):
self.__model = model
self.__thing_id = model.get("thingId", "")
if not self.__thing_id:
self.__thing_id = ID().identifier
self.__policy_id = model.get("policyId", "")
self.__grant_type = grant_type
self.__username = username
......@@ -134,7 +137,7 @@ def __json_syn(self, freq=0.1):
def __dir_syn(self, freq=0.1):
while True:
try:
#try:
time.sleep(freq)
old_dir_json = self.dir_json
self.to_dir_json()
......@@ -142,8 +145,8 @@ def __dir_syn(self, freq=0.1):
continue
else:
self.dir.updateThingIDBased(thingID=self.thing_id, payload=self.dir_json)
except:
continue
# except:
# continue
def __repo_syn(self, freq=0.1):
while self.__is_repo:
......@@ -464,7 +467,6 @@ def on_get_value_reply(self, msg):
BColors.ENDC)
)
def on_service_reply(self, msg):
print(
BColors.OKBLUE
......@@ -491,19 +493,39 @@ def to_dir_json(self):
self.dir_json["thingId"] = self.thing_id
if self.policy_id is not None:
self.dir_json["policyId"] = self.policy_id
self.dir_json["attributes"]["class"] = "ml40::Thing"
self.dir_json["attributes"]["name"] = self.name
if self.roles:
self.dir_json["attributes"]["roles"] = list()
if self.features:
self.dir_json["attributes"]["features"] = list()
if self.name is not None:
self.dir_json["attributes"]["name"] = self.name
if self.features.get("ml40::Location") is not None:
self.dir_json["attributes"]["location"] = {
"longitude": self.features.get("ml40::Location").to_json()["longitude"],
"latitude": self.features.get("ml40::Location").to_json()["latitude"]
}
self.dir_json["attributes"]["dataModel"] = "fml40"
self.dir_json["attributes"]["thingStructure"] = {
"class": "ml40::Thing",
"links": []
}
for key in self.roles.keys():
self.dir_json["attributes"]["roles"].append(self.roles[key].to_json())
for key in self.features.keys():
self.dir_json["attributes"]["features"].append(self.features[key].to_json())
role_entry = {
"association": "roles",
"target": self.roles[key].to_json()
}
self.dir_json["attributes"]["thingStructure"]["links"].append(role_entry)
for key in self.features.keys():
feature_entry = {
"association": "features",
"target": {
"class": self.features[key].class_name,
}
}
# if the feature has targets, like ml40::Composite
if hasattr(self.features[key], 'targets'):
feature_entry["target"]["links"] = list()
for target in self.features[key].targets.keys():
target_json = self.features[key].targets[target].to_subthing_dir_json()
feature_entry["target"]["links"].append(target_json)
self.dir_json["attributes"]["thingStructure"]["links"].append(feature_entry)
return self.dir_json
def to_repo_json(self):
......@@ -532,6 +554,7 @@ def to_json(self):
def to_subthing_json(self):
json_out = {
"class": "ml40::Thing",
"identifier": self.thing_id,
"name": self.name,
"roles": [],
"features": []
......@@ -541,3 +564,23 @@ def to_subthing_json(self):
for key in self.features.keys():
json_out["features"].append(self.features[key].to_json())
return json_out
def to_subthing_dir_json(self):
json_out = {
"class": "ml40::Thing",
"identifier": self.thing_id,
"links": []
}
for key in self.roles.keys():
role_entry = {
"association": "roles",
"target":self.roles[key].to_json()
}
json_out["links"].append(role_entry)
for key in self.features.keys():
feature_entry = {
"association": "features",
"target": self.features[key].to_json()
}
json_out["links"].append(feature_entry)
return json_out
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