diff --git a/ml/thing.py b/ml/thing.py index 3c92b6c1df8172c38e1382b1b9dd0e7a6ee95415..4fed98b8c18e0a95b2e2d44c2d19a59aaa46df2b 100644 --- a/ml/thing.py +++ b/ml/thing.py @@ -299,6 +299,7 @@ def on_get_value_request(self, msg): value = self._uriToData(attribute_path) except KeyError: value = "invalid attribute path" + APP_LOGGER.critical(value) get_value_reply.fillGetValueReply( senderUUID=self.thing_id, @@ -416,6 +417,7 @@ def on_service_request(self, body_json): ) service_type = body_json.get("serviceType") parameters = body_json.get("parameters") + service_reply = ServiceReply() service_functionality = service_type.split('/')[0] service_functionality_obj = self.features.get(service_functionality) if service_functionality_obj is None: @@ -423,46 +425,79 @@ def on_service_request(self, body_json): "Functionality %s is not one of the built-in functionalities in %s!" % ( service_functionality, self.name) ) - else: - # TODO: Call right functionality. - print( - BColors.OKBLUE - + "[S³I][Broker]" - + BColors.ENDC - + ": Execute the function {0} of the class {1}.".format(service_type.split('/')[1], - service_type.split('/')[0]) - ) - method = getattr(service_functionality_obj, service_type.split('/')[1]) - result = method(**parameters) - - if isinstance(result, bool): - result = {"ok": result} - - service_reply = ServiceReply() service_reply.fillServiceReply( senderUUID=self.thing_id, receiverUUID=body_json.get("sender", None), serviceType=body_json.get("serviceType", None), - results=result, + results={"error": "invalid functionalities (serviceType) {}".format(service_functionality)}, replyingToUUID=body_json.get("identifier", None), msgUUID="s3i:{}".format(uuid.uuid4()) ) - res = self.broker.send(receiver_endpoints=[body_json.get("replyToEndpoint", None)], - msg=json.dumps(service_reply.msg)) - if self.__is_broker_rest: - if res.status_code == 201: - print( - BColors.OKBLUE - + "[S³I][Broker]" - + BColors.ENDC - + ": Send a S³I-B ServiceReply back to the requester " + else: + # TODO: Call right functionality. + try: + method = getattr(service_functionality_obj, service_type.split('/')[1]) + except AttributeError: + APP_LOGGER.critical( + "Functionality %s is not one of the built-in functionalities in %s!" % ( + service_type.split('/')[1], self.name) + ) + service_reply.fillServiceReply( + senderUUID=self.thing_id, + receiverUUID=body_json.get("sender", None), + serviceType=body_json.get("serviceType", None), + results={"error": "invalid functionalities (serviceType) {}".format(service_type.split('/')[1])}, + replyingToUUID=body_json.get("identifier", None), + msgUUID="s3i:{}".format(uuid.uuid4()) + ) + else: + print( + BColors.OKBLUE + + "[S³I][Broker]" + + BColors.ENDC + + ": Execute the function {0} of the class {1}.".format(service_type.split('/')[1], + service_type.split('/')[0]) + ) + try: + result = method(**parameters) + except TypeError: + APP_LOGGER.critical("Invalid function arguments") + service_reply.fillServiceReply( + senderUUID=self.thing_id, + receiverUUID=body_json.get("sender", None), + serviceType=body_json.get("serviceType", None), + results={"error": "invalid function arguments (parameters)"}, + replyingToUUID=body_json.get("identifier", None), + msgUUID="s3i:{}".format(uuid.uuid4()) ) else: - print( BColors.OKBLUE - + "[S³I][Broker]" - + BColors.ENDC - + res.text) + if isinstance(result, bool): + result = {"ok": result} + service_reply.fillServiceReply( + senderUUID=self.thing_id, + receiverUUID=body_json.get("sender", None), + serviceType=body_json.get("serviceType", None), + results=result, + replyingToUUID=body_json.get("identifier", None), + msgUUID="s3i:{}".format(uuid.uuid4()) + ) + + res = self.broker.send(receiver_endpoints=[body_json.get("replyToEndpoint", None)], + msg=json.dumps(service_reply.msg)) + if self.__is_broker_rest: + if res.status_code == 201: + print( + BColors.OKBLUE + + "[S³I][Broker]" + + BColors.ENDC + + ": Send a S³I-B ServiceReply back to the requester " + ) + else: + print( BColors.OKBLUE + + "[S³I][Broker]" + + BColors.ENDC + + res.text) def on_get_value_reply(self, msg): print(