You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
874 B

import json
import paho.mqtt.client as mqtt
from .base import MQTTClientBase
class MQTTClient(MQTTClientBase):
def _connect(self):
self._mqtt = mqtt.Client(self.client_id)
self._mqtt.username_pw_set(self.user, self.password)
self._mqtt.on_connect = self.on_connect
self._mqtt.connect(self.host)
def on_connect(self, *args, **kwargs):
#print("Connected! client={} userdata={} msg={}".format(client, userdata, msg))
print("Connected!", *args, **kwargs)
def send_data(self, topic, data):
self._mqtt.publish(topic, json.dumps(data))
def set_callback(self, callback):
self._mqtt.on_message = lambda client, userdata, msg: callback(msg.topic, msg.payload.decode())
def process_mqtt(self):
self._mqtt.loop(0.01)
def subscribe(self, topic):
self._mqtt.subscribe(topic)