From f101bf61bffe54f3ea340676cdd4ce9930286ef7 Mon Sep 17 00:00:00 2001 From: "DSTO\\pivatom" Date: Mon, 14 Jan 2019 08:55:36 +1030 Subject: [PATCH] Put custom on_vote message for voter to use their own method. This maintains cohesion so the voter class is just focussed on how to vote, not also on how to collect the votes, which could be by a means such as an image or video sensor. --- .../CentralisedDecision/ballotvoter.py | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/DecisionSystem/CentralisedDecision/ballotvoter.py b/DecisionSystem/CentralisedDecision/ballotvoter.py index 5c994cf..b2e1bff 100644 --- a/DecisionSystem/CentralisedDecision/ballotvoter.py +++ b/DecisionSystem/CentralisedDecision/ballotvoter.py @@ -2,10 +2,11 @@ import paho.mqtt.client as mqtt import umsgpack class BallotVoter: - def __init__(self): + def __init__(self, on_vote): self.client = mqtt.Client() # id is generated automatically. self.client.connect('172.16.13.128') + self.on_vote = on_vote def on_connect(self, client, userdata, flags, rc): print("Connected with result code " + str(rc)) @@ -19,19 +20,26 @@ class BallotVoter: # Leaving this until core centralised system is working. #will_message = {"type": "UDisconnect"} - # Send a connected message to let any commanders know that - # it is available. - self.client.publish("swarm/commander", "connect," + self.client._client_id) + self.send_connect() def on_message(self, client, userdata, message): messageDict = umsgpack.unpackb(message) if "type" in messageDict.keys: # Ok message. if messageDict["type"] == "reqVote": - submit_vote() + self.submit_vote() + if messageDict["type"] == "listening": + self.send_connect() else: # Bad message. + pass def submit_vote(self): - self.client.publish('swarm1/') - \ No newline at end of file + binary = umsgpack.packb("type": "vote", "client": self.client._client_id, "vote": self.on_vote()) + self.client.publish('swarm1/commander', binary) + + def send_connect(self): + # Send a connected message to let any commanders know that + # it is available. + binary = umsgpack.packb({"type": "connect", "id": self.client._client_id}) + self.client.publish("swarm/commander", binary) \ No newline at end of file