From 6f0bd56b4a79bf96194848228012eb36bbed5f86 Mon Sep 17 00:00:00 2001 From: "DSTO\\pivatom" Date: Thu, 13 Dec 2018 11:00:27 +1030 Subject: [PATCH] Implementing stuff (I suck at commits sometimes lol) --- DecisionSystem/commander.py | 35 ++++++++++++++++++++++++--- DecisionSystem/mqttvoter.py | 21 ++++++++++++++++ DecisionSystem/vote.py | 35 ++++++++++++++++++--------- DecisionSystem/voter.py | 7 ++++-- GestureRecognition/HandRecognition.py | 2 +- GestureRecognition/handrecogniser.py | 15 ++++++++++++ Messaging/messagingInterface.py | 11 ++++++++- Messaging/mqttsession.py | 25 +++++++++++++++++++ Messaging/packmessage.py | 14 +++++++++++ 9 files changed, 146 insertions(+), 19 deletions(-) create mode 100644 DecisionSystem/mqttvoter.py create mode 100644 GestureRecognition/handrecogniser.py create mode 100644 Messaging/mqttsession.py create mode 100644 Messaging/packmessage.py diff --git a/DecisionSystem/commander.py b/DecisionSystem/commander.py index 1342806..188d51c 100644 --- a/DecisionSystem/commander.py +++ b/DecisionSystem/commander.py @@ -1,6 +1,35 @@ +import Messaging.mqttsession as ms +import time +import umsgpack + class Commander: - def get_votes(self): - raise Exception("Vote cannot be submitted as it's not implemented") + + def __init__(self, timeout = 60): + self.timeout = timeout + self._votes = [] + self._client = ms.Client() + + def on_message(client, userdata, message): + self._votes.append(umsgpack.unpackb(message.payload)) + + self.add_subscription("FakeSwarm/FirstTest", on_message) + + self._client.loop_start() + + def get_votes(self, topic, message, qos=0): + # Publish a message that votes are needed. + ms.client.publish(topic, message, qos) + time.sleep(self.timeout) + make_decision() def make_decision(self): - raise Exception("Vote cannot be submitted as it's not implemented") \ No newline at end of file + votes = self._votes + + for vote in votes: + continue + + def add_subscription(self, topic, callback=None, qos=0): + self._client.subscribe(topic) + + if callback is not None: + self._client.message_callback_add(topic, callback) \ No newline at end of file diff --git a/DecisionSystem/mqttvoter.py b/DecisionSystem/mqttvoter.py new file mode 100644 index 0000000..0c0ccb5 --- /dev/null +++ b/DecisionSystem/mqttvoter.py @@ -0,0 +1,21 @@ +from voter import Voter +import Messaging.mqttsession as ms +from Messaging.packmessage import PackMessage + +class MqttVoter(Voter): + def __init__(self): + # Should use a factory here to always get the same session. + # Just going to use a singleton for now, as there should only be + # one session in the program. + ms.client = ms.Client() + ms.host = "10.0.123.11" + + def submit_vote(self): + + pass + + def set_vote(self): + pass + + def get_vote(self): + pass \ No newline at end of file diff --git a/DecisionSystem/vote.py b/DecisionSystem/vote.py index e1523c2..ff55df4 100644 --- a/DecisionSystem/vote.py +++ b/DecisionSystem/vote.py @@ -1,16 +1,9 @@ -class Vote: +from Messaging.packmessage import PackMessage + +class Vote(PackMessage): def __init__(self, voter, vote): - self._voter = voter self._vote = vote - @property - def voter(self): - return self._voter - - @voter.setter - def voter(self, value): - self._voter = value - @property def vote(self): return self._vote @@ -19,5 +12,23 @@ class Vote: def vote(self, value): self._vote = value - def to_string(self): - pass \ No newline at end of file + def serialise(self): + + # Still need associated device as well? to know who went the message, + # to check that we don't add too many messages in case of multiple + # sends. + self.message = {"vote": self.vote} + + # Call super method equivalent. + super(Vote, self).serialise() + + def deserialise(self): + super(Vote, self).deserialise() + + if "vote" in self.message: + self.vote = self.message["vote"] + else: + # Do something. + pass + + \ No newline at end of file diff --git a/DecisionSystem/voter.py b/DecisionSystem/voter.py index 36d8616..af39d76 100644 --- a/DecisionSystem/voter.py +++ b/DecisionSystem/voter.py @@ -1,4 +1,7 @@ +from Messaging.packmessage import PackMessage + class Voter: - def submit_vote(self): - raise Exception("Vote cannot be submitted as it's not implemented") + def submit_vote(self, vote_contents): + NotImplementedError + \ No newline at end of file diff --git a/GestureRecognition/HandRecognition.py b/GestureRecognition/HandRecognition.py index c191350..e13b766 100644 --- a/GestureRecognition/HandRecognition.py +++ b/GestureRecognition/HandRecognition.py @@ -7,7 +7,7 @@ img = cv2.imread('H:\car\GestureRecognition\IMG_0818.png', 1) img = cv2.resize(img, None, fx=0.1, fy=0.1, interpolation = cv2.INTER_AREA) min_seg_threshold = 1.2 -max_seg_threshold = 1.8 +max_seg_threshold = 3 # prevent divide by zero, by just forcing pixel to be ignored. np.where(img[:,:,1] == 0, 0, img[:,:,1]) diff --git a/GestureRecognition/handrecogniser.py b/GestureRecognition/handrecogniser.py new file mode 100644 index 0000000..832946f --- /dev/null +++ b/GestureRecognition/handrecogniser.py @@ -0,0 +1,15 @@ +class HandRecogniser: + """ + Interface for Recognising simple hand gestures from an image (or frame of a video) + """ + def load_image(self, image_path): + """ + Loads the given image, can be lazy loading. + """ + pass + + def get_gesture(self): + """ + Gets a the gesture recognised in the image. + """ + pass diff --git a/Messaging/messagingInterface.py b/Messaging/messagingInterface.py index 556333f..934d458 100644 --- a/Messaging/messagingInterface.py +++ b/Messaging/messagingInterface.py @@ -1,2 +1,11 @@ class Message: - \ No newline at end of file + """ + Message Format + + + """ + def serialise(self): + NotImplementedError + + def deserialise(self): + NotImplementedError diff --git a/Messaging/mqttsession.py b/Messaging/mqttsession.py new file mode 100644 index 0000000..f791b64 --- /dev/null +++ b/Messaging/mqttsession.py @@ -0,0 +1,25 @@ +import paho.mqtt.client as mqtt + +client = None +host = None + +# Arguably not needed, just want to make the client static, but here anyway. +def connect(): + if client is None or host is None: + print("Error: Client and/or host are not initialised.") + else: + client.connect(host, port=1883, keepalive=60, bind_address="") + client.loop_start() + +def disconnect(): + if client is not None: + client.loop_stop() + client.disconnect() + else: + print("Error: Client is not initialised.") + +def Client(): + if client is not None: + return mqtt.Client() + else: + return client diff --git a/Messaging/packmessage.py b/Messaging/packmessage.py new file mode 100644 index 0000000..23b9b83 --- /dev/null +++ b/Messaging/packmessage.py @@ -0,0 +1,14 @@ +import umsgpack +from messaginginterface import Message + +class PackMessage(Message): + + def __init__(self, message=None): + self.message = message + + def serialise(self): + return umsgpack.packb(self.message) + + def deserialise(self): + return umsgpack.unpackb(self.message) +