-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.js
114 lines (103 loc) · 3.28 KB
/
Camera.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
const rp = require('request-promise')
const STATUS_ON = 'on'
const STATUS_OFF = 'off'
module.exports = class Camera {
constructor(ip, username, password) {
this.url = 'http://' + ip + '/vb.htm'
this.username = username || ''
this.password = password || ''
this.timeout = 5000
}
/**
* send an HTTP get request to the camera
* @param {object} params to send to the cam
* @param {boolean} get_first_value of the matching returned response from the cam
*/
send(params, get_first_value = false) {
return new Promise( (resolve, reject) => {
rp.get({
url: this.url,
qs: params,
timeout: this.timeout,
auth: {
user: this.username,
pass: this.password,
sendImmediately: false
},
})
.then(response => {
var result = true
Object.keys(params).forEach( param => {
if (!response) {
reject("invalid response from camera")
return
}
// "OK getprivacystate=0
const regex = new RegExp('(.*) ' + param + '(=([01]))?')
const matches = response.match(regex)
if (matches) {
if (matches[1] != 'OK') {
result = false
}
if( result && get_first_value && matches.length > 2 ) {
// return the first value for get
result = matches[3]
}
}
else {
result = false
}
})
resolve(result)
})
.catch(error => {
console.error(error)
reject(error)
})
})
}
/**
* get human readable status of the camera
*/
getStatus() {
return new Promise( (resolve, reject) => {
const params = {
'getprivacystate': 1,
}
this.send(params, true)
.then(result => {
const status = result == 0 ? STATUS_ON : STATUS_OFF
resolve(status)
})
.catch(error => {
console.error("could not retrieve camera status")
reject(error)
})
})
}
/**
* modify privacy state of the camera
* @param {string} state on|off
*/
setState(state) {
return new Promise( (resolve, reject) => {
let params = {
'setprivacycontrol': 1,
'setprivacystate': 0,
'ledmode': 1,
}
if (state == STATUS_OFF) {
params.setprivacystate = 1
params.ledmode = 0
}
this.send(params)
.then(result => {
resolve(result)
})
.catch(error => {
console.error("could not set camera status")
reject(error)
})
})
}
}