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

add callback function for setValue msg

parent b1ba2165
Pipeline #459654 passed with stages
in 52 seconds
......@@ -97,6 +97,7 @@ def __init__(
self.__ditto_features = {}
# ??? Is this property necessary? Only used as return value in _getValue()
self.__resGetValue = list()
self.__source_obj = None
if attributes:
self.__name = attributes.get("name", "")
......@@ -756,12 +757,14 @@ def on_set_value_request(self, msg):
reply_msg_uuid = "s3i:" + str(uuid.uuid4())
__log = "[S3I]: Search the given attribute path: {}".format(attribute_path)
__log = "[S3I]: Search for the given attribute path: {}".format(attribute_path)
self.dt_json[attribute_path] = new_value
result = True
except KeyError:
old_value = self._uriToData(attribute_path)
ins = self._uriToIns(attribute_path)"[S3I]: Change value from {} to {}".format(old_value, new_value))
result = self._set_value_req(ins, new_value, attribute_path)
__log = "[S3I]: Invalid attribute path"
result = False
......@@ -786,6 +789,65 @@ def on_set_value_request(self, msg):
__log = "[S3I[Broker]: " + res.text
def _set_value_req(self, ins, new_value, attribute_path):
if not isinstance(new_value, dict):
attr_list = attribute_path.split("/")
if attr_list.__len__() <= 2:"Not allowed to set attribute {}".format(attribute_path))
return False
if hasattr(ins, attr_list[attr_list.__len__() - 1]):
setattr(ins, attr_list[attr_list.__len__() - 1], new_value)
return True"{} is not one of the attributes".format(attr_list[attr_list.__len__() - 1]))
return False
for key in new_value.keys():
if hasattr(ins, key):
setattr(ins, key, new_value[key])
else:"{} is not one of the attributes".format(key))
return False
return True
def _uriToIns(self, uri):
if not uri:
return None
uri_list = uri.split("/")
uri_list.pop(0) # delete first element "attributes"
return self._getInstance(self, uri_list)
def _getInstance(self, source_obj, uri_list):
if uri_list.__len__() == 0 or uri_list.__len__() == 1:
### the original uri was "attributes/features"
return source_obj
if "ml40" in uri_list[0]:
_uri = uri_list[0]
return self._getInstance(source_obj.features[_uri], uri_list)
elif uri_list[0] == "features":
return self._getInstance(source_obj, uri_list)
elif uri_list[0] == "targets":
for key in source_obj.targets.keys():
subthing_dict = source_obj.targets[key].to_subthing_json()
if subthing_dict.get("name", "") == uri_list[0] or subthing_dict.get("identifier", "") == uri_list[0] \
or subthing_dict.get("class", "") == uri_list[0]:
return self._getInstance(source_obj.targets[key], uri_list)
elif uri_list[0] == "subFeatures":
for key in source_obj.subFeatures.keys():
subfeature_dict = source_obj.subFeatures[key].to_json()
if subfeature_dict.get("name", "") == uri_list[0] or subfeature_dict.get("identifier", "")==uri_list[0] \
or subfeature_dict.get("class", "") == uri_list[0]:
return self._getInstance(source_obj.subFeatures[key], uri_list)
def on_get_value_reply(self, msg):
"""Handles incoming S³I-B GetValueReply. Prints the content of msg to stdout.
......@@ -828,7 +890,7 @@ def on_set_value_reply(self, msg):
__log = "[S3I][Broker]: You have received a S3I-B SetValueReply"
result = msg.get("ok", None)
__log = "[S3I][Broker]: The queried value is: {0}".format(result)
__log = "[S3I][Broker]: The status of value setting: {0}".format(result)
def to_dir_json(self):
Supports Markdown
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