Commit 0e013aff authored by Jiahang Chen's avatar Jiahang Chen
Browse files

add ditto features

parent e677d636
Pipeline #388769 passed with stages
in 1 minute and 5 seconds
from abc import ABC
class ditto_feature(ABC):
def __init__(self, id, key, value):
self.__id = id
self.__key = key
self.__value = value
@property
def id(self):
return self.__id
@id.setter
def id(self, value):
self.__id = value
@property
def key(self):
return self.__key
@key.setter
def key(self, value):
self.__key = value
def json_out(self):
return {
"properties": {
self.__key: self.__value
}
}
\ No newline at end of file
......@@ -2,7 +2,7 @@
from ml.app_logger import APP_LOGGER
from ml.tools import remove_namespace
from ml.ditto_feature import ditto_feature
from ml.thing import Thing
from ml.ml40.roles.servives.service import Service
from ml.ml40.roles.hmis.app import App
......@@ -206,18 +206,21 @@ def build_sub_thing(feature_ins, feature):
feature_ins.targets[sub_thing_name] = sub_thing_ref
def build(thing, attributes):
def build(thing, model):
"""
Builds a ml40 thing instance
:param thing: ml40 thing instance
:type thing: object
:param attributes: attributes in ml40 thing JSON
:type attributes: dict
:param model: model in ml40 thing JSON
:type model: dict
"""
if not isinstance(attributes, dict):
APP_LOGGER.critical("Attributes is no valid JSON")
attributes = model.get("attributes", None)
ditto_features = model.get("features", None)
if not isinstance(model, dict):
APP_LOGGER.critical("model is no valid JSON")
return
roles = attributes.get("roles", [])
for role in roles:
......@@ -229,6 +232,19 @@ def build(thing, attributes):
feature_ins = build_feature(feature=feature)
thing.features[feature.get("class")] = feature_ins
if ditto_features is not None:
build_ditto_features(thing, ditto_features)
def build_ditto_features(thing, ditto_features):
for id in ditto_features.keys():
ditto_feature.id = id
for key in ditto_features[id]["properties"]:
ditto_feature.key = key
ditto_feature.value = ditto_features[id]["properties"][key]
thing.ditto_features[ditto_feature.id] = ditto_feature
def build_role(role):
"""
......@@ -331,15 +347,9 @@ def create_thing(model, grant_type="password",
"""
attributes = model.get("attributes", None)
if attributes is None:
sys.exit("Incomplete model: attributes missing!")
sys.exit("attributes are none")
thing_type = remove_namespace(attributes.get("class", ""))
roles = attributes.get("roles", None)
if roles is None:
sys.exit("Incomplete model: roles missing!")
thing_name = attributes.get("name", "")
APP_LOGGER.info("Build digital twin {} with id {}".format(thing_name, model.get("thingId", "")))
......@@ -355,5 +365,6 @@ def create_thing(model, grant_type="password",
is_broker=is_broker,
is_repo=is_repo
)
build(thing_ref, attributes)
build(thing_ref, model)
return thing_ref
......@@ -96,6 +96,7 @@ def __init__(
self.__name = ""
self.__roles = {}
self.__features = {}
self.__ditto_features = {}
# ??? Is this property necessary? Only used as return value in _getValue()
self.__resGetValue = list()
if attributes:
......@@ -112,6 +113,15 @@ def model(self):
return self.__model
@property
def ditto_features(self):
return self.__ditto_features
@ditto_features.setter
def ditto_features(self, value):
self.__ditto_features = value
@property
def features(self):
"""Returns thing's features.
......@@ -889,6 +899,8 @@ def to_json(self):
self.dt_json["attributes"]["roles"] = list()
if self.features:
self.dt_json["attributes"]["features"] = list()
if self.ditto_features:
self.dt_json["features"] = self.ditto_features
for key in self.roles.keys():
self.dt_json["attributes"]["roles"].append(self.roles[key].to_json())
for key in self.features.keys():
......
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