@@ -56,25 +56,29 @@ module.exports = function (RED) {
5656
5757 if ( config . thing !== "" && config . property !== "" ) {
5858 this . arduinoRestClient = await connectionManager . getClientHttp ( connectionConfig ) ;
59- this . thing = config . thing ;
60- this . propertyId = config . property ;
61- this . propertyName = config . name ;
62- this . on ( 'input' , async function ( msg ) {
63- try {
64- this . arduinoRestClient . setProperty ( this . thing , this . propertyId , msg . payload ) ;
65- const s = getStatus ( msg . payload ) ;
66- if ( s != undefined )
67- this . status ( { fill : "grey" , shape : "dot" , text : s } ) ;
68- else
69- this . status ( { } ) ;
70- } catch ( err ) {
71- console . log ( err ) ;
72- this . status ( { fill : "red" , shape : "dot" , text : "Error setting value" } ) ;
73- }
74- } ) ;
75- this . on ( 'close' , function ( ) {
76- connectionManager . deleteClientHttp ( connectionConfig . credentials . clientid ) ;
77- } ) ;
59+ if ( this . arduinoRestClient ) {
60+ this . thing = config . thing ;
61+ this . propertyId = config . property ;
62+ this . propertyName = config . name ;
63+ this . on ( 'input' , async function ( msg ) {
64+ try {
65+ this . arduinoRestClient . setProperty ( this . thing , this . propertyId , msg . payload ) ;
66+ const s = getStatus ( msg . payload ) ;
67+ if ( s != undefined )
68+ this . status ( { fill : "grey" , shape : "dot" , text : s } ) ;
69+ else
70+ this . status ( { } ) ;
71+ } catch ( err ) {
72+ console . log ( err ) ;
73+ this . status ( { fill : "red" , shape : "dot" , text : "Error setting value" } ) ;
74+ }
75+ } ) ;
76+ this . on ( 'close' , function ( ) {
77+ connectionManager . deleteClientHttp ( connectionConfig . credentials . clientid ) ;
78+ } ) ;
79+ } else {
80+ this . status ( { fill : "red" , shape : "ring" , text : "Connection Error" } ) ;
81+ }
7882 }
7983 } catch ( err ) {
8084 console . log ( err ) ;
@@ -95,44 +99,48 @@ module.exports = function (RED) {
9599 if ( connectionConfig && config . thing !== "" && config . thing !== "0" && config . property !== "" && config . property !== "0" ) {
96100 try {
97101 this . arduinoRestClient = await connectionManager . getClientHttp ( connectionConfig ) ;
98- if ( config . thing !== "" && config . property !== "" ) {
99- this . thing = config . thing ;
100- this . propertyId = config . property ;
101- this . propertyName = config . name ;
102- node . on ( 'input' , async function ( ) {
103- const now = moment ( ) ;
104- const end = now . format ( ) ;
105- const count = this . timeWindowCount
106- if ( count !== null && count !== "" && count !== undefined && Number . isInteger ( parseInt ( count ) ) && parseInt ( count ) !== 0 ) {
107- const start = now . subtract ( count * this . timeWindowUnit , 'second' ) . format ( ) ;
102+ if ( this . arduinoRestClient ) {
103+ if ( config . thing !== "" && config . property !== "" ) {
104+ this . thing = config . thing ;
105+ this . propertyId = config . property ;
106+ this . propertyName = config . name ;
107+ node . on ( 'input' , async function ( ) {
108+ const now = moment ( ) ;
109+ const end = now . format ( ) ;
110+ const count = this . timeWindowCount
111+ if ( count !== null && count !== "" && count !== undefined && Number . isInteger ( parseInt ( count ) ) && parseInt ( count ) !== 0 ) {
112+ const start = now . subtract ( count * this . timeWindowUnit , 'second' ) . format ( ) ;
108113
109- const result = await this . arduinoRestClient . getSeries ( this . thing , this . propertyId , start , end ) ;
110- const times = result . responses [ 0 ] . times ;
111- const values = result . responses [ 0 ] . values ;
112- let data = [ ] ;
113- if ( values && times ) {
114- values . forEach ( function ( item , index , array ) {
115- data . push ( {
116- x : moment ( times [ index ] ) . unix ( ) * 1000 ,
117- y : values [ index ]
114+ const result = await this . arduinoRestClient . getSeries ( this . thing , this . propertyId , start , end ) ;
115+ const times = result . responses [ 0 ] . times ;
116+ const values = result . responses [ 0 ] . values ;
117+ let data = [ ] ;
118+ if ( values && times ) {
119+ values . forEach ( function ( item , index , array ) {
120+ data . push ( {
121+ x : moment ( times [ index ] ) . unix ( ) * 1000 ,
122+ y : values [ index ]
123+ } ) ;
118124 } ) ;
119- } ) ;
120- }
121- node . send (
122- {
123- topic : config . name ,
124- payload : [ {
125- series : [ ] ,
126- data : [ data ]
127- } ]
128125 }
129- ) ;
130- }
131- } ) ;
126+ node . send (
127+ {
128+ topic : config . name ,
129+ payload : [ {
130+ series : [ ] ,
131+ data : [ data ]
132+ } ]
133+ }
134+ ) ;
135+ }
136+ } ) ;
132137
133- this . on ( 'close' , function ( ) {
134- connectionManager . deleteClientHttp ( connectionConfig . credentials . clientid ) ;
135- } ) ;
138+ this . on ( 'close' , function ( ) {
139+ connectionManager . deleteClientHttp ( connectionConfig . credentials . clientid ) ;
140+ } ) ;
141+ }
142+ } else {
143+ this . status ( { fill : "red" , shape : "ring" , text : "Connection Error" } ) ;
136144 }
137145 } catch ( err ) {
138146 console . log ( err ) ;
@@ -153,20 +161,24 @@ module.exports = function (RED) {
153161 if ( connectionConfig && config . thing !== "" && config . thing !== "0" && config . property !== "" && config . property !== "0" ) {
154162 try {
155163 this . arduinoRestClient = await connectionManager . getClientHttp ( connectionConfig ) ;
156- if ( config . thing !== "" && config . property !== "" ) {
157- this . thing = config . thing ;
158- this . propertyId = config . property ;
159- this . propertyName = config . name ;
160- const pollTime = this . timeWindowCount * this . timeWindowUnit ;
161- if ( pollTime !== null && pollTime !== "" && pollTime !== undefined && Number . isInteger ( parseInt ( pollTime ) ) && parseInt ( pollTime ) !== 0 ) {
162- this . poll ( connectionConfig , pollTime ) ;
163- this . on ( 'close' , function ( ) {
164- connectionManager . deleteClientHttp ( connectionConfig . credentials . clientid ) ;
165- if ( this . pollTimeoutPoll )
166- clearTimeout ( this . pollTimeoutPoll ) ;
164+ if ( this . arduinoRestClient ) {
165+ if ( config . thing !== "" && config . property !== "" ) {
166+ this . thing = config . thing ;
167+ this . propertyId = config . property ;
168+ this . propertyName = config . name ;
169+ const pollTime = this . timeWindowCount * this . timeWindowUnit ;
170+ if ( pollTime !== null && pollTime !== "" && pollTime !== undefined && Number . isInteger ( parseInt ( pollTime ) ) && parseInt ( pollTime ) !== 0 ) {
171+ this . poll ( connectionConfig , pollTime ) ;
172+ this . on ( 'close' , function ( ) {
173+ connectionManager . deleteClientHttp ( connectionConfig . credentials . clientid ) ;
174+ if ( this . pollTimeoutPoll )
175+ clearTimeout ( this . pollTimeoutPoll ) ;
167176
168- } ) ;
177+ } ) ;
178+ }
169179 }
180+ } else {
181+ this . status ( { fill : "red" , shape : "ring" , text : "Connection Error" } ) ;
170182 }
171183 } catch ( err ) {
172184 console . log ( err ) ;
@@ -211,28 +223,32 @@ module.exports = function (RED) {
211223
212224 if ( config . thing !== "" && config . property !== "" ) {
213225 this . arduinoRestClient = await connectionManager . getClientHttp ( connectionConfig ) ;
214- this . thing = config . thing ;
215- this . propertyId = config . property ;
216- this . propertyName = config . name ;
217- node . on ( 'input' , async function ( ) {
226+ if ( this . arduinoRestClient ) {
227+ this . thing = config . thing ;
228+ this . propertyId = config . property ;
229+ this . propertyName = config . name ;
230+ node . on ( 'input' , async function ( ) {
218231
219- const property = await this . arduinoRestClient . getProperty ( this . thing , this . propertyId ) ;
220- this . send (
221- {
222- topic : property . name ,
223- payload : property . last_value ,
224- timestamp : property . value_updated_at
225- }
226- ) ;
227- const s = getStatus ( property . last_value ) ;
228- if ( s != undefined )
229- this . status ( { fill : "grey" , shape : "dot" , text : s } ) ;
230- else
231- this . status ( { } ) ;
232- } ) ;
233- this . on ( 'close' , function ( ) {
234- connectionManager . deleteClientHttp ( connectionConfig . credentials . clientid ) ;
235- } ) ;
232+ const property = await this . arduinoRestClient . getProperty ( this . thing , this . propertyId ) ;
233+ this . send (
234+ {
235+ topic : property . name ,
236+ payload : property . last_value ,
237+ timestamp : property . value_updated_at
238+ }
239+ ) ;
240+ const s = getStatus ( property . last_value ) ;
241+ if ( s != undefined )
242+ this . status ( { fill : "grey" , shape : "dot" , text : s } ) ;
243+ else
244+ this . status ( { } ) ;
245+ } ) ;
246+ this . on ( 'close' , function ( ) {
247+ connectionManager . deleteClientHttp ( connectionConfig . credentials . clientid ) ;
248+ } ) ;
249+ } else {
250+ this . status ( { fill : "red" , shape : "ring" , text : "Connection Error" } ) ;
251+ }
236252 }
237253 } catch ( err ) {
238254 console . log ( err ) ;
0 commit comments