-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathblueiris.py
executable file
·175 lines (143 loc) · 6.25 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/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('--get-profile', action='store_true', help='Get current profile', default=False)
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)
parser.add_argument('--ptzbutton', action='store', help='Send PTZ Button Number', metavar='ptz-button-name', default=None)
parser.add_argument('--ptzcam', action='store', help='Send PTZ Command', metavar='ptz-cam-name', default=None)
args = parser.parse_args()
bi = BlueIris(args.host, args.user, args.password, args.debug)
if args.get_profile:
print(bi.get_profile())
sys.exit(0)
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})
if args.ptzbutton:
#0: Pan left
#1: Pan right
#2: Tilt up
#3: Tilt down
#4: Center or home (if supported by camera)
#5: Zoom in
#6: Zoom out
#8..10: Power mode, 50, 60, or outdoor
#11..26: Brightness 0-15
#27..33: Contrast 0-6
#34..35: IR on, off
#101..120: Go to preset position 1..20
if not args.ptzcam:
print("Using --ptzcmdnum requires argument --ptzcam with valid Cam Name..")
sys.exit(0)
print("Sending PTZ Command Button:" + args.ptzbutton + " to Cam: " + args.ptzcam)
bi.cmd("ptz", {"camera": args.ptzcam,"button": int(args.ptzbutton),"updown": 0})
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)).encode('utf-8')).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"]
if debug:
print("Connected to '%s'" % self.system_name)
def cmd(self, cmd, params=dict()):
args = {"session": self.session, "cmd": cmd}
args.update(params)
# print self.url
# print "Sending Data: "
# print json.dumps(args)
r = requests.post(self.url, data=json.dumps(args))
if r.status_code != 200:
print(r.status_code)
print(r.text)
sys.exit(1)
else:
pass
#print "success: " + str(r.status_code)
#print r.text
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()