blob: 592cfbcb90358097d5d89a9223e7a92402b7733a [file] [log] [blame]
"""
SPDX-FileCopyrightText: 2022-present Intel Corporation
SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org>
SPDX-License-Identifier: Apache-2.0
"""
import paho.mqtt.client as mqtt
import time
import os
import sys
class Mqtt:
def __init__(self, ip, port):
self.topic = "camera/" + port
self.ip = ip
self.client = mqtt.Client()
self.client.on_connect = self.on_connect
self.client.on_message = self.on_message
# The callback for when the client receives a CONNACK response from the server.
def on_connect(self, client, userdata, flags, rc):
print("Connected to {} with result code ".format(self.ip, str(rc)))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe(self.topic)
# The callback for when a PUBLISH message is received from the server.
def on_message(self, client, userdata, msg):
cmd = msg.payload.decode()
print(msg.topic+" "+cmd)
if cmd == "high":
print("Change to high resolution")
os.system("sudo systemctl stop openvino-camera@low.service")
os.system("sudo systemctl start openvino-camera@high.service")
elif cmd == "low":
print("Change to low resolution")
os.system("sudo systemctl stop openvino-camera@high.service")
os.system("sudo systemctl start openvino-camera@low.service")
else:
print("Unknown cmd: " + cmd)
def start(self):
while True:
try:
self.client.connect(self.ip)
except:
time.sleep(1)
continue
break
self.client.loop_forever()
if __name__ == '__main__':
mqtt = Mqtt(sys.argv[1], sys.argv[2])
mqtt.start()