@@ -2,34 +2,34 @@ const connectionManager = require("./arduino-connection-manager");
22const moment = require ( "moment" ) ;
33const _ = require ( 'lodash' ) ;
44
5- module . exports = function ( RED ) {
5+ module . exports = function ( RED ) {
66 function ArduinoIotInput ( config ) {
77 const realConstructor = async ( config ) => {
88 RED . nodes . createNode ( this , config ) ;
99 const connectionConfig = RED . nodes . getNode ( config . connection ) ;
1010 this . lastValue = undefined ;
11- try {
12- await connectionManager . connect ( connectionConfig ) ;
13- if ( config . thing !== "" && config . property !== "" ) {
11+ if ( config . thing !== "" && config . thing !== "0" && config . property !== "" && config . property !== "0" ) {
12+ try {
13+ await connectionManager . connect ( connectionConfig ) ;
1414 this . arduinoRestClient = connectionManager . apiRest ;
1515 this . thing = config . thing ;
1616 this . propertyId = config . property ;
1717 this . propertyName = config . name ;
1818 this . poll ( connectionConfig ) ;
19+ } catch ( err ) {
20+ console . log ( err ) ;
1921 }
20- } catch ( err ) {
21- console . log ( err ) ;
2222 }
2323 }
2424 realConstructor . apply ( this , [ config ] ) ;
2525 }
2626 ArduinoIotInput . prototype = {
27- poll : async function ( connectionConfig ) {
27+ poll : async function ( connectionConfig ) {
2828 try {
29- await connectionManager . connect ( connectionConfig ) ;
29+ await connectionManager . connect ( connectionConfig ) ;
3030 const property = await this . arduinoRestClient . getProperty ( this . thing , this . propertyId ) ;
3131 if ( typeof ( property . last_value ) !== "object" && property . last_value !== this . lastValue ||
32- typeof ( property . last_value ) === "object" && ! _ . isEqual ( property . last_value , this . lastValue )
32+ typeof ( property . last_value ) === "object" && ! _ . isEqual ( property . last_value , this . lastValue )
3333 ) {
3434 this . send (
3535 {
@@ -40,13 +40,13 @@ module.exports = function(RED) {
4040 ) ;
4141 const s = getStatus ( property . last_value ) ;
4242 if ( s )
43- this . status ( { fill : "grey" , shape : "dot" , text : s } ) ;
43+ this . status ( { fill : "grey" , shape : "dot" , text : s } ) ;
4444 else
4545 this . status ( { } ) ;
4646 this . lastValue = property . last_value ;
4747 }
4848
49- this . pollTimeout = setTimeout ( ( ) => { this . poll ( connectionConfig ) } , 1000 ) ;
49+ this . pollTimeout = setTimeout ( ( ) => { this . poll ( connectionConfig ) } , 1000 ) ;
5050 } catch ( err ) {
5151 console . log ( err ) ;
5252 }
@@ -58,29 +58,31 @@ module.exports = function(RED) {
5858 const realConstructor = async ( config ) => {
5959 RED . nodes . createNode ( this , config ) ;
6060 const connectionConfig = RED . nodes . getNode ( config . connection ) ;
61- try {
62- await connectionManager . connect ( connectionConfig ) ;
63- if ( config . thing !== "" && config . property !== "" ) {
64- this . arduinoRestClient = connectionManager . apiRest ;
65- this . thing = config . thing ;
66- this . propertyId = config . property ;
67- this . propertyName = config . name ;
68- this . on ( 'input' , async function ( msg ) {
69- try {
70- await connectionManager . connect ( connectionConfig ) ;
71- this . arduinoRestClient . setProperty ( this . thing , this . propertyId , msg . payload ) ;
72- const s = getStatus ( msg . payload ) ;
73- if ( s )
74- this . status ( { fill : "grey" , shape : "dot" , text : s } ) ;
75- else
76- this . status ( { } ) ;
77- } catch ( err ) {
78- console . log ( err ) ;
79- }
80- } ) ;
61+ if ( config . thing !== "" && config . thing !== "0" && config . property !== "" && config . property !== "0" ) {
62+ try {
63+ await connectionManager . connect ( connectionConfig ) ;
64+ if ( config . thing !== "" && config . property !== "" ) {
65+ this . arduinoRestClient = connectionManager . apiRest ;
66+ this . thing = config . thing ;
67+ this . propertyId = config . property ;
68+ this . propertyName = config . name ;
69+ this . on ( 'input' , async function ( msg ) {
70+ try {
71+ await connectionManager . connect ( connectionConfig ) ;
72+ this . arduinoRestClient . setProperty ( this . thing , this . propertyId , msg . payload ) ;
73+ const s = getStatus ( msg . payload ) ;
74+ if ( s )
75+ this . status ( { fill : "grey" , shape : "dot" , text : s } ) ;
76+ else
77+ this . status ( { } ) ;
78+ } catch ( err ) {
79+ console . log ( err ) ;
80+ }
81+ } ) ;
82+ }
83+ } catch ( err ) {
84+ console . log ( err ) ;
8185 }
82- } catch ( err ) {
83- console . log ( err ) ;
8486 }
8587 }
8688 realConstructor . apply ( this , [ config ] ) ;
@@ -94,42 +96,44 @@ module.exports = function(RED) {
9496 const node = this ;
9597 this . timeWindowCount = config . timeWindowCount ;
9698 this . timeWindowUnit = config . timeWindowUnit ;
97- try {
98- this . arduinoRestClient = connectionManager . apiRest ;
99- if ( config . thing !== "" && config . property !== "" ) {
100- this . thing = config . thing ;
101- this . propertyId = config . property ;
102- this . propertyName = config . name ;
103- node . on ( 'input' , async function ( ) {
104- const now = moment ( ) ;
105- const end = now . format ( ) ;
106- const start = now . subtract ( this . timeWindowCount * this . timeWindowUnit , 'second' ) . format ( ) ;
107- await connectionManager . connect ( connectionConfig ) ;
108- const result = await this . arduinoRestClient . getSeries ( this . thing , this . propertyId , start , end ) ;
109- const times = result . responses [ 0 ] . times ;
110- const values = result . responses [ 0 ] . values ;
111- let data = [ ] ;
112- if ( values && times ) {
99+ if ( config . thing !== "" && config . thing !== "0" && config . property !== "" && config . property !== "0" ) {
100+ try {
101+ this . arduinoRestClient = connectionManager . apiRest ;
102+ if ( config . thing !== "" && config . property !== "" ) {
103+ this . thing = config . thing ;
104+ this . propertyId = config . property ;
105+ this . propertyName = config . name ;
106+ node . on ( 'input' , async function ( ) {
107+ const now = moment ( ) ;
108+ const end = now . format ( ) ;
109+ const start = now . subtract ( this . timeWindowCount * this . timeWindowUnit , 'second' ) . format ( ) ;
110+ await connectionManager . connect ( connectionConfig ) ;
111+ const result = await this . arduinoRestClient . getSeries ( this . thing , this . propertyId , start , end ) ;
112+ const times = result . responses [ 0 ] . times ;
113+ const values = result . responses [ 0 ] . values ;
114+ let data = [ ] ;
115+ if ( values && times ) {
113116 values . forEach ( function ( item , index , array ) {
114117 data . push ( {
115118 x : moment ( times [ index ] ) . unix ( ) * 1000 ,
116119 y : values [ index ]
117120 } ) ;
118121 } ) ;
119122 }
120- node . send (
121- {
122- topic : config . name ,
123- payload : [ {
124- series : [ ] ,
125- data : [ data ]
126- } ]
127- }
128- ) ;
129- } ) ;
123+ node . send (
124+ {
125+ topic : config . name ,
126+ payload : [ {
127+ series : [ ] ,
128+ data : [ data ]
129+ } ]
130+ }
131+ ) ;
132+ } ) ;
133+ }
134+ } catch ( err ) {
135+ console . log ( err ) ;
130136 }
131- } catch ( err ) {
132- console . log ( err ) ;
133137 }
134138 }
135139 realConstructor . apply ( this , [ config ] ) ;
@@ -140,43 +144,43 @@ module.exports = function(RED) {
140144 const realConstructor = async ( config ) => {
141145 RED . nodes . createNode ( this , config ) ;
142146 const connectionConfig = RED . nodes . getNode ( config . connection ) ;
143- const node = this ;
144-
145147 this . timeWindowCount = config . timeWindowCount ;
146148 this . timeWindowUnit = config . timeWindowUnit ;
147- try {
148- this . arduinoRestClient = connectionManager . apiRest ;
149- if ( config . thing !== "" && config . property !== "" ) {
150- this . thing = config . thing ;
151- this . propertyId = config . property ;
152- this . propertyName = config . name ;
153- const pollTime = this . timeWindowCount * this . timeWindowUnit ;
154- this . poll ( connectionConfig , pollTime ) ;
149+ if ( config . thing !== "" && config . thing !== "0" && config . property !== "" && config . property !== "0" ) {
150+ try {
151+ this . arduinoRestClient = connectionManager . apiRest ;
152+ if ( config . thing !== "" && config . property !== "" ) {
153+ this . thing = config . thing ;
154+ this . propertyId = config . property ;
155+ this . propertyName = config . name ;
156+ const pollTime = this . timeWindowCount * this . timeWindowUnit ;
157+ this . poll ( connectionConfig , pollTime ) ;
158+ }
159+ } catch ( err ) {
160+ console . log ( err ) ;
155161 }
156- } catch ( err ) {
157- console . log ( err ) ;
158162 }
159163 }
160164 realConstructor . apply ( this , [ config ] ) ;
161165 }
162166 ArduinoIotInputPoll . prototype = {
163- poll : async function ( connectionConfig , pollTime ) {
167+ poll : async function ( connectionConfig , pollTime ) {
164168 try {
165169 await connectionManager . connect ( connectionConfig ) ;
166170 const property = await this . arduinoRestClient . getProperty ( this . thing , this . propertyId ) ;
167- this . send (
168- {
169- topic : property . name ,
170- payload : property . last_value ,
171- timestamp : property . value_updated_at
172- }
173- ) ;
174- const s = getStatus ( property . last_value ) ;
175- if ( s )
176- this . status ( { fill : "grey" , shape : "dot" , text : s } ) ;
177- else
178- this . status ( { } ) ;
179- this . pollTimeoutPoll = setTimeout ( ( ) => { this . poll ( connectionConfig , pollTime ) } , pollTime * 1000 ) ;
171+ this . send (
172+ {
173+ topic : property . name ,
174+ payload : property . last_value ,
175+ timestamp : property . value_updated_at
176+ }
177+ ) ;
178+ const s = getStatus ( property . last_value ) ;
179+ if ( s )
180+ this . status ( { fill : "grey" , shape : "dot" , text : s } ) ;
181+ else
182+ this . status ( { } ) ;
183+ this . pollTimeoutPoll = setTimeout ( ( ) => { this . poll ( connectionConfig , pollTime ) } , pollTime * 1000 ) ;
180184 } catch ( err ) {
181185 console . log ( err ) ;
182186 }
@@ -190,32 +194,34 @@ module.exports = function(RED) {
190194 RED . nodes . createNode ( this , config ) ;
191195 const connectionConfig = RED . nodes . getNode ( config . connection ) ;
192196 const node = this ;
193- try {
194- await connectionManager . connect ( connectionConfig ) ;
195- if ( config . thing !== "" && config . property !== "" ) {
196- this . arduinoRestClient = connectionManager . apiRest ;
197- this . thing = config . thing ;
198- this . propertyId = config . property ;
199- this . propertyName = config . name ;
200- node . on ( 'input' , async function ( ) {
201- await connectionManager . connect ( connectionConfig ) ;
202- const property = await this . arduinoRestClient . getProperty ( this . thing , this . propertyId ) ;
203- this . send (
204- {
205- topic : property . name ,
206- payload : property . last_value ,
207- timestamp : property . value_updated_at
208- }
209- ) ;
210- const s = getStatus ( property . last_value ) ;
211- if ( s )
212- this . status ( { fill : "grey" , shape : "dot" , text : s } ) ;
213- else
214- this . status ( { } ) ;
197+ if ( config . thing !== "" && config . thing !== "0" && config . property !== "" && config . property !== "0" ) {
198+ try {
199+ await connectionManager . connect ( connectionConfig ) ;
200+ if ( config . thing !== "" && config . property !== "" ) {
201+ this . arduinoRestClient = connectionManager . apiRest ;
202+ this . thing = config . thing ;
203+ this . propertyId = config . property ;
204+ this . propertyName = config . name ;
205+ node . on ( 'input' , async function ( ) {
206+ await connectionManager . connect ( connectionConfig ) ;
207+ const property = await this . arduinoRestClient . getProperty ( this . thing , this . propertyId ) ;
208+ this . send (
209+ {
210+ topic : property . name ,
211+ payload : property . last_value ,
212+ timestamp : property . value_updated_at
213+ }
214+ ) ;
215+ const s = getStatus ( property . last_value ) ;
216+ if ( s )
217+ this . status ( { fill : "grey" , shape : "dot" , text : s } ) ;
218+ else
219+ this . status ( { } ) ;
215220 } ) ;
221+ }
222+ } catch ( err ) {
223+ console . log ( err ) ;
216224 }
217- } catch ( err ) {
218- console . log ( err ) ;
219225 }
220226 }
221227 realConstructor . apply ( this , [ config ] ) ;
@@ -224,14 +230,14 @@ module.exports = function(RED) {
224230 RED . nodes . registerType ( "property in push" , ArduinoIotInputPush ) ;
225231
226232 function ArduinoConnectionNode ( config ) {
227- RED . nodes . createNode ( this , config ) ;
233+ RED . nodes . createNode ( this , config ) ;
228234 this . applicationname = config . applicationname ;
229235 this . clientid = config . clientid ;
230236 this . clientsecret = config . clientsecret ;
231237 }
232- RED . nodes . registerType ( "arduino-connection" , ArduinoConnectionNode ) ;
238+ RED . nodes . registerType ( "arduino-connection" , ArduinoConnectionNode ) ;
233239
234- RED . httpAdmin . get ( "/things" , RED . auth . needsPermission ( 'Property-in.read' ) , async function ( req , res ) {
240+ RED . httpAdmin . get ( "/things" , RED . auth . needsPermission ( 'Property-in.read' ) , async function ( req , res ) {
235241 try {
236242 const connectionConfig = {
237243 clientid : req . query . clientid ,
@@ -246,7 +252,7 @@ module.exports = function(RED) {
246252 }
247253 } ) ;
248254
249- RED . httpAdmin . get ( "/properties" , RED . auth . needsPermission ( 'Property-in.read' ) , async function ( req , res ) {
255+ RED . httpAdmin . get ( "/properties" , RED . auth . needsPermission ( 'Property-in.read' ) , async function ( req , res ) {
250256 try {
251257 const connectionConfig = {
252258 clientid : req . query . clientid ,
@@ -267,7 +273,7 @@ module.exports = function(RED) {
267273}
268274
269275function getStatus ( value ) {
270- if ( typeof value !== "object" ) {
276+ if ( typeof value !== "object" ) {
271277 if ( typeof value === "number" && ! ( Number . isInteger ( value ) ) )
272278 return value . toFixed ( 3 ) ;
273279 else
0 commit comments