-
Notifications
You must be signed in to change notification settings - Fork 4
/
foscam2mqtt.py
80 lines (63 loc) · 2.52 KB
/
foscam2mqtt.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
74
75
76
77
78
79
80
#!/usr/bin/env python
import json
import time
import argparse
import paho.mqtt.client as mqtt
from foscam.foscam import FoscamCamera, FOSCAM_SUCCESS
client = None
args = None
lastAttrValue = {}
lastTimePublish = 0
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("-N", "--name", help="foscam camera name")
parser.add_argument("-H", "--host", help="foscam camera host/ip")
parser.add_argument("-p", "--port", help="foscam camera port", type=int)
parser.add_argument("-u", "--username", help="foscam camera username")
parser.add_argument("-w", "--password", help="foscam camera password")
parser.add_argument("-M", "--mqtthost", help="mqtt host")
parser.add_argument("-P", "--mqttport", help="mqtt port", type=int)
parser.add_argument("-U", "--mqttusername", help="mqtt camera username")
parser.add_argument("-W", "--mqttpassword", help="mqtt camera password")
parser.add_argument("-v", "--verbose", action="store_true",
help="increase output verbosity")
return parser.parse_args()
def print_devinfo(params):
global lastAttrValue, lastTimePublish
data = {}
changed = False
attrs = ('motionDetectAlarm', 'soundAlarm', 'infraLedState')
for attr in attrs:
if attr not in lastAttrValue or params[attr] != lastAttrValue[attr]:
changed = True
data[attr] = lastAttrValue[attr] = params[attr]
json_data = json.dumps(data)
if changed or (time.time() - lastTimePublish) > 10:
(res, _) = client.publish("ipcamera/"+args.name, json_data, qos=0)
lastTimePublish = time.time()
if args.verbose:
print("Publish to mqtt: %s" % mqtt.error_string(res))
def main():
global args, client
args = parse_args()
if args.verbose:
print("Init mqtt..")
client = mqtt.Client(client_id="", clean_session=True, userdata=None,
protocol=mqtt.MQTTv311)
client.username_pw_set(args.mqttusername, args.mqttpassword)
client.connect(args.mqtthost, args.mqttport, 60)
client.loop_start()
if args.verbose:
print("Init foscam..")
mycam = FoscamCamera(args.host, args.port, args.username, args.password,
daemon=False, verbose=args.verbose)
while True:
rc, params = mycam.get_dev_state()
if rc == FOSCAM_SUCCESS:
print_devinfo(params)
else:
print("Failed to get camera info: %d" % rc)
time.sleep(30)
time.sleep(1)
if __name__ == "__main__":
main()