baseimage-tutorial-framework/src/event_handlers/message_sender.py

22 lines
693 B
Python
Raw Normal View History

import json
from datetime import datetime
2018-01-29 15:48:32 +00:00
from server_connector import ServerUplinkConnector
class MessageSender:
def __init__(self, custom_anchor=None):
self.server_connector = ServerUplinkConnector()
if isinstance(custom_anchor, str):
custom_anchor = custom_anchor.encode('utf-8')
self.anchor = custom_anchor or b'message'
def send(self, originator, message):
response = {
'originator': originator,
'timestamp': datetime.now().isoformat(),
'message': message
}
encoded_response = json.dumps(response).encode('utf-8')
self.server_connector.send(self.anchor, encoded_response)