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):
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): def onMqttMessage(self, client, userdata, message):
#currently only react on trigger="ON" msg = str(message.payload.decode("utf-8"))
if (str(message.payload.decode("utf-8")) == "ON"): topics = message.topic.split("/")
self.mqtt_client.publish(message.topic,"OFF", 0,True) #print(f"{message.topic}: {msg}")
for s in self.Scenarios: if (topics[0]=="stat" and topics[2]=="MA"):
topic = "bsh/scenarios/"+s["name"].replace(" ","_")+"/trigger" self.handleMobileAlertsStatus(topics,msg)
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 #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):
@@ -64,15 +68,6 @@ 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
@@ -84,3 +79,4 @@ class Mqtt2ThingBoard:
Mqtt2ThingBoardGateWay = Mqtt2ThingBoard() Mqtt2ThingBoardGateWay = Mqtt2ThingBoard()
Mqtt2ThingBoardGateWay.loop()