@@ -27,6 +27,8 @@ module.exports = function(RED) {
27
27
28
28
var node = this ;
29
29
30
+ node . timer = null ;
31
+
30
32
if ( ! this . ip || ( this . ip . length < 10 ) ) {
31
33
node . status ( {
32
34
fill : "red" ,
@@ -37,9 +39,12 @@ module.exports = function(RED) {
37
39
}
38
40
39
41
var setStatus = function ( json ) {
42
+ const oldVertical = node . VERTICAL ;
43
+ const oldHorizontal = node . HORIZONTAL ;
40
44
node . VERTICAL = json [ 'VERTICAL' ] || 'Unknown' ;
41
45
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 ) ;
43
48
44
49
if ( ! node . working ) {
45
50
node . error ( json ) ;
@@ -50,38 +55,59 @@ module.exports = function(RED) {
50
55
} ) ;
51
56
return ;
52
57
}
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
+ }
58
70
} ;
59
71
60
72
setStatus ( { } ) ; // start in an unknown state
61
73
62
74
//
63
75
// check connectivity/update current status
64
76
//
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
+ } ) ;
76
93
} ) ;
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
+ } ) ;
78
104
79
105
//const nodeId = node.id.replace(/\./g, '_');
80
106
// currentNodes && currentNodes.add(nodeId);
81
107
82
108
// send Commands to the TV LIFT
83
109
84
- node . on ( 'input' , function ( msg ) {
110
+ node . on ( 'input' , function ( msg , send , done ) {
85
111
node . log ( 'tv lift input: ' + JSON . stringify ( msg . payload , null , 2 ) ) ;
86
112
87
113
if ( ! msg . payload || ( [ 'UP' , 'DOWN' , 'MEM1' , 'MEM2' , 'MEM3' ] . indexOf ( msg . payload ) < 0 ) ) {
@@ -91,23 +117,43 @@ module.exports = function(RED) {
91
117
shape : 'dot' ,
92
118
text : 'invalid payload'
93
119
} ) ;
120
+ if ( done ) {
121
+ done ( ) ;
122
+ }
94
123
return ;
95
124
}
96
125
126
+ // fallback to using `node.send`
127
+ // send = send || function() { node.send.apply(node,arguments) }
128
+
97
129
axios . post ( `http://${ node . ip } /api/command` , { "COMMAND" : msg . payload } )
98
130
. then ( response => {
99
131
node . log ( msg . payload + ': ' + ( ( response . data && response . data . STATUS ) || "?" ) ) ;
132
+ if ( done ) {
133
+ done ( ) ;
134
+ }
100
135
} )
101
136
. catch ( error => {
102
- node . error ( error . message || 'Unknown Error' ) ;
103
137
node . status ( {
104
138
fill : 'red' ,
105
139
shape : 'dot' ,
106
140
text : error . message || 'Unknown Error'
107
141
} ) ;
142
+ if ( done ) {
143
+ done ( error ) ;
144
+ } else {
145
+ node . error ( error . message || 'Unknown Error' ) ;
146
+ }
108
147
} ) ;
109
148
} ) ;
110
149
150
+ node . on ( 'close' , function ( ) {
151
+ if ( node . timer ) {
152
+ clearInterval ( node . timer ) ;
153
+ node . timer = null ;
154
+ }
155
+ } ) ;
156
+
111
157
}
112
158
113
159
RED . nodes . registerType ( "TV Lift" , TVLift ) ;
0 commit comments