@@ -11,14 +11,28 @@ module.exports = function (RED) {
1111 if ( connectionConfig && config . thing !== "" && config . thing !== "0" && config . property !== "" && config . property !== "0" ) {
1212 try {
1313
14- this . arduinoClient = await connectionManager . getClientMqtt ( connectionConfig ) ;
1514 this . thing = config . thing ;
1615 this . propertyId = config . property ;
17- this . propertyName = config . name ;
18- await this . arduinoClient . onPropertyValue ( this . thing , this . propertyName , this . update ) ;
19-
20- this . on ( 'close' , function ( done ) {
21- connectionManager . deleteClientMqtt ( connectionConfig . credentials . clientid , this . propertyName ) . then ( ( ) => { done ( ) ; } ) ;
16+ this . propertyName = config . propname ;
17+ this . arduinoClient = await connectionManager . getClientMqtt ( connectionConfig ) ;
18+ if ( this . arduinoClient && this . arduinoClient . connection . isConnected ( ) ) {
19+ await this . arduinoClient . onPropertyValue ( this . thing , this . propertyName , ( msg ) => {
20+ this . send (
21+ {
22+ topic : this . propertyName ,
23+ payload : msg ,
24+ timestamp : ( new Date ( ) ) . getTime ( )
25+ }
26+ ) ;
27+ const s = getStatus ( msg ) ;
28+ if ( s != undefined )
29+ this . status ( { fill : "grey" , shape : "dot" , text : s } ) ;
30+ else
31+ this . status ( { } ) ;
32+ } ) ;
33+ }
34+ this . on ( 'close' , function ( done ) {
35+ connectionManager . deleteClientMqtt ( connectionConfig . credentials . clientid , this . thing , this . propertyName ) . then ( ( ) => { done ( ) ; } ) ;
2236 } ) ;
2337
2438 //this.poll(connectionConfig);
@@ -29,46 +43,6 @@ module.exports = function (RED) {
2943 }
3044 realConstructor . apply ( this , [ config ] ) ;
3145 }
32- ArduinoIotInput . prototype = {
33- /*poll: async function (connectionConfig) {
34- try {
35- await connectionManager.connect(connectionConfig);
36- const property = await this.arduinoRestClient.getProperty(this.thing, this.propertyId);
37- if (typeof (property.last_value) !== "object" && property.last_value !== this.lastValue ||
38- typeof (property.last_value) === "object" && !_.isEqual(property.last_value, this.lastValue)
39- ) {
40- this.send(
41- {
42- topic: property.name,
43- payload: property.last_value,
44- timestamp: property.value_updated_at
45- }
46- );
47- const s = getStatus(property.last_value);
48- if (s != undefined)
49- this.status({ fill: "grey", shape: "dot", text: s });
50- else
51- this.status({});
52- this.lastValue = property.last_value;
53- }
54-
55- this.pollTimeout = setTimeout(() => { this.poll(connectionConfig) }, 1000);
56- } catch (err) {
57- this.status({ fill: "red", shape: "dot", text: "Error getting value" });
58- console.log(err);
59- }
60- }*/
61- update : function ( msg ) {
62- var date = new Date ( ) ;
63- this . send (
64- {
65- topic : this . propertyName ,
66- payload : msg ,
67- timestamp : date . getTime ( )
68- }
69- ) ;
70- }
71- }
7246 RED . nodes . registerType ( "property in" , ArduinoIotInput ) ;
7347
7448 function ArduinoIotOutput ( config ) {
@@ -96,7 +70,7 @@ module.exports = function (RED) {
9670 this . status ( { fill : "red" , shape : "dot" , text : "Error setting value" } ) ;
9771 }
9872 } ) ;
99- this . on ( 'close' , function ( ) {
73+ this . on ( 'close' , function ( ) {
10074 connectionManager . deleteClientHttp ( connectionConfig . credentials . clientid ) ;
10175 } ) ;
10276 }
@@ -127,7 +101,7 @@ module.exports = function (RED) {
127101 const now = moment ( ) ;
128102 const end = now . format ( ) ;
129103 const count = this . timeWindowCount
130- if ( count !== null && count !== "" && count !== undefined && Number . isInteger ( parseInt ( count ) ) && parseInt ( count ) !== 0 ) {
104+ if ( count !== null && count !== "" && count !== undefined && Number . isInteger ( parseInt ( count ) ) && parseInt ( count ) !== 0 ) {
131105 const start = now . subtract ( count * this . timeWindowUnit , 'second' ) . format ( ) ;
132106
133107 const result = await this . arduinoRestClient . getSeries ( this . thing , this . propertyId , start , end ) ;
@@ -154,7 +128,7 @@ module.exports = function (RED) {
154128 }
155129 } ) ;
156130
157- this . on ( 'close' , function ( ) {
131+ this . on ( 'close' , function ( ) {
158132 connectionManager . deleteClientHttp ( connectionConfig . credentials . clientid ) ;
159133 } ) ;
160134 }
@@ -182,11 +156,11 @@ module.exports = function (RED) {
182156 this . propertyId = config . property ;
183157 this . propertyName = config . name ;
184158 const pollTime = this . timeWindowCount * this . timeWindowUnit ;
185- if ( pollTime !== null && pollTime !== "" && pollTime !== undefined && Number . isInteger ( parseInt ( pollTime ) ) && parseInt ( pollTime ) !== 0 ) {
159+ if ( pollTime !== null && pollTime !== "" && pollTime !== undefined && Number . isInteger ( parseInt ( pollTime ) ) && parseInt ( pollTime ) !== 0 ) {
186160 this . poll ( connectionConfig , pollTime ) ;
187161 this . on ( 'close' , function ( ) {
188162 connectionManager . deleteClientHttp ( connectionConfig . credentials . clientid ) ;
189- if ( this . pollTimeoutPoll )
163+ if ( this . pollTimeoutPoll )
190164 clearTimeout ( this . pollTimeoutPoll ) ;
191165
192166 } ) ;
@@ -282,69 +256,48 @@ module.exports = function (RED) {
282256 }
283257 } ) ;
284258
285- RED . httpAdmin . get ( "/things" , RED . auth . needsPermission ( 'Property-in.read' ) , async function ( req , res ) {
259+ async function getThingsOrProperties ( req , res , thingsOrProperties ) {
260+ let arduinoRestClient ;
286261 try {
287262 if ( req . query . clientid || req . query . clientsecret ) {
288- await connectionManager . connect (
289- {
290- credentials : {
291- clientid : req . query . clientid ,
292- clientsecret : req . query . clientsecret
293- }
263+ arduinoRestClient = await connectionManager . getClientHttp ( {
264+ credentials : {
265+ clientid : req . query . clientid ,
266+ clientsecret : req . query . clientsecret
294267 }
295- ) ;
268+ } ) ;
296269 } else if ( req . query . connectionid ) {
297270 const connectionConfig = RED . nodes . getNode ( req . query . connectionid ) ;
298271 if ( ! connectionConfig ) {
299272 console . log ( "No credentials available." ) ;
300273 return res . send ( JSON . stringify ( { error : "No credentials available." } ) ) ;
301274 }
302- await connectionManager . connect ( connectionConfig ) ;
275+ arduinoRestClient = await connectionManager . getClientHttp ( connectionConfig ) ;
303276 } else {
304277 console . log ( "No credentials available." ) ;
305278 return res . send ( JSON . stringify ( { error : "No credentials available." } ) ) ;
306279 }
307- const arduinoRestClient = connectionManager . apiRest ;
308- const things = await arduinoRestClient . getThings ( ) ;
309- return res . send ( JSON . stringify ( things ) ) ;
280+ if ( thingsOrProperties === "things" ) {
281+ return res . send ( JSON . stringify ( await arduinoRestClient . getThings ( ) ) ) ;
282+ } else if ( thingsOrProperties === "properties" ) {
283+ const thing_id = req . query . thing_id ;
284+ return res . send ( JSON . stringify ( await arduinoRestClient . getProperties ( thing_id ) ) ) ;
285+ } else {
286+ console . log ( "Wrong parameter in getThingsOrProperties." ) ;
287+ return res . send ( JSON . stringify ( { error : "Wrong parameter in getThingsOrProperties." } ) ) ;
288+ }
310289 } catch ( err ) {
311290 console . log ( `Status: ${ err . status } , message: ${ err . error } ` ) ;
312291 return res . send ( JSON . stringify ( { error : "Wrong credentials or system unavailable." } ) ) ;
313292 }
293+ }
294+ RED . httpAdmin . get ( "/things" , RED . auth . needsPermission ( 'Property-in.read' ) , async function ( req , res ) {
295+ return getThingsOrProperties ( req , res , "things" ) ;
314296 } ) ;
315297
316298 RED . httpAdmin . get ( "/properties" , RED . auth . needsPermission ( 'Property-in.read' ) , async function ( req , res ) {
317- try {
318- if ( req . query . clientid && req . query . clientsecret ) {
319- await connectionManager . connect (
320- {
321- credentials : {
322- clientid : req . query . clientid ,
323- clientsecret : req . query . clientsecret
324- }
325- }
326- ) ;
327- } else if ( req . query . connectionid ) {
328- const connectionConfig = RED . nodes . getNode ( req . query . connectionid ) ;
329- if ( ! connectionConfig )
330- return res . send ( JSON . stringify ( [ ] ) ) ;
331- await connectionManager . connect ( connectionConfig ) ;
332- } else {
333- console . log ( "No credentials available." ) ;
334- return res . send ( JSON . stringify ( [ ] ) ) ;
335- }
336- const ArduinoRestClient = connectionManager . apiRest ;
337- const thing_id = req . query . thing_id ;
338- const properties = await ArduinoRestClient . getProperties ( thing_id ) ;
339- return res . send ( JSON . stringify ( properties ) ) ;
340- } catch ( err ) {
341- console . log ( `Status: ${ err . status } , message: ${ err . error } ` ) ;
342- return res . send ( { error : "Wrong credentials or system unavailable." } ) ;
343- }
299+ return getThingsOrProperties ( req , res , "properties" ) ;
344300 } ) ;
345-
346-
347-
348301}
349302
350303function getStatus ( value ) {
0 commit comments