Add 'car/' from commit 'eee0e8dc445691e600680f4abc77f2814b20b054'

git-subtree-dir: car
git-subtree-mainline: 1d29a5526c
git-subtree-split: eee0e8dc44
This commit is contained in:
Piv
2020-04-19 11:07:44 +09:30
93 changed files with 8401 additions and 0 deletions

View File

View File

@@ -0,0 +1,64 @@
import zmq
class ZmqPubSubStreamer:
'''
Not thread-safe. Always get this inside the thread/process where you intend
to use it.
'''
def __init__(self, port):
self._socket = zmq.Context.instance().socket(zmq.PUB)
print('Starting socket with address: ' + 'tcp://*:' + str(port))
self._socket.bind("tcp://*:" + str(port))
def send_message(self, message):
'''
Args
----
message: A message type that has the serialise() method.
'''
self.send_message_topic("", message)
def send_message_topic(self, topic, message):
self._socket.send_multipart([bytes(topic), message.serialise()])
class BluetoothStreamer:
def __init__(self):
pass
def send_message(self, message_bytes):
pass
class TestStreamer:
def __init__(self):
self._listeners = []
def send_message(self, message_bytes):
print('Got a message')
def send_message_topic(self, topic, message):
print('Got a message with topic: ' + str(topic))
self._fire_message_received(message)
def add_message_listener(self, listener):
self._listeners.append(listener)
def _fire_message_received(self, message):
for listener in self._listeners:
listener(message)
def getZmqPubSubStreamer(port):
'''
Not thread-safe. Always get this inside the thread/process where you intend
to use it.
'''
return ZmqPubSubStreamer(port)
def getTestingStreamer():
return TestStreamer()
# TODO: Create a general get method that will get the streamer based on an
# environment variable that is set.

34
car/Messaging/messages.py Normal file
View File

@@ -0,0 +1,34 @@
import umsgpack
class Message():
def __init__(self, message=None):
self.message = message
def serialise(self):
raise NotImplementedError
def deserialise(self, message):
raise NotImplementedError
class PackMessage(Message):
def serialise(self):
return umsgpack.packb(self.message)
def deserialise(self, message):
return PackMessage(umsgpack.unpackb(self.message))
class ProtoMessage(Message):
def __init__(self, proto_type=None, message=None):
super().__init__(message)
self._type = proto_type
def serialise(self):
return self.message.SerializeToString()
def deserialise(self, message):
return ProtoMessage(self._type, self._type.ParseFromString(message))

View File

@@ -0,0 +1,64 @@
import paho.mqtt.client as mqtt
"""
Wrapper module for paho mqtt library, providing a singleton instance of the client to be used.
Also adds some convenience functions such as having multiple connected callbacks,
and managing whether the client is still connected.
"""
client = mqtt.Client()
host = None
connect_callbacks = []
disconnect_callbacks = []
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
if rc == 0:
global connected
connected = True
for callback in connect_callbacks:
callback()
client.subscribe('hello/test', qos=1)
# Arguably not needed, just want to make the client static, but here anyway.
def connect():
global client
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 add_connect_callback(callback):
global connect_callbacks
connect_callbacks += callback
connectted = True
def add_disconnect_callback(callback):
global
def disconnect():
global client
if client is not None:
client.loop_stop()
client.disconnect()
else:
print("Error: Client is not initialised.")
def on_disconnect(client, userdata, rc):
if rc != 0:
print("Unexpected disconnection.")
global connected
connected = False
def Client():
global client
if client is None:
client = mqtt.Client()
return client