forked from Achronite/mqtt-energenie-ener314rt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
energenie.js
164 lines (145 loc) · 6.34 KB
/
energenie.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
//
// This is the async energenie child node program with 2-way comms to not block parent node.js in any way
//
// This is designed to handle all the Rx & Tx requests, it communicates with the parent using process.on & process.send
//
// @Achronite - January 2023
//
"use strict";
var path = require('path');
let monitoring = false;
let initialised = false;
// OpenThings Device constants
const MIHO004 = 1;
const MIHO005 = 2;
const MIHO006 = 5;
const MIHO013 = 3;
const MIHO032 = 12;
const MIHO033 = 13;
const MIHO069 = 18;
//const MIHO089|MiHome Click - Smart Button|?|No|Yes||
var events = require('events');
this.events = new events.EventEmitter();
// import my dependant node.js module
var ener314rt = require('energenie-ener314rt');
// main processing section that does stuff when asked by parent
process.on('message', msg => {
console.log("energenie: received command:", msg);
switch (msg.cmd) {
case 'init':
case 'reset':
// Normally we initialise automatically anyway, this is a forced reset
console.log("energenie: reset");
process.send({ cmd: "initialised" })
break;
case 'send':
//console.log("energenie: Sending:", msg.payload);
// Check xmit times (advanced), 26ms per payload transmission
var xmits = Number(msg.repeat) || 20;
let switchState = Boolean(msg.switchState);
switch (msg.mode) {
case 'ook':
case 'OOK':
// Check and set parameters
let zone = Number(msg.zone);
if (zone < 0 || zone > 1048575 || isNaN(zone)) {
console.log("ERROR: zone err: " + msg.zone + " (" + typeof (msg.zone) + ")");
break;
}
let switchNum = Number(msg.switchNum);
if (switchNum < 0 || switchNum > 6 || isNaN(switchNum)) {
console.log("ERROR: SwitchNum not 0-6: " + msg.switchNum + " (" + typeof (msg.switchNum) + ")");
break;
}
// Invoke C function to do the send
if (initialised){
var ret = ener314rt.ookSwitch(zone, switchNum, switchState, xmits);
if (ret == 0){
msg.state = switchState;
}
} else {
console.log("EMULATION: calling ookSwitch(", zone, ",", switchNum, ",", switchState,",", xmits,")");
msg.emulated = true;
msg.state = switchState;
}
// return result to parent
process.send(msg);
break;
case 'fsk':
case 'FSK':
// Check and set parameters
let productId = Number(msg.productId);
let deviceId = Number(msg.deviceId);
if (deviceId < 0 || isNaN(deviceId)) {
console.log("ERROR: deviceId err: " + msg.deviceId + " (" + typeof (msg.deviceId) + ")");
break;
}
switch (productId){
case MIHO005: //Adapter+
// switch only device, call specifc function for this
// Invoke C function to do the send
if (initialised){
var res = ener314rt.openThingsSwitch(productId, deviceId, switchState, xmits);
// monitoring loop should respond for us
} else {
console.log(`EMULATION: calling openThingsSwitch( ${productId}, ${deviceId}, ${switchState}, ${xmits})`);
// for emulation mode we need to respond, otherwise monitoring loop will do it for us
msg.emulated = true;
msg.state = switchState;
process.send(msg);
}
break;
case MIHO013: //eTRV
break;
}
break;
default:
// Unknown message mode //
}
break;
case 'monitor':
// start monitoring (if not started already)
//console.log("energenie: monitor enabled=", msg.enabled);
if (!monitoring && initialised) {
monitoring = true;
//getMonitorMsg();
startMonitoringThread();
console.log("energenie: Monitoring thread started");
} else if (!initialised){
console.log("energenie: Monitoring thread cannot be enabled, ENER314-RT unavailable");
}
break;
case 'close':
console.log("energenie: closing");
ener314rt.closeEner314rt();
process.exit();
case 'cacheCmd':
if (msg.data === undefined || msg.data === null){
msg.data = 0;
}
var res = ener314rt.openThingsCacheCmd(msg.deviceId, msg.otCommand, msg.data);
console.log(`energenie: cached cmd=${msg.otCommand} res=${res}`);
break;
default:
console.log("energenie: Unknown or missing command:", msg.cmd);
}
});
// monitor thread version in ener314rt uses a callback to return monitor messages directly (collected below), it needs the callback passing in
function startMonitoringThread() {
ener314rt.openThingsReceiveThread(10000, (msg) => {
//console.log(`asyncOpenThingsReceive ret=${ret}`);
console.log(`energenie: monitor received=${msg}`);
var OTmsg = JSON.parse(msg);
OTmsg.cmd = 'monitor';
process.send(OTmsg);
});
};
// Initialise
//console.log("energenie: Initialising adaptor");
var ret = ener314rt.initEner314rt(false);
if (ret==0){
initialised = true;
console.log(`energenie: ENER314-RT initialised succesfully.`);
} else {
console.log(`energenie ERROR: failed to initialise ENER314-RT ${ret}, EMULATION mode enabled`);
}