Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
KWH40
fml40-reference-implementation
Commits
7a38946a
Commit
7a38946a
authored
Apr 03, 2021
by
Jiahang Chen
Browse files
ADD logs
parent
580dbfa7
Changes
1
Hide whitespace changes
Inline
Side-by-side
ml/thing.py
View file @
7a38946a
...
...
@@ -11,7 +11,6 @@
import
s3i.exception
from
s3i.broker
import
Broker
,
BrokerREST
from
s3i.messages
import
ServiceReply
from
ml.tools
import
BColors
from
ml.tools
import
find_broker_endpoint
from
ml.app_logger
import
APP_LOGGER
...
...
@@ -230,9 +229,8 @@ def run_forever(self):
"""Starts the thing in permanent mode.
"""
# TODO: Use logger instead!
print
(
"[S³I]: Launch {}{}{}"
.
format
(
BColors
.
OKGREEN
,
self
.
name
,
BColors
.
ENDC
))
__log
=
"[S3I]: Launch {}"
.
format
(
self
.
name
)
APP_LOGGER
.
info
(
__log
)
self
.
__connect_with_idp
()
threading
.
Thread
(
target
=
self
.
__json_syn
).
start
()
...
...
@@ -301,14 +299,8 @@ def __connect_with_idp(self):
be renewed if it has expired.
"""
# TODO: Use logger!
print
(
BColors
.
OKBLUE
+
"[S³I][IdP]"
+
BColors
.
ENDC
+
": Connect with S3I IdentityProvider"
)
__log
=
"[S3I][IdP]: Connect with S3I IdentityProvider"
APP_LOGGER
.
info
(
__log
)
idp
=
IdentityProvider
(
grant_type
=
self
.
__grant_type
,
client_id
=
self
.
__thing_id
,
...
...
@@ -346,13 +338,8 @@ def __connect_with_dir(self):
"""
# TODO: Use logger
print
(
BColors
.
OKBLUE
+
"[S³I][Dir]"
+
BColors
.
ENDC
+
": Connect with S3I Directory"
)
__log
=
"[S3I][Dir]: Connect with S3I Directory"
APP_LOGGER
.
info
(
__log
)
self
.
dir
=
Directory
(
s3i_dir_url
=
BaseVariable
.
DIR_URL
,
token
=
self
.
__access_token
)
...
...
@@ -366,13 +353,8 @@ def __connect_with_repo(self):
"""
# TODO: Use logger
print
(
BColors
.
OKBLUE
+
"[S³I][Repo]"
+
BColors
.
ENDC
+
": Connect with S3I Repository"
)
__log
=
"[S3I][Repo]: Connect with S3I Repository"
APP_LOGGER
.
info
(
__log
)
self
.
repo
=
Repository
(
s3i_repo_url
=
BaseVariable
.
REPO_URL
,
token
=
self
.
__access_token
)
...
...
@@ -384,13 +366,8 @@ def __connect_with_broker(self):
"""
# TODO: Use logger
print
(
BColors
.
OKBLUE
+
"[S³I][Broker]"
+
BColors
.
ENDC
+
": Connect with S3I Broker"
)
__log
=
"[S3I][Broker]: Connect with S3I Broker"
APP_LOGGER
.
info
(
__log
)
if
self
.
__is_broker_rest
:
self
.
broker
=
BrokerREST
(
token
=
self
.
access_token
)
...
...
@@ -480,17 +457,13 @@ def __send_message_to_broker(self, receiver_endpoints, msg):
receiver_endpoints
=
receiver_endpoints
,
msg
=
json
.
dumps
(
msg
)
)
print
(
BColors
.
OKBLUE
+
"[S³I][Broker]"
+
BColors
.
ENDC
+
": Send a S³I-B GetValueReply back to the requester "
)
__log
=
"[S3I][Broker]: Send a S3I-B message back to the requester"
APP_LOGGER
.
info
(
__log
)
return
res
except
s3i
.
exception
.
S3IBrokerAMQPError
:
value
=
"i
nvalid request sender endpoint {}"
.
format
(
receiver_endpoints
)
APP_LOGGER
.
critical
(
value
)
__log
=
"[S3I]: I
nvalid request sender endpoint {}"
.
format
(
receiver_endpoints
)
APP_LOGGER
.
critical
(
__log
)
def
on_user_message
(
self
,
msg
):
"""Handles incoming S³I-B UserMessages.
...
...
@@ -498,14 +471,8 @@ def on_user_message(self, msg):
:param msg: S³I-B UserMessages
"""
print
(
BColors
.
OKBLUE
+
"[S³I][Broker]"
+
BColors
.
ENDC
+
": You have received a S³I-B UserMessage"
+
json
.
dumps
(
msg
,
indent
=
2
)
)
__log
=
"[S3I][Broker]: You have received a S3I-B UserMessage"
APP_LOGGER
.
info
(
__log
)
def
on_get_value_request
(
self
,
msg
):
"""Handles incoming GetValueRequest message. Looks up the value specified in msg and
...
...
@@ -514,14 +481,9 @@ def on_get_value_request(self, msg):
: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
()
request_sender
=
msg
.
get
(
"sender"
)
request_msg_id
=
msg
.
get
(
"identifier"
)
...
...
@@ -529,17 +491,13 @@ def on_get_value_request(self, msg):
attribute_path
=
msg
.
get
(
"attributePath"
)
reply_msg_uuid
=
"s3i:"
+
str
(
uuid
.
uuid4
())
try
:
print
(
BColors
.
OKBLUE
+
"[S³I]"
+
BColors
.
ENDC
+
": Search the attribute with path: "
+
attribute_path
)
__log
=
"[S3I]: Search the given attribute path: "
APP_LOGGER
.
info
(
__log
)
value
=
self
.
_uriToData
(
attribute_path
)
except
KeyError
:
value
=
"invalid attribute path"
APP_LOGGER
.
critical
(
value
)
value
=
"Invalid attribute path"
__log
=
"[S3I]: "
+
value
APP_LOGGER
.
critical
(
__log
)
get_value_reply
.
fillGetValueReply
(
senderUUID
=
self
.
thing_id
,
...
...
@@ -556,17 +514,11 @@ def on_get_value_request(self, msg):
if
self
.
__is_broker_rest
:
if
res
.
status_code
==
201
:
print
(
BColors
.
OKBLUE
+
"[S³I][Broker]"
+
BColors
.
ENDC
+
": Send a S³I-B GetValueReply back to the requester "
)
__log
=
"[S3I][Broker]: Send S3I-B GetValueReply back to the requester"
APP_LOGGER
.
info
(
__log
)
else
:
print
(
BColors
.
OKBLUE
+
"[S³I][Broker]"
+
BColors
.
ENDC
+
res
.
text
)
__log
=
"[S3I[Broker]: "
+
res
.
text
APP_LOGGER
.
info
(
__log
)
def
_uriToData
(
self
,
uri
):
"""Returns a copy of the value found at uri.
...
...
@@ -584,14 +536,14 @@ def _uriToData(self, uri):
try
:
return
self
.
dt_json
[
uri
]
except
KeyError
:
return
"
i
nvalid attribute path"
return
"
I
nvalid attribute path"
try
:
self
.
_getValue
(
self
.
dt_json
,
uri_list
)
except
:
return
"
i
nvalid attribute path"
return
"
I
nvalid attribute path"
if
self
.
__resGetValue
.
__len__
()
==
0
:
return
"
i
nvalid attribute path"
return
"
I
nvalid attribute path"
response
=
copy
.
deepcopy
(
self
.
__resGetValue
)
self
.
__resGetValue
.
clear
()
if
response
.
__len__
()
==
1
:
...
...
@@ -692,14 +644,7 @@ def on_service_request(self, body_json):
:param body_json: ServiceRequest
"""
print
(
BColors
.
OKBLUE
+
"[S³I][Broker]"
+
BColors
.
ENDC
+
": You have received a S³I-B ServiceRequest "
+
json
.
dumps
(
body_json
,
indent
=
2
)
)
__log
=
"[S3I][Broker]: You have received a S3I-B ServiceRequest"
service_type
=
body_json
.
get
(
"serviceType"
)
parameters
=
body_json
.
get
(
"parameters"
)
service_reply
=
ServiceReply
()
...
...
@@ -707,7 +652,7 @@ def on_service_request(self, body_json):
service_functionality_obj
=
self
.
features
.
get
(
service_functionality
)
if
service_functionality_obj
is
None
:
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_reply
.
fillServiceReply
(
...
...
@@ -724,7 +669,7 @@ def on_service_request(self, body_json):
method
=
getattr
(
service_functionality_obj
,
service_type
.
split
(
'/'
)[
1
])
except
AttributeError
:
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_reply
.
fillServiceReply
(
...
...
@@ -737,7 +682,7 @@ def on_service_request(self, body_json):
)
except
IndexError
:
APP_LOGGER
.
critical
(
"ServiceType consists of functionality and method name."
"
[S3I]:
ServiceType consists of functionality and method name."
)
service_reply
.
fillServiceReply
(
senderUUID
=
self
.
thing_id
,
...
...
@@ -748,17 +693,13 @@ def on_service_request(self, body_json):
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
])
)
__log
=
"[S3I][Broker]: Execute the function {0} of the class {1}"
.
format
(
service_type
.
split
(
'/'
)[
1
],
service_type
.
split
(
'/'
)[
0
])
APP_LOGGER
.
info
(
__log
)
try
:
result
=
method
(
**
parameters
)
except
TypeError
:
APP_LOGGER
.
critical
(
"Invalid function arguments"
)
APP_LOGGER
.
critical
(
"
[S3I]:
Invalid function arguments"
)
service_reply
.
fillServiceReply
(
senderUUID
=
self
.
thing_id
,
receiverUUIDs
=
[
body_json
.
get
(
"sender"
,
None
)],
...
...
@@ -788,17 +729,10 @@ def on_service_request(self, body_json):
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 "
)
__log
=
"[S3I][Broker]: Send a S3I-B ServiceReply back to the requester"
APP_LOGGER
.
info
(
__log
)
else
:
print
(
BColors
.
OKBLUE
+
"[S³I][Broker]"
+
BColors
.
ENDC
+
res
.
text
)
APP_LOGGER
.
critical
(
__log
)
def
on_get_value_reply
(
self
,
msg
):
"""Handles incoming S³I-B GetValueReply. Prints the content of msg to stdout.
...
...
@@ -809,25 +743,13 @@ def on_get_value_reply(self, msg):
# ???: Behavior should be defined by the user! Maybe he want
# to process the result!
print
(
BColors
.
OKBLUE
+
"[S³I][Broker]"
+
BColors
.
ENDC
+
": You have received a S³I-B GetValueReply"
+
json
.
dumps
(
msg
,
indent
=
2
)
)
__log
=
"[S3I][Broker]: You have received a S3I-B GetValueReply"
APP_LOGGER
.
info
(
__log
)
value
=
msg
.
get
(
"value"
,
None
)
if
isinstance
(
value
,
dict
):
value
=
json
.
dumps
(
value
,
indent
=
2
)
print
(
BColors
.
OKBLUE
+
"[S³I][Broker]"
+
BColors
.
ENDC
+
": The queried value is: {0}{1}{2}"
.
format
(
BColors
.
OKGREEN
,
value
,
BColors
.
ENDC
)
)
__log
=
"[S3I][Broker]: The queried value is: {0}"
.
format
(
value
)
APP_LOGGER
.
info
(
__log
)
def
on_service_reply
(
self
,
msg
):
"""Handles incoming S³I-B ServiceReply. Prints the content of msg to stdout.
...
...
@@ -835,26 +757,14 @@ def on_service_reply(self, msg):
:param msg: ServiceReply
"""
print
(
BColors
.
OKBLUE
+
"[S³I][Broker]"
+
BColors
.
ENDC
+
": You have received a S³I-B ServiceReply"
+
json
.
dumps
(
msg
,
indent
=
2
)
)
__log
=
"[S3I][Broker]: You have received a S3I-B ServiceReply"
APP_LOGGER
.
info
(
__log
)
results
=
msg
.
get
(
"results"
,
None
)
if
isinstance
(
results
,
dict
):
results
=
json
.
dumps
(
results
,
indent
=
2
)
print
(
BColors
.
OKBLUE
+
"[S³I][Broker]"
+
BColors
.
ENDC
+
": The result is: {0}{1}{2}"
.
format
(
BColors
.
OKGREEN
,
results
,
BColors
.
ENDC
)
)
__log
=
"[S3I][Broker]: The result is: {0}"
.
format
(
results
)
APP_LOGGER
.
info
(
__log
)
def
to_dir_json
(
self
):
"""Returns a dictionary representing this thing's directory entry.
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment