blob: 592cfbcb90358097d5d89a9223e7a92402b7733a [file] [log] [blame]
Shad Ansarif53bc4f2022-06-07 14:31:11 -07001"""
2SPDX-FileCopyrightText: 2022-present Intel Corporation
3SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org>
4SPDX-License-Identifier: Apache-2.0
5"""
Shad Ansari6c155c42021-11-17 19:48:02 +00006import paho.mqtt.client as mqtt
7import time
8import os
9import sys
10
11class Mqtt:
12
13 def __init__(self, ip, port):
14 self.topic = "camera/" + port
15 self.ip = ip
16 self.client = mqtt.Client()
17 self.client.on_connect = self.on_connect
18 self.client.on_message = self.on_message
19
20 # The callback for when the client receives a CONNACK response from the server.
21 def on_connect(self, client, userdata, flags, rc):
22 print("Connected to {} with result code ".format(self.ip, str(rc)))
23 # Subscribing in on_connect() means that if we lose the connection and
24 # reconnect then subscriptions will be renewed.
25 client.subscribe(self.topic)
26
27 # The callback for when a PUBLISH message is received from the server.
28 def on_message(self, client, userdata, msg):
29 cmd = msg.payload.decode()
30 print(msg.topic+" "+cmd)
31 if cmd == "high":
32 print("Change to high resolution")
33 os.system("sudo systemctl stop openvino-camera@low.service")
34 os.system("sudo systemctl start openvino-camera@high.service")
35 elif cmd == "low":
36 print("Change to low resolution")
37 os.system("sudo systemctl stop openvino-camera@high.service")
38 os.system("sudo systemctl start openvino-camera@low.service")
39 else:
40 print("Unknown cmd: " + cmd)
41
42 def start(self):
43 while True:
44 try:
45 self.client.connect(self.ip)
46 except:
47 time.sleep(1)
48 continue
49 break
50 self.client.loop_forever()
51
52if __name__ == '__main__':
53 mqtt = Mqtt(sys.argv[1], sys.argv[2])
54 mqtt.start()