Shad Ansari | f53bc4f | 2022-06-07 14:31:11 -0700 | [diff] [blame^] | 1 | """ |
| 2 | SPDX-FileCopyrightText: 2022-present Intel Corporation |
| 3 | SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org> |
| 4 | SPDX-License-Identifier: Apache-2.0 |
| 5 | """ |
Shad Ansari | 6c155c4 | 2021-11-17 19:48:02 +0000 | [diff] [blame] | 6 | import paho.mqtt.client as mqtt |
| 7 | import time |
| 8 | import os |
| 9 | import sys |
| 10 | |
| 11 | class 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 | |
| 52 | if __name__ == '__main__': |
| 53 | mqtt = Mqtt(sys.argv[1], sys.argv[2]) |
| 54 | mqtt.start() |