-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
206 lines (179 loc) · 7.05 KB
/
node_helper.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
var NodeHelper = require('node_helper');
module.exports = NodeHelper.create({
setCircuit: function(circuitState) {
setCircuitState(circuitState, (poolStatus) => {
this.sendSocketNotification('SCREENLOGIC_CIRCUIT_DONE', {circuitState: circuitState, status: poolStatus});
});
},
setHeatpoint: function(heatpoint) {
setHeatpointState(heatpoint, (poolStatus) => {
this.sendSocketNotification('SCREENLOGIC_HEATPOINT_DONE', {heatpoint: heatpoint, status: poolStatus});
});
},
setHeatstate: function(heatstate) {
setHeatstateState(heatstate, (poolStatus) => {
this.sendSocketNotification('SCREENLOGIC_HEATSTATE_DONE', {heatstate: heatstate, status: poolStatus});
});
},
setLightcmd: function(lightCmd) {
setLights(lightCmd, (poolStatus) => {
this.sendSocketNotification('SCREENLOGIC_LIGHTCMD_DONE', {lightCmd: lightCmd, status: poolStatus});
});
},
notifyReconnecting: function() {
this.sendSocketNotification('SCREENLOGIC_RECONNECTING');
},
socketNotificationReceived: function(notification, payload) {
if (notification === 'SCREENLOGIC_CONFIG') {
if (!this.config) {
this.config = payload;
connect((status) => {
this.sendSocketNotification('SCREENLOGIC_RESULT', status);
}, () => {
this.notifyReconnecting();
});
} else if (poolData.status) {
this.sendSocketNotification('SCREENLOGIC_RESULT', poolData);
}
// if we don't have a status yet, assume the initial connection is still in progress and this socket notification will be delivered when setup is done
}
if (notification === 'SCREENLOGIC_CIRCUIT') {
this.setCircuit(payload);
}
if (notification === 'SCREENLOGIC_HEATPOINT') {
this.setHeatpoint(payload);
}
if (notification === 'SCREENLOGIC_HEATSTATE') {
this.setHeatstate(payload);
}
if (notification === 'SCREENLOGIC_LIGHTCMD') {
this.setLightcmd(payload);
}
}
});
const ScreenLogic = require('node-screenlogic');
const Log = require('logger');
const reconnectDelayMs = 10 * 1000;
const unitFinderTimeoutMs = 5 * 1000;
var foundUnit;
var poolData = {};
var refreshTimer;
var unitFinderRetry;
var unitReconnectTimer;
function connect(cb, reconnectCb) {
if (!foundUnit && typeof config !== 'undefined' && config.serverAddress && config.serverPort) {
Log.info(`[MMM-ScreenLogic] connecting directly to configured unit at ${config.serverAddress}:${config.serverPort}`);
foundUnit = new ScreenLogic.UnitConnection(config.serverPort, config.serverAddress);
}
if (foundUnit) {
setupUnit(cb, reconnectCb);
} else {
findServer(cb, reconnectCb);
}
}
function findServer(cb, reconnectCb) {
Log.info('[MMM-ScreenLogic] starting search for local units');
var finder = new ScreenLogic.FindUnits();
finder.on('serverFound', (server) => {
finder.close();
Log.info(`[MMM-ScreenLogic] local unit found at ${server.address}:${server.port}`);
foundUnit = new ScreenLogic.UnitConnection(server);
setupUnit(cb, reconnectCb);
clearInterval(unitFinderRetry);
unitFinderRetry = null;
}).on('error', (e) => {
Log.error(`[MMM-ScreenLogic] error trying to find a server. scheduling a retry in ${reconnectDelayMs / 1000} seconds`);
Log.error(e);
resetFoundUnit();
setTimeout(() => { findServer(cb); }, reconnectDelayMs);
});
finder.search();
unitFinderRetry = setInterval(() => {
Log.info(`[MMM-ScreenLogic] didn't find any units within ${unitFinderTimeoutMs / 1000} seconds, trying again...`);
finder.search();
}, unitFinderTimeoutMs);
}
function resetFoundUnit() {
foundUnit = null;
if (refreshTimer) {
clearInterval(refreshTimer);
refreshTimer = null;
}
if (unitFinderRetry) {
clearInterval(unitFinderRetry);
unitFinderRetry = null;
}
if (unitReconnectTimer) {
clearTimeout(unitReconnectTimer);
unitReconnectTimer = null;
}
}
function setupUnit(cb, reconnectCb) {
Log.info('[MMM-ScreenLogic] initial connection to unit...');
foundUnit.on('error', (e) => {
Log.error(`[MMM-ScreenLogic] error in unit connection. restarting the connection process in ${reconnectDelayMs / 1000} seconds`);
Log.error(e);
reconnectCb();
resetFoundUnit();
unitReconnectTimer = setTimeout(() => { connect(cb, reconnectCb); }, reconnectDelayMs);
}).on('close', () => {
Log.error(`[MMM-ScreenLogic] unit connection closed unexpectedly. restarting the connection process in ${reconnectDelayMs / 1000} seconds`);
reconnectCb();
resetFoundUnit();
unitReconnectTimer = setTimeout(() => { connect(cb, reconnectCb); }, reconnectDelayMs);
}).once('loggedIn', () => {
Log.info('[MMM-ScreenLogic] logged into unit. getting basic configuration...');
foundUnit.getControllerConfig();
}).once('controllerConfig', (config) => {
Log.info('[MMM-ScreenLogic] configuration received. adding client...');
poolData.controllerConfig = config;
poolData.degStr = config.degC ? 'C' : 'F';
foundUnit.addClient(1234);
}).once('addClient', () => {
Log.info('[MMM-ScreenLogic] client added successfully and listening for changes');
foundUnit.getPoolStatus();
// connection seems to time out every 10 minutes without some sort of request made
refreshTimer = setInterval(() => { foundUnit.pingServer(); }, 1 * 60 * 1000);
}).on('poolStatus', (status) => {
Log.info('[MMM-ScreenLogic] received pool status update');
poolData.status = status;
cb(poolData);
});
foundUnit.connect();
}
function setCircuitState(circuitState, cb) {
if (!foundUnit) {
cb();
return;
}
Log.info(`[MMM-ScreenLogic] setting circuit ${circuitState.id} to ${circuitState.state}`);
foundUnit.setCircuitState(0, circuitState.id, circuitState.state);
foundUnit.getPoolStatus();
}
function setHeatpointState(heatpoint, cb) {
if (!foundUnit) {
cb();
return;
}
Log.info(`[MMM-ScreenLogic] setting heatpoint for body ${heatpoint.body} to ${heatpoint.temperature} deg`);
foundUnit.setSetPoint(0, heatpoint.body, heatpoint.temperature);
foundUnit.getPoolStatus();
}
function setHeatstateState(heatstate, cb) {
if (!foundUnit) {
cb();
return;
}
Log.info(`[MMM-ScreenLogic] setting heat state for body ${heatstate.body} to ${heatstate.state}`);
foundUnit.setHeatMode(0, heatstate.body, heatstate.state);
foundUnit.getPoolStatus();
}
function setLights(lightCmd, cb) {
if (!foundUnit) {
cb();
return;
}
Log.info(`[MMM-ScreenLogic] sending light command ${lightCmd}`);
foundUnit.sendLightCommand(0, lightCmd);
foundUnit.getPoolStatus();
}