-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.js
91 lines (81 loc) · 2.34 KB
/
tools.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
const {spawn} = require("child_process")
const onvif = require('node-onvif');
process.on('message', (m) => {
let result = ''
switch (m.tool) {
case 'pingtool':
ping(m.ip)
break
case 'ptzmove':
ptzMove(m.params)
break
case 'ptzhome':
ptzHome(m.params)
break
}
})
function ping(data) {
const isWin = process.platform === "win32";
let options = [];
if (isWin) {
options = [`${data.toString()}`, '-n', '4']
} else {
options = [`${data.toString()}`, '-c', '4']
}
let ping = spawn(`ping`, options, {shell: true})
let result = '';
ping.stdout.on('data', function (out) {
result += out + '';
})
ping.stderr.on('data', (e) => {
//res.status(0)
console.log(e.toString())
})
ping.on('close', function () {
process.send({m: 'pingout', data: result})
})
}
async function ptzMove(params) {
let move = params.speed
let odevice = await new onvif.OnvifDevice({
xaddr: 'http://' + params.addr + '/onvif/device_service',
user : params.user,
pass : params.pass
});
odevice.init().then(() => {
// Move the camera
return odevice.ptzMove({
'speed': move,
'timeout': 1 // seconds
});
}).then(() => {
}).catch((error) => {
console.error(error);
});
}
async function ptzHome(options) {
let odevice = await new onvif.OnvifDevice({
xaddr: 'http://' + options.addr + '/onvif/device_service',
user : options.user,
pass : options.pass
});
odevice.init().then(() => {
// The OnvifServicePtz object
let ptz = odevice.services.ptz;
if(!ptz) {
throw new Error('Your ONVIF network camera does not support the PTZ service.');
}
// The parameters for the gotoHomePosition() method
let profile = odevice.getCurrentProfile();
let params = {
'ProfileToken': profile['token'],
'Speed' : 1
};
// Send the GotoHomePosition command using the gotoHomePosition() method
return ptz.gotoHomePosition(params);
}).then((result) => {
console.log(JSON.stringify(result.data, null, ' '));
}).catch((error) => {
console.error(error);
});
}