From 49de393edb9a2c4a2ec5ba34c05b24e364382a0c Mon Sep 17 00:00:00 2001 From: "DSTO\\pivatom" Date: Fri, 11 Jan 2019 11:39:04 +1030 Subject: [PATCH] Add some comments for future details, as well as partial of on_message --- .../CentralisedDecision/ballotvoter.py | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/DecisionSystem/CentralisedDecision/ballotvoter.py b/DecisionSystem/CentralisedDecision/ballotvoter.py index fd24914..0394c35 100644 --- a/DecisionSystem/CentralisedDecision/ballotvoter.py +++ b/DecisionSystem/CentralisedDecision/ballotvoter.py @@ -1,20 +1,34 @@ import paho.mqtt.client as mqtt +import umsgpack class BallotVoter: def __init__(self): self.client = mqtt.Client() - self.client.connect('172.16.43.2') + # id is generated automatically. + self.client.connect('172.16.13.128') def on_connect(self, client, userdata, flags, rc): print("Connected with result code " + str(rc)) - if rc == 0: - global connected - connected = True self.client.subscribe('swarm1/voters', qos=1) + # Create the message to send that it is now part of the swarm. + + # 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. + # 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) + def on_message(self, client, userdata, message): - + messageDict = umsgpack.unpackb(message) + if "type" in messageDict.keys: + # Ok message. + else: + # Bad message. def submit_vote(self): self.client.publish('swarm1/')