Added Mobile Alert Sensors
This commit is contained in:
@@ -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,23 +68,15 @@ 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()
|
||||||
|
|||||||
Reference in New Issue
Block a user