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)
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 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):
#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"])
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):
@@ -64,15 +68,6 @@ 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
@@ -84,3 +79,4 @@ class Mqtt2ThingBoard:
Mqtt2ThingBoardGateWay = Mqtt2ThingBoard()
Mqtt2ThingBoardGateWay.loop()