baseimage-tutorial-framework/tfw/components/frontend/message_sender.py

39 lines
1.1 KiB
Python
Raw Normal View History

2018-01-29 15:48:32 +00:00
class MessageSender:
def __init__(self, uplink):
self.uplink = uplink
2018-01-29 15:48:32 +00:00
2019-08-07 07:49:05 +00:00
def send(self, message, originator=None):
2019-05-26 16:26:33 +00:00
message = {
2019-08-07 07:49:05 +00:00
'key': 'message.send',
'message': message
2019-05-26 16:26:33 +00:00
}
2019-08-07 07:49:05 +00:00
if originator:
message['originator'] = originator
2019-06-28 15:34:22 +00:00
self.uplink.send_message(message)
2019-08-07 07:49:05 +00:00
def queue_messages(self, messages, originator=None):
message_queue = {
'key': 'message.queue',
'value': []
2019-05-26 16:26:33 +00:00
}
2019-08-07 07:49:05 +00:00
for message in messages:
next_message = {'message': message}
if originator:
next_message['originator'] = originator
message_queue['value'].append(next_message)
self.uplink.send_message(message_queue)
2019-08-07 07:49:05 +00:00
def set_originator(self, originator):
self.uplink.send_message({
'key': 'message.config',
'originator': originator
})
2019-08-07 09:24:56 +00:00
@staticmethod
def generate_messages_from_queue(queue_message):
for message in queue_message['value']:
yield {
'key': 'message.send',
**message
}