rework mqtt api
This commit is contained in:
parent
c9602ba3e6
commit
3cd7167e90
@ -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__":
|
||||||
|
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user