Remove use of external mqtt file, just use paho library
This commit is contained in:
@@ -1,26 +1,31 @@
|
|||||||
import Messaging.mqttsession as ms
|
import paho.mqtt.client as mqtt
|
||||||
import time
|
import time
|
||||||
import umsgpack
|
import umsgpack
|
||||||
|
|
||||||
class Commander:
|
class Commander:
|
||||||
currentVote = None
|
currentVote = None
|
||||||
|
|
||||||
|
# Stores voters that connect to maintain a majority.
|
||||||
|
# Voters who do not vote in latest round are removed.
|
||||||
|
connectedVoters = {}
|
||||||
|
votes = []
|
||||||
|
taking_votes = False
|
||||||
|
|
||||||
def __init__(self, timeout = 60):
|
def __init__(self, timeout = 60):
|
||||||
'''
|
'''
|
||||||
Initial/default waiting time is 1 minute for votes to come in.
|
Initial/default waiting time is 1 minute for votes to come in.
|
||||||
'''
|
'''
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
self._votes = []
|
|
||||||
ms.Client()
|
|
||||||
|
|
||||||
def on_message(client, userdata, message):
|
self.client = mqtt.Client()
|
||||||
self._votes.append(umsgpack.unpackb(message.payload))
|
self.client.connect('172.16.13.128')
|
||||||
|
|
||||||
ms.client.subscribe("FakeSwarm/FirstTest", on_message)
|
# Commander needs a will message too, for the decentralised version, so the
|
||||||
|
# voters know to pick a new commander.
|
||||||
ms.client.loop_start()
|
|
||||||
|
|
||||||
def make_decision(self):
|
def make_decision(self):
|
||||||
|
# Could change this to be a strategy, for different implementations of making
|
||||||
|
# a decision on the votes.
|
||||||
votes = self._votes
|
votes = self._votes
|
||||||
dif_votes = {}
|
dif_votes = {}
|
||||||
|
|
||||||
@@ -40,9 +45,11 @@ class Commander:
|
|||||||
|
|
||||||
return max_vote
|
return max_vote
|
||||||
|
|
||||||
def get_votes(self, topic, message, qos=0):
|
def get_votes():
|
||||||
|
message = {"type": "voteRequest"}
|
||||||
|
message_packed = umsgpack.packb(message)
|
||||||
# Publish a message that votes are needed.
|
# Publish a message that votes are needed.
|
||||||
ms.client.publish(topic, message, qos)
|
ms.client.publish("swarm1/voters", message_packed, qos)
|
||||||
time.sleep(self.timeout)
|
time.sleep(self.timeout)
|
||||||
self.make_decision()
|
self.make_decision()
|
||||||
|
|
||||||
@@ -51,3 +58,21 @@ class Commander:
|
|||||||
|
|
||||||
if callback is not None:
|
if callback is not None:
|
||||||
ms.client.message_callback_add(topic, callback)
|
ms.client.message_callback_add(topic, callback)
|
||||||
|
|
||||||
|
def on_message(self, client, userdata, message):
|
||||||
|
messageDict = umsgpack.unpackb()
|
||||||
|
|
||||||
|
if "type" in messageDict.keys:
|
||||||
|
if messageDict["type"] == "connect":
|
||||||
|
# Voter just connected/reconnnected.
|
||||||
|
|
||||||
|
elif messageDict["type"] == "vote":
|
||||||
|
# Voter is sending in their vote.
|
||||||
|
if messageDict[""]
|
||||||
|
|
||||||
|
else:
|
||||||
|
# Not a message we should be using.
|
||||||
|
NotImplementedError
|
||||||
|
|
||||||
|
def on_connect(client, userdata, flags, rc):
|
||||||
|
self.client.subscribe("swarm1/commander")
|
||||||
Reference in New Issue
Block a user