-
Notifications
You must be signed in to change notification settings - Fork 25
/
onvif_ptz.js
484 lines (415 loc) · 25.1 KB
/
onvif_ptz.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
/**
* Copyright 2018 Bart Butenaers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
module.exports = function(RED) {
var settings = RED.settings;
const onvif = require('onvif');
const utils = require('./utils');
function OnVifPtzNode(config) {
RED.nodes.createNode(this, config);
this.panSpeed = parseFloat(config.panSpeed);
this.tiltSpeed = parseFloat(config.tiltSpeed);
this.zoomSpeed = parseFloat(config.zoomSpeed);
this.panPosition = parseFloat(config.panPosition);
this.tiltPosition = parseFloat(config.tiltPosition);
this.zoomPosition = parseFloat(config.zoomPosition);
this.panTranslation = parseFloat(config.panTranslation);
this.tiltTranslation = parseFloat(config.tiltTranslation);
this.zoomTranslation = parseFloat(config.zoomTranslation);
this.time = parseInt(config.time);
this.profileName = config.profileName;
this.action = config.action;
this.preset = config.preset;
this.presetName = config.presetName;
this.stopPanTilt = config.stopPanTilt;
this.stopZoom = config.stopZoom
this.configurationToken = config.configurationToken;
var node = this;
// Retrieve the config node, where the device is configured
node.deviceConfig = RED.nodes.getNode(config.deviceConfig);
if (node.deviceConfig) {
node.listener = function(onvifStatus) {
utils.setNodeStatus(node, 'ptz', onvifStatus);
}
// Start listening for Onvif config nodes status changes
node.deviceConfig.addListener("onvif_status", node.listener);
// Show the current Onvif config node status already
utils.setNodeStatus(node, 'ptz', node.deviceConfig.onvifStatus);
node.deviceConfig.initialize();
}
node.on("input", function(msg) {
var newMsg = {};
var profileToken = msg.profileToken;
var profileName = node.profileName || msg.profileName;
var panSpeed = node.panSpeed;
var tiltSpeed = node.tiltSpeed;
var zoomSpeed = node.zoomSpeed;
var panPosition = node.panPosition;
var tiltPosition = node.tiltPosition;
var zoomPosition = node.zoomPosition;
var panTranslation = node.panTranslation;
var tiltTranslation = node.tiltTranslation;
var zoomTranslation = node.zoomTranslation;
var stopPanTilt = node.stopPanTilt;
var stopZoom = node.stopZoom;
var configurationToken = node.configurationToken;
var action = node.action || msg.action;
if (!action) {
// When no action specified in the node, it should be specified in the msg.action
node.error("No action specified (in node or msg)");
return;
}
// Don't perform these checks when e.g. the device is currently disconnected (because then e.g. no capabilities are loaded yet)
if (action !== "reconnect") {
if (!node.deviceConfig || node.deviceConfig.onvifStatus != "connected") {
node.error("This node is not connected to a device");
return;
}
if (!utils.hasService(node.deviceConfig.cam, 'ptz')) {
node.error("The device has no support for a ptz service");
return;
}
}
var preset = node.preset || msg.preset;
var presetName = node.presetName || msg.presetName;
/*var currentProfile = node.device.getCurrentProfile();
if (!currentProfile) {
// Avoid errors during ptzMove, by ensuring that the device has a profile.
// This can be a temporary issue at flow startup, since the device initialization above can take some time...
console.warn('Ignoring input message because the OnVif device has no current profile');
return;
}*/
// TODO check whether the node.token exists in the ONVIF device
// Check whether a 'pan_speed' value is specified in the input message
if (msg.hasOwnProperty('pan_speed') || msg.pan_speed < -1.0 || msg.pan_speed > 1.0) {
if (isNaN(msg.pan_speed)) {
node.error('The msg.pan_speed value should be a number between -1.0 and 1.0');
}
else {
panSpeed = msg.pan_speed;
}
}
// Check whether a 'tilt_speed' value is specified in the input message
if (msg.hasOwnProperty('tilt_speed') || msg.tilt_speed < -1.0 || msg.tilt_speed > 1.0) {
if (isNaN(msg.tilt_speed)) {
node.error('The msg.tilt_speed value should be a number between -1.0 and 1.0');
}
else {
tiltSpeed = msg.tilt_speed;
}
}
// Check whether a 'zoom_speed' value is specified in the input message
if (msg.hasOwnProperty('zoom_speed')) {
if (isNaN(msg.zoom_speed) || msg.zoom_speed < -1.0 || msg.zoom_speed > 1.0) {
node.error('The msg.zoom_speed value should be a number between -1.0 and 1.0');
}
else {
zoomSpeed = msg.zoom_speed;
}
}
// Check whether a 'pan_position' value is specified in the input message
if (msg.hasOwnProperty('pan_position') /*TODO CHECK VIA PROFILE RANGE || msg.pan_position < -1.0 || msg.pan_position > 1.0*/) {
if (isNaN(msg.pan_position)) {
node.error('The msg.pan_position value should be a number between ?? and ??'); // TODO find boundaries in profile
}
else {
panPosition = msg.pan_position;
}
}
// Check whether a 'tilt_position' value is specified in the input message
if (msg.hasOwnProperty('tilt_position') /*TODO CHECK VIA PROFILE RANGE || msg.tilt_position < -1.0 || msg.tilt_position > 1.0*/) {
if (isNaN(msg.tilt_position)) {
node.error('The msg.tilt_position value should be a number between ?? and ??'); // TODO find boundaries in profile
}
else {
tiltPosition = msg.tilt_position;
}
}
// Check whether a 'zoom_position' value is specified in the input message
if (msg.hasOwnProperty('zoom_position') /*TODO CHECK VIA PROFILE RANGE || msg.zoom_position < -1.0 || msg.zoom_position > 1.0*/) {
if (isNaN(msg.zoom_position)) {
node.error('The msg.zoom_position value should be a number between ?? and ??'); // TODO find boundaries in profile
}
else {
zoomPosition = msg.zoom_position;
}
}
// Check whether a 'pan_translation' value is specified in the input message
if (msg.hasOwnProperty('pan_translation') /*TODO CHECK VIA PROFILE RANGE || msg.pan_translation < -1.0 || msg.pan_translation > 1.0*/) {
if (isNaN(msg.pan_translation)) {
node.error('The msg.pan_translation value should be a number between ?? and ??'); // TODO find boundaries in profile
}
else {
panTranslation = msg.pan_translation;
}
}
// Check whether a 'tilt_translation' value is specified in the input message
if (msg.hasOwnProperty('tilt_translation') /*TODO CHECK VIA PROFILE RANGE || msg.tilt_translation < -1.0 || msg.tilt_translation > 1.0*/) {
if (isNaN(msg.tilt_translation)) {
node.error('The msg.tilt_translation value should be a number between ?? and ??'); // TODO find boundaries in profile
}
else {
tiltTranslation = msg.tilt_translation;
}
}
// Check whether a 'zoom_translation' value is specified in the input message
if (msg.hasOwnProperty('zoom_translation') /*TODO CHECK VIA PROFILE RANGE || msg.zoom_translation < -1.0 || msg.zoom_translation > 1.0*/) {
if (isNaN(msg.zoom_translation)) {
node.error('The msg.zoom_translation value should be a number between ?? and ??'); // TODO find boundaries in profile
}
else {
zoomTranslation = msg.zoom_translation;
}
}
// Make sure the values are between -1.0 and 1.0
panSpeed = Math.min(Math.max(panSpeed, -1.0), 1.0);
tiltSpeed = Math.min(Math.max(tiltSpeed, -1.0), 1.0);
zoomSpeed = Math.min(Math.max(zoomSpeed, -1.0), 1.0);
// To make things easier for a user, we will let the user specify profile names.
// The corresponding profile token (which is required in Onvif to work with profiles) will be searched here...
if (!profileToken && profileName) {
profileToken = node.deviceConfig.getProfileTokenByName(profileName);
}
newMsg.xaddr = this.deviceConfig.xaddress;
newMsg.action = action;
try {
switch (action) {
case "continuousMove":
var options = {
'profileToken': profileToken,
'x': panSpeed,
'y': tiltSpeed,
'zoom': zoomSpeed,
'timeout': node.time
};
// Move the camera with the specified speed(s) and during the specified time
node.deviceConfig.cam.continuousMove(options, function(err, stream, xml) {
utils.handleResult(node, err, stream, xml, newMsg);
});
break;
case "absoluteMove":
// TODO The 'position' value range is specified in the profile
var options = {
'profileToken': profileToken,
'x': panPosition,
'y': tiltPosition,
'zoom': zoomPosition,
'speed': {
'x': panSpeed,
'y': tiltSpeed,
'zoom': zoomSpeed
}
};
// Move the camera with the specified speed(s) and during the specified time
node.deviceConfig.cam.absoluteMove(options, function(err, stream, xml) {
utils.handleResult(node, err, stream, xml, newMsg);
});
break;
case "relativeMove":
var options = {
'profileToken': profileToken,
'x': panTranslation,
'y': tiltTranslation,
'zoom': zoomTranslation,
'speed': {
'x': panSpeed,
'y': tiltSpeed,
'zoom': zoomSpeed
}
};
// Move the camera with the specified speed(s) and during the specified time
node.deviceConfig.cam.relativeMove(options, function(err, stream, xml) {
utils.handleResult(node, err, stream, xml, newMsg);
});
break;
case "gotoHomePosition":
var options = {
'profileToken': profileToken,
'speed': {
'x': panSpeed,
'y': tiltSpeed,
'zoom': zoomSpeed
}
};
// Let the camera go to the home position.
// Make sure a home position is set in advance, otherwise you get a 'No HomePosition' error.
node.deviceConfig.cam.gotoHomePosition(options, function(err, stream, xml) {
utils.handleResult(node, err, stream, xml, newMsg);
});
break;
case "setHomePosition":
var options = {
'profileToken': profileToken,
};
// Set the CURRENT camera position as the home position
node.deviceConfig.cam.setHomePosition(options, function(err, stream, xml) {
utils.handleResult(node, err, stream, xml, newMsg);
});
break;
case "getPresets":
var options = {
'profileToken': profileToken,
};
node.deviceConfig.cam.getPresets(options, function(err, stream, xml) {
utils.handleResult(node, err, stream, xml, newMsg);
});
break;
case "setPreset":
var options = {
'profileToken': profileToken
};
// We will not ask the user to specify the preset token in the input message, to avoid that the preset tokens will
// need to be stored somewhere in the Node-Red flow. Instead the user can specify a preset NAME, and we will lookup
// to which existing preset token this name corresponds...
node.deviceConfig.cam.getPresets(options, function(err, stream, xml) {
// Get the preset token of the specified preset name
var presetToken = stream[presetName];
// When the preset token exists, we are dealing with an already existing preset.
// Then pass the preset token to the device, so it will UPDATE the existing preset.
// When the preset token is not passed to the device, the device will create a NEW preset (and a new preset token).
if (presetToken) {
options.presetToken = presetToken;
}
options.presetName = presetName;
// Create/update the preset, based on the preset token. The device will save the current camera parameters
// (XY coordinates, zoom level and a focus adjustment) so that the device can move afterwards to that saved
// preset position (via the GotoPreset action).
node.deviceConfig.cam.setPreset(options, function(err, stream, xml) {
// The response contains the PresetToken which uniquely identifies the Preset.
// The operation will fail when the PTZ device is moving during the SetPreset operation.
utils.handleResult(node, err, stream, xml, newMsg);
});
});
break;
case "removePreset":
var options = {
'profileToken': profileToken
};
// We will not ask the user to specify the preset token in the input message, to avoid that the preset tokens will
// need to be stored somewhere in the Node-Red flow. Instead the user can specify a preset NAME, and we will lookup
// to which existing preset token this name corresponds...
node.deviceConfig.cam.getPresets(options, function(err, stream, xml) {
// Get the preset token of the specified preset name
var presetToken = stream[presetName];
// When the preset token exists, we are dealing with an already existing preset.
// Then pass the preset token to the device, so it will UPDATE the existing preset.
// When the preset token is not passed to the device, the device will create a NEW preset (and a new preset token).
if (presetToken) {
options.presetToken = presetToken;
}
options.presetName = presetName;
// Create/update the preset, based on the preset token. The device will save the current camera parameters
// (XY coordinates, zoom level and a focus adjustment) so that the device can move afterwards to that saved
// preset position (via the GotoPreset action).
node.deviceConfig.cam.removePreset(options, function(err, stream, xml) {
utils.handleResult(node, err, stream, xml, newMsg);
});
});
break;
case "gotoPreset":
var options = {
'profileToken': profileToken
};
// We will not ask the user to specify the preset token in the input message, to avoid that the preset tokens will
// need to be stored somewhere in the Node-Red flow. Instead the user can specify a preset NAME, and we will lookup
// to which existing preset token this name corresponds...
node.deviceConfig.cam.getPresets(options, function(err, stream, xml) {
// Get the preset token of the specified preset name
var preset = stream[presetName];
// When the preset token doesn't exists, we cannot goto it ...
if (!preset) {
console.warn("Preset token with name " + presetName + " does not exist.");
return;
}
options.preset = preset;
// TODO enkel doorgeven indien gedefinieerd.
options.speed = {
x: panSpeed,
y: tiltSpeed,
z: zoomSpeed
};
// Create/update the preset, based on the preset token. The device will save the current camera parameters
// (XY coordinates, zoom level and a focus adjustment) so that the device can move afterwards to that saved
// preset position (via the GotoPreset action).
node.deviceConfig.cam.gotoPreset(options, function(err, stream, xml) {
utils.handleResult(node, err, stream, xml, newMsg);
});
});
break;
case "getNodes":
node.deviceConfig.cam.getNodes(function(err, stream, xml) {
utils.handleResult(node, err, stream, xml, newMsg);
});
break;
case "getConfigurations":
node.deviceConfig.cam.getConfigurations(function(err, stream, xml) {
utils.handleResult(node, err, stream, xml, newMsg);
});
break;
case "getConfigurationOptions":
configurationToken = 'PtzConf1'; // TODO make adjustable (is reeds voorzien op config screen, maar nog niet via input message)
// getConfigurationOptions will result in an uncaught exception when getConfigurations hasn't been called in advance...
if (!node.deviceConfig.cam.configurations) {
node.deviceConfig.cam.getConfigurations(function(err, stream, xml) {
node.deviceConfig.cam.getConfigurationOptions(configurationToken, function(err, stream, xml) {
utils.handleResult(node, err, stream, xml, newMsg);
});
});
}
else {
node.deviceConfig.cam.getConfigurationOptions(configurationToken, function(err, stream, xml) {
utils.handleResult(node, err, stream, xml, newMsg);
});
}
break;
case "getStatus":
// TODO error in onvif library due to missing zoom in Panasonic cam (see https://github.com/agsh/onvif/issues/103)
var options = {
'profileToken': profileToken,
};
node.deviceConfig.cam.getStatus(options, function(err, stream, xml) {
utils.handleResult(node, err, stream, xml, newMsg);
});
break;
case "stop":
var options = {
'profileToken': profileToken,
'panTilt': node.stopPanTilt,
'zoom': node.stopZoom
};
node.deviceConfig.cam.stop(options, function(err, stream, xml) {
utils.handleResult(node, err, stream, xml, newMsg);
});
break;
case "reconnect":
node.deviceConfig.cam.connect();
break;
default:
//node.status({fill:"red",shape:"dot",text: "unsupported action"});
node.error("Action " + action + " is not supported");
}
}
catch (exc) {
node.error("Action " + action + " failed: " + exc);
}
});
node.on("close",function() {
if (node.listener) {
node.deviceConfig.removeListener("onvif_status", node.listener);
}
});
}
RED.nodes.registerType("onvif-ptz",OnVifPtzNode);
}