This repository has been archived by the owner on Apr 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqttlistener.py
72 lines (60 loc) · 2.14 KB
/
mqttlistener.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
#!/usr/bin/python3
import json
import time
import yaml
import tempfile
import filecmp
import os.path
import paho.mqtt.client as mqtt
import shutil
import argparse
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
def on_message(client, userdata, msg):
print (f"mesg: {msg.topic}")
basename = f"/config/www/frigate_history/{msg.topic.replace('/','_')}"
try:
if not os.path.isdir("/config/www/frigate_history"):
os.mkdir(basename)
except OSError as error:
print (error)
try:
if not os.path.isdir(basename):
os.mkdir(basename)
except OSError as error:
print (error)
with tempfile.NamedTemporaryFile() as tempf:
tempf.write(msg.payload)
latest = f"{basename}/1.jpg"
if os.path.isfile(latest):
if filecmp.cmp(tempf.name,latest):
print('duplicate image, ignoring')
return
else:
with open(latest,'wb') as outfile:
print('write image to disk')
outfile.write(msg.payload)
for i in range(HISTORY_SIZE -1,0,-1):
source = f"{basename}/{i}.jpg"
target = f"{basename}/{i+1}.jpg"
print ("Rock & Roll history")
if not os.path.isfile(source):
shutil.copy(tempf.name,source)
shutil.copy(source,target)
shutil.copy(tempf.name,f"{basename}/1.jpg")
def on_subscribe(mqttc, obj, mid, granted_qos):
print("Subscribed: " + str(mid) + " " + str(granted_qos))
def on_log(mqttc, obj, level, string):
print(string)
HISTORY_SIZE = int(os.environ.get('HISTORY_SIZE',20))
MQTT_PORT = int(os.environ.get('MQTT_PORT',1883))
topic = f"{os.environ.get('MQTT_TOPIC_PREFIX', 'frigate')}/+/+/snapshot"
client = mqtt.Client()
client.username_pw_set(username=os.environ.get('MQTT_USER'),password=os.environ.get('MQTT_PASSWORD'))
client.on_connect = on_connect
client.on_message = on_message
client.on_subscribe = on_subscribe
#client.on_log = on_log
client.connect(host=os.environ.get('MQTT_HOST'),port=MQTT_PORT)
client.subscribe(topic,2)
client.loop_forever()