Add config file usage and messages module usage

This commit is contained in:
Michael Pivato
2019-01-18 16:35:52 +10:30
parent 451c3c7830
commit 741823ae25

View File

@@ -1,11 +1,22 @@
import paho.mqtt.client as mqtt import paho.mqtt.client as mqtt
import umsgpack import json
from DecisionSystem.messages import ConnectSwarm, SubmitVote, Message, deserialise, RequestVote
class BallotVoter: class BallotVoter:
def __init__(self, on_vote): def __init__(self, on_vote):
# Load config file
cfg = None
with open('../config.json') as json_config:
cfg = json.load(json_config)
self._cfg = cfg
self.client = mqtt.Client() self.client = mqtt.Client()
self.client.on_connect = self.on_connect
self.client.on_message = self.on_message
# id is generated automatically. # id is generated automatically.
self.client.connect('172.16.13.128') self.client.connect(cfg["mqtt"]["host"], cfg["mqtt"]["port"], cfg["mqtt"]["timeout"])
self.client.loop_start()
self.on_vote = on_vote self.on_vote = on_vote
def on_connect(self, client, userdata, flags, rc): def on_connect(self, client, userdata, flags, rc):
@@ -13,33 +24,41 @@ class BallotVoter:
self.client.subscribe('swarm1/voters', qos=1) 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 # 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. # knows it is no longer part of the set of voters.
# Leaving this until core centralised system is working. # Leaving this until core centralised system is working.
#will_message = {"type": "UDisconnect"} #will_message = {"type": "UDisconnect"}
self.send_connect() # Tell commander we are now connected.
# self.send_connect()
def on_message(self, client, userdata, message): def on_message(self, client, userdata, message):
messageDict = umsgpack.unpackb(message) print("Message Received!")
if "type" in messageDict.keys: messageD = deserialise(message.payload)
# Ok message. print("Message Type: " + messageD.type)
if messageDict["type"] == "reqVote": # Ok message.
self.submit_vote() if messageD.type == RequestVote().type:
if messageDict["type"] == "listening": print('Received vote message')
self.send_connect() self.submit_vote()
else: elif messageD.type == "listening":
# Bad message. self.send_connect()
elif messageD.type == "disconnectedcommander":
# Elect new leader...
# Might just use raft's method for electing a leader,
# by doing a random timeout then choosing itself as the candidate,
# and collects who is part of the swarm. This may need to be another class...
pass pass
def submit_vote(self): def submit_vote(self):
binary = umsgpack.packb("type": "vote", "client": self.client._client_id, "vote": self.on_vote()) v = self.on_vote()
self.client.publish('swarm1/commander', binary) print("Got Vote")
vote = SubmitVote(v, self.client._client_id)
print('created vote')
self.client.publish('swarm1/commander', vote.serialise())
print('published vote')
def send_connect(self): def send_connect(self):
# Send a connected message to let any commanders know that # Send a connected message to let any commanders know that
# it is available. # it is available.
binary = umsgpack.packb({"type": "connect", "id": self.client._client_id}) self.client.publish("swarm1/commander", ConnectSwarm(self.client._client_id).serialise())
self.client.publish("swarm/commander", binary)