Shad Ansari | 6c155c4 | 2021-11-17 19:48:02 +0000 | [diff] [blame] | 1 | import paho.mqtt.client as mqtt |
| 2 | import time |
| 3 | import os |
| 4 | import sys |
| 5 | |
| 6 | class Mqtt: |
| 7 | |
| 8 | def __init__(self, ip, port): |
| 9 | self.topic = "camera/" + port |
| 10 | self.ip = ip |
| 11 | self.client = mqtt.Client() |
| 12 | self.client.on_connect = self.on_connect |
| 13 | self.client.on_message = self.on_message |
| 14 | |
| 15 | # The callback for when the client receives a CONNACK response from the server. |
| 16 | def on_connect(self, client, userdata, flags, rc): |
| 17 | print("Connected to {} with result code ".format(self.ip, str(rc))) |
| 18 | # Subscribing in on_connect() means that if we lose the connection and |
| 19 | # reconnect then subscriptions will be renewed. |
| 20 | client.subscribe(self.topic) |
| 21 | |
| 22 | # The callback for when a PUBLISH message is received from the server. |
| 23 | def on_message(self, client, userdata, msg): |
| 24 | cmd = msg.payload.decode() |
| 25 | print(msg.topic+" "+cmd) |
| 26 | if cmd == "high": |
| 27 | print("Change to high resolution") |
| 28 | os.system("sudo systemctl stop openvino-camera@low.service") |
| 29 | os.system("sudo systemctl start openvino-camera@high.service") |
| 30 | elif cmd == "low": |
| 31 | print("Change to low resolution") |
| 32 | os.system("sudo systemctl stop openvino-camera@high.service") |
| 33 | os.system("sudo systemctl start openvino-camera@low.service") |
| 34 | else: |
| 35 | print("Unknown cmd: " + cmd) |
| 36 | |
| 37 | def start(self): |
| 38 | while True: |
| 39 | try: |
| 40 | self.client.connect(self.ip) |
| 41 | except: |
| 42 | time.sleep(1) |
| 43 | continue |
| 44 | break |
| 45 | self.client.loop_forever() |
| 46 | |
| 47 | if __name__ == '__main__': |
| 48 | mqtt = Mqtt(sys.argv[1], sys.argv[2]) |
| 49 | mqtt.start() |