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())
try:
__log = "[S3I]: Search the given attribute path: {}".format(attribute_path)
__log = "[S3I]: Search for the given attribute path: {}".format(attribute_path)
APP_LOGGER.info(__log)
self._uriToData(attribute_path)
self.dt_json[attribute_path] = new_value
result = True
except KeyError:
old_value = self._uriToData(attribute_path)
ins = self._uriToIns(attribute_path)
APP_LOGGER.info("[S3I]: Change value from {} to {}".format(old_value, new_value))
result = self._set_value_req(ins, new_value, attribute_path)
except:
__log = "[S3I]: Invalid attribute path"
APP_LOGGER.critical(__log)
result = False
......@@ -786,6 +789,65 @@ def on_set_value_request(self, msg):
__log = "[S3I[Broker]: " + res.text
APP_LOGGER.info(__log)
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:
APP_LOGGER.info("Not allowed to set attribute {}".format(attribute_path))
return False
else:
if hasattr(ins, attr_list[attr_list.__len__() - 1]):
setattr(ins, attr_list[attr_list.__len__() - 1], new_value)
return True
APP_LOGGER.info("{} is not one of the attributes".format(attr_list[attr_list.__len__() - 1]))
return False
else:
for key in new_value.keys():
if hasattr(ins, key):
setattr(ins, key, new_value[key])
else:
APP_LOGGER.info("{} 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]
uri_list.pop(0)
return self._getInstance(source_obj.features[_uri], uri_list)
elif uri_list[0] == "features":
uri_list.pop(0)
return self._getInstance(source_obj, uri_list)
elif uri_list[0] == "targets":
uri_list.pop(0)
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]:
uri_list.pop(0)
return self._getInstance(source_obj.targets[key], uri_list)
elif uri_list[0] == "subFeatures":
uri_list.pop(0)
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]:
uri_list.pop(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"
APP_LOGGER.info(__log)
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)
APP_LOGGER.info(__log)
def to_dir_json(self):
......
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