Commit 7a38946a authored by Jiahang Chen's avatar Jiahang Chen
Browse files

ADD logs

parent 580dbfa7
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
import s3i.exception import s3i.exception
from s3i.broker import Broker, BrokerREST from s3i.broker import Broker, BrokerREST
from s3i.messages import ServiceReply from s3i.messages import ServiceReply
from ml.tools import BColors
from ml.tools import find_broker_endpoint from ml.tools import find_broker_endpoint
from ml.app_logger import APP_LOGGER from ml.app_logger import APP_LOGGER
...@@ -230,9 +229,8 @@ def run_forever(self): ...@@ -230,9 +229,8 @@ def run_forever(self):
"""Starts the thing in permanent mode. """Starts the thing in permanent mode.
""" """
__log = "[S3I]: Launch {}".format(self.name)
# TODO: Use logger instead! APP_LOGGER.info(__log)
print("[S³I]: Launch {}{}{}".format(BColors.OKGREEN, self.name, BColors.ENDC))
self.__connect_with_idp() self.__connect_with_idp()
threading.Thread(target=self.__json_syn).start() threading.Thread(target=self.__json_syn).start()
...@@ -301,14 +299,8 @@ def __connect_with_idp(self): ...@@ -301,14 +299,8 @@ def __connect_with_idp(self):
be renewed if it has expired. be renewed if it has expired.
""" """
__log = "[S3I][IdP]: Connect with S3I IdentityProvider"
# TODO: Use logger! APP_LOGGER.info(__log)
print(
BColors.OKBLUE
+ "[S³I][IdP]"
+ BColors.ENDC
+ ": Connect with S3I IdentityProvider"
)
idp = IdentityProvider( idp = IdentityProvider(
grant_type=self.__grant_type, grant_type=self.__grant_type,
client_id=self.__thing_id, client_id=self.__thing_id,
...@@ -346,13 +338,8 @@ def __connect_with_dir(self): ...@@ -346,13 +338,8 @@ def __connect_with_dir(self):
""" """
# TODO: Use logger __log = "[S3I][Dir]: Connect with S3I Directory"
print( APP_LOGGER.info(__log)
BColors.OKBLUE
+ "[S³I][Dir]"
+ BColors.ENDC
+ ": Connect with S3I Directory"
)
self.dir = Directory( self.dir = Directory(
s3i_dir_url=BaseVariable.DIR_URL, token=self.__access_token s3i_dir_url=BaseVariable.DIR_URL, token=self.__access_token
) )
...@@ -366,13 +353,8 @@ def __connect_with_repo(self): ...@@ -366,13 +353,8 @@ def __connect_with_repo(self):
""" """
# TODO: Use logger __log = "[S3I][Repo]: Connect with S3I Repository"
print( APP_LOGGER.info(__log)
BColors.OKBLUE
+ "[S³I][Repo]"
+ BColors.ENDC
+ ": Connect with S3I Repository"
)
self.repo = Repository( self.repo = Repository(
s3i_repo_url=BaseVariable.REPO_URL, token=self.__access_token s3i_repo_url=BaseVariable.REPO_URL, token=self.__access_token
) )
...@@ -384,13 +366,8 @@ def __connect_with_broker(self): ...@@ -384,13 +366,8 @@ def __connect_with_broker(self):
""" """
# TODO: Use logger __log = "[S3I][Broker]: Connect with S3I Broker"
print( APP_LOGGER.info(__log)
BColors.OKBLUE
+ "[S³I][Broker]"
+ BColors.ENDC
+ ": Connect with S3I Broker"
)
if self.__is_broker_rest: if self.__is_broker_rest:
self.broker = BrokerREST(token=self.access_token) self.broker = BrokerREST(token=self.access_token)
...@@ -480,17 +457,13 @@ def __send_message_to_broker(self, receiver_endpoints, msg): ...@@ -480,17 +457,13 @@ def __send_message_to_broker(self, receiver_endpoints, msg):
receiver_endpoints=receiver_endpoints, receiver_endpoints=receiver_endpoints,
msg=json.dumps(msg) msg=json.dumps(msg)
) )
print( __log = "[S3I][Broker]: Send a S3I-B message back to the requester"
BColors.OKBLUE APP_LOGGER.info(__log)
+ "[S³I][Broker]"
+ BColors.ENDC
+ ": Send a S³I-B GetValueReply back to the requester "
)
return res return res
except s3i.exception.S3IBrokerAMQPError: except s3i.exception.S3IBrokerAMQPError:
value = "invalid request sender endpoint {}".format(receiver_endpoints) __log = "[S3I]: Invalid request sender endpoint {}".format(receiver_endpoints)
APP_LOGGER.critical(value) APP_LOGGER.critical(__log)
def on_user_message(self, msg): def on_user_message(self, msg):
"""Handles incoming S³I-B UserMessages. """Handles incoming S³I-B UserMessages.
...@@ -498,14 +471,8 @@ def on_user_message(self, msg): ...@@ -498,14 +471,8 @@ def on_user_message(self, msg):
:param msg: S³I-B UserMessages :param msg: S³I-B UserMessages
""" """
__log = "[S3I][Broker]: You have received a S3I-B UserMessage"
print( APP_LOGGER.info(__log)
BColors.OKBLUE
+ "[S³I][Broker]"
+ BColors.ENDC
+ ": You have received a S³I-B UserMessage"
+ json.dumps(msg, indent=2)
)
def on_get_value_request(self, msg): def on_get_value_request(self, msg):
"""Handles incoming GetValueRequest message. Looks up the value specified in msg and """Handles incoming GetValueRequest message. Looks up the value specified in msg and
...@@ -514,14 +481,9 @@ def on_get_value_request(self, msg): ...@@ -514,14 +481,9 @@ def on_get_value_request(self, msg):
:param msg: GetValueRequest :param msg: GetValueRequest
""" """
__log = "[S3I][Broker]: You have received a S3I-B GetValueRequest"
APP_LOGGER.info(__log)
print(
BColors.OKBLUE
+ "[S³I][Broker]"
+ BColors.ENDC
+ ": You have received a S³I-B GetValueRequest"
+ json.dumps(msg, indent=2)
)
get_value_reply = GetValueReply() get_value_reply = GetValueReply()
request_sender = msg.get("sender") request_sender = msg.get("sender")
request_msg_id = msg.get("identifier") request_msg_id = msg.get("identifier")
...@@ -529,17 +491,13 @@ def on_get_value_request(self, msg): ...@@ -529,17 +491,13 @@ def on_get_value_request(self, msg):
attribute_path = msg.get("attributePath") attribute_path = msg.get("attributePath")
reply_msg_uuid = "s3i:" + str(uuid.uuid4()) reply_msg_uuid = "s3i:" + str(uuid.uuid4())
try: try:
print( __log = "[S3I]: Search the given attribute path: "
BColors.OKBLUE APP_LOGGER.info(__log)
+ "[S³I]"
+ BColors.ENDC
+ ": Search the attribute with path: "
+ attribute_path
)
value = self._uriToData(attribute_path) value = self._uriToData(attribute_path)
except KeyError: except KeyError:
value = "invalid attribute path" value = "Invalid attribute path"
APP_LOGGER.critical(value) __log = "[S3I]: " + value
APP_LOGGER.critical(__log)
get_value_reply.fillGetValueReply( get_value_reply.fillGetValueReply(
senderUUID=self.thing_id, senderUUID=self.thing_id,
...@@ -556,17 +514,11 @@ def on_get_value_request(self, msg): ...@@ -556,17 +514,11 @@ def on_get_value_request(self, msg):
if self.__is_broker_rest: if self.__is_broker_rest:
if res.status_code == 201: if res.status_code == 201:
print( __log = "[S3I][Broker]: Send S3I-B GetValueReply back to the requester"
BColors.OKBLUE APP_LOGGER.info(__log)
+ "[S³I][Broker]"
+ BColors.ENDC
+ ": Send a S³I-B GetValueReply back to the requester "
)
else: else:
print(BColors.OKBLUE __log = "[S3I[Broker]: " + res.text
+ "[S³I][Broker]" APP_LOGGER.info(__log)
+ BColors.ENDC
+ res.text)
def _uriToData(self, uri): def _uriToData(self, uri):
"""Returns a copy of the value found at uri. """Returns a copy of the value found at uri.
...@@ -584,14 +536,14 @@ def _uriToData(self, uri): ...@@ -584,14 +536,14 @@ def _uriToData(self, uri):
try: try:
return self.dt_json[uri] return self.dt_json[uri]
except KeyError: except KeyError:
return "invalid attribute path" return "Invalid attribute path"
try: try:
self._getValue(self.dt_json, uri_list) self._getValue(self.dt_json, uri_list)
except: except:
return "invalid attribute path" return "Invalid attribute path"
if self.__resGetValue.__len__() == 0: if self.__resGetValue.__len__() == 0:
return "invalid attribute path" return "Invalid attribute path"
response = copy.deepcopy(self.__resGetValue) response = copy.deepcopy(self.__resGetValue)
self.__resGetValue.clear() self.__resGetValue.clear()
if response.__len__() == 1: if response.__len__() == 1:
...@@ -692,14 +644,7 @@ def on_service_request(self, body_json): ...@@ -692,14 +644,7 @@ def on_service_request(self, body_json):
:param body_json: ServiceRequest :param body_json: ServiceRequest
""" """
__log = "[S3I][Broker]: You have received a S3I-B ServiceRequest"
print(
BColors.OKBLUE
+ "[S³I][Broker]"
+ BColors.ENDC
+ ": You have received a S³I-B ServiceRequest "
+ json.dumps(body_json, indent=2)
)
service_type = body_json.get("serviceType") service_type = body_json.get("serviceType")
parameters = body_json.get("parameters") parameters = body_json.get("parameters")
service_reply = ServiceReply() service_reply = ServiceReply()
...@@ -707,7 +652,7 @@ def on_service_request(self, body_json): ...@@ -707,7 +652,7 @@ def on_service_request(self, body_json):
service_functionality_obj = self.features.get(service_functionality) service_functionality_obj = self.features.get(service_functionality)
if service_functionality_obj is None: if service_functionality_obj is None:
APP_LOGGER.critical( APP_LOGGER.critical(
"Functionality %s is not one of the built-in functionalities in %s!" "[S3I]: Functionality %s is not one of the built-in functionalities in %s!"
% (service_functionality, self.name) % (service_functionality, self.name)
) )
service_reply.fillServiceReply( service_reply.fillServiceReply(
...@@ -724,7 +669,7 @@ def on_service_request(self, body_json): ...@@ -724,7 +669,7 @@ def on_service_request(self, body_json):
method = getattr(service_functionality_obj, service_type.split('/')[1]) method = getattr(service_functionality_obj, service_type.split('/')[1])
except AttributeError: except AttributeError:
APP_LOGGER.critical( APP_LOGGER.critical(
"Method %s is not one of the built-in functionalities in %s!" % ( "[S3I]: Method %s is not one of the built-in functionalities in %s!" % (
service_type.split('/')[1], self.name) service_type.split('/')[1], self.name)
) )
service_reply.fillServiceReply( service_reply.fillServiceReply(
...@@ -737,7 +682,7 @@ def on_service_request(self, body_json): ...@@ -737,7 +682,7 @@ def on_service_request(self, body_json):
) )
except IndexError: except IndexError:
APP_LOGGER.critical( APP_LOGGER.critical(
"ServiceType consists of functionality and method name." "[S3I]: ServiceType consists of functionality and method name."
) )
service_reply.fillServiceReply( service_reply.fillServiceReply(
senderUUID=self.thing_id, senderUUID=self.thing_id,
...@@ -748,17 +693,13 @@ def on_service_request(self, body_json): ...@@ -748,17 +693,13 @@ def on_service_request(self, body_json):
msgUUID="s3i:{}".format(uuid.uuid4()) msgUUID="s3i:{}".format(uuid.uuid4())
) )
else: else:
print( __log = "[S3I][Broker]: Execute the function {0} of the class {1}".format(service_type.split('/')[1],
BColors.OKBLUE
+ "[S³I][Broker]"
+ BColors.ENDC
+ ": Execute the function {0} of the class {1}.".format(service_type.split('/')[1],
service_type.split('/')[0]) service_type.split('/')[0])
) APP_LOGGER.info(__log)
try: try:
result = method(**parameters) result = method(**parameters)
except TypeError: except TypeError:
APP_LOGGER.critical("Invalid function arguments") APP_LOGGER.critical("[S3I]: Invalid function arguments")
service_reply.fillServiceReply( service_reply.fillServiceReply(
senderUUID=self.thing_id, senderUUID=self.thing_id,
receiverUUIDs=[body_json.get("sender", None)], receiverUUIDs=[body_json.get("sender", None)],
...@@ -788,17 +729,10 @@ def on_service_request(self, body_json): ...@@ -788,17 +729,10 @@ def on_service_request(self, body_json):
if self.__is_broker_rest: if self.__is_broker_rest:
if res.status_code == 201: if res.status_code == 201:
print( __log = "[S3I][Broker]: Send a S3I-B ServiceReply back to the requester"
BColors.OKBLUE APP_LOGGER.info(__log)
+ "[S³I][Broker]"
+ BColors.ENDC
+ ": Send a S³I-B ServiceReply back to the requester "
)
else: else:
print(BColors.OKBLUE APP_LOGGER.critical(__log)
+ "[S³I][Broker]"
+ BColors.ENDC
+ res.text)
def on_get_value_reply(self, msg): def on_get_value_reply(self, msg):
"""Handles incoming S³I-B GetValueReply. Prints the content of msg to stdout. """Handles incoming S³I-B GetValueReply. Prints the content of msg to stdout.
...@@ -809,25 +743,13 @@ def on_get_value_reply(self, msg): ...@@ -809,25 +743,13 @@ def on_get_value_reply(self, msg):
# ???: Behavior should be defined by the user! Maybe he want # ???: Behavior should be defined by the user! Maybe he want
# to process the result! # to process the result!
print( __log = "[S3I][Broker]: You have received a S3I-B GetValueReply"
BColors.OKBLUE APP_LOGGER.info(__log)
+ "[S³I][Broker]"
+ BColors.ENDC
+ ": You have received a S³I-B GetValueReply"
+ json.dumps(msg, indent=2)
)
value = msg.get("value", None) value = msg.get("value", None)
if isinstance(value, dict): if isinstance(value, dict):
value = json.dumps(value, indent=2) value = json.dumps(value, indent=2)
__log = "[S3I][Broker]: The queried value is: {0}".format(value)
print( APP_LOGGER.info(__log)
BColors.OKBLUE
+ "[S³I][Broker]"
+ BColors.ENDC
+ ": The queried value is: {0}{1}{2}".format(
BColors.OKGREEN, value, BColors.ENDC
)
)
def on_service_reply(self, msg): def on_service_reply(self, msg):
"""Handles incoming S³I-B ServiceReply. Prints the content of msg to stdout. """Handles incoming S³I-B ServiceReply. Prints the content of msg to stdout.
...@@ -835,26 +757,14 @@ def on_service_reply(self, msg): ...@@ -835,26 +757,14 @@ def on_service_reply(self, msg):
:param msg: ServiceReply :param msg: ServiceReply
""" """
__log = "[S3I][Broker]: You have received a S3I-B ServiceReply"
print( APP_LOGGER.info(__log)
BColors.OKBLUE
+ "[S³I][Broker]"
+ BColors.ENDC
+ ": You have received a S³I-B ServiceReply"
+ json.dumps(msg, indent=2)
)
results = msg.get("results", None) results = msg.get("results", None)
if isinstance(results, dict): if isinstance(results, dict):
results = json.dumps(results, indent=2) results = json.dumps(results, indent=2)
print( __log = "[S3I][Broker]: The result is: {0}".format(results)
BColors.OKBLUE APP_LOGGER.info(__log)
+ "[S³I][Broker]"
+ BColors.ENDC
+ ": The result is: {0}{1}{2}".format(
BColors.OKGREEN, results, BColors.ENDC
)
)
def to_dir_json(self): def to_dir_json(self):
"""Returns a dictionary representing this thing's directory entry. """Returns a dictionary representing this thing's directory entry.
......
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