signalk-onvif-camera 0.0.2

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/README.md ADDED
@@ -0,0 +1,49 @@
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
+ - Select port for server
9
+ - Select https/wss if you would like to use secure server
10
+ - Enter Onvif profile username
11
+ - Enter Onvif profile password
12
+ - Add camera IP to list (user/pass are used to login to camera)
13
+
14
+
15
+ ## Onvif Camera Webapp.
16
+
17
+ ![webapp](doc/webapp.jpg)
18
+ - Service can be accessed from Webapps menu, press "Signalk-onvif-camera" button
19
+
20
+ ## Onvif Camera service.
21
+
22
+ ![service](doc/service.jpg)
23
+ - Onvif cameras are searched from local network
24
+ - When search is ready then "Select a device" is prompted
25
+ - Camera is selected from dropdown menu and then press "Connect" button
26
+
27
+ ## Onvif Camera in Use.
28
+
29
+ ![inuse](doc/inuse.jpg)
30
+ - Cursors and home button for PTZ camera
31
+ - Zoom in/out
32
+ - Control speed
33
+ - Disconnect
34
+
35
+ ## Installation
36
+
37
+ ```
38
+ $ npm install signalk-onvif-camera --save
39
+ ```
40
+ or
41
+ ```
42
+ $ npm install https://github.com/KEGustafsson/signalk-onvif-camera.git --save
43
+ ```
44
+ ## Version control
45
+
46
+ - v0.0.1, 1st version for testing
47
+
48
+ ## Credits
49
+ https://github.com/futomi/node-onvif
package/doc/config.jpg ADDED
Binary file
package/doc/inuse.jpg ADDED
Binary file
Binary file
package/doc/webapp.jpg ADDED
Binary file
package/index.html ADDED
@@ -0,0 +1,138 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <title>ONVIF Network Camera Manager</title>
6
+ <meta http-equiv="X-UA-Compatible" content="IE=edge" />
7
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
8
+ <!-- jQuery -->
9
+ <script
10
+ src="https://code.jquery.com/jquery-3.1.1.min.js"
11
+ integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
12
+ crossorigin="anonymous"
13
+ ></script>
14
+ <!-- Bootstrap -->
15
+ <link
16
+ rel="stylesheet"
17
+ href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
18
+ integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
19
+ crossorigin="anonymous"
20
+ />
21
+ <link
22
+ rel="stylesheet"
23
+ href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"
24
+ integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp"
25
+ crossorigin="anonymous"
26
+ />
27
+ <script
28
+ src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
29
+ integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
30
+ crossorigin="anonymous"
31
+ ></script>
32
+ <!-- -->
33
+ <link href="style.css" rel="stylesheet" />
34
+ <script src="onvif.js"></script>
35
+ </head>
36
+ <div class="container theme-showcase" role="main" id="main-wrapper">
37
+ <form class="form-horizontal" id="connect-form">
38
+ <div class="form-group">
39
+ <label for="device" class="col-sm-2 control-label">Device</label>
40
+ <div class="col-sm-10">
41
+ <select class="form-control" id="device" name="device" disabled>
42
+ <option>now searching...</option>
43
+ </select>
44
+ </div>
45
+ </div>
46
+ <button
47
+ type="button"
48
+ class="form-control btn btn-primary"
49
+ name="connect"
50
+ disabled
51
+ >
52
+ Connect
53
+ </button>
54
+ </form>
55
+
56
+ <div id="connected-device">
57
+ <p><img class="snapshot" src="" /></p>
58
+ <div class="device-info-box">
59
+ <span class="name"></span> (<span class="address"></span>)
60
+ </div>
61
+ <div class="ptz-ctl-box">
62
+ <div class="ptz-pad-box">
63
+ <button type="button" class="ptz-goto-home">
64
+ <span class="glyphicon glyphicon-home"></span>
65
+ </button>
66
+ <span class="left glyphicon glyphicon-menu-left"></span>
67
+ <span class="right glyphicon glyphicon-menu-right"></span>
68
+ <span class="up glyphicon glyphicon-menu-up"></span>
69
+ <span class="down glyphicon glyphicon-menu-down"></span>
70
+ </div>
71
+ </div>
72
+ <div class="ptz-spd-ctl-box">
73
+ <span class="label">PTZ speed for keyboard</span>
74
+ <div class="btn-group btn-group-sm" data-toggle="buttons">
75
+ <label class="btn btn-default"
76
+ ><input type="radio" name="ptz-speed" value="0.5" /> slow</label
77
+ >
78
+ <label class="btn btn-default"
79
+ ><input type="radio" name="ptz-speed" value="0.75" />
80
+ medium</label
81
+ >
82
+ <label class="btn btn-default active"
83
+ ><input type="radio" name="ptz-speed" value="1.0" checked />
84
+ fast</label
85
+ >
86
+ </div>
87
+ </div>
88
+ <div
89
+ class="ptz-zom-ctl-box btn-group btn-group-lg"
90
+ role="group"
91
+ aria-label="Zoom"
92
+ >
93
+ <button type="button" class="ptz-zom ptz-zom-ot btn btn-default">
94
+ <span class="glyphicon glyphicon-zoom-out"></span>
95
+ </button>
96
+ <button type="button" class="ptz-zom ptz-zom-in btn btn-default">
97
+ <span class="glyphicon glyphicon-zoom-in"></span>
98
+ </button>
99
+ </div>
100
+ <div class="disconnect-box">
101
+ <button
102
+ type="button"
103
+ class="form-control btn btn-default"
104
+ name="disconnect"
105
+ >
106
+ Disconnect
107
+ </button>
108
+ </div>
109
+ </div>
110
+ </div>
111
+
112
+ <div class="modal fade" tabindex="-1" role="dialog" id="message-modal">
113
+ <div class="modal-dialog" role="document">
114
+ <div class="modal-content">
115
+ <div class="modal-header">
116
+ <button
117
+ type="button"
118
+ class="close"
119
+ data-dismiss="modal"
120
+ aria-label="Close"
121
+ >
122
+ <span aria-hidden="true">&times;</span>
123
+ </button>
124
+ <h4 class="modal-title"></h4>
125
+ </div>
126
+ <div class="modal-body">
127
+ <p class="modal-message"></p>
128
+ </div>
129
+ <div class="modal-footer">
130
+ <button type="button" class="btn btn-default" data-dismiss="modal">
131
+ Close
132
+ </button>
133
+ </div>
134
+ </div>
135
+ </div>
136
+ </div>
137
+ </body>
138
+ </html>
package/index.js ADDED
@@ -0,0 +1,418 @@
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 port;
44
+ let secure;
45
+ let wsServer;
46
+ let webServer;
47
+ let userName;
48
+ let password;
49
+ let certStatus = false;
50
+ let startServer
51
+
52
+ plugin.start = function (options, restartPlugin) {
53
+ userName = options.userName;
54
+ password = options.password;
55
+ port = options.port;
56
+ secure = options.secure;
57
+ const browserData = [{"secure": secure,"port": port}];
58
+ fs.writeFileSync(path.join(__dirname, 'browserdata.json'), JSON.stringify(browserData));
59
+
60
+ const certFile = './tls.cert'
61
+ fs.access(certFile, fs.F_OK, (err) => {
62
+ if (err) {
63
+ devcert.certificateFor([
64
+ 'localhost'
65
+ ])
66
+ .then(({key, cert}) => {
67
+ fs.writeFileSync(path.join(__dirname, 'tls.key'), key);
68
+ fs.writeFileSync(path.join(__dirname, 'tls.cert'), cert);
69
+ certStatus = true;
70
+ })
71
+ .catch(console.error);
72
+ } else {
73
+ certStatus = true;
74
+ }
75
+ });
76
+
77
+ startServer = setInterval(() => {
78
+ if (secure) {
79
+ if (certStatus) {
80
+ certStatus = false;
81
+ clearInterval(startServer);
82
+ const httpsSec = {
83
+ key: fs.readFileSync(path.join(__dirname, 'tls.key')),
84
+ cert: fs.readFileSync(path.join(__dirname, 'tls.cert')),
85
+ };
86
+ webServer = https.createServer(httpsSec, httpServerRequest);
87
+ webServer.listen(port, () => {
88
+ console.log(`Onvif Camera https/wss server running at 0.0.0.0:${port}`);
89
+ });
90
+ wsServer = new WebSocketServer({
91
+ httpServer: webServer,
92
+ });
93
+ wsServer.on('request', wsServerRequest);
94
+ }
95
+ } else {
96
+ clearInterval(startServer);
97
+ webServer = http.createServer(httpServerRequest);
98
+ webServer.listen(port, () => {
99
+ console.log(`Onvif Camera http/ws server running at 0.0.0.0:${port}`);
100
+ });
101
+ wsServer = new WebSocketServer({
102
+ httpServer: webServer,
103
+ });
104
+ wsServer.on('request', wsServerRequest);
105
+ }
106
+ }, 1000);
107
+ };
108
+
109
+ plugin.stop = function stop() {
110
+ clearInterval(startServer);
111
+ if (webServer) {
112
+ wsServer.shutDown();
113
+ webServer.close(() => {
114
+ console.log("Onvif Camera server closed");
115
+ });
116
+ }
117
+ };
118
+
119
+ plugin.uiSchema = {
120
+ //hide password from ui
121
+ password: {
122
+ 'ui:widget': 'password'
123
+ },
124
+ }
125
+
126
+ plugin.schema = {
127
+ type: "object",
128
+ title: 'Onvif Camera Interface',
129
+ description: 'Make an ONVIF user profile to camera(s) and add camera(s) IP below',
130
+ properties: {
131
+ port: {
132
+ type: 'number',
133
+ title: 'Server port number',
134
+ default: 8880
135
+ },
136
+ secure: {
137
+ type: 'boolean',
138
+ title: 'Use https/wss instead of http/ws'
139
+ },
140
+ userName: {
141
+ type: 'string',
142
+ title: 'ONVIF username for camera(s)'
143
+ },
144
+ password: {
145
+ type: 'string',
146
+ title: 'ONVIF password for camera(s)'
147
+ },
148
+ cameras: {
149
+ type: 'array',
150
+ title: 'Camera List',
151
+ items: {
152
+ type: 'object',
153
+ required: [],
154
+ properties: {
155
+ address: {
156
+ type: 'string',
157
+ title: 'Camera address'
158
+ },
159
+ },
160
+ },
161
+ },
162
+ },
163
+ };
164
+
165
+ function httpServerRequest(req, res) {
166
+ var path = req.url.replace(/\?.*$/, "");
167
+ if (path.match(/\.{2,}/) || path.match(/[^a-zA-Z\d\_\-\.\/]/)) {
168
+ httpServerResponse404(req.url, res);
169
+ return;
170
+ }
171
+ if (path === "/") {
172
+ path = "/index.html";
173
+ }
174
+ var fpath = "." + path;
175
+ fs.readFile(fpath, "utf-8", function (err, data) {
176
+ if (err) {
177
+ httpServerResponse404(req.url, res);
178
+ return;
179
+ } else {
180
+ var ctype = getContentType(fpath);
181
+ res.writeHead(200, { "Content-Type": ctype });
182
+ res.write(data);
183
+ res.end();
184
+ }
185
+ });
186
+ }
187
+
188
+ function getContentType(fpath) {
189
+ var ext = fpath.split(".").pop().toLowerCase();
190
+ if (ext.match(/^(html|htm)$/)) {
191
+ return "text/html";
192
+ } else if (ext.match(/^(jpeg|jpg)$/)) {
193
+ return "image/jpeg";
194
+ } else if (ext.match(/^(png|gif)$/)) {
195
+ return "image/" + ext;
196
+ } else if (ext === "css") {
197
+ return "text/css";
198
+ } else if (ext === "js") {
199
+ return "text/javascript";
200
+ } else if (ext === "woff2") {
201
+ return "application/font-woff";
202
+ } else if (ext === "woff") {
203
+ return "application/font-woff";
204
+ } else if (ext === "ttf") {
205
+ return "application/font-ttf";
206
+ } else if (ext === "svg") {
207
+ return "image/svg+xml";
208
+ } else if (ext === "eot") {
209
+ return "application/vnd.ms-fontobject";
210
+ } else if (ext === "oft") {
211
+ return "application/x-font-otf";
212
+ } else {
213
+ return "application/octet-stream";
214
+ }
215
+ }
216
+
217
+ function httpServerResponse404(url, res) {
218
+ res.write("404 Not Found: " + url);
219
+ res.end();
220
+ console.log("HTTP : 404 Not Found : " + url);
221
+ }
222
+
223
+ var client_list = [];
224
+
225
+ function wsServerRequest(request) {
226
+ var conn = request.accept(null, request.origin);
227
+ conn.on("message", function (message) {
228
+ if (message.type !== "utf8") {
229
+ return;
230
+ }
231
+ var data = JSON.parse(message.utf8Data);
232
+ var method = data["method"];
233
+ var params = data["params"];
234
+ if (method === "startDiscovery") {
235
+ startDiscovery(conn);
236
+ } else if (method === "connect") {
237
+ connect(conn, params);
238
+ } else if (method === "fetchSnapshot") {
239
+ fetchSnapshot(conn, params);
240
+ } else if (method === "ptzMove") {
241
+ ptzMove(conn, params);
242
+ } else if (method === "ptzStop") {
243
+ ptzStop(conn, params);
244
+ } else if (method === "ptzHome") {
245
+ ptzHome(conn, params);
246
+ }
247
+ });
248
+
249
+ conn.on("close", function (message) {});
250
+ conn.on("error", function (error) {
251
+ console.log(error);
252
+ });
253
+ }
254
+
255
+ var devices = {};
256
+ function startDiscovery(conn) {
257
+ devices = {};
258
+ let names = {};
259
+ onvif
260
+ .startProbe()
261
+ .then((device_list) => {
262
+ device_list.forEach((device) => {
263
+ let odevice = new onvif.OnvifDevice({
264
+ xaddr: device.xaddrs[0],
265
+ });
266
+ let addr = odevice.address;
267
+ devices[addr] = odevice;
268
+ names[addr] = (device.name).replace(/%20/g, " ");
269
+ });
270
+ var devs = {};
271
+ for (var addr in devices) {
272
+ devs[addr] = {
273
+ name: names[addr],
274
+ address: addr,
275
+ };
276
+ }
277
+ let res = { id: "startDiscovery", result: devs };
278
+ conn.send(JSON.stringify(res));
279
+ })
280
+ .catch((error) => {
281
+ let res = { id: "connect", error: error.message };
282
+ conn.send(JSON.stringify(res));
283
+ });
284
+ }
285
+
286
+ function connect(conn, params) {
287
+ var device = devices[params.address];
288
+ if (!device) {
289
+ var res = {
290
+ id: "connect",
291
+ error: "The specified device is not found: " + params.address,
292
+ };
293
+ conn.send(JSON.stringify(res));
294
+ return;
295
+ }
296
+
297
+ if (userName) {
298
+ params.user = userName;
299
+ params.pass = password;
300
+ device.setAuth(params.user, params.pass);
301
+ }
302
+
303
+ device.init((error, result) => {
304
+ var res = { id: "connect" };
305
+ if (error) {
306
+ res["error"] = error.toString();
307
+ } else {
308
+ res["result"] = result;
309
+ }
310
+ conn.send(JSON.stringify(res));
311
+ });
312
+ }
313
+
314
+ function fetchSnapshot(conn, params) {
315
+ var device = devices[params.address];
316
+ if (!device) {
317
+ var res = {
318
+ id: "fetchSnapshot",
319
+ error: "The specified device is not found: " + params.address,
320
+ };
321
+ conn.send(JSON.stringify(res));
322
+ return;
323
+ }
324
+ device.fetchSnapshot((error, result) => {
325
+ var res = { id: "fetchSnapshot" };
326
+ if (error) {
327
+ res["error"] = error.toString();
328
+ } else {
329
+ var ct = result["headers"]["content-type"];
330
+ var buffer = result["body"];
331
+ var b64 = buffer.toString("base64");
332
+ var uri = "data:" + ct + ";base64," + b64;
333
+ res["result"] = uri;
334
+ }
335
+ conn.send(JSON.stringify(res));
336
+ });
337
+ }
338
+
339
+ function ptzMove(conn, params) {
340
+ var device = devices[params.address];
341
+ if (!device) {
342
+ var res = {
343
+ id: "ptzMove",
344
+ error: "The specified device is not found: " + params.address,
345
+ };
346
+ conn.send(JSON.stringify(res));
347
+ return;
348
+ }
349
+ device.ptzMove(params, (error) => {
350
+ var res = { id: "ptzMove" };
351
+ if (error) {
352
+ res["error"] = error.toString();
353
+ } else {
354
+ res["result"] = true;
355
+ }
356
+ conn.send(JSON.stringify(res));
357
+ });
358
+ }
359
+
360
+ function ptzStop(conn, params) {
361
+ var device = devices[params.address];
362
+ if (!device) {
363
+ var res = {
364
+ id: "ptzStop",
365
+ error: "The specified device is not found: " + params.address,
366
+ };
367
+ conn.send(JSON.stringify(res));
368
+ return;
369
+ }
370
+ device.ptzStop((error) => {
371
+ var res = { id: "ptzStop" };
372
+ if (error) {
373
+ res["error"] = error.toString();
374
+ } else {
375
+ res["result"] = true;
376
+ }
377
+ conn.send(JSON.stringify(res));
378
+ });
379
+ }
380
+
381
+ function ptzHome(conn, params) {
382
+ var device = devices[params.address];
383
+ if (!device) {
384
+ var res = {
385
+ id: "ptzMove",
386
+ error: "The specified device is not found: " + params.address,
387
+ };
388
+ conn.send(JSON.stringify(res));
389
+ return;
390
+ }
391
+ if (!device.services.ptz) {
392
+ var res = {
393
+ id: "ptzHome",
394
+ error: "The specified device does not support PTZ.",
395
+ };
396
+ conn.send(JSON.stringify(res));
397
+ return;
398
+ }
399
+
400
+ var ptz = device.services.ptz;
401
+ var profile = device.getCurrentProfile();
402
+ var params = {
403
+ ProfileToken: profile["token"],
404
+ Speed: 1,
405
+ };
406
+ ptz.gotoHomePosition(params, (error, result) => {
407
+ var res = { id: "ptzMove" };
408
+ if (error) {
409
+ res["error"] = error.toString();
410
+ } else {
411
+ res["result"] = true;
412
+ }
413
+ conn.send(JSON.stringify(res));
414
+ });
415
+ }
416
+
417
+ return plugin;
418
+ };