Итак, я работаю над MQTT Publisher и хочу создать класс паба.
Я нашел похожий вопрос здесь , но я не знаю, как он отвечает моей проблеме.
Это мой код:
def send_on_sensor(q, topic, delay, pub):
while q.empty is not True:
payload = json.dumps(q.get())
pub.publish(payload)
time.sleep(delay)
class Pub:
def __init__(self, MQTT_BROKER, MQTT_TOPIC):
self.MQTT_BROKER = MQTT_BROKER
self.MQTT_TOPIC = MQTT_TOPIC
self.mqttc = mqtt.Client()
self.mqttc.on_connect = self.on_connect
self.mqttc.publish = self.publish
# Connect automatically on the creation of the object, but disconnect manually
self.connect()
def on_connect(self, client, userdata, flags, rc):
print("Connecting to {}. Connection returned result: {}".format(self.MQTT_BROKER, rc))
self.mqttc.subscribe(self.MQTT_TOPIC)
def publish(self, MQTT_MSG):
self.mqttc.publish(self.MQTT_TOPIC, MQTT_MSG)
def connect(self):
self.mqttc.connect(self.MQTT_BROKER)
def disconnect(self):
self.mqttc.disconnect()
Я получаю эту ошибку:
Exception in thread Thread-4:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "/Users//PycharmProjects//V3_multiTops/mt_GenPub.py", line 76, in send_on_sensor
pub.publish(payload)
File "/Users//PycharmProjects//V3_multiTops/mt_GenPub.py", line 129, in publish
self.mqttc.publish(self.MQTT_TOPIC, MQTT_MSG)
TypeError: publish() takes 2 positional arguments but 3 were given
В чем именно заключается проблема, и как мне ее исправить?