-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathipcam.py
More file actions
274 lines (239 loc) · 8.77 KB
/
Copy pathipcam.py
File metadata and controls
274 lines (239 loc) · 8.77 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import requests
import logging
from flufl.enum import Enum
# Logger settings
#logger = logging.getLogger('ipcam')
#formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
#hdlr.setFormatter(formatter)
#logger.addHandler(hdlr)
#logger.setLevel(logging.DEBUG)
# this list is meant to be accessed by the status code returned
# so the order matters,
# eg: ddns_status[0] returns 'No action', because status 0 means 'No action'
ddns_status = ['No Action',
'It\'s connecting...',
'Can\'t connect to the Server',
'Dyndns Succeed',
'DynDns Failed: Dyndns.org Server Error',
'DynDns Failed: Incorrect User or Password',
'DynDns Failed: Need Credited User',
'DynDns Failed: Illegal Host Format',
'DynDns Failed: The Host Does not Exist',
'DynDns Failed: The Host Does not Belong to You',
'DynDns Failed: Too Many or Too Few Hosts',
'DynDns Failed: The Host is Blocked for Abusing',
'DynDns Failed: Bad Reply from Server',
'DynDns Failed: Bad Reply from Server',
'Oray Failed: Bad Reply from Server',
'Oray Failed: Incorrect User or Password',
'Oray Failed: Incorrect Hostname',
'Oray Succeed',
'Reserved',
'Reserved',
'Reserved',
'Reserved']
# this list is meant to be accessed by the status code returned
# so the order matters
upnp_status = ['No Action',
'Succeed',
'Device System Error',
'Errors in Network Communication',
'Errors in Chat with UPnP Device',
'Rejected by UPnP Device, Maybe Port Conflict']
# this list is meant to be accessed by the status code returned
# so the order matters
alarm_status = ['No Alarm',
'Motion Alarm',
'Input Alarm']
class upnp(Enum):
"""Values to set UPnP settings"""
disable = 0
enable = 1
class api(Enum):
"""Device cgi APIs"""
snapshot = 'snapshot.cgi'
videostream = 'videostream.cgi'
videostream = 'videostream.asf'
get_status = 'get_status.cgi'
decoder_control = 'decoder_control.cgi'
camera_control = 'camera_control.cgi'
reboot = 'reboot.cgi'
restore_factory = 'restore_factory.cgi'
get_params = 'get_params.cgi'
upgrade_firmware = 'upgrade_firmware.cgi'
upgrade_htmls = 'upgrade_htmls.cgi'
set_alias = 'set_alias.cgi'
set_datetime = 'set_datetime.cgi'
set_users = 'set_users.cgi'
set_devices = 'set_devices.cgi'
set_network = 'set_network.cgi'
set_wifi = 'set_wifi.cgi'
set_pppoe = 'set_pppoe.cgi'
set_upnp = 'set_upnp.cgi'
set_ddns = 'set_ddns.cgi'
set_ftp = 'set_ftp.cgi'
set_mail = 'set_mail.cgi'
set_alarm = 'set_alarm.cgi'
comm_write = 'comm_write.cgi'
set_forbidden = 'set_forbidden.cgi'
set_misc = 'set_misc.cgi'
get_misc = 'get_misc.cgi'
set_decoder = 'set_decoder.cgi'
class decoderctl(Enum):
"""
Decoder control commands.
Values [8, 24] and [30, 93] are reserved.
"""
up = 0
stop_up = 1
down = 2
stop_down = 3
left = 4
stop_left = 5
right = 6
stop_right = 7
center = 25
vertical_patrol = 26
stop_vertical_patrol = 27
horizon_patrol = 28
stop_horizon_patrol = 29
io_output_high = 94
io_output_low = 95
def _parse_status_response(response):
"""
Parses the reponse from get_status cgi call.
:param response: (Required) String response from get_status cgi call.
"""
print response
return dict(now='', alarm_status=alarm_status[0],
ddns_status=ddns_status[0], upnp_status=upnp_status[0])
class IPCam(object):
def __init__(self, ip, port, user='admin', password=''):
self.ip = ip
self.port = port
self.user = user
self.password = password
def send_command(self, cmd, **request_params):
"""
Performs the cgi command call.
:param cmd: (Required) The cgi command to call.
:param request_params: (Optional) key=value params to send to the cgi command
"""
url = 'http://{ip}:{port}/{cmd}'.format(ip=self.ip, port=self.port, cmd=cmd.value)
request_params['user'] = self.user
request_params['pwd'] = self.password
r = requests.get(url, params=request_params)
print r.url
print r.content
if r.content == 'error: illegal params.':
raise Exception('Error: illegal params.')
elif r.content == 'ok.':
return True
return r.content
def snapshot(self, name=None):
"""
Obtains a snapshot from the camera.
Requires visitor permission.
:param name: (Optional) Snapshot's name. Defaults to 'device_id(Alias)_Currenttime.jpg'
"""
# Use "next_url" (for example:next_url=1
# will name the photo: 1.jpg)
params = dict(next_url=name) if name else {}
self.send_command(api.snapshot, **params)
def videostream(self, resolution='640*480'):
"""
Use server push mode to send videostream to Client APP.
Requires visitor permission.
:param resolution: (Optional) Output's resolution. Can be either 320*240 or 640*480.
"""
self.send_command(api.videostream, resolution=resolution)
def videostream_asf(self, resolution='640*480'):
"""
Ipcam send videostream of asf format, only support vlc player and mplayer.
Requires visitor permission.
:param resolution: (Optional) Output's resolution. Can be either 320*240 or 640*480.
"""
self.send_command(api.videostream_asf, resolution=resolution)
def move_a_little(self, direction):
pass
def get_status(self):
"""
Obtains the device status info.
Requires visitor permission.
"""
return _parse_status_response(self.send_command(api.get_status))
def reboot(self):
"""
Reboots device.
Requires administrator permission.
"""
self.send_command(api.reboot)
def set_alias(self, alias):
"""
Sets device alias.
Requires administrator permission.
:param alias: (Optional) New device alias. Defaults to 'IPCAM'.
"""
self.send_command(api.set_alias, alias='IPCAM')
def decoder_control(self, command):
"""
Sends a control command to operate the device.
Requires operator permission.
:param command: (Required) Control command to operate the device.
"""
self.send_command(api.decoder_control, command=command)
def restore_factory(self):
"""
Restores factory settings.
Requires administrator permission.
"""
self.send_command(api.restore_factory)
def set_upnp(self, status):
"""
Enables/Disables the UPnP settings.
Requires administrator permission.
:param status: (Required) upnp.enable or upnp.disable
"""
self.send_command(api.set_upnp, enable=status)
def set_resolution(self, resolution=8):
"""
Controls camera sensor parameters.
Requires operator permission.
:param resolution: (Optional) Possible values: 8:QVGA or 32:VGA. Default QVGA.
"""
self.send_command(api.camera_control, param=0, value=resolution)
def set_brightness(self, brightness=125):
"""
Controls camera sensor parameters.
Requires operator permission.
:param brightness: (Optional) From 0 to 255. Default 125.
"""
self.send_command(api.camera_control, param=1, value=brightness)
def set_contrast(self, contrast=3):
"""
Controls camera sensor parameters.
Requires operator permission.
:param contrast: (Optional) From 0 to 6. Default 3.
"""
self.send_command(api.camera_control, param=2, value=contrast)
def set_mode(self, mode=1):
"""
Controls camera sensor parameters.
Requires operator permission.
:param mode: (Optional) Possible values: 0:50hz, 1:60hz, 2:Outdoor. Default 50hz.
"""
self.send_command(api.camera_control, param=3, value=mode)
def flip_and_mirror(self, value=0):
"""
Controls camera sensor parameters.
Requires operator permission.
:param flip_and_mirror: (Optional) Possible values: 0:default, 1:flip, 2:mirror, 3:flip+mirror. Default default.
"""
self.send_command(api.camera_control, param="5", value=value)
def upgrade_firmware(self):
"""
Upgrades device firmware.
Requires administrator permission.
"""
# FIXME uses post
self.send_command(api.upgrade_firmware)