Compare commits

..

2 Commits

Author SHA1 Message Date
c8c96f3007
Merge branch 'master' of gitea:birbnetes/guard-service
All checks were successful
continuous-integration/drone/push Build is passing
2020-07-26 19:38:17 +02:00
3cd7167e90
rework mqtt api 2020-07-26 19:38:10 +02:00
2 changed files with 8 additions and 3 deletions

View File

@ -45,7 +45,8 @@ def on_message(channel, method_frame, header_frame, body):
if msg_json['probability'] > 0.5: if msg_json['probability'] > 0.5:
r = requests.get(f"http://{config.INPUT_HOSTNAME}/sample/{msg_json['tag']}") r = requests.get(f"http://{config.INPUT_HOSTNAME}/sample/{msg_json['tag']}")
r.raise_for_status() r.raise_for_status()
mqtt.publish(json.dumps({"deviceID": r.json()['device_id'], "sensorID": "", "command": "doAlert"})) mqtt.publish(subtopic=r.json()['device_id'],
message=json.dumps({"command": "doAlert"}))
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -61,10 +61,14 @@ class MQTT:
transport="tcp") transport="tcp")
self.client.connect(host=self.host, port=self.port, keepalive=60) self.client.connect(host=self.host, port=self.port, keepalive=60)
def publish(self, message: str) -> None: def publish(self, message: str, subtopic: str = "") -> None:
""" """
Publish a message Publish a message
:param message: :param message:
:param subtopic:
:return: :return:
""" """
if subtopic:
self.client.publish(f"{self.topic}/{subtopic}", message, qos=self.qos, retain=self.retain)
else:
self.client.publish(self.topic, message, qos=self.qos, retain=self.retain) self.client.publish(self.topic, message, qos=self.qos, retain=self.retain)