Skip to content

Commit 53b7d33

Browse files
AndrewAndrew
Andrew
authored and
Andrew
committed
first polling version to detect state
1 parent 4d261da commit 53b7d33

File tree

3 files changed

+68
-21
lines changed

3 files changed

+68
-21
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,4 @@ fabric.properties
143143
# Android studio 3.1+ serialized cache file
144144
.idea/caches/build_file_checksums.ser
145145

146+
/index.js

nexus21/tvlift.js

+66-20
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ module.exports = function(RED) {
2727

2828
var node = this;
2929

30+
node.timer = null;
31+
3032
if (!this.ip || (this.ip.length < 10)) {
3133
node.status({
3234
fill: "red",
@@ -37,9 +39,12 @@ module.exports = function(RED) {
3739
}
3840

3941
var setStatus = function(json) {
42+
const oldVertical = node.VERTICAL;
43+
const oldHorizontal = node.HORIZONTAL;
4044
node.VERTICAL = json['VERTICAL'] || 'Unknown';
4145
node.HORIZONTAL = json['HORIZONTAL'] || 'Unknown';
42-
node.working = node.VERTICAL != 'Unknown';
46+
node.working = node.VERTICAL !== 'Unknown';
47+
const changed = (oldVertical !== node.VERTICAL) || (oldHorizontal !== node.HORIZONTAL);
4348

4449
if (!node.working) {
4550
node.error(json);
@@ -50,38 +55,59 @@ module.exports = function(RED) {
5055
});
5156
return;
5257
}
53-
node.status({
54-
fill: 'green',
55-
shape: 'dot',
56-
text: node.VERTICAL + ( node.HORIZONTAL != 'NA' ? '/' + node.HORIZONTAL : '')
57-
});
58+
if (changed) {
59+
// todo: add decoded error payload if required
60+
const msg = {
61+
payload: Object.assign({}, json)
62+
};
63+
node.send(msg); // pass on the status update to any listening nodes
64+
node.status({
65+
fill: 'green',
66+
shape: 'dot',
67+
text: node.VERTICAL + (node.HORIZONTAL != 'NA' ? '/' + node.HORIZONTAL : '')
68+
});
69+
}
5870
};
5971

6072
setStatus({}); // start in an unknown state
6173

6274
//
6375
// check connectivity/update current status
6476
//
65-
axios.get(`http://${node.ip}/api/status`)
66-
.then(response => {
67-
setStatus(response.data);
68-
// don't go active here, wait to see if socket connects, it will maintain the correct active state
69-
})
70-
.catch(error => {
71-
node.error(error.message || 'Unknown Error');
72-
node.status({
73-
fill: 'red',
74-
shape: 'dot',
75-
text: error.message || 'Unknown Error'
77+
78+
const pollStatus = function(callback) {
79+
axios.get(`http://${node.ip}/api/status`)
80+
.then(response => {
81+
setStatus(response.data);
82+
if (callback) {
83+
callback(null);
84+
}
85+
})
86+
.catch(error => {
87+
node.error(error.message || 'Unknown Error');
88+
node.status({
89+
fill: 'red',
90+
shape: 'dot',
91+
text: error.message || 'Unknown Error'
92+
});
7693
});
77-
});
94+
};
95+
96+
pollStatus(function(err) {
97+
if (err) { return; }
98+
// todo: use a socket connection intead of polling
99+
// todo: don't go active here, wait to see if socket connects, it will maintain the correct active state
100+
node.timer = setInterval(function(){
101+
pollStatus();
102+
}, 5000);
103+
});
78104

79105
//const nodeId = node.id.replace(/\./g, '_');
80106
// currentNodes && currentNodes.add(nodeId);
81107

82108
// send Commands to the TV LIFT
83109

84-
node.on('input', function(msg) {
110+
node.on('input', function(msg, send, done) {
85111
node.log('tv lift input: ' + JSON.stringify(msg.payload, null, 2));
86112

87113
if (!msg.payload || (['UP', 'DOWN', 'MEM1', 'MEM2', 'MEM3'].indexOf(msg.payload) < 0)) {
@@ -91,23 +117,43 @@ module.exports = function(RED) {
91117
shape: 'dot',
92118
text: 'invalid payload'
93119
});
120+
if (done) {
121+
done();
122+
}
94123
return;
95124
}
96125

126+
// fallback to using `node.send`
127+
// send = send || function() { node.send.apply(node,arguments) }
128+
97129
axios.post(`http://${node.ip}/api/command`, { "COMMAND": msg.payload })
98130
.then(response => {
99131
node.log(msg.payload + ': ' + ((response.data && response.data.STATUS) || "?"));
132+
if (done) {
133+
done();
134+
}
100135
})
101136
.catch(error => {
102-
node.error(error.message || 'Unknown Error');
103137
node.status({
104138
fill: 'red',
105139
shape: 'dot',
106140
text: error.message || 'Unknown Error'
107141
});
142+
if (done) {
143+
done(error);
144+
} else {
145+
node.error(error.message || 'Unknown Error');
146+
}
108147
});
109148
});
110149

150+
node.on('close', function() {
151+
if (node.timer) {
152+
clearInterval(node.timer);
153+
node.timer = null;
154+
}
155+
});
156+
111157
}
112158

113159
RED.nodes.registerType("TV Lift", TVLift);

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-red-contrib-nexus21",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Nodes to help automate Nexus21 TV Lifts.",
55
"scripts": {
66
"test": "echo \"Error: no test specified\" && exit 1"

0 commit comments

Comments
 (0)