derberg / python-mqtt-client-template

This template generates MQTT Python client module. Its purpose is to abstract for the application client technical information about the broker and the names of topics it should use. Instead, it generates a module that provides a set of indent-driven functions that one can invoke to send a message to the application through the message broker.
4 stars 2 forks source link

feat: add support for receive operation #12

Closed thulieblack closed 7 months ago

thulieblack commented 1 year ago
  1. Update Topic.js Function with subscribe code
  2. Extend the AsyncAPI.yml by adding an additional channel
  3. Channel comment/views get total views count

solve #4

thulieblack commented 1 year 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:

  1. Find a way to get comment/views count as it keeps saying 0
  2. I only made sure that we subscribe to comment/views not sure if this is correct
  3. Clean-up code after review.
  4. Sorry @derberg for the workload 😁😁 I tried
thulieblack commented 1 year ago

@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
thulieblack commented 10 months ago

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)