-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmbus-out.js
92 lines (72 loc) · 2.39 KB
/
mbus-out.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
module.exports = (RED) => {
function MbusOut(config) {
RED.nodes.createNode(this, config);
this.name = config.name;
const client = RED.nodes.getNode(config.client);
const node = this;
// ----- EVENTS HANDLERS ----------------------------------------------------
const subscribedEvents = {
mbError: onError,
mbClose: onClose,
mbConnect: onConnect,
mbReconnect: onReconnect,
mbScan: onScan,
mbScanComplete: onScanComplete,
mbDeviceUpdated: onDeviceUpdated,
mbDevicesLoaded: onDevicesLoaded,
mbPrimarySet: onPrimarySet,
};
function onConnect() {
setStatus('Connected', 'success');
}
function onError(failureMsg) {
setStatus(`Error: ${failureMsg}`, 'error');
}
function onClose() {
setStatus('Closed', 'error');
}
function onScan() {
setStatus('Scanning devices...', 'info');
}
function onScanComplete(devices) {
setStatus(`Scan complete, ${devices.length} devices found`, 'success');
node.send({ topic: 'mbScanComplete', payload: devices });
}
function onDeviceUpdated(device) {
setStatus(`Device ${device.SlaveInformation.Id} updated`, 'success');
node.send({ topic: 'mbDeviceUpdated', payload: device });
}
function onPrimarySet(data) {
setStatus(`Device ${data.old} successfully set to primary ID ${data.new}`, 'success');
node.send({ topic: 'mbPrimarySet', payload: data });
}
function onDevicesLoaded(devices) {
setStatus(`${devices.length} devices loaded from file`, 'success');
node.send({ topic: 'mbDevicesLoaded', payload: devices });
}
function onReconnect() {
setStatus('Reconnecting...', 'warning');
}
// ----- SUBSCRIBE TO CLIENT EVENTS -----------------------------------------
if (client) {
// update status
subscribedEvents[client.getStatus().event]();
client.registerForMbus(node);
Object.keys(subscribedEvents).forEach((evt) => {
client.on(evt, subscribedEvents[evt]);
});
}
// Set node status
function setStatus(message, type) {
const types = {
info: 'blue', error: 'red', warning: 'yellow', success: 'green',
};
node.status({
fill: types[type] || 'grey',
shape: 'dot',
text: message,
});
}
}// end mbus out
RED.nodes.registerType('mbus-out', MbusOut);
};