rework mqtt api

This commit is contained in:
Torma Kristóf 2020-07-26 19:38:10 +02:00
parent c9602ba3e6
commit 3cd7167e90
Signed by: tormakris
GPG Key ID: DC83C4F2C41B1047
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:
""" """
self.client.publish(self.topic, message, qos=self.qos, retain=self.retain) 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)