-
Notifications
You must be signed in to change notification settings - Fork 0
/
axis-discovery-wrapper.js
61 lines (55 loc) · 1.61 KB
/
axis-discovery-wrapper.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
//Copyright (c) 2021 Fred Juhlin
const axis = require('axis-discovery');
module.exports = function(RED) {
function AxisDiscovery(config) {
RED.nodes.createNode(this, config);
const discovery = new axis.Discovery();
discovery.on('hello', (device) => {
var devices = node.context().get("devices");
if(!devices)
devices = {};
if( node.output === "Once" && devices.hasOwnProperty(device.macAddress) )
return;
var newDevice = {
serial: device.macAddress,
address: device.address,
linkLocal: device.linkLocalAddress?device.linkLocalAddress:null,
name: device.friendlyName,
port: device.port,
timestamp: new Date().getTime()
}
devices[device.macAddress] = newDevice;
node.context().set("devices",devices);
node.send({
topic: newDevice.name,
payload: newDevice
});
})
var discoveryMode = false;
this.output = config.output;
var node = this;
node.status({fill:"red",shape:"dot",text:"Discovery stopped"});
node.on("input", function(msg) {
if(msg.payload === "start" || msg.payload === true ) {
if( discoveryMode === true )
return;
discovery.start();
discovery.search();
discoveryMode = true;
node.status({fill:"green",shape:"dot",text:"Discovery running"});
return;
}
if(msg.payload === "stop" || msg.payload === false ) {
if( discoveryMode === false )
return;
discovery.stop();
node.status({fill:"red",shape:"dot",text:"Discovery stopped"});
discoveryMode = false;
}
});
}
RED.nodes.registerType("Axis Discovery", AxisDiscovery, {
name: { type:"text" },
output: { type:"text" }
});
};