@@ -10,20 +10,16 @@ module.exports = function (RED) {
1010 this . lastValue = undefined ;
1111 if ( connectionConfig && config . thing !== "" && config . thing !== "0" && config . property !== "" && config . property !== "0" ) {
1212 try {
13- await connectionManager . connect ( connectionConfig ) ;
14- this . arduinoRestClient = connectionManager . apiRest ;
13+
14+ this . arduinoClient = await connectionManager . getClientMqtt ( connectionConfig ) ;
1515 this . thing = config . thing ;
1616 this . propertyId = config . property ;
1717 this . propertyName = config . name ;
18- var token = connectionManager . getToken ( connectionConfig . credentials . clientid ) ;
19- connectionManager . mqttClient . readProperty ( connectionConfig . credentials . clientid , token , this . thing , this . propertyName , this . update ) ;
18+ await this . arduinoClient . onPropertyValue ( this . thing , this . propertyName , this . update ) ;
2019
2120 this . on ( 'close' , function ( done ) {
22- await connectionManager . mqttClient . disconnect ( connectionConfig . credentials . clientid , this . thing , this . propertyName ) ;
23-
24- done ( ) ;
25-
26- } ) ;
21+ connectionManager . deleteClientMqtt ( connectionConfig . credentials . clientid , this . propertyName ) . then ( ( ) => { done ( ) ; } ) ;
22+ } ) ;
2723
2824 //this.poll(connectionConfig);
2925 } catch ( err ) {
@@ -81,15 +77,14 @@ module.exports = function (RED) {
8177 const connectionConfig = RED . nodes . getNode ( config . connection ) ;
8278 if ( connectionConfig && config . thing !== "" && config . thing !== "0" && config . property !== "" && config . property !== "0" ) {
8379 try {
84- await connectionManager . connect ( connectionConfig ) ;
80+
8581 if ( config . thing !== "" && config . property !== "" ) {
86- this . arduinoRestClient = connectionManager . apiRest ;
82+ this . arduinoRestClient = await connectionManager . getClientHttp ( connectionConfig ) ;
8783 this . thing = config . thing ;
8884 this . propertyId = config . property ;
8985 this . propertyName = config . name ;
9086 this . on ( 'input' , async function ( msg ) {
9187 try {
92- await connectionManager . connect ( connectionConfig ) ;
9388 this . arduinoRestClient . setProperty ( this . thing , this . propertyId , msg . payload ) ;
9489 const s = getStatus ( msg . payload ) ;
9590 if ( s != undefined )
@@ -101,6 +96,9 @@ module.exports = function (RED) {
10196 this . status ( { fill : "red" , shape : "dot" , text : "Error setting value" } ) ;
10297 }
10398 } ) ;
99+ this . on ( 'close' , function ( ) {
100+ connectionManager . deleteClientHttp ( connectionConfig . credentials . clientid ) ;
101+ } ) ;
104102 }
105103 } catch ( err ) {
106104 console . log ( err ) ;
@@ -120,7 +118,7 @@ module.exports = function (RED) {
120118 this . timeWindowUnit = config . timeWindowUnit ;
121119 if ( connectionConfig && config . thing !== "" && config . thing !== "0" && config . property !== "" && config . property !== "0" ) {
122120 try {
123- this . arduinoRestClient = connectionManager . apiRest ;
121+ this . arduinoRestClient = await connectionManager . getClientHttp ( connectionConfig ) ;
124122 if ( config . thing !== "" && config . property !== "" ) {
125123 this . thing = config . thing ;
126124 this . propertyId = config . property ;
@@ -131,7 +129,7 @@ module.exports = function (RED) {
131129 const count = this . timeWindowCount
132130 if ( count !== null && count !== "" && count !== undefined && Number . isInteger ( parseInt ( count ) ) && parseInt ( count ) !== 0 ) {
133131 const start = now . subtract ( count * this . timeWindowUnit , 'second' ) . format ( ) ;
134- await connectionManager . connect ( connectionConfig ) ;
132+
135133 const result = await this . arduinoRestClient . getSeries ( this . thing , this . propertyId , start , end ) ;
136134 const times = result . responses [ 0 ] . times ;
137135 const values = result . responses [ 0 ] . values ;
@@ -155,6 +153,10 @@ module.exports = function (RED) {
155153 ) ;
156154 }
157155 } ) ;
156+
157+ this . on ( 'close' , function ( ) {
158+ connectionManager . deleteClientHttp ( connectionConfig . credentials . clientid ) ;
159+ } ) ;
158160 }
159161 } catch ( err ) {
160162 console . log ( err ) ;
@@ -174,18 +176,20 @@ module.exports = function (RED) {
174176 this . timeWindowUnit = config . timeWindowUnit ;
175177 if ( connectionConfig && config . thing !== "" && config . thing !== "0" && config . property !== "" && config . property !== "0" ) {
176178 try {
177- this . arduinoRestClient = connectionManager . apiRest ;
179+ this . arduinoRestClient = await connectionManager . getClientHttp ( connectionConfig ) ;
178180 if ( config . thing !== "" && config . property !== "" ) {
179181 this . thing = config . thing ;
180182 this . propertyId = config . property ;
181183 this . propertyName = config . name ;
182184 const pollTime = this . timeWindowCount * this . timeWindowUnit ;
183185 if ( pollTime !== null && pollTime !== "" && pollTime !== undefined && Number . isInteger ( parseInt ( pollTime ) ) && parseInt ( pollTime ) !== 0 ) {
184- this . poll ( connectionConfig , pollTime ) ;
186+ this . poll ( connectionConfig , pollTime ) ;
185187 this . on ( 'close' , function ( ) {
188+ connectionManager . deleteClientHttp ( connectionConfig . credentials . clientid ) ;
186189 if ( this . pollTimeoutPoll )
187190 clearTimeout ( this . pollTimeoutPoll ) ;
188- } ) ;
191+
192+ } ) ;
189193 }
190194 }
191195 } catch ( err ) {
@@ -229,14 +233,14 @@ module.exports = function (RED) {
229233 const node = this ;
230234 if ( connectionConfig && config . thing !== "" && config . thing !== "0" && config . property !== "" && config . property !== "0" ) {
231235 try {
232- await connectionManager . connect ( connectionConfig ) ;
236+
233237 if ( config . thing !== "" && config . property !== "" ) {
234- this . arduinoRestClient = connectionManager . apiRest ;
238+ this . arduinoRestClient = await connectionManager . getClientHttp ( connectionConfig ) ;
235239 this . thing = config . thing ;
236240 this . propertyId = config . property ;
237241 this . propertyName = config . name ;
238242 node . on ( 'input' , async function ( ) {
239- await connectionManager . connect ( connectionConfig ) ;
243+
240244 const property = await this . arduinoRestClient . getProperty ( this . thing , this . propertyId ) ;
241245 this . send (
242246 {
@@ -251,6 +255,9 @@ module.exports = function (RED) {
251255 else
252256 this . status ( { } ) ;
253257 } ) ;
258+ this . on ( 'close' , function ( ) {
259+ connectionManager . deleteClientHttp ( connectionConfig . credentials . clientid ) ;
260+ } ) ;
254261 }
255262 } catch ( err ) {
256263 console . log ( err ) ;
0 commit comments