forked from magapp/blueiriscmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblueiris.py
executable file
·143 lines (114 loc) · 5.03 KB
/
blueiris.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Magnus Appelquist 2014-06-02 Initial
#
import requests, json, hashlib, sys, argparse
def main():
parser = argparse.ArgumentParser(description='Blue Iris controller', prog='blueiris')
parser.add_argument('--version', action='version', version='%(prog)s 1.0 https://github.com/magapp/blueiris')
parser.add_argument("--host", help="Blue Iris host to connect to ", required=True)
parser.add_argument('--user', help='User to use when connecting', required=True)
parser.add_argument('--password', help='Password to use when connecting', required=True)
parser.add_argument('--debug', action='store_true', help='Print debug messages')
parser.add_argument('--list-profiles', action='store_true', help='List all available profiles')
parser.add_argument('--set-profile', action='store', help='Set current profile', metavar='profile-name', default=None)
parser.add_argument('--set-schedule', action='store', help='Set current schedule', metavar='schedule-name', default=None)
parser.add_argument('--set-signal', action='store', help='Set current signal', metavar='signal-name', default=None, choices=['red','yellow','green'])
parser.add_argument('--trigger', action='store', help='Trigger camera', metavar='camera-short-name', default=None)
args = parser.parse_args()
bi = BlueIris(args.host, args.user, args.password, args.debug)
print "Profile '%s' is active" % bi.get_profile()
print "Schedule '%s' is active" % bi.get_schedule()
print "Signal is %s" % bi.get_signal()
if args.list_profiles:
print "Available profiles are:"
print ", ".join(bi.profiles_list)
if args.set_profile:
try:
profile_id = bi.profiles_list.index(args.set_profile)
except:
print "Could not find any profile with that name. Use --list-profiles to see available profiles."
sys.exit(0)
print "Setting active profile to '%s' (id: %d)" % (args.set_profile, profile_id)
bi.cmd("status", {"profile": profile_id})
if args.set_signal:
signal = bi.get_signal()
print "Switching signal %s -> %s" % (signal, args.set_signal)
bi.set_signal(args.set_signal)
if args.set_schedule:
schedule = bi.get_schedule()
print "Switching schedule %s -> %s" % (schedule, args.set_schedule)
bi.set_schedule(args.set_schedule)
if args.trigger:
print "Triggering camera '%s'" % args.trigger
bi.cmd("trigger", {"camera": args.trigger})
bi.logout()
sys.exit(0)
class BlueIris:
session = None
response = None
signals = ['red', 'green', 'yellow']
def __init__(self, host, user, password, debug=False):
self.host = host
self.user = user
self.password = password
self.debug = debug
self.url = "http://"+host+"/json"
r = requests.post(self.url, data=json.dumps({"cmd":"login"}))
if r.status_code != 200:
print r.status_code
print r.text
sys.exit(1)
self.session = r.json()["session"]
self.response = hashlib.md5("%s:%s:%s" % (user, self.session, password)).hexdigest()
if self.debug:
print "session: %s response: %s" % (self.session, self.response)
r = requests.post(self.url, data=json.dumps({"cmd":"login", "session": self.session, "response": self.response}))
if r.status_code != 200 or r.json()["result"] != "success":
print r.status_code
print r.text
sys.exit(1)
self.system_name = r.json()["data"]["system name"]
self.profiles_list = r.json()["data"]["profiles"]
print "Connected to '%s'" % self.system_name
def cmd(self, cmd, params=dict()):
args = {"session": self.session, "response": self.response, "cmd": cmd}
args.update(params)
# print self.url
# print json.dumps(args)
r = requests.post(self.url, data=json.dumps(args))
# if r.status_code != 200 or r.json()["result"] != "success":
if r.status_code != 200:
print r.status_code
print r.text
sys.exit(1)
if self.debug:
print str(r.json())
try:
return r.json()["data"]
except:
return r.json()
def get_profile(self):
r = self.cmd("status")
profile_id = int(r["profile"])
if profile_id == -1:
return "Undefined"
return self.profiles_list[profile_id]
def get_signal(self):
r = self.cmd("status")
signal_id = int(r["signal"])
return self.signals[signal_id]
def get_schedule(self):
r = self.cmd("status")
schedule = r["schedule"]
return schedule
def set_signal(self, signal_name):
signal_id = self.signals.index(signal_name)
self.cmd("status", {"signal": signal_id})
def set_schedule(self, schedule_name):
self.cmd("status", {"schedule": schedule_name})
def logout(self):
self.cmd("logout")
if __name__ == "__main__":
main()