-
Notifications
You must be signed in to change notification settings - Fork 3
/
foscam.js
528 lines (443 loc) · 19.7 KB
/
foscam.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
var http = require('http'),
querystring =require('querystring'),
xml2js = require('xml2js'),
iconv = require('iconv-lite');
var parser = new xml2js.Parser();
// Implemention of format.
// First, checks if it isn't implemented yet.
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
};
var Foscam = {
// A JavaScript implementation of the foscam HD816W
// e.g.
// foscam = Foscam.Init('host', port, 'username', 'password')
// foscam.getIPInfo(console.log);
Init: function(host, port, usr, pwd) {
// Initialize
var foscam = { };
foscam.host = host;
foscam.port = port;
foscam.usr = usr;
foscam.pwd = pwd;
// Get camera's url.
foscam.url = function() {
return "/cgi-bin/CGIProxy.fcgi?usr={2}&pwd={3}".format(foscam.host, foscam.port, foscam.usr, foscam.pwd);
};
// Encode object into url string.
foscam.executeCommand= function(cmd, params, callback) {
// Send cmd to foscam.
var paramstr = '';
if (params && 'devName' in params) {
paramstr = 'devName=' + encodeURIComponent_GBK(params.devName);
} else {
paramstr = querystring.stringify(params);
}
if(paramstr) {
paramstr = '&' + paramstr;
}
op = {
host: foscam.host,
port: foscam.port,
method: 'GET',
path: "{0}&cmd={1}{2}".format(foscam.url(), cmd, paramstr),
headers: {
'Accept': 'text/html'
}
};
result = '';
var req = http.request(op, function(response) {
// response
response.setEncoding('utf8');
var data = '';
response.on( 'data', function(chunk) { data += chunk; });
response.on( 'end', function() {
data = data.trim();
// parser xml to json
var parxml = '';
parser.on('end', function(result) {
parxml = result;
});
parser.parseString(data);
parser.reset();
if ( typeof callback == 'function') { callback(cmd, parxml.CGI_Result); }
result = parxml.CGI_Result;
});// on end
});// request
req.on( 'error', function( err ) {
console.log( 'Connection-error:' + err );
});
req.end();
return result;
}; // executeCommand
// *************************** Network ************************************
foscam.getIPInfo = function(callback) {
// Get IP infoamtion.
return foscam.executeCommand('getIPInfo', null, callback);
};
foscam.setIpInfo = function(isDHCP, ip, gate, mask, dns1, dns2, callback) {
//isDHCP: 0(False), 1(True)
//System will reboot automatically to take effect after call this CGI command.
var params = {
'isDHCP': isDHCP,
'ip': ip,
'gate': gate,
'mask': mask,
'dns1': dns1,
'dns2': dns2,
};
return foscam.executeCommand('setIpInfo', params, callback);
};
foscam.setPortInfo = function(webPort, mediaPort, httpsPort, onvifPort, callback) {
//Set http port and media port of camera.
var params = {
'webPort' : webPort,
'mediaPort' : mediaPort,
'httpsPort' : httpsPort,
'onvifPort' : onvifPort,
};
return foscam.executeCommand('setPortInfo', params, callback);
};
foscam.refreshWifiList = function(callback) {
//Start scan the aps around.
//This operation may takes a while, about 20s or above,
//the other operation on this device will be blocked during the period.
return foscam.executeCommand('refreshWifiList', null, callback);
};
foscam.getWifiList = function(startNo, callback) {
//Get the aps around after refreshWifiList.
//Note: Only 10 aps will be returned one time.
params = {'startNo': startNo};
return foscam.executeCommand('getWifiList', params, callback);
};
foscam.setWifiSetting = function(ssid, psk, isEnable, isUseWifi, netType,
encrypType, authMode, keyFormat, defaultKey,
key1, key2, key3, key4,
key1Len, key2Len, key3Len, key4Len, callback) {
//Set wifi config.
//Camera will not connect to AP unless you enject your cable.
params = {'isEnable' : isEnable,
'isUseWifi' : isUseWifi,
'ssid' : ssid,
'netType' : netType,
'encryptType': encrypType,
'psk' : psk,
'authMode' : authMode,
'keyFormat' : keyFormat,
'defaultKey' : defaultKey,
'key1' : key1,
'key2' : key2,
'key3' : key3,
'key4' : key4,
'key1Len' : key1Len,
'key2Len' : key2Len,
'key3Len' : key3Len,
'key4Len' : key4Len,
};
return foscam.executeCommand('setWifiSetting', params, callback);
};
foscam.getUPnPConfig = function(callback) {
return foscam.executeCommand( 'getUPnPConfig', null, callback );
};
foscam.setUPnPConfig = function(isEnable, callback) {
params = {'isEnable': isEnable};
return foscam.executeCommand('setUPnPConfig', params, callback);
};
foscam.getDDNSConfig = function(callback) {
return foscam.executeCommand('getDDNSConfig', null, callback);
};
foscam.setDDNSConfig = function(isEnable, hostName, ddnsServer, user, password, callback) {
params = {'isEnable': isEnable,
'hostName': hostName,
'ddnsServer': ddnsServer,
'user': user,
'password': password,
};
return foscam.executeCommand('setDDNSConfig', params, callback);
};
// *************** AV Settings ******************
foscam.getSubVideoStreamType = function(callback) {
//Get the stream type of sub stream.
return foscam.executeCommand('getSubVideoStreamType', null, callback);
};
foscam.setSubVideoStreamType = function(format, callback) {
params = {'format': format};
return foscam.executeCommand('setSubVideoStreamType', params, callback);
};
foscam.setSubStreamFormat = function(format, callback) {
params = {'format': format};
return foscam.executeCommand('setSubStreamFormat', params, callback);
};
foscam.getMainVideoStreamType = function(callback) {
return foscam.executeCommand('getMainVideoStreamType', null, callback);
};
foscam.setMainVideoStreamType = function(streamType, callback) {
//Set the stream type of main stream
params = {'streamType': streamType};
return foscam.executeCommand('setMainVideoStreamType', params, callback);
};
foscam.getVideoStreamParam = function(callback){
//Get video stream param
return foscam.executeCommand('getVideoStreamParam', null, callback);
};
foscam.setVideoStreamParam = function(streamType, resolution, bitRate,
frameRate, GOP, isVBR, callback) {
//Set the video stream param of stream N
//resolution(0~4): 0 720P,
// 1 VGA(640*480),
// 2 VGA(640*360),
// 3 QVGA(320*240),
// 4 QVGA(320*180)
//bitrate: Bit rate of stream type N(20480~2097152)
//framerate: Frame rate of stream type N
//GOP: P frames between 1 frame of stream type N.
// The suggest value is: X * framerate.
//isvbr: 0(Not in use currently), 1(In use)
params = {'streamType': streamType,
'resolution': resolution,
'bitRate' : bitRate,
'frameRate' : frameRate,
'GOP' : GOP,
'isVBR' : isVBR,
};
return foscam.executeCommand('setVideoStreamParam', params, callback);
};
foscam.mirrorVideo = function(isMirror, callback) {
// Mirror Video
// 0 Not mirror
// 1 Mirror
params = {'isMirror': isMirror};
return foscam.executeCommand('mirrorVideo', params, callback);
};
foscam.flipVideo = function(isFlip, callback) {
// Flip video
// 0 Not flip
// 1 Flip
params = {'isFlip': isFlip};
return foscam.executeCommand('flipVideo', params, callback);
};
foscam.getMirrorAndFlipSetting = function(callback) {
return foscam.executeCommand('getMirrorAndFlipSetting', null, callback);
};
// *************** User account ******************
foscam.changeUserName = function(usrName, newUsrName, callback) {
// Change user name.
params = {
'usrName': usrName,
'newUsrName': newUsrName,
};
return foscam.executeCommand('changeUserName', params, callback);
};
foscam.changePassword = function(usrName, oldPwd, newPwd, callback) {
// Change password.
params = {
'usrName': usrName,
'oldPwd' : oldPwd,
'newPwd' : newPwd,
};
return foscam.executeCommand('changePassword', params, callback);
};
// *************** Device manage *******************
foscam.setSystemTime = function(timeSource, ntpServer, dateFormat,
timeFormat, timeZone, isDst, dst,
year, mon, day, hour, minute, sec,
callback) {
// Only support timeSource = 0(Get time from NTP server)
// Supported ntpServer 'time.nist.gov',
// 'time.kriss.re.kr',
// 'time.windows.com',
// 'time.nuri.net',
params = {'timeSource': timeSource,
'ntpServer' : ntpServer,
'dateFormat': dateFormat,
'timeFormat': timeFormat,
'timeZone' : timeZone,
'isDst' : isDst,
'dst' : dst,
'year' : year,
'mon' : mon,
'day' : day,
'hour' : hour,
'minute' : minute,
'sec' : sec
};
return foscam.executeCommand('setSystemTime', params, callback);
};
foscam.getSystemTime = function(callback) {
// Get system time.
return foscam.executeCommand('getSystemTime', null, callback);
};
foscam.getDevName = function(callback) {
//Get camera name.
return foscam.executeCommand('getDevName', null, callback);
};
foscam.setDevName = function(devName, callback){
// Set camera name
params = {'devName': devName};
return foscam.executeCommand('setDevName', params, callback);
};
foscam.getDevState = function(callback) {
return foscam.executeCommand('getDevState', null, callback);
};
// *************** PTZ Control *******************
foscam.ptzMoveUp = function(callback) {
return foscam.executeCommand('ptzMoveUp', null, callback);
};
foscam.ptzMoveDown = function(callback) {
return foscam.executeCommand('ptzMoveDown', null, callback);
};
foscam.ptzMoveLeft = function(callback) {
return foscam.executeCommand('ptzMoveLeft', null, callback);
};
foscam.ptzMoveRight = function(callback) {
return foscam.executeCommand('ptzMoveRight', null, callback);
};
foscam.ptzMoveTopLeft = function(callback) {
return foscam.executeCommand('ptzMoveTopLeft', null, callback);
};
foscam.ptzMoveTopRight = function(callback) {
return foscam.executeCommand('ptzMoveTopRight', null, callback);
};
foscam.ptzMoveBottomLeft = function(callback) {
return foscam.executeCommand('ptzMoveBottomLeft', null, callback);
};
foscam.ptzMoveBottomRight = function(callback) {
return foscam.executeCommand('ptzMoveBottomRight', null, callback);
};
foscam.ptzStopRun = function(callback) {
return foscam.executeCommand('ptzStopRun', null, callback);
};
foscam.ptzReset = function(callback) {
return foscam.executeCommand('ptzReset', null, callback);
};
foscam.getPTZSpeed = function(callback) {
return foscam.executeCommand('getPTZSpeed', null, callback);
};
foscam.setPTZSpeed = function(speed, callback) {
params = {'speed': speed};
return foscam.executeCommand('setPTZSpeed', params, callback);
};
foscam.getPTZSelfTestMode = function(callback) {
return foscam.executeCommand('getPTZSelfTestMode', null, callback);
};
foscam.setPTZSelfTestMode = function(mode, callback) {
//Set the selftest mode of PTZ
//mode = 0: No selftest
//mode = 1: Normal selftest
//mode = 1: After normal selftest, then goto presetpoint-appointed
params = {'mode': mode}
return foscam.executeCommand('setPTZSelfTestMode', params, callback);
};
// *************** AV Function *******************
foscam.getAlarmRecordConfig = function(callback) {
// Get alarm record config
return foscam.executeCommand('getAlarmRecordConfig', null, callback);
};
foscam.setAlarmRecordConfig = function(isEnablePreRecord, preRecordSecs, alarmRecordSecs, callback) {
// Set alarm record config
params = {'isEnablePreRecord': isEnablePreRecord,
'preRecordSecs' : preRecordSecs,
'alarmRecordSecs' : alarmRecordSecs,
};
return foscam.executeCommand('setAlarmRecordConfig', params, callback);
};
foscam.getLocalAlarmRecordConfig = function(callback) {
// Get local alarm-record config
return foscam.executeCommand('getLocalAlarmRecordConfig', null, callback);
};
foscam.setLocalAlarmRecordConfig = function(isEnableLocalAlarmRecord, localAlarmRecordSecs, callback) {
// Set local alarm-record config
params = {'isEnableLocalAlarmRecord': isEnableLocalAlarmRecord,
'localAlarmRecordSecs' : localAlarmRecordSecs
};
return foscam.executeCommand('setLocalAlarmRecordConfig', params, callback);
};
foscam.getH264FrmRefMode = function(callback) {
// Get grame shipping reference mode of H264 encode stream.
// Return args:
// mode: 0 Normal reference mode
// 1 Two frames are seprated by four skipping frames
return foscam.executeCommand('getH264FrmRefMode', null, callback);
};
foscam.setH264FrmRefMode = function(mode, callback) {
// Set frame shipping reference mode of H264 encode stream.
params = {'mode': mode};
return foscam.executeCommand('setH264FrmRefMode', params, callback);
};
foscam.getScheduleRecordConfig = function(callback) {
// Get schedule record config.
// cmd: getScheduleRecordConfig
// Return args:
// isEnable: 0/1
// recordLevel: 0 ~ ?
// spaceFullMode: 0 ~ ?
// isEnableAudio: 0/1
// schedule[N]: N <- (0 ~ 6)
return foscam.executeCommand('getScheduleRecordConfig', null, callback);
};
foscam.setScheduleRecordConfig = function(isEnable, recordLevel,
spaceFullMode, isEnableAudio,
schedule0, schedule1, schedule2,
schedule3, schedule4, schedule5,
schedule6, callback) {
// Set schedule record config.
// cmd: setScheduleRecordConfig
// args: See meth::getScheduleRecordConfig
params = {'isEnable' : isEnable,
'isEnableAudio': isEnableAudio,
'recordLevel' : recordLevel,
'spaceFullMode': spaceFullMode,
'schedule0' : schedule0,
'schedule1' : schedule1,
'schedule2' : schedule2,
'schedule3' : schedule3,
'schedule4' : schedule4,
'schedule5' : schedule5,
'schedule6' : schedule6,
};
return foscam.executeCommand('setScheduleRecordConfig', params, callback);
}
foscam.getRecordPath = function(callback){
// Get Record path: sd/ftp
// cmd: getRecordPath
// return args:
// path: (0, SD), (2, FTP)
// free: free size(K)
// total: total size(K)
return foscam.executeCommand('getRecordPath', null, callback);
};
foscam.setRecordPath = function(Path, callback){
// Set Record path: sd/ftp
// cmd: setRecordPath
// params:
// path: (0, SD), (2, FTP)
params = {'Path': Path};
return foscam.executeCommand('setRecordPath', params, callback);
};
return foscam;
}
};
function encodeURIComponent_GBK(str) {
if(str==null || typeof(str)=='undefined' || str=='')
return '';
var a = str.toString().split('');
for(var i=0; i<a.length; i++) {
var ai = a[i];
if( (ai>='0' && ai<='9') || (ai>='A' && ai<='Z') || (ai>='a' && ai<='z') || ai==='.' || ai==='-' || ai==='_') continue;
var b = iconv.encode(ai, 'gbk');
var e = [''];
for(var j = 0; j<b.length; j++)
e.push( b.toString('hex', j, j+1).toUpperCase() );
a[i] = e.join('%');
}
return a.join('');
};
exports.Foscam = Foscam;