initial check-in
This commit is contained in:
79
Mqtt2ThingBoardGateWay.py
Normal file
79
Mqtt2ThingBoardGateWay.py
Normal file
@@ -0,0 +1,79 @@
|
||||
#!/usr/bin/python
|
||||
#Bosch Home Automation <> MQTT Gateway
|
||||
|
||||
import requests, json, time
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
class Mqtt2ThingBoard:
|
||||
|
||||
#Mosquitto Settings
|
||||
client_name = "Mqtt2ThingBoardGateWay"
|
||||
host_name = "192.168.178.36"
|
||||
host_port = 1884
|
||||
|
||||
mqtt_client = mqtt.Client(client_name)
|
||||
|
||||
tb_gateway_user = "K9iW4T5E7tVeP4ZyapxB"
|
||||
tb_port = 1883
|
||||
tb_client = mqtt.Client(tb_gateway_user)
|
||||
|
||||
#Sent MQTT Debugging Message
|
||||
def publishDebugMsg(self,msg):
|
||||
self.mqtt_client.publish("Mqtt2ThingBoardGateWay/debug",msg,0,True)
|
||||
print(msg)
|
||||
|
||||
#Sent message to MQTT telemetry
|
||||
def publishTelemetryMsg(self,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"])
|
||||
|
||||
#subscribe to message to trigger scenarios
|
||||
def subscribeToMqttInputs(self):
|
||||
pass
|
||||
#TODO
|
||||
|
||||
#set up the system: login to MQTT + get all needed data from the BSH
|
||||
def __init__(self):
|
||||
self.publishDebugMsg("Connecting to MQTT Server")
|
||||
self.mqtt_client.connect(self.host_name,self.host_port)
|
||||
|
||||
self.publishDebugMsg("Connecting to ThingsBoard Mqtt Gateway")
|
||||
self.tb_client.connect(self.host_name,self.tb_port)
|
||||
|
||||
self.publishDebugMsg("Setting up the Environment...")
|
||||
|
||||
self.publishDebugMsg("Subscribe to MQTT Messages")
|
||||
self.subscribeToMqttInputs()
|
||||
|
||||
self.publishTelemetryMsg("{\"TempSensorBuero\":[{\"temperature\":11.8}]}")
|
||||
|
||||
|
||||
def loop(self):
|
||||
#Restart Long Polling every second
|
||||
while True:
|
||||
self.mqtt_client.loop_start()
|
||||
time.sleep(1)
|
||||
self.mqtt_client.loop_stop()
|
||||
|
||||
|
||||
|
||||
Mqtt2ThingBoardGateWay = Mqtt2ThingBoard()
|
||||
Mqtt2ThingBoardGateWay.loop()
|
||||
Reference in New Issue
Block a user