Skip to content

Commit 13cbe80

Browse files
committed
Use plain-text json instead of bson (DISCE demo hack)
1 parent f0f68f6 commit 13cbe80

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

cpopservice/core/models.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
import abc
2+
import json
23

34
import bson
45

56

67
class Serializable(abc.ABC):
78

8-
def to_bson(self):
9+
def to_bson(self) -> bytes:
910
raise NotImplementedError
1011

1112

1213
class BaseModel(Serializable):
1314
def __init__(self, params={}):
1415
self.__dict__ = params
1516

16-
def to_bson(self):
17-
result = bson.dumps(self.__dict__)
17+
def to_bson(self) -> bytes:
18+
# result = bson.dumps(self.__dict__)
19+
result = json.dumps(self.__dict__).encode('UTF-8')
1820
return result
1921

2022
@classmethod

cpopservice/core/pubsub.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import logging
2+
from socket import gethostname
23

34
import paho.mqtt.client as mqtt
5+
import paho.mqtt.subscribe as subscribe
46

57
from cpopservice import config
68
from cpopservice.core.models import Serializable
@@ -32,9 +34,24 @@ def name():
3234
return 'mqtt'
3335

3436
def __init__(self):
35-
self.client = mqtt.Client()
36-
self.client.connect(config.BROKER_HOST, config.BROKER_PORT)
37+
self.client_id = 'cpop-service-%s' % gethostname()
38+
self.client = mqtt.Client(client_id=self.client_id)
39+
self.client.connect(config.BROKER_HOST, config.BROKER_PORT, keepalive=30)
3740

3841
def publish_event(self, event: Serializable):
3942
LOG.debug('publishing message to topic %s: %s', config.MQTT_TOPIC_NAME, event)
4043
self.client.publish(config.MQTT_TOPIC_NAME, event.to_bson())
44+
45+
46+
class CPOPSubscriberMQTT:
47+
48+
def __init__(self, callback=None):
49+
self.callback = callback
50+
pass
51+
52+
def listen(self):
53+
subscribe.callback(self.on_message, config.MQTT_TOPIC_NAME, hostname=config.BROKER_HOST,
54+
port=config.BROKER_PORT)
55+
56+
def on_message(self, client, userdata, message):
57+
print(message)

0 commit comments

Comments
 (0)