diff --git a/Mqtt2ThingBoardGateWay.py b/Mqtt2ThingBoardGateWay.py index b9cd6dd..c90185b 100644 --- a/Mqtt2ThingBoardGateWay.py +++ b/Mqtt2ThingBoardGateWay.py @@ -23,33 +23,37 @@ class Mqtt2ThingBoard: self.mqtt_client.publish("Mqtt2ThingBoardGateWay/debug",msg,0,True) print(msg) - #Sent message to MQTT telemetry + #Sent message to MQTT ThingsBoard Gateway def publishTelemetryMsg(self,msg): - print(msg) self.tb_client.publish("v1/gateway/telemetry",msg,0,True) - #react to MQTT scenario requests: - def onMqttMessage(self, client, userdata, message): - #currently only react on trigger="ON" - if (str(message.payload.decode("utf-8")) == "ON"): - self.mqtt_client.publish(message.topic,"OFF", 0,True) - for s in self.Scenarios: - topic = "bsh/scenarios/"+s["name"].replace(" ","_")+"/trigger" - if (topic == message.topic): - self.publishDebugMsg(" Scenario triggered: "+s["name"]) - url = self.baseurl + "/smarthome/scenarios/"+s["id"]+"/triggers" - postRequest = requests.post(url, data="", headers = self.getURLHeaders(), - verify = False, cert = self.getCertificate()) - data = json.loads(postRequest.text)[0]['result'] - if "errorCode" in data: - self.publishDebugMsg("Error occured during Scenario Call:") - self.publishDebugMsg(data["errorCode"]) + def handleMobileAlertsStatus(self,topics,msg): + device = topics[1]+"_"+topics[2] + msg = { + device: [ + { + topics[3]: msg + } + ] + } + print(f"{device}: {json.dumps(msg)}") + self.publishTelemetryMsg(json.dumps(msg)) + + #react to MQTT incoming message + def onMqttMessage(self, client, userdata, message): + msg = str(message.payload.decode("utf-8")) + topics = message.topic.split("/") + #print(f"{message.topic}: {msg}") + if (topics[0]=="stat" and topics[2]=="MA"): + self.handleMobileAlertsStatus(topics,msg) + #subscribe to message to trigger scenarios def subscribeToMqttInputs(self): - pass - #TODO + self.mqtt_client.subscribe('#') + self.mqtt_client.on_message = self.onMqttMessage + #set up the system: login to MQTT + get all needed data from the BSH def __init__(self): @@ -63,24 +67,16 @@ class Mqtt2ThingBoard: self.publishDebugMsg("Subscribe to MQTT Messages") self.subscribeToMqttInputs() - - msg = { - 'TempSensorBuero': [ - { - 'temperature': 33.8 - } - ] - } - self.publishTelemetryMsg(json.dumps(msg)) def loop(self): - #Restart Long Polling every second - while True: - self.mqtt_client.loop_start() - time.sleep(1) - self.mqtt_client.loop_stop() + #Restart Long Polling every second + while True: + self.mqtt_client.loop_start() + time.sleep(1) + self.mqtt_client.loop_stop() Mqtt2ThingBoardGateWay = Mqtt2ThingBoard() +Mqtt2ThingBoardGateWay.loop()