Remove last traces of external mqtt abstraction

Also subscribed to commander and changed name of vote request message.
This commit is contained in:
DSTO\pivatom
2019-01-14 08:35:44 +10:30
parent 97fd356038
commit 8d92467a99

View File

@@ -20,11 +20,13 @@ class Commander:
self.client = mqtt.Client() self.client = mqtt.Client()
self.client.connect('172.16.13.128') self.client.connect('172.16.13.128')
self.client.subscribe("swarm1/commander")
# Commander needs a will message too, for the decentralised version, so the # Commander needs a will message too, for the decentralised version, so the
# voters know to pick a new commander. # voters know to pick a new commander.
# If using apache zookeeper this won't be needed.
def make_decision(self): def make_decision(self):
# Could change this to be a strategy, for different implementations of making # Should change this to be a strategy, for different implementations of making
# a decision on the votes. # a decision on the votes.
votes = self._votes votes = self._votes
dif_votes = {} dif_votes = {}
@@ -46,26 +48,28 @@ class Commander:
return max_vote return max_vote
def get_votes(): def get_votes():
message = {"type": "voteRequest"} message = {"type": "reqVote"}
message_packed = umsgpack.packb(message) message_packed = umsgpack.packb(message)
# Publish a message that votes are needed. # Publish a message that votes are needed.
ms.client.publish("swarm1/voters", message_packed, qos) self.client.publish("swarm1/voters", message_packed, qos)
time.sleep(self.timeout) time.sleep(self.timeout)
self.make_decision() self.make_decision()
def add_subscription(self, topic, callback=None, qos=0): # Shouldn't be needed anymore.
ms.client.subscribe(topic) # def add_subscription(self, topic, callback=None, qos=0):
# self.client.subscribe(topic)
if callback is not None: # if callback is not None:
ms.client.message_callback_add(topic, callback) # selfclient.message_callback_add(topic, callback)
def on_message(self, client, userdata, message): def on_message(self, client, userdata, message):
messageDict = umsgpack.unpackb() messageDict = umsgpack.unpackb()
if "type" in messageDict.keys: if "type" in messageDict.keys:
if messageDict["type"] == "connect": if messageDict["type"] == "connect":
# Voter just connected/reconnnected. # Voter just connected/reconnnected.
pass
elif messageDict["type"] == "vote": elif messageDict["type"] == "vote":
# Voter is sending in their vote. # Voter is sending in their vote.
if messageDict[""] if messageDict[""]