Implementing stuff (I suck at commits sometimes lol)

This commit is contained in:
DSTO\pivatom
2018-12-13 11:00:27 +10:30
parent 750624fe2b
commit 6f0bd56b4a
9 changed files with 146 additions and 19 deletions

View File

@@ -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")
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)

View File

@@ -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

View File

@@ -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
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

View File

@@ -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

View File

@@ -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])

View File

@@ -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

View File

@@ -1,2 +1,11 @@
class Message:
"""
Message Format
"""
def serialise(self):
NotImplementedError
def deserialise(self):
NotImplementedError

25
Messaging/mqttsession.py Normal file
View File

@@ -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

14
Messaging/packmessage.py Normal file
View File

@@ -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)