Closed thulieblack closed 7 months ago
Okay, honestly, I'm not good with this debugging stuff. I tried my best to make half of the code readable and correct.
Challenges:
@derberg the genereted client file
import paho.mqtt.client as mqtt
mqttBroker = "test.mosquitto.org"
class CommentsServiceClient:
def __init__(self):
self.client = mqtt.Client()
self.view_count = 0
self.subscribe("comment/views")
self.client.connect(mqttBroker)
def sendCommentLiked(self, id):
topic = "comment/liked"
self.client.publish(topic, id)
def sendCommentUnliked(self, id):
topic = "comment/unliked"
self.client.publish(topic, id)
def on_message(self, client, userdata, message):
topic = message.topic
payload = message.payload.decode("utf-8")
if topic == "comment/views":
self.view_count += 1
def subscribe(self, topic):
self.client.subscribe(topic)
self.client.on_message = self.on_message
okay, so far, the code works without being hardcoded
The next and Final phase is to figure out how the client can stop generating both channels for subscribe and publish functions.
As it stands, this how the current code generates/produces the client file:
import paho.mqtt.client as mqtt
mqttBroker = "test.mosquitto.org"
class CommentsServiceClient:
def __init__(self):
self.client = mqtt.Client()
self.client.connect(mqttBroker)
def receiveCommentViews(self, callback):
topic = "comment/views"
self.client.subscribe(topic)
self.client.message_callback_add(topic, callback)
def sendCommentViews(self, id):
topic = "comment/views"
self.client.publish(topic, id)
def receiveCommentLiked(self, callback):
topic = "comment/liked"
self.client.subscribe(topic)
self.client.message_callback_add(topic, callback)
def receiveCommentUnliked(self, callback):
topic = "comment/unliked"
self.client.subscribe(topic)
self.client.message_callback_add(topic, callback)
def sendCommentLiked(self, id):
topic = "comment/liked"
self.client.publish(topic, id)
def sendCommentUnliked(self, id):
topic = "comment/unliked"
self.client.publish(topic, id)
solve #4