-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathwebAPI.py
More file actions
496 lines (377 loc) · 17 KB
/
webAPI.py
File metadata and controls
496 lines (377 loc) · 17 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
import requests
import json
import queue
import threading
import time
import cv2
import math
import os
from utils import get_latest_frame
# replace the following with your NVR and user account
IP_ADDR = 'xxx.xxx.xxx.xxx'
PORT = 'xxxx'
ACCOUNT = 'xxxxxx'
PASSWORD = 'xxxxxx'
# replace the following tokens with the ones from your Action Rule settings
SEND_NOTIFICATION_TOKEN = '1GRSPdGqYnOPjoNtiY0XiLEyX8GiL2FsVBdPOwvm3cmzjZ5ujxZpmNVkshP4iWpJ'
ACTION_RULE_RECORDING_TOKEN = 'kG5wxbuXjCn27QmykHywMx6NxkwjJaFf7g5SwPe8r6gl70xVGJun9ow4rXHTxwEe'
class WebAPI:
"""This is a wrapper class which has Synology Surveillance Station Web API methods.
"""
GET_REQUEST = 0
POST_REQUEST = 1
def __init__(self, ip_addr, port, account, password):
""" WebAPI constructor
:param ip_addr: (str)
:param port: (str)
:param account: (str)
:param password: (str)
"""
self.ip_port = '{}:{}'.format(ip_addr, port)
self.sid = self.login(account, password)
def send_request(self, api, payload, request_type):
""" Send request to call Web API
:param api: (str) the API to be called
:param payload: (dict) the parameters for the desired API method
:param request_type: (int) GET_REQUEST or POST_REQUEST
:return: (dict) response from Web API request
"""
url = '{}{}'.format(self.ip_port, api)
if request_type == self.GET_REQUEST:
r = requests.get(url, params=payload).json()
elif request_type == self.POST_REQUEST:
r = requests.post(url, data=payload).json()
else:
raise NameError('Invalid request type!')
if not r['success']:
web_API_document = 'https://global.download.synology.com/download/Document/Software/DeveloperGuide/Package/SurveillanceStation/All/enu/Surveillance_Station_Web_API.pdf'
error_message = '{} {}\n{}\n'.format(
api, 'request failed.\nPlease go to the following website to read the error code for this API.', web_API_document)
raise NameError(error_message)
return r
def login(self, account, password):
""" Login to Surveillance Station
:param account: (str)
:param password: (str)
:return: (str) session id of this login
"""
api = '/webapi/auth.cgi?api=SYNO.API.Auth'
payload = {'method': 'Login',
'version': 6,
'account': account,
'passwd': password,
'session': 'SurveillanceStation',
'format': 'sid'}
r = self.send_request(api, payload, self.GET_REQUEST)
sid = r['data']['sid']
return sid
def logout(self):
""" Logout Surveillance Station
:return: (dict) response from Web API request
"""
api = '/webapi/auth.cgi?api=SYNO.API.Auth'
payload = {'method': 'Logout',
'version': 6,
'_sid': self.sid}
r = self.send_request(api, payload, self.GET_REQUEST)
return r
def list_cameras(self):
""" List all the cameras in the Surveillance Station
:return: (list) list of cameras in the Surveillance Station
"""
api = '/webapi/entry.cgi?api=SYNO.SurveillanceStation.Camera'
payload = {'method': 'List',
'version': 9,
'privCamType': 1,
'camStm': 0,
'_sid': self.sid}
r = self.send_request(api, payload, self.GET_REQUEST)
cameras = r['data']['cameras']
return cameras
def list_recordings(self, camera_ids=[]):
""" List all the recordings in the Surveillance Station. If camera_ids is not given,
this method will get recordings from all the cameras in the Surveillance Station.
:param camera_ids: (list) list of camera_id(int)
:return: (list) list of recordings
"""
api = '/webapi/entry.cgi?api=SYNO.SurveillanceStation.Recording'
payload = {'method': 'List',
'version': 5,
'_sid': self.sid}
if len(camera_ids) > 0:
payload['cameraIds'] = ','.join(
[str(camera_id) for camera_id in camera_ids])
r = self.send_request(api, payload, self.GET_REQUEST)
recordings = r['data']['events']
return recordings
def get_liveview_rtsp(self, camera_id):
""" Get the rtsp path of a specific camera
:param camera_id: (int)
:return: (str) rtsp path of the camera
"""
api = '/webapi/entry.cgi?api=SYNO.SurveillanceStation.Camera'
payload = {'method': 'GetLiveViewPath',
'version': 9,
'idList': str(camera_id),
'_sid': self.sid}
r = self.send_request(api, payload, self.GET_REQUEST)
rtsp = r['data'][0]['rtspPath']
return rtsp
def send_notification(self):
""" Send notification to users.
Please follow the setup in live-stream/README.md before you use this method.
:return: (dict) response from Web API request
"""
api = '/webapi/entry.cgi?api=SYNO.SurveillanceStation.Webhook'
payload = {'method': 'Incoming',
'version': 1,
'token': SEND_NOTIFICATION_TOKEN}
r = self.send_request(api, payload, self.GET_REQUEST)
return r
def start_action_rule_recording(self):
""" Start action Rule Recording from a specific camera.
Please follow the setup in live-stream/README.md before you use this method.
:return: (dict) response from Web API request
"""
api = '/webapi/entry.cgi?api=SYNO.SurveillanceStation.Webhook'
payload = {'method': 'Incoming',
'version': 1,
'token': ACTION_RULE_RECORDING_TOKEN}
r = self.send_request(api, payload, self.GET_REQUEST)
return r
def _get_available_type_index(self, setting):
""" helper function of create_recording_label
:return: (int) an available type index for a new label category in Recording app
"""
if len(setting) == 0:
return 1
max_label_id = 0
for label in setting:
max_label_id = max(label['type'], max_label_id)
full_length = int(math.log(max_label_id, 2)) + 1
if len(setting) == full_length:
return max_label_id << 1
else:
occupy = [False] * full_length
for label in setting:
index = int(math.log(label['type'], 2))
occupy[index] = True
for i, occupied in enumerate(occupy):
if not occupied:
return 1 << i
def _get_label_setting(self):
""" helper function of create_recording_label
:return: (list) all the label settings in Recording app
"""
api = '/webapi/entry.cgi?api=SYNO.SurveillanceStation.Recording'
payload = {'method': 'GetLabelSetting',
'version': 5,
'_sid': self.sid}
r = self.send_request(api, payload, self.GET_REQUEST)
return r['data']['setting']
def _set_label_setting(self, setting):
""" helper function for create_recording_label and erase_recording_labels.
:param setting: (list) desired label setting to set label categories in Recording app
:return: (dict) response from Web API request
"""
api = '/webapi/entry.cgi?api=SYNO.SurveillanceStation.Recording'
payload = {'method': 'SetLabelSetting',
'version': 5,
'setting': json.dumps(setting),
'_sid': self.sid}
r = self.send_request(api, payload, self.POST_REQUEST)
return r
def _save_tag(self, recording_id, custom_label):
""" helper function for add_label_to_recording, remove_label_on_recording and clean_labels_on_recording
:param recording_id: (int) the id of the recording to be set
:param custom_label: (int) the 'bit-wise or' value of all the desired label type indexes for this recording
:return: (dict) response from Web API request
"""
api = '/webapi/entry.cgi?api=SYNO.SurveillanceStation.Recording'
payload = {'method': 'SaveTag',
'version': 5,
'archId': 0,
'customLabel': custom_label,
'id': recording_id,
'systemLabel': 0,
'_sid': self.sid}
r = self.send_request(api, payload, self.POST_REQUEST)
return r
def _get_tag(self, recording_id):
""" helper function for add_label_to_recording and remove_label_on_recording
:param recording_id: (int) the id of the recording
:return: (int) the 'bit-wise or' value of all the label type indexes on this recording
"""
recordings = self.list_recordings()
for recording in recordings:
if recording['id'] == recording_id:
return recording['customLabel']
raise NameError('recording_id {} does not exist!'.format(recording_id))
def create_recording_label(self, name):
"""Create a new label category on Recording app
The type(label_id) for a label setting is by bit position.
For example, when you create the first label, it's type is 1,
the second is 2, the third is 4, and so on.
To add a new category, we have to use SetLabelSetting method in
SYNO.SurveillanceStation.Recording Web API.
However, when we use this method, we have to give it all the label settings,
including the new one and all the other previous ones.
As a result, we have to get previous label settings, add a new label setting,
then call SetLabelSetting method.
:param name: (str) name of the label category
:return: (int) label id of the created category
"""
setting = self._get_label_setting()
label_id = self._get_available_type_index(setting)
setting.append({'categ': 2,
'enabled': True,
'text': name,
'background': '#E3D000',
'createTime': int(time.time()),
'type': label_id})
r = self._set_label_setting(setting)
return label_id
def delete_recording_label(self, label_id):
""" Delete a label category on Recording app
:param label_id: (int) the id of the label to be deleted
:return: (dict) response from Web API request
"""
api = '/webapi/entry.cgi?api=SYNO.SurveillanceStation.Recording'
payload = {'method': 'DeleteLabel',
'version': 5,
'customLabel': label_id,
'_sid': self.sid}
r = self.send_request(api, payload, self.POST_REQUEST)
return r
def erase_recording_labels(self):
""" Erase all label categories on Recording app
:return: (dict) response from Web API request
"""
r = self._set_label_setting([])
return r
def add_label_to_recording(self, recording_id, label_id):
""" Add a label to a specific recording in Recording app
The type(label_id) for a label setting is by bit position.
For example, when you create the first label, it's type is 1,
the second is 2, the third is 4, and so on.
Therefore, when we want to add labels to a recording, we simply
'bitwise or' all the type(label_id) for each label.
Then, use that as customLabel parameter for SaveTag method
in SYNO.SurveillanceStation.Recording Web API.
:param recording_id: (int) the id of the recording to add label
:param label_id: (int) the id of the label
:return: (dict) response from Web API request
"""
custom_label = self._get_tag(recording_id) | label_id
r = self._save_tag(recording_id, custom_label)
return r
def remove_label_on_recording(self, recording_id, label_id):
""" Remove a label on a specific recording in Recording app
:param recording_id: (int) the id of the recording to remove label
:param label_id: (int) the id of the label to be removed from the recording
:return: (dict) response from Web API request
"""
custom_label = self._get_tag(recording_id)
if custom_label & label_id:
custom_label ^= label_id
r = self._save_tag(recording_id, custom_label)
else:
raise NameError('label_id {} does not exist on recording {}!'.format(
label_id, recording_id))
return r
def clean_labels_on_recording(self, recording_id):
""" Clean all labels on a specific recording in Recording app
:param recording_id: (int) the id of the recording
:return: (dict) response from Web API request
"""
r = self._save_tag(recording_id, 0)
return r
def download_recording(self, recording_id, recording_storing_path):
""" Download a specific recording
We can also use Download method in SYNO.SurveillanceStation.Recording.
However, if we use that, we cannot get the timestamp of the recording
since its filename is something like: example_recording-20200827-141256.mp4
On the contrast, when we download recording with this method,
we can get the timestamp in the end of the filename, such as
example_recording-20200827-141256-1598508776.mp4
:param recording_id: (int) the id of the recording to be downloaded
:param recording_storing_path: (str) path to put the downloaded recording
:return: (str) filename of the downloaded recording
"""
api = '/webapi/entry.cgi?api=SYNO.SurveillanceStation.Recording.ShareRecording'
payload = {'method': 'EnableShare',
'version': 1,
'id': recording_id,
'blHttps': 'false',
'evtSrcId': 0,
'evtType': 0,
'_sid': self.sid}
r = self.send_request(api, payload, self.POST_REQUEST)
link = r['data']['evtDownloadLink']
content = requests.get(self.ip_port + link).content
filename = link.replace('?', '/').split('/')[3]
open(os.path.join(recording_storing_path, filename), 'wb').write(content)
return filename
def add_bookmark(self, recording_id, name, comment, timestamp):
""" Add bookmark to a specific recording
:param recording_id: (int) the id of the recording to add bookmark
:param name: (str) name of the bookmark
:param comment: (str) comment of the bookmark
:param timestamp: (int) timestamp for the starting time of the recording
:return: (dict) response from Web API request
"""
api = '/webapi/entry.cgi?api=SYNO.SurveillanceStation.Recording.Bookmark'
payload = {'method': 'SaveBookmark',
'version': 1,
'eventId': recording_id,
'name': name,
'comment': comment,
'timestamp': timestamp,
'_sid': self.sid}
r = self.send_request(api, payload, self.POST_REQUEST)
return r
def read_frame(q, rtsp):
stream = cv2.VideoCapture(rtsp)
while True:
ret, frame = stream.read()
if ret:
q.put(frame)
def process_frame(q):
while True:
frame = get_latest_frame(q)
cv2.imwrite('rtsp-frame.jpg', frame)
def test():
webapi = WebAPI(IP_ADDR, PORT, ACCOUNT, PASSWORD)
cameras = webapi.list_cameras()
camera_ids = [camera['id'] for camera in cameras]
if len(camera_ids) > 0:
# the last added camera in the Surveillance Station
camera_id = camera_ids[-1]
else:
raise LookupError('There is no camera in the Serveillance Station!')
recordings = webapi.list_recordings([camera_id])
recording_ids = [recording['id'] for recording in recordings]
recording_id = recording_ids[1]
filename = webapi.download_recording(recording_id, './')
timestamp = int(os.path.splitext(filename)[0].split('-')[-1])
webapi.add_bookmark(recording_id, "Title", "Comment", timestamp)
rtsp = webapi.get_liveview_rtsp(camera_id)
label_1 = webapi.create_recording_label('aaa')
label_2 = webapi.create_recording_label('bbb')
label_3 = webapi.create_recording_label('ccc')
webapi.delete_recording_label(label_2)
webapi.delete_recording_label(label_1)
label_1 = webapi.create_recording_label('ddd')
webapi.add_label_to_recording(recording_ids[-1], label_1)
webapi.add_label_to_recording(recording_ids[-1], label_3)
webapi.remove_label_on_recording(recording_ids[-1], label_1)
q = queue.Queue()
p1 = threading.Thread(target=read_frame, args=([q, rtsp]))
p2 = threading.Thread(target=process_frame, args=([q]))
p1.start()
p2.start()
p1.join()
p2.join()
webapi.logout()
if __name__ == '__main__':
test()