Added Mobile Alert Sensors

This commit is contained in:
2023-08-28 18:42:33 +02:00
parent 8e11c2a992
commit 272b82d463

View File

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