2018-01-30 16:24:17 +00:00
|
|
|
import json
|
|
|
|
from datetime import datetime
|
|
|
|
|
2018-01-31 14:50:52 +00:00
|
|
|
from .networking.server_connector import ServerUplinkConnector
|
2018-01-29 15:48:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MessageSender:
|
|
|
|
def __init__(self, custom_anchor=None):
|
|
|
|
self.server_connector = ServerUplinkConnector()
|
2018-02-02 15:49:15 +00:00
|
|
|
if isinstance(custom_anchor, bytes):
|
|
|
|
custom_anchor = custom_anchor.decode('utf-8')
|
|
|
|
self.anchor = custom_anchor or 'message'
|
2018-01-29 15:48:32 +00:00
|
|
|
|
2018-01-30 16:24:17 +00:00
|
|
|
def send(self, originator, message):
|
2018-02-02 15:49:15 +00:00
|
|
|
data = {
|
2018-01-30 16:24:17 +00:00
|
|
|
'originator': originator,
|
|
|
|
'timestamp': datetime.now().isoformat(),
|
|
|
|
'message': message
|
|
|
|
}
|
2018-02-02 15:49:15 +00:00
|
|
|
response = {
|
|
|
|
'anchor': self.anchor,
|
|
|
|
'data': data
|
|
|
|
}
|
|
|
|
self.server_connector.send(*[frame.encode('utf-8') for frame in (self.anchor, json.dumps(response))])
|