signalk-onvif-camera 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,22 +1,22 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2016 - 2018 Futomi Hatano
4
- Copyright (c) 2022 Karl-Erik Gustafsson
5
-
6
- Permission is hereby granted, free of charge, to any person obtaining a copy
7
- of this software and associated documentation files (the "Software"), to deal
8
- in the Software without restriction, including without limitation the rights
9
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- copies of the Software, and to permit persons to whom the Software is
11
- furnished to do so, subject to the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be included in all
14
- copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
- SOFTWARE.
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 - 2018 Futomi Hatano
4
+ Copyright (c) 2022 Karl-Erik Gustafsson
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md CHANGED
@@ -1,65 +1,66 @@
1
- # signalk-onvif-camera
2
-
3
- ONVIF Camera interface for Signal K. For IP cameras that support ONVIF control, fixed and PTZ.
4
-
5
- ## ONVIF Camera plugin config in Signal K server.
6
-
7
- ![config](doc/config.jpg)
8
- - IP address of ONVIF LAN. Default, leave empty
9
- - Select port for server
10
- - Select https/wss if you would like to use secure server
11
- - Enter ONVIF profile username
12
- - Enter ONVIF profile password
13
- - Add camera IP to list (user/pass are used to login to camera)
14
-
15
- ## ONVIF in IP Camera.
16
-
17
- Example of HikVision IP camera ONVIF
18
-
19
- ![onvif](doc/onvif.jpg)
20
- - Add ONVIF user to IP camera if applicable
21
-
22
- ## ONVIF Camera Webapp.
23
-
24
- ![webapp](doc/webapp.jpg)
25
- - Service can be accessed from Webapps menu, press "Signalk-onvif-camera" button
26
-
27
- ## ONVIF Camera service.
28
-
29
- ![service](doc/service.jpg)
30
- - ONVIF cameras are searched from local network
31
- - When search is ready then "Select a device" is prompted
32
- - Camera is selected from dropdown menu and then press "Connect" button
33
-
34
- ## ONVIF Camera in Use.
35
-
36
- ![inuse](doc/inuse.jpg)
37
- - Cursors and home button for PTZ camera
38
- - Zoom in/out
39
- - Control speed
40
- - Disconnect
41
- - Hide/show controls
42
-
43
- ## Installation
44
-
45
- ```
46
- $ npm install signalk-onvif-camera --save
47
- ```
48
- or
49
- ```
50
- $ npm install https://github.com/KEGustafsson/signalk-onvif-camera.git --save
51
- ```
52
- ## Version control
53
-
54
- - v0.0.1, 1st version for testing
55
- - v0.0.2, license added
56
- - v0.1.0, 1st release
57
- - v0.1.1, ONVIF LAN selection
58
- - v0.1.2, Webpack bundled
59
- - v0.1.3, Webapp icon added
60
- - v0.1.4, updated to npm packages
61
- - v0.2.0, hide controls
62
- - v0.2.1, version update
63
-
64
- ## Credits
65
- https://github.com/futomi/node-onvif
1
+ # signalk-onvif-camera
2
+
3
+ ONVIF Camera interface for Signal K. For IP cameras that support ONVIF control, fixed and PTZ.
4
+
5
+ ## ONVIF Camera plugin config in Signal K server.
6
+
7
+ ![config](doc/config.jpg)
8
+ - IP address of ONVIF LAN. Default, leave empty
9
+ - Select port for server
10
+ - Select https/wss if you would like to use secure server
11
+ - Enter ONVIF profile username
12
+ - Enter ONVIF profile password
13
+ - Add camera IP to list (user/pass are used to login to camera)
14
+
15
+ ## ONVIF in IP Camera.
16
+
17
+ Example of HikVision IP camera ONVIF
18
+
19
+ ![onvif](doc/onvif.jpg)
20
+ - Add ONVIF user to IP camera if applicable
21
+
22
+ ## ONVIF Camera Webapp.
23
+
24
+ ![webapp](doc/webapp.jpg)
25
+ - Service can be accessed from Webapps menu, press "Signalk-onvif-camera" button
26
+
27
+ ## ONVIF Camera service.
28
+
29
+ ![service](doc/service.jpg)
30
+ - ONVIF cameras are searched from local network
31
+ - When search is ready then "Select a device" is prompted
32
+ - Camera is selected from dropdown menu and then press "Connect" button
33
+
34
+ ## ONVIF Camera in Use.
35
+
36
+ ![inuse](doc/inuse.jpg)
37
+ - Cursors and home button for PTZ camera
38
+ - Zoom in/out
39
+ - Control speed
40
+ - Disconnect
41
+ - Hide/show controls
42
+
43
+ ## Installation
44
+
45
+ ```
46
+ $ npm install signalk-onvif-camera --save
47
+ ```
48
+ or
49
+ ```
50
+ $ npm install https://github.com/KEGustafsson/signalk-onvif-camera.git --save
51
+ ```
52
+ ## Version control
53
+
54
+ - v0.0.1, 1st version for testing
55
+ - v0.0.2, license added
56
+ - v0.1.0, 1st release
57
+ - v0.1.1, ONVIF LAN selection
58
+ - v0.1.2, Webpack bundled
59
+ - v0.1.3, Webapp icon added
60
+ - v0.1.4, updated to npm packages
61
+ - v0.2.0, hide controls
62
+ - v0.2.1, version update
63
+ - v0.3.0, ws error handling
64
+
65
+ ## Credits
66
+ https://github.com/futomi/node-onvif
package/doc/config.jpg CHANGED
File without changes
package/doc/inuse.jpg CHANGED
File without changes
package/doc/onvif.jpg CHANGED
File without changes
package/doc/service.jpg CHANGED
File without changes
package/doc/webapp.jpg CHANGED
File without changes
package/index.js CHANGED
@@ -1,428 +1,428 @@
1
- /*
2
- MIT License
3
-
4
- Copyright (c) 2022 Karl-Erik Gustafsson
5
-
6
- Permission is hereby granted, free of charge, to any person obtaining a copy
7
- of this software and associated documentation files (the "Software"), to deal
8
- in the Software without restriction, including without limitation the rights
9
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- copies of the Software, and to permit persons to whom the Software is
11
- furnished to do so, subject to the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be included in all
14
- copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
- SOFTWARE.
23
- */
24
-
25
- "use strict";
26
- process.chdir(__dirname);
27
-
28
- const onvif = require("./lib/node-onvif.js");
29
- const WebSocketServer = require("websocket").server;
30
- const https = require('https');
31
- const http = require('http');
32
- const fs = require('fs');
33
- const path = require('path');
34
- const devcert = require('devcert');
35
-
36
- module.exports = function createPlugin(app) {
37
- const plugin = {};
38
- plugin.id = "signalk-onvif-camera";
39
- plugin.name = "Signal K Onvif Camera Interface";
40
- plugin.description = "Signal K Onvif Camera Interface";
41
- const setStatus = app.setPluginStatus || app.setProviderStatus;
42
-
43
- let ipAddress;
44
- let port;
45
- let secure;
46
- let wsServer;
47
- let webServer;
48
- let userName;
49
- let password;
50
- let certStatus = false;
51
- let startServer;
52
-
53
- plugin.start = function (options, restartPlugin) {
54
- userName = options.userName;
55
- password = options.password;
56
- ipAddress = options.ipAddress;
57
- port = options.port;
58
- secure = options.secure;
59
- const browserData = [{"secure": secure,"port": port}];
60
- fs.writeFileSync(path.join(__dirname, 'public/browserdata.json'), JSON.stringify(browserData));
61
-
62
- if ((secure) && (!fs.existsSync('./cert'))){
63
- fs.mkdirSync('./cert');
64
- devcert.certificateFor([
65
- 'localhost'
66
- ])
67
- .then(({key, cert}) => {
68
- fs.writeFileSync(path.join(__dirname, 'cert/tls.key'), key);
69
- fs.writeFileSync(path.join(__dirname, 'cert/tls.cert'), cert);
70
- certStatus = true;
71
- })
72
- .catch(console.error);
73
- }
74
-
75
- try {
76
- if (fs.existsSync(path.join(__dirname, 'cert/tls.key'))) {
77
- certStatus = true;
78
- }
79
- } catch {
80
- certStatus = false;
81
- }
82
-
83
- startServer = setInterval(() => {
84
- if (secure) {
85
- if (certStatus) {
86
- certStatus = false;
87
- clearInterval(startServer);
88
- const httpsSec = {
89
- key: fs.readFileSync(path.join(__dirname, 'cert/tls.key')),
90
- cert: fs.readFileSync(path.join(__dirname, 'cert/tls.cert')),
91
- };
92
- webServer = https.createServer(httpsSec, httpServerRequest);
93
- webServer.listen(port, () => {
94
- console.log(`Onvif Camera https/wss server running at 0.0.0.0:${port}`);
95
- });
96
- wsServer = new WebSocketServer({
97
- httpServer: webServer,
98
- });
99
- wsServer.on('request', wsServerRequest);
100
- }
101
- } else {
102
- clearInterval(startServer);
103
- webServer = http.createServer(httpServerRequest);
104
- webServer.listen(port, () => {
105
- console.log(`Onvif Camera http/ws server running at 0.0.0.0:${port}`);
106
- });
107
- wsServer = new WebSocketServer({
108
- httpServer: webServer,
109
- });
110
- wsServer.on('request', wsServerRequest);
111
- }
112
- }, 1000);
113
- };
114
-
115
- plugin.stop = function stop() {
116
- clearInterval(startServer);
117
- if (webServer) {
118
- wsServer.shutDown();
119
- webServer.close(() => {
120
- console.log("Onvif Camera server closed");
121
- });
122
- }
123
- };
124
-
125
- plugin.uiSchema = {
126
- //hide password from ui
127
- password: {
128
- 'ui:widget': 'password'
129
- },
130
- }
131
-
132
- plugin.schema = {
133
- type: "object",
134
- title: 'Onvif Camera Interface',
135
- description: 'Make an ONVIF user profile to camera(s) and add camera(s) IP below',
136
- properties: {
137
- ipAddress: {
138
- type: 'string',
139
- title: 'IP address of LAN, where ONVIF devices are located. Default, leave empty.',
140
- },
141
- port: {
142
- type: 'number',
143
- title: 'Server port number',
144
- default: 8880
145
- },
146
- secure: {
147
- type: 'boolean',
148
- title: 'Use https/wss instead of http/ws'
149
- },
150
- userName: {
151
- type: 'string',
152
- title: 'ONVIF username for camera(s)'
153
- },
154
- password: {
155
- type: 'string',
156
- title: 'ONVIF password for camera(s)'
157
- },
158
- cameras: {
159
- type: 'array',
160
- title: 'Camera List',
161
- items: {
162
- type: 'object',
163
- required: [],
164
- properties: {
165
- address: {
166
- type: 'string',
167
- title: 'Camera address'
168
- },
169
- },
170
- },
171
- },
172
- },
173
- };
174
-
175
- function httpServerRequest(req, res) {
176
- var path = req.url.replace(/\?.*$/, "");
177
- if (path.match(/\.{2,}/) || path.match(/[^a-zA-Z\d\_\-\.\/]/)) {
178
- httpServerResponse404(req.url, res);
179
- return;
180
- }
181
- if (path === "/") {
182
- path = "/index.html";
183
- }
184
- var fpath = "." + path;
185
- fs.readFile(fpath, "utf-8", function (err, data) {
186
- if (err) {
187
- httpServerResponse404(req.url, res);
188
- return;
189
- } else {
190
- var ctype = getContentType(fpath);
191
- res.writeHead(200, { "Content-Type": ctype });
192
- res.write(data);
193
- res.end();
194
- }
195
- });
196
- }
197
-
198
- function getContentType(fpath) {
199
- var ext = fpath.split(".").pop().toLowerCase();
200
- if (ext.match(/^(html|htm)$/)) {
201
- return "text/html";
202
- } else if (ext.match(/^(jpeg|jpg)$/)) {
203
- return "image/jpeg";
204
- } else if (ext.match(/^(png|gif)$/)) {
205
- return "image/" + ext;
206
- } else if (ext === "css") {
207
- return "text/css";
208
- } else if (ext === "js") {
209
- return "text/javascript";
210
- } else if (ext === "woff2") {
211
- return "application/font-woff";
212
- } else if (ext === "woff") {
213
- return "application/font-woff";
214
- } else if (ext === "ttf") {
215
- return "application/font-ttf";
216
- } else if (ext === "svg") {
217
- return "image/svg+xml";
218
- } else if (ext === "eot") {
219
- return "application/vnd.ms-fontobject";
220
- } else if (ext === "oft") {
221
- return "application/x-font-otf";
222
- } else {
223
- return "application/octet-stream";
224
- }
225
- }
226
-
227
- function httpServerResponse404(url, res) {
228
- res.write("404 Not Found: " + url);
229
- res.end();
230
- console.log("HTTP : 404 Not Found : " + url);
231
- }
232
-
233
- var client_list = [];
234
-
235
- function wsServerRequest(request) {
236
- var conn = request.accept(null, request.origin);
237
- conn.on("message", function (message) {
238
- if (message.type !== "utf8") {
239
- return;
240
- }
241
- var data = JSON.parse(message.utf8Data);
242
- var method = data["method"];
243
- var params = data["params"];
244
- if (method === "startDiscovery") {
245
- startDiscovery(conn);
246
- } else if (method === "connect") {
247
- connect(conn, params);
248
- } else if (method === "fetchSnapshot") {
249
- fetchSnapshot(conn, params);
250
- } else if (method === "ptzMove") {
251
- ptzMove(conn, params);
252
- } else if (method === "ptzStop") {
253
- ptzStop(conn, params);
254
- } else if (method === "ptzHome") {
255
- ptzHome(conn, params);
256
- }
257
- });
258
-
259
- conn.on("close", function (message) {});
260
- conn.on("error", function (error) {
261
- console.log(error);
262
- });
263
- }
264
-
265
- var devices = {};
266
- function startDiscovery(conn) {
267
- devices = {};
268
- let names = {};
269
- onvif
270
- .startProbe(ipAddress)
271
- .then((device_list) => {
272
- device_list.forEach((device) => {
273
- let odevice = new onvif.OnvifDevice({
274
- xaddr: device.xaddrs[0],
275
- });
276
- let addr = odevice.address;
277
- devices[addr] = odevice;
278
- names[addr] = (device.name).replace(/%20/g, " ");
279
- });
280
- var devs = {};
281
- for (var addr in devices) {
282
- devs[addr] = {
283
- name: names[addr],
284
- address: addr,
285
- };
286
- }
287
- let res = { id: "startDiscovery", result: devs };
288
- conn.send(JSON.stringify(res));
289
- })
290
- .catch((error) => {
291
- let res = { id: "connect", error: error.message };
292
- conn.send(JSON.stringify(res));
293
- });
294
- }
295
-
296
- function connect(conn, params) {
297
- var device = devices[params.address];
298
- if (!device) {
299
- var res = {
300
- id: "connect",
301
- error: "The specified device is not found: " + params.address,
302
- };
303
- conn.send(JSON.stringify(res));
304
- return;
305
- }
306
-
307
- if (userName) {
308
- params.user = userName;
309
- params.pass = password;
310
- device.setAuth(params.user, params.pass);
311
- }
312
-
313
- device.init((error, result) => {
314
- var res = { id: "connect" };
315
- if (error) {
316
- res["error"] = error.toString();
317
- } else {
318
- res["result"] = result;
319
- }
320
- conn.send(JSON.stringify(res));
321
- });
322
- }
323
-
324
- function fetchSnapshot(conn, params) {
325
- var device = devices[params.address];
326
- if (!device) {
327
- var res = {
328
- id: "fetchSnapshot",
329
- error: "The specified device is not found: " + params.address,
330
- };
331
- conn.send(JSON.stringify(res));
332
- return;
333
- }
334
- device.fetchSnapshot((error, result) => {
335
- var res = { id: "fetchSnapshot" };
336
- if (error) {
337
- res["error"] = error.toString();
338
- } else {
339
- var ct = result["headers"]["content-type"];
340
- var buffer = result["body"];
341
- var b64 = buffer.toString("base64");
342
- var uri = "data:" + ct + ";base64," + b64;
343
- res["result"] = uri;
344
- }
345
- conn.send(JSON.stringify(res));
346
- });
347
- }
348
-
349
- function ptzMove(conn, params) {
350
- var device = devices[params.address];
351
- if (!device) {
352
- var res = {
353
- id: "ptzMove",
354
- error: "The specified device is not found: " + params.address,
355
- };
356
- conn.send(JSON.stringify(res));
357
- return;
358
- }
359
- device.ptzMove(params, (error) => {
360
- var res = { id: "ptzMove" };
361
- if (error) {
362
- res["error"] = error.toString();
363
- } else {
364
- res["result"] = true;
365
- }
366
- conn.send(JSON.stringify(res));
367
- });
368
- }
369
-
370
- function ptzStop(conn, params) {
371
- var device = devices[params.address];
372
- if (!device) {
373
- var res = {
374
- id: "ptzStop",
375
- error: "The specified device is not found: " + params.address,
376
- };
377
- conn.send(JSON.stringify(res));
378
- return;
379
- }
380
- device.ptzStop((error) => {
381
- var res = { id: "ptzStop" };
382
- if (error) {
383
- res["error"] = error.toString();
384
- } else {
385
- res["result"] = true;
386
- }
387
- conn.send(JSON.stringify(res));
388
- });
389
- }
390
-
391
- function ptzHome(conn, params) {
392
- var device = devices[params.address];
393
- if (!device) {
394
- var res = {
395
- id: "ptzMove",
396
- error: "The specified device is not found: " + params.address,
397
- };
398
- conn.send(JSON.stringify(res));
399
- return;
400
- }
401
- if (!device.services.ptz) {
402
- var res = {
403
- id: "ptzHome",
404
- error: "The specified device does not support PTZ.",
405
- };
406
- conn.send(JSON.stringify(res));
407
- return;
408
- }
409
-
410
- var ptz = device.services.ptz;
411
- var profile = device.getCurrentProfile();
412
- var params = {
413
- ProfileToken: profile["token"],
414
- Speed: 1,
415
- };
416
- ptz.gotoHomePosition(params, (error, result) => {
417
- var res = { id: "ptzMove" };
418
- if (error) {
419
- res["error"] = error.toString();
420
- } else {
421
- res["result"] = true;
422
- }
423
- conn.send(JSON.stringify(res));
424
- });
425
- }
426
-
427
- return plugin;
428
- };
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2022 Karl-Erik Gustafsson
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
24
+
25
+ "use strict";
26
+ process.chdir(__dirname);
27
+
28
+ const onvif = require("./lib/node-onvif.js");
29
+ const WebSocketServer = require("websocket").server;
30
+ const https = require('https');
31
+ const http = require('http');
32
+ const fs = require('fs');
33
+ const path = require('path');
34
+ const devcert = require('devcert');
35
+
36
+ module.exports = function createPlugin(app) {
37
+ const plugin = {};
38
+ plugin.id = "signalk-onvif-camera";
39
+ plugin.name = "Signal K Onvif Camera Interface";
40
+ plugin.description = "Signal K Onvif Camera Interface";
41
+ const setStatus = app.setPluginStatus || app.setProviderStatus;
42
+
43
+ let ipAddress;
44
+ let port;
45
+ let secure;
46
+ let wsServer;
47
+ let webServer;
48
+ let userName;
49
+ let password;
50
+ let certStatus = false;
51
+ let startServer;
52
+
53
+ plugin.start = function (options, restartPlugin) {
54
+ userName = options.userName;
55
+ password = options.password;
56
+ ipAddress = options.ipAddress;
57
+ port = options.port;
58
+ secure = options.secure;
59
+ const browserData = [{"secure": secure,"port": port}];
60
+ fs.writeFileSync(path.join(__dirname, 'public/browserdata.json'), JSON.stringify(browserData));
61
+
62
+ if ((secure) && (!fs.existsSync('./cert'))){
63
+ fs.mkdirSync('./cert');
64
+ devcert.certificateFor([
65
+ 'localhost'
66
+ ])
67
+ .then(({key, cert}) => {
68
+ fs.writeFileSync(path.join(__dirname, 'cert/tls.key'), key);
69
+ fs.writeFileSync(path.join(__dirname, 'cert/tls.cert'), cert);
70
+ certStatus = true;
71
+ })
72
+ .catch(console.error);
73
+ }
74
+
75
+ try {
76
+ if (fs.existsSync(path.join(__dirname, 'cert/tls.key'))) {
77
+ certStatus = true;
78
+ }
79
+ } catch {
80
+ certStatus = false;
81
+ }
82
+
83
+ startServer = setInterval(() => {
84
+ if (secure) {
85
+ if (certStatus) {
86
+ certStatus = false;
87
+ clearInterval(startServer);
88
+ const httpsSec = {
89
+ key: fs.readFileSync(path.join(__dirname, 'cert/tls.key')),
90
+ cert: fs.readFileSync(path.join(__dirname, 'cert/tls.cert')),
91
+ };
92
+ webServer = https.createServer(httpsSec, httpServerRequest);
93
+ webServer.listen(port, () => {
94
+ console.log(`Onvif Camera https/wss server running at 0.0.0.0:${port}`);
95
+ });
96
+ wsServer = new WebSocketServer({
97
+ httpServer: webServer,
98
+ });
99
+ wsServer.on('request', wsServerRequest);
100
+ }
101
+ } else {
102
+ clearInterval(startServer);
103
+ webServer = http.createServer(httpServerRequest);
104
+ webServer.listen(port, () => {
105
+ console.log(`Onvif Camera http/ws server running at 0.0.0.0:${port}`);
106
+ });
107
+ wsServer = new WebSocketServer({
108
+ httpServer: webServer,
109
+ });
110
+ wsServer.on('request', wsServerRequest);
111
+ }
112
+ }, 1000);
113
+ };
114
+
115
+ plugin.stop = function stop() {
116
+ clearInterval(startServer);
117
+ if (webServer) {
118
+ wsServer.shutDown();
119
+ webServer.close(() => {
120
+ console.log("Onvif Camera server closed");
121
+ });
122
+ }
123
+ };
124
+
125
+ plugin.uiSchema = {
126
+ //hide password from ui
127
+ password: {
128
+ 'ui:widget': 'password'
129
+ },
130
+ }
131
+
132
+ plugin.schema = {
133
+ type: "object",
134
+ title: 'Onvif Camera Interface',
135
+ description: 'Make an ONVIF user profile to camera(s) and add camera(s) IP below',
136
+ properties: {
137
+ ipAddress: {
138
+ type: 'string',
139
+ title: 'IP address of LAN, where ONVIF devices are located. Default, leave empty.',
140
+ },
141
+ port: {
142
+ type: 'number',
143
+ title: 'Server port number',
144
+ default: 8880
145
+ },
146
+ secure: {
147
+ type: 'boolean',
148
+ title: 'Use https/wss instead of http/ws'
149
+ },
150
+ userName: {
151
+ type: 'string',
152
+ title: 'ONVIF username for camera(s)'
153
+ },
154
+ password: {
155
+ type: 'string',
156
+ title: 'ONVIF password for camera(s)'
157
+ },
158
+ cameras: {
159
+ type: 'array',
160
+ title: 'Camera List',
161
+ items: {
162
+ type: 'object',
163
+ required: [],
164
+ properties: {
165
+ address: {
166
+ type: 'string',
167
+ title: 'Camera address'
168
+ },
169
+ },
170
+ },
171
+ },
172
+ },
173
+ };
174
+
175
+ function httpServerRequest(req, res) {
176
+ var path = req.url.replace(/\?.*$/, "");
177
+ if (path.match(/\.{2,}/) || path.match(/[^a-zA-Z\d\_\-\.\/]/)) {
178
+ httpServerResponse404(req.url, res);
179
+ return;
180
+ }
181
+ if (path === "/") {
182
+ path = "/index.html";
183
+ }
184
+ var fpath = "." + path;
185
+ fs.readFile(fpath, "utf-8", function (err, data) {
186
+ if (err) {
187
+ httpServerResponse404(req.url, res);
188
+ return;
189
+ } else {
190
+ var ctype = getContentType(fpath);
191
+ res.writeHead(200, { "Content-Type": ctype });
192
+ res.write(data);
193
+ res.end();
194
+ }
195
+ });
196
+ }
197
+
198
+ function getContentType(fpath) {
199
+ var ext = fpath.split(".").pop().toLowerCase();
200
+ if (ext.match(/^(html|htm)$/)) {
201
+ return "text/html";
202
+ } else if (ext.match(/^(jpeg|jpg)$/)) {
203
+ return "image/jpeg";
204
+ } else if (ext.match(/^(png|gif)$/)) {
205
+ return "image/" + ext;
206
+ } else if (ext === "css") {
207
+ return "text/css";
208
+ } else if (ext === "js") {
209
+ return "text/javascript";
210
+ } else if (ext === "woff2") {
211
+ return "application/font-woff";
212
+ } else if (ext === "woff") {
213
+ return "application/font-woff";
214
+ } else if (ext === "ttf") {
215
+ return "application/font-ttf";
216
+ } else if (ext === "svg") {
217
+ return "image/svg+xml";
218
+ } else if (ext === "eot") {
219
+ return "application/vnd.ms-fontobject";
220
+ } else if (ext === "oft") {
221
+ return "application/x-font-otf";
222
+ } else {
223
+ return "application/octet-stream";
224
+ }
225
+ }
226
+
227
+ function httpServerResponse404(url, res) {
228
+ res.write("404 Not Found: " + url);
229
+ res.end();
230
+ console.log("HTTP : 404 Not Found : " + url);
231
+ }
232
+
233
+ var client_list = [];
234
+
235
+ function wsServerRequest(request) {
236
+ var conn = request.accept(null, request.origin);
237
+ conn.on("message", function (message) {
238
+ if (message.type !== "utf8") {
239
+ return;
240
+ }
241
+ var data = JSON.parse(message.utf8Data);
242
+ var method = data["method"];
243
+ var params = data["params"];
244
+ if (method === "startDiscovery") {
245
+ startDiscovery(conn);
246
+ } else if (method === "connect") {
247
+ connect(conn, params);
248
+ } else if (method === "fetchSnapshot") {
249
+ fetchSnapshot(conn, params);
250
+ } else if (method === "ptzMove") {
251
+ ptzMove(conn, params);
252
+ } else if (method === "ptzStop") {
253
+ ptzStop(conn, params);
254
+ } else if (method === "ptzHome") {
255
+ ptzHome(conn, params);
256
+ }
257
+ });
258
+
259
+ conn.on("close", function (message) {});
260
+ conn.on("error", function (error) {
261
+ console.log(error);
262
+ });
263
+ }
264
+
265
+ var devices = {};
266
+ function startDiscovery(conn) {
267
+ devices = {};
268
+ let names = {};
269
+ onvif
270
+ .startProbe(ipAddress)
271
+ .then((device_list) => {
272
+ device_list.forEach((device) => {
273
+ let odevice = new onvif.OnvifDevice({
274
+ xaddr: device.xaddrs[0],
275
+ });
276
+ let addr = odevice.address;
277
+ devices[addr] = odevice;
278
+ names[addr] = (device.name).replace(/%20/g, " ");
279
+ });
280
+ var devs = {};
281
+ for (var addr in devices) {
282
+ devs[addr] = {
283
+ name: names[addr],
284
+ address: addr,
285
+ };
286
+ }
287
+ let res = { id: "startDiscovery", result: devs };
288
+ if (conn.connected) conn.send(JSON.stringify(res));
289
+ })
290
+ .catch((error) => {
291
+ let res = { id: "connect", error: error.message };
292
+ if (conn.connected) conn.send(JSON.stringify(res));
293
+ });
294
+ }
295
+
296
+ function connect(conn, params) {
297
+ var device = devices[params.address];
298
+ if (!device) {
299
+ var res = {
300
+ id: "connect",
301
+ error: "The specified device is not found: " + params.address,
302
+ };
303
+ if (conn.connected) conn.send(JSON.stringify(res));
304
+ return;
305
+ }
306
+
307
+ if (userName) {
308
+ params.user = userName;
309
+ params.pass = password;
310
+ device.setAuth(params.user, params.pass);
311
+ }
312
+
313
+ device.init((error, result) => {
314
+ var res = { id: "connect" };
315
+ if (error) {
316
+ res["error"] = error.toString();
317
+ } else {
318
+ res["result"] = result;
319
+ }
320
+ if (conn.connected) conn.send(JSON.stringify(res));
321
+ });
322
+ }
323
+
324
+ function fetchSnapshot(conn, params) {
325
+ var device = devices[params.address];
326
+ if (!device) {
327
+ var res = {
328
+ id: "fetchSnapshot",
329
+ error: "The specified device is not found: " + params.address,
330
+ };
331
+ if (conn.connected) conn.send(JSON.stringify(res));
332
+ return;
333
+ }
334
+ device.fetchSnapshot((error, result) => {
335
+ var res = { id: "fetchSnapshot" };
336
+ if (error) {
337
+ res["error"] = error.toString();
338
+ } else {
339
+ var ct = result["headers"]["content-type"];
340
+ var buffer = result["body"];
341
+ var b64 = buffer.toString("base64");
342
+ var uri = "data:" + ct + ";base64," + b64;
343
+ res["result"] = uri;
344
+ }
345
+ if (conn.connected) conn.send(JSON.stringify(res));
346
+ });
347
+ }
348
+
349
+ function ptzMove(conn, params) {
350
+ var device = devices[params.address];
351
+ if (!device) {
352
+ var res = {
353
+ id: "ptzMove",
354
+ error: "The specified device is not found: " + params.address,
355
+ };
356
+ if (conn.connected) conn.send(JSON.stringify(res));
357
+ return;
358
+ }
359
+ device.ptzMove(params, (error) => {
360
+ var res = { id: "ptzMove" };
361
+ if (error) {
362
+ res["error"] = error.toString();
363
+ } else {
364
+ res["result"] = true;
365
+ }
366
+ if (conn.connected) conn.send(JSON.stringify(res));
367
+ });
368
+ }
369
+
370
+ function ptzStop(conn, params) {
371
+ var device = devices[params.address];
372
+ if (!device) {
373
+ var res = {
374
+ id: "ptzStop",
375
+ error: "The specified device is not found: " + params.address,
376
+ };
377
+ if (conn.connected) conn.send(JSON.stringify(res));
378
+ return;
379
+ }
380
+ device.ptzStop((error) => {
381
+ var res = { id: "ptzStop" };
382
+ if (error) {
383
+ res["error"] = error.toString();
384
+ } else {
385
+ res["result"] = true;
386
+ }
387
+ if (conn.connected) conn.send(JSON.stringify(res));
388
+ });
389
+ }
390
+
391
+ function ptzHome(conn, params) {
392
+ var device = devices[params.address];
393
+ if (!device) {
394
+ var res = {
395
+ id: "ptzMove",
396
+ error: "The specified device is not found: " + params.address,
397
+ };
398
+ if (conn.connected) conn.send(JSON.stringify(res));
399
+ return;
400
+ }
401
+ if (!device.services.ptz) {
402
+ var res = {
403
+ id: "ptzHome",
404
+ error: "The specified device does not support PTZ.",
405
+ };
406
+ if (conn.connected) conn.send(JSON.stringify(res));
407
+ return;
408
+ }
409
+
410
+ var ptz = device.services.ptz;
411
+ var profile = device.getCurrentProfile();
412
+ var params = {
413
+ ProfileToken: profile["token"],
414
+ Speed: 1,
415
+ };
416
+ ptz.gotoHomePosition(params, (error, result) => {
417
+ var res = { id: "ptzMove" };
418
+ if (error) {
419
+ res["error"] = error.toString();
420
+ } else {
421
+ res["result"] = true;
422
+ }
423
+ if (conn.connected) conn.send(JSON.stringify(res));
424
+ });
425
+ }
426
+
427
+ return plugin;
428
+ };
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
package/lib/node-onvif.js CHANGED
File without changes
package/package.json CHANGED
@@ -1,34 +1,34 @@
1
- {
2
- "name": "signalk-onvif-camera",
3
- "version": "0.2.1",
4
- "description": "Signal K Onvif Camera Interface",
5
- "main": "index.js",
6
- "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1",
8
- "prepublish": "npm run build",
9
- "build": "webpack --config webpack.config.js --mode=production"
10
- },
11
- "signalk": {
12
- "appIcon": "./icons/icon-72x72.png",
13
- "displayName": "Signalk-onvif-camera"
14
- },
15
- "keywords": [
16
- "signalk-node-server-plugin",
17
- "signalk-webapp",
18
- "signalk-category-utility"
19
- ],
20
- "repository": "https://github.com/KEGustafsson/signalk-onvif-camera",
21
- "author": "Karl-Erik Gustafsson",
22
- "license": "MIT",
23
- "dependencies": {
24
- "devcert": "^1.2.0",
25
- "websocket": "^1.0.34",
26
- "xml2js": "^0.6.2"
27
- },
28
- "devDependencies": {
29
- "copy-webpack-plugin": "^13.0.0",
30
- "html-webpack-plugin": "^5.5.0",
31
- "webpack": "^5.67.0",
32
- "webpack-cli": "^6.0.1"
33
- }
34
- }
1
+ {
2
+ "name": "signalk-onvif-camera",
3
+ "version": "0.3.0",
4
+ "description": "Signal K Onvif Camera Interface",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1",
8
+ "prepublish": "npm run build",
9
+ "build": "webpack --config webpack.config.js --mode=production"
10
+ },
11
+ "signalk": {
12
+ "appIcon": "./icons/icon-72x72.png",
13
+ "displayName": "Signalk-onvif-camera"
14
+ },
15
+ "keywords": [
16
+ "signalk-node-server-plugin",
17
+ "signalk-webapp",
18
+ "signalk-category-utility"
19
+ ],
20
+ "repository": "https://github.com/KEGustafsson/signalk-onvif-camera",
21
+ "author": "Karl-Erik Gustafsson",
22
+ "license": "MIT",
23
+ "dependencies": {
24
+ "devcert": "^1.2.0",
25
+ "websocket": "^1.0.34",
26
+ "xml2js": "^0.6.2"
27
+ },
28
+ "devDependencies": {
29
+ "copy-webpack-plugin": "^13.0.0",
30
+ "html-webpack-plugin": "^5.5.0",
31
+ "webpack": "^5.67.0",
32
+ "webpack-cli": "^6.0.1"
33
+ }
34
+ }
Binary file
File without changes
package/public/index.html CHANGED
File without changes
package/public/main.js CHANGED
File without changes
package/public/style.css CHANGED
File without changes