Change to use external messaging module
This commit is contained in:
@@ -1,28 +1,27 @@
|
||||
import paho.mqtt.client as mqtt
|
||||
import json
|
||||
from DecisionSystem.messages import ConnectSwarm, SubmitVote, Message, deserialise, RequestVote
|
||||
from multiprocessing import Pool
|
||||
from messenger import Messenger
|
||||
|
||||
class BallotVoter:
|
||||
def __init__(self, on_vote):
|
||||
def __init__(self, on_vote, messenger: Messenger):
|
||||
|
||||
# Load config file
|
||||
cfg = None
|
||||
with open('../config.json') as json_config:
|
||||
with open('config.json') as json_config:
|
||||
cfg = json.load(json_config)
|
||||
self._cfg = cfg
|
||||
|
||||
self.client = mqtt.Client()
|
||||
self.client.on_connect = self.on_connect
|
||||
self.client.on_message = self.on_message
|
||||
# id is generated automatically.
|
||||
self.client.connect(cfg["mqtt"]["host"], cfg["mqtt"]["port"], cfg["mqtt"]["timeout"])
|
||||
self.client.loop_start()
|
||||
self.messenger = messenger
|
||||
self.messenger.add_message_callback(self.on_message)
|
||||
self.messenger.add_connect(self.on_connect)
|
||||
self.on_vote = on_vote
|
||||
|
||||
def on_connect(self, client, userdata, flags, rc):
|
||||
def on_connect(self, rc):
|
||||
print("Connected with result code " + str(rc))
|
||||
|
||||
self.client.subscribe('swarm1/voters', qos=1)
|
||||
# Should subscribes be handled by the messenger?
|
||||
#self.client.subscribe('swarm1/voters', qos=1)
|
||||
|
||||
# Need to set a will as well to broadcast on unexpected disconnection, so commander
|
||||
# knows it is no longer part of the set of voters.
|
||||
@@ -30,9 +29,9 @@ class BallotVoter:
|
||||
#will_message = {"type": "UDisconnect"}
|
||||
|
||||
# Tell commander we are now connected.
|
||||
# self.send_connect()
|
||||
self.send_connect()
|
||||
|
||||
def on_message(self, client, userdata, message):
|
||||
def on_message(self, message):
|
||||
print("Message Received!")
|
||||
messageD = deserialise(message.payload)
|
||||
print("Message Type: " + messageD.type)
|
||||
@@ -51,14 +50,24 @@ class BallotVoter:
|
||||
|
||||
def submit_vote(self):
|
||||
v = self.on_vote()
|
||||
if v == None:
|
||||
print('Could not vote on the frame')
|
||||
return
|
||||
print("Got Vote")
|
||||
vote = SubmitVote(v, self.client._client_id)
|
||||
vote = SubmitVote(v, self.messenger.id)
|
||||
print('created vote')
|
||||
self.client.publish('swarm1/commander', vote.serialise())
|
||||
self.messenger.broadcast_message(self.messenger.swarm, vote.serialise())
|
||||
print('published vote')
|
||||
|
||||
|
||||
def submit_vote_multicore(self):
|
||||
'''
|
||||
Uses multiple processes to work on multiple frames at the same time
|
||||
to take an average for this voter, or if it could not find a hand
|
||||
in one frame but can in another.
|
||||
'''
|
||||
pass
|
||||
|
||||
def send_connect(self):
|
||||
# Send a connected message to let any commanders know that
|
||||
# it is available.
|
||||
self.client.publish("swarm1/commander", ConnectSwarm(self.client._client_id).serialise())
|
||||
|
||||
self.messenger.broadcast_message(self.messenger.swarm, ConnectSwarm(self.messenger.id).serialise())
|
||||
Reference in New Issue
Block a user