-
Notifications
You must be signed in to change notification settings - Fork 1
/
MySynoSSClient.js
executable file
·687 lines (611 loc) · 25.8 KB
/
MySynoSSClient.js
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
const axios = require("axios").default;
const https = require("https");
const CommonErrorCodes = {
100: "Unknown error",
101: "Invalid parameters",
102: "API does not exist",
103: "Method does not exist",
104: "This API version is not supported",
105: "Insufficient user privilege",
106: "Connection time out",
107: "Multiple login detected"
}
const AuthErrorCodes = {
100: "Unkown error",
101: "The account parameter is not specified",
400: "Invalid user or password",
401: "Guest or diabled account",
402: "Permission denied",
403: "One time password not specified",
404: "One time password authenticate failed",
405: "App portal incorrect",
405: "OTP code enforced",
407: "Max tries (if auto blocking is set to true)",
408: "Password Expired can not Change",
409: "Password Expired",
410: "Password must change (when first time use or after reset password by admin)",
411: "Account Locked (when account max try exeed)"
}
const CamErrorCodes = {
400: "Execution failed",
401: "Parameter invalid",
402: "Camera disabled"
}
const PTZErrorCodes = {
400: "Execution failed",
401: "Parameter invalid",
402: "Camera disabled"
}
const PTZPresetErroCodes = {
400: "Operation failed",
401: "Parameter invalid"
}
class QueryApiVersionError extends Error {
constructor(message, cause) {
if (typeof cause.returnCode !== "undefined"){
if (typeof CommonErrorCodes[cause.returnCode] !== "undefined"){
message = message + "\n"+CommonErrorCodes[cause.returnCode]
}
}
if (typeof cause.error !== "undefined"){
message = message + "\n" + cause.error.toString()
}
super(message)
this.cause = cause
this.name = "QueryApiVersionError"
}
}
class LoginError extends Error {
constructor(message, cause) {
if (typeof cause.returnCode!== "undefined"){
if (typeof AuthErrorCodes[cause.returnCode] !== "undefined"){
message = message + "\n"+AuthErrorCodes[cause.returnCode]
} else if (typeof CommonErrorCodes[cause.returnCode] !== "undefined"){
message = message + "\n"+CommonErrorCodes[cause.returnCode]
}
}
if (typeof cause.error !== "undefined"){
message = message + "\n" + cause.error.toString()
}
super(message)
this.cause = cause
this.name = "LoginError"
}
}
class LogoutError extends Error {
constructor(message, cause) {
if (typeof cause.returnCode !== "undefined"){
if (typeof AuthErrorCodes[cause.returnCode] !== "undefined"){
message = message + "\n"+AuthErrorCodes[cause.returnCode]
} else if (typeof CommonErrorCodes[cause.returnCode] !== "undefined"){
message = message + "\n"+CommonErrorCodes[cause.returnCode]
}
}
if (typeof cause.error !== "undefined"){
message = message + "\n" + cause.error.toString()
}
super(message)
this.cause = cause
this.name = "LogoutError"
}
}
class ListCameraError extends Error {
constructor(message, cause) {
if (typeof cause.returnCode !== "undefined"){
if (typeof CamErrorCodes[cause.returnCode] !== "undefined"){
message = message + "\n"+CamErrorCodes[cause.returnCode]
} else if (typeof CommonErrorCodes[cause.returnCode] !== "undefined"){
message = message + "\n"+CommonErrorCodes[cause.returnCode]
}
}
if (typeof cause.error !== "undefined"){
message = message + "\n" + cause.error.toString()
}
super(message)
this.cause = cause
this.name = "ListCameraError"
}
}
class GetCameraStreamInfoError extends Error {
constructor(message, cause) {
if (typeof cause.returnCode !== "undefined"){
if (typeof CamErrorCodes[cause.returnCode] !== "undefined"){
message = message + "\n"+CamErrorCodes[cause.returnCode]
} else if (typeof CommonErrorCodes[cause.returnCode] !== "undefined"){
message = message + "\n"+CommonErrorCodes[cause.returnCode]
}
}
if (typeof cause.error !== "undefined"){
message = message + "\n" + cause.error.toString()
}
super(message);
this.cause = cause
this.name = "GetCameraStreamInfoError"
}
}
class ListPTZInfoError extends Error {
constructor(message, cause) {
if (typeof cause.returnCode !== "undefined"){
if (typeof PTZPresetErroCodes[cause.returnCode] !== "undefined"){
message = message + "\n"+PTZPresetErroCodes[cause.returnCode]
} else if (typeof CommonErrorCodes[cause.returnCode] !== "undefined"){
message = message + "\n"+CommonErrorCodes[cause.returnCode]
}
}
if (typeof cause.error !== "undefined"){
message = message + "\n" + cause.error.toString()
}
super(message)
this.cause = cause
this.name = "ListPTZInfoError"
}
}
class GoPTZPositionError extends Error {
constructor(message, cause) {
if (typeof cause.returnCode !== "undefined"){
if (typeof PTZErrorCodes[cause.returnCode] !== "undefined"){
message = message + "\n"+PTZErrorCodes[cause.returnCode]
} else if (typeof CommonErrorCodes[cause.returnCode] !== "undefined"){
message = message + "\n"+CommonErrorCodes[cause.returnCode]
}
}
if (typeof cause.error !== "undefined"){
message = message + "\n" + cause.error.toString()
}
super(message)
this.cause = cause
this.name = "GoPTZPositionError"
}
}
class MySynoSSClient {
//Look-at: https://global.download.synology.com/download/Document/Software/DeveloperGuide/Package/SurveillanceStation/All/enu/Surveillance_Station_Web_API.pdf
//webapi/query.cgi?api=SYNO.API.Info&method=Query&version=1
#queryClient;
//webapi/entry.cgi?api=SYNO.API.Auth
//webapi/entry.cgi?api=SYNO.SurveillanceStation.Camera
//webapi/entry.cgi?api=SYNO.SurveillanceStation.PTZ
//webapi/entry.cgi?api=SYNO.SurveillanceStation.PTZ.Preset
#entryClient;
#apiVersions = null
//The login object containing the sid and token
#loginInfo
//The inital options set during construction
#opts
#debug
constructor(opts) {
this._init(opts);
}
_init(opts) {
this.#loginInfo = null
this.#opts = opts
this.#debug = opts.debug
let baseURL = opts.host+":"+opts.port+"/webapi"
if (opts.protocol === "http"){
this.#queryClient = axios.create({
baseURL: "http://"+baseURL+"/query.cgi?api=SYNO.API.Info&method=Query&version=1",
});
this.#entryClient = axios.create({
baseURL: "http://"+baseURL+"/",
});
} else {
this.#queryClient = axios.create({
baseURL: "https://"+baseURL+"/query.cgi?api=SYNO.API.Info&method=Query&version=1",
httpsAgent: new https.Agent({
// This setting disables SSL certificate errors and reduces security
rejectUnauthorized: !opts.ignoreCertErrors
})
});
this.#entryClient = axios.create({
baseURL: "https://"+baseURL+"/",
httpsAgent: new https.Agent({
// This setting disables SSL certificate errors and reduces security
rejectUnauthorized: !opts.ignoreCertErrors
})
});
}
}
setDebugFlag(flag){
if(flag === true){
debug = true
} else {
debug = false
}
}
queryApiVersions(useCachedData){
const self = this
if (typeof useCachedData === "undefined"){
useCachedData = true
}
if (useCachedData && (self.#apiVersions != null)){
if (self.#debug){
console.log("Cached information about the api versions available. Using it.")
}
return new Promise(function(myResolve, myReject) {
myResolve(self.#apiVersions)
});
} else {
if (self.#debug){
console.log("Either no cached api version information is available or we should not use it. Query the api version information.")
}
return self.#queryClient.get()
.then((response) => {
let newVersionInfo = {}
for (let curApi in response.data.data){
newVersionInfo[curApi] = { version: response.data.data[curApi].maxVersion, path: response.data.data[curApi].path}
}
self.#apiVersions = newVersionInfo
return newVersionInfo
})
.catch(error => {
if (!error instanceof QueryApiVersionError){
throw new QueryApiVersionError("Could not query the version information of the apis", { error: error})
} else {
throw error
}
})
}
}
login(useCachedData) {
const self = this
if (typeof useCachedData === "undefined"){
useCachedData = true
}
if (useCachedData && (self.#loginInfo != null)){
if (self.#debug){
console.log("Login not neccessary. Using cached login information.")
}
return new Promise(function(myResolve, myReject) {
myResolve(self.#loginInfo)
});
} else {
if (self.#debug){
console.log("Either no cached information is available or should be used. Trying to login.")
}
return self.logout(useCachedData).then( () =>
self.queryApiVersions(true).then(
(apiVersions) => {
let api = "SYNO.API.Auth"
let loginObj = {
api: api,
version: apiVersions[api].version,
method: "login",
account: self.#opts.user,
passwd: self.#opts.password,
session: "SurveillanceStation",
format: "sid",
enable_syno_token: "yes"
}
return self.#entryClient
.get(
apiVersions[api].path,{
params: loginObj
})
.then((response) => {
if (response.data.success){
self.#loginInfo = {sid: response.data.data.sid, synotoken: response.data.data.synotoken}
return self.#loginInfo
} else {
throw new LoginError("Login not possible",{response: response, returnCode: response.data.error.code})
}
}).catch(error => {
if (!error instanceof LoginError){
throw new LoginError("Login not possible", { error:error})
} else {
throw error
}
})
}
)
)
}
}
logout(useCachedData) {
if (typeof useCachedData === "undefined"){
useCachedData = true
}
const self = this
if (self.#loginInfo != null){
if (self.#debug){
console.log("Trying to logout.")
}
return self.queryApiVersions(useCachedData).then(
(apiVersions) => {
let api = "SYNO.API.Auth"
let logoutObj = {
api: api,
version: apiVersions[api].version,
method: "logout",
session: "SurveillanceStation",
_sid: self.#loginInfo.sid,
SynoToken: self.#loginInfo.synotoken
}
return self.#entryClient
.get(
apiVersions[api].path,{
params: logoutObj
})
.then((response) => {
if (response.data.success){
self.#loginInfo = null
return new Promise(function(myResolve, myReject) {
myResolve(true)
});
} else {
throw new LogoutError("Logout not possible. API retured with error.",{response: response, returnCode: response.data.error.code})
}
}).catch(error => {
if (!error instanceof LogoutError){
throw new LogoutError("Could not logout", { error:error})
} else {
throw error
}
})
}
)
} else {
return new Promise(function(myResolve) {
myResolve(true)
});
}
}
getCamIds(useCachedData){
const self = this
if (typeof useCachedData === "undefined"){
useCachedData = true
}
return self.login(useCachedData).then(
() => self.queryApiVersions(true).then(
(apiVersions) => {
let api = "SYNO.SurveillanceStation.Camera"
let camListObj = {
api: api,
method: "List",
version: apiVersions[api].version,
_sid: this.#loginInfo.sid,
SynoToken: this.#loginInfo.synotoken
}
return this.#entryClient
.get(
apiVersions[api].path,{
params: camListObj,
})
.then((response) => {
if (response.data.success){
let camIds = []
let camIdNameMapping = {}
let camNameIdMapping = {}
for (let camIdx = 0; camIdx < response.data.data.cameras.length; camIdx ++){
let camObj = response.data.data.cameras[camIdx]
camNameIdMapping[camObj.newName] = camObj.id
camIdNameMapping[camObj.id] = camObj.newName
camIds.push(camObj.id)
}
return {camIds: camIds, nameIdMapping: camNameIdMapping,idNameMapping:camIdNameMapping}
} else {
throw new ListCameraError("Could not list cameras", {response: response, returnCode: response.data.error.code})
}
}).catch(error => {
if (!error instanceof ListCameraError){
throw new ListCameraError("Could not list cameras", { error:error})
} else {
throw error
}
}
)
}
)
)
}
getCamStreamInfo(camIds, useCachedData){
const self = this
if (typeof useCachedData === "undefined"){
useCachedData = true
}
if ((typeof camIds === "undefined") || (!Array.isArray(camIds)) || (camIds.length < 1)){
throw new GetCameraStreamInfoError("Could not get stream info of the cameras. The list of camera ids is missing!")
}
camIds = camIds.join(",")
return self.login(useCachedData).then(
() => self.queryApiVersions(true).then(
(apiVersions) => {
let api = "SYNO.SurveillanceStation.Camera"
let camStreamInfoObj = {
api: api,
method: "GetLiveViewPath",
idList: camIds,
version: apiVersions[api].version,
_sid: this.#loginInfo.sid,
SynoToken: this.#loginInfo.synotoken
}
return this.#entryClient
.get(
apiVersions[api].path,{
params: camStreamInfoObj,
})
.then((response) => {
if (response.data.success){
let camIdStreamMapping = {}
for (let camIdx = 0; camIdx < response.data.data.length; camIdx ++){
let camObj = response.data.data[camIdx]
camIdStreamMapping[camObj.id] = camObj.mjpegHttpPath
}
return camIdStreamMapping
} else {
throw new GetCameraStreamInfoError("Could not get stream info of the cameras", {response: response, returnCode: response.data.error.code})
}
}).catch(error => {
if (!error instanceof GetCameraStreamInfoError){
throw new GetCameraStreamInfoError("Could not get stream info of the cameras", { error: error})
} else {
throw error
}
}
)
}
)
)
}
getPTZPresetInfoOfOneCam(camId, useCachedData){
const self = this
if (typeof useCachedData === "undefined"){
useCachedData = true
}
if (typeof camId === "undefined"){
throw new ListPTZInfoError("Could not get PTZ info of the camera. The camera id is missing!")
}
return self.login(useCachedData).then(
() => self.queryApiVersions(true).then(
(apiVersions) => {
let api = "SYNO.SurveillanceStation.PTZ.Preset"
let camPTZInfoObj = {
api: api,
method: "Enum",
cameraId: camId,
version: apiVersions[api].version,
_sid: this.#loginInfo.sid,
SynoToken: this.#loginInfo.synotoken
}
return this.#entryClient
.get(
apiVersions[api].path,{
params: camPTZInfoObj,
})
.then((response) => {
if (response.data.success){
return response.data.data.preset
} else {
throw new ListPTZInfoError("Could not get PTZ info of camera with id "+camId, {response: response, returnCode: response.data.error.code})
}
}).catch(error => {
if (!error instanceof ListPTZInfoError){
throw new ListPTZInfoError("Could not get PTZ info of camera with id "+camId, { error: error})
} else {
throw error
}
}
)
}
)
)
}
getPTZPresetInfoOfCams(camIds, useCachedData){
const self = this
if((typeof camIds !== "undefined") && Array.isArray(camIds) && (camIds.length > 0)){
let presetInfos = {}
let chain = Promise.resolve()
for (let camId of camIds){
chain = chain.then(() => self.getPTZPresetInfoOfOneCam(camId, useCachedData).then((camResult) => {
presetInfos[camId] = camResult
}))
}
return chain.then(() => {
return new Promise(function(myResolve) {
myResolve(presetInfos)
});
})
} else {
throw new ListPTZInfoError("Could not list PTZ preset infos of the cameras. The camera IDs are missing!")
}
}
goPTZPosition(camId, position, useCachedData){
const self = this
if (typeof useCachedData === "undefined"){
useCachedData = true
}
if (typeof camId === "undefined"){
throw new GoPTZPositionError("Could not set PTZ preset of the camera. The camera id is missing!")
}
if (typeof position === "undefined"){
throw new GoPTZPositionError("Could not set PTZ preset of the camera. The position id is missing!")
}
return self.login(useCachedData).then(
() => self.queryApiVersions(true).then(
(apiVersions) => {
let api = "SYNO.SurveillanceStation.PTZ"
let camPTZInfoObj = {
api: api,
method: "GoPreset",
cameraId: camId,
position: position,
version: apiVersions[api].version,
_sid: this.#loginInfo.sid,
SynoToken: this.#loginInfo.synotoken
}
return this.#entryClient
.get(
apiVersions[api].path,{
params: camPTZInfoObj,
})
.then((response) => {
if (response.data.success){
return true
} else {
throw new GoPTZPositionError("Could not set PTZ preset of camera with id "+camId, {response: reponse, returnCode: response.data.error.code})
}
}).catch(error => {
if (!error instanceof GoPTZPositionError){
throw new GoPTZPositionError("Could not set PTZ preset of camera with id "+camId, { error: error})
} else {
throw error
}
}
)
}
)
)
}
getStreamInfoOfAllCams(useCachedData){
const self = this
let result = {}
if (typeof useCachedData === "undefined"){
useCachedData = true
}
return self.getCamIds(useCachedData).then((camIdInfo) => {
result.camIds = camIdInfo.camIds
result.camNameIdMapping = camIdInfo.nameIdMapping
result.camIdNameMapping = camIdInfo.idNameMapping
result.infosPerId = {}
for (let camId of result.camIds){
result.infosPerId[camId] = {}
}
return self.getCamStreamInfo(result.camIds, useCachedData).then((camIdStreamMapping) => {
for (let camId in camIdStreamMapping){
result.infosPerId[camId].streamInfo = camIdStreamMapping[camId]
}
return new Promise(function(myResolve) {
myResolve(result)
})
})
})
}
getAllInfosOfAllCams(useCachedData){
const self = this
let result = {}
if (typeof useCachedData === "undefined"){
useCachedData = true
}
return self.getCamIds(useCachedData).then((camIdInfo) => {
result.camIds = camIdInfo.camIds
result.camNameIdMapping = camIdInfo.nameIdMapping
result.camIdNameMapping = camIdInfo.idNameMapping
result.infosPerId = {}
for (let camId of result.camIds){
result.infosPerId[camId] = {}
}
return self.getCamStreamInfo(result.camIds, useCachedData).then((camIdStreamMapping) => {
for (let camId in camIdStreamMapping){
result.infosPerId[camId].streamInfo = camIdStreamMapping[camId]
}
return self.getPTZPresetInfoOfCams(result.camIds, useCachedData).then((presetInfo) => {
for (let camId in presetInfo){
result.infosPerId[camId].presets = presetInfo[camId]
}
return new Promise(function(myResolve) {
myResolve(result)
});
})
})
})
}
}
module.exports = MySynoSSClient;