-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnifiVideo.py
73 lines (63 loc) · 2.5 KB
/
UnifiVideo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# mqtt References
import time
import paho.mqtt.client as mqtt
# api references
import requests
import json
#defaults
FailureRestart = "10"
def on_message(client, userdata, message):
for L1 in Config['MQTT_Subscribe']:
for L2 in L1['Rec']:
MQTT_Subscribe = L2['MQTT']
if message.topic == MQTT_Subscribe:
print("matched " + message.topic )
for L3 in L2['data']:
CameraTag = L3['CameraTag']
CameraID = L3['CameraID']
CameraName = L3['CameraName']
CameraTagPrefix = L3['CameraTagPrefix']
CameraTagPostfix = L3['CameraTagPostfix']
s = requests.Session()
url = "http://" + Host + "/api/2.0/camera/" + CameraID + "?apiKey=" + API_Key
MessageString = CameraTag + CameraTagPrefix + str(message.payload.decode("utf-8")) + CameraTagPostfix
data = { "name": CameraName, "osdSettings": { "tag": MessageString } }
headers = {"Content-Type" : "application/json"}
response = requests.put(url, data=json.dumps(data), headers=headers )
MessageSent = 'request: ' + str(response.status_code) + ', CameraID: ' + CameraID + ', Message:' + MessageString
print(MessageSent)
while True:
try:
print("........................")
print("Starting")
with open('UnifiCamera.json','r') as ConfigFile:
Config = json.load(ConfigFile)
ConfigMain = Config['Config']
print("........................")
API_Key = ConfigMain[0]["API_Key"]
Host = ConfigMain[0]["Host"]
FailureRestart = ConfigMain[0]["FailureRestart"]
MQTT_ForceReconnect = ConfigMain[0]["MQTT_ForceReconnect"]
#Broker Config
MQTT_Broker = ConfigMain[0]["MQTT_Broker"]
MQTT_Name = ConfigMain[0]["MQTT_Name"]
MQTT_Subscription = ConfigMain[0]["MQTT_Subscription"]
print("FailureRestart: " + FailureRestart)
print("MQTT_ForceReconnect: " + MQTT_ForceReconnect)
print("MQTT_Broker: " + MQTT_Broker)
print("MQTT_Name: " + MQTT_Name)
print("MQTT_Subscription: " + MQTT_Subscription)
print("API_Key: " + API_Key)
print("........................")
print("creating new instance")
client = mqtt.Client(MQTT_Name) #create new instance
client.on_message=on_message #attach function to callback
print("connecting to broker " + MQTT_Broker + " with subscription=" + MQTT_Subscription)
client.connect(MQTT_Broker) #connect to broker
client.loop_start() #start the loop
print("Subscribing to topic",MQTT_Subscription)
client.subscribe(MQTT_Subscription)
time.sleep(int(MQTT_ForceReconnect)) # wait
client.loop_stop() #stop the loop
except:
time.sleep(FailureRestart) # wait