Change to double quotes and implement unicast messaging
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import paho.mqtt.client as mqtt
|
import paho.mqtt.client as mqtt
|
||||||
import json
|
import json
|
||||||
|
import random
|
||||||
|
|
||||||
class Messenger:
|
class Messenger:
|
||||||
_connect_callbacks = []
|
_connect_callbacks = []
|
||||||
@@ -9,93 +10,90 @@ class Messenger:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def broadcast_message(self, topic, message):
|
def broadcast_message(self, message):
|
||||||
'''
|
"""
|
||||||
Broadcasts the specified message to the swarm based upon its topic(or group).
|
Broadcasts the specified message to the swarm based upon its topic(or group).
|
||||||
'''
|
"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def unicast_message(self, target, message):
|
def unicast_message(self, target, message):
|
||||||
'''
|
"""
|
||||||
Broadcasts the specified message to the single target.
|
Broadcasts the specified message to the single target.
|
||||||
'''
|
"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
'''
|
"""
|
||||||
Connect to the swarm.
|
Connect to the swarm.
|
||||||
'''
|
"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def disconnect(self):
|
def disconnect(self):
|
||||||
'''
|
"""
|
||||||
Disconnect from the swarm.
|
Disconnect from the swarm.
|
||||||
'''
|
"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def add_connect(self, connect):
|
def add_connect(self, connect):
|
||||||
'''
|
"""
|
||||||
Adds a callback to do something else once we are connected.
|
Adds a callback to do something else once we are connected.
|
||||||
'''
|
"""
|
||||||
self._connect_callbacks.append(connect)
|
self._connect_callbacks.append(connect)
|
||||||
|
|
||||||
def on_connect(self, code = None):
|
def on_connect(self, code = None):
|
||||||
'''
|
"""
|
||||||
Called once the messenger connects to the swarm.
|
Called once the messenger connects to the swarm.
|
||||||
'''
|
"""
|
||||||
for cb in self._connect_callbacks:
|
for cb in self._connect_callbacks:
|
||||||
cb(code)
|
cb(code)
|
||||||
|
|
||||||
def on_disconnect(self, code = None):
|
def on_disconnect(self, code = None):
|
||||||
'''
|
"""
|
||||||
Called when the messenger is disconnected from the swarm.
|
Called when the messenger is disconnected from the swarm.
|
||||||
'''
|
"""
|
||||||
for cb in self._disconnect_callbacks:
|
for cb in self._disconnect_callbacks:
|
||||||
cb(code)
|
cb(code)
|
||||||
|
|
||||||
def add_disconnect_callback(self, on_disconnect):
|
def add_disconnect_callback(self, on_disconnect):
|
||||||
'''
|
"""
|
||||||
Adds a callback for when the messenger is disconnected.
|
Adds a callback for when the messenger is disconnected.
|
||||||
'''
|
"""
|
||||||
self._disconnect_callbacks.append(on_disconnect)
|
self._disconnect_callbacks.append(on_disconnect)
|
||||||
|
|
||||||
def add_message_callback(self, on_message):
|
def add_message_callback(self, on_message):
|
||||||
'''
|
"""
|
||||||
Adds a callback
|
Adds a callback
|
||||||
'''
|
"""
|
||||||
self._message_callbacks.append(on_message)
|
self._message_callbacks.append(on_message)
|
||||||
|
|
||||||
def on_message(self, message):
|
def on_message(self, message):
|
||||||
'''
|
"""
|
||||||
Called when the messenger receives a message.
|
Called when the messenger receives a message.
|
||||||
'''
|
"""
|
||||||
for cb in self._message_callbacks:
|
for cb in self._message_callbacks:
|
||||||
cb(message)
|
cb(message)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def id(self):
|
def id(self):
|
||||||
'''
|
"""
|
||||||
The id for this messenger that is being used in communication.
|
The id for this messenger that is being used in communication.
|
||||||
'''
|
"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def swarm(self):
|
def swarm(self):
|
||||||
'''
|
"""
|
||||||
Gets the name of the swarm this instance is a part of.
|
Gets the name of the swarm this instance is a part of.
|
||||||
'''
|
"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
class MqttMessenger(Messenger):
|
class MqttMessenger(Messenger):
|
||||||
'''
|
"""A messenger that uses MQTT."""
|
||||||
A messenger that uses MQTT.
|
|
||||||
'''
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
with open('config.json') as json_config:
|
with open('config.json') as json_config:
|
||||||
self._cfg = json.load(json_config)
|
self._cfg = json.load(json_config)
|
||||||
|
self._client = mqtt.Client(client_id=str(random.randint(0,500)))
|
||||||
self._client = mqtt.Client()
|
|
||||||
self._client.on_connect = self.on_connect
|
self._client.on_connect = self.on_connect
|
||||||
self._client.on_message = self.on_message
|
self._client.on_message = self.on_message
|
||||||
self._client.on_disconnect = self.on_disconnect
|
self._client.on_disconnect = self.on_disconnect
|
||||||
@@ -106,13 +104,19 @@ class MqttMessenger(Messenger):
|
|||||||
def on_connect(self, client, userdata, flags, rc):
|
def on_connect(self, client, userdata, flags, rc):
|
||||||
# Subscribe to the swarm specified in the config.
|
# Subscribe to the swarm specified in the config.
|
||||||
self._client.subscribe(self._cfg['mqtt']['swarm'])
|
self._client.subscribe(self._cfg['mqtt']['swarm'])
|
||||||
|
|
||||||
|
# Also subscribe to our own topic for unicast messages.
|
||||||
|
self._client.subscribe(self._cfg['mqtt']['swarm'] + str(self._client._client_id))
|
||||||
Messenger.on_connect(self, rc)
|
Messenger.on_connect(self, rc)
|
||||||
|
|
||||||
def on_disconnect(self, client, userdata, rc):
|
def on_disconnect(self, client, userdata, rc):
|
||||||
Messenger.on_disconnect(self, rc)
|
Messenger.on_disconnect(self, rc)
|
||||||
|
|
||||||
def broadcast_message(self, topic, message):
|
def broadcast_message(self, message):
|
||||||
self._client.publish(topic, message, qos=1)
|
self._client.publish(self._cfg['mqtt']['swarm'], message, qos=1)
|
||||||
|
|
||||||
|
def unicast_message(self, target, message):
|
||||||
|
self._client.publish(target, message, qos=1)
|
||||||
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
self._client.connect(self._cfg['mqtt']['host'], \
|
self._client.connect(self._cfg['mqtt']['host'], \
|
||||||
|
|||||||
Reference in New Issue
Block a user