@@ -17,6 +17,16 @@ module.exports = function (RED) {
1717
1818 const node = this
1919
20+ node . plan = 'unknown'
21+
22+ RED . httpAdmin . get (
23+ `/vsh-connection/${ node . id } ` ,
24+ RED . auth . needsPermission ( 'vsh-virtual-device.read' ) ,
25+ function ( req , res ) {
26+ res . json ( { plan : node . plan } )
27+ }
28+ )
29+
2030 this . logger = config . debug
2131 ? ( logMessage , variable = undefined , logLevel = 'log' ) => {
2232 //logLevel: log | warn | error | trace | debug
@@ -90,12 +100,17 @@ module.exports = function (RED) {
90100
91101 this . registerChildNode = function ( nodeId , callbacks ) {
92102 if ( Object . keys ( this . childNodes ) . length >= this . allowedDeviceCount ) {
93- callbacks . setStatus ( {
94- shape : 'dot' ,
95- fill : 'gray' ,
96- text : 'device limit reached! Upgrade your VSH subscription to get more devices!' ,
97- } )
98- return
103+ callbacks . setActive ( false )
104+ callbacks . setStatus (
105+ {
106+ shape : 'dot' ,
107+ fill : 'gray' ,
108+ text : 'device limit reached! Upgrade your VSH subscription to get more devices!' ,
109+ } ,
110+ true //force!
111+ )
112+ } else {
113+ callbacks . setActive ( true )
99114 }
100115
101116 this . childNodes [ nodeId ] = callbacks
@@ -125,10 +140,6 @@ module.exports = function (RED) {
125140 }
126141 }
127142
128- this . isChildNodeRegistered = function ( nodeId ) {
129- return this . childNodes [ nodeId ] !== undefined
130- }
131-
132143 this . getLocalDevices = function ( ) {
133144 const localDevices = { }
134145
@@ -149,16 +160,20 @@ module.exports = function (RED) {
149160 return result
150161 }
151162
152- this . execCallbackForOne = function ( nodeId , eventName , eventDetails ) {
163+ this . execCallbackForOne = function (
164+ nodeId ,
165+ eventName ,
166+ params ,
167+ ...moreParams
168+ ) {
153169 if ( this . childNodes [ nodeId ] [ eventName ] ) {
154- return this . childNodes [ nodeId ] [ eventName ] ( eventDetails )
170+ return this . childNodes [ nodeId ] [ eventName ] ( params , ... moreParams )
155171 }
156172 }
157173
158174 this . requestConfig = function ( ) {
159175 this . isRequestConfigCompleted = false
160176 this . refreshChildrenNodeStatus ( )
161-
162177 this . publish ( `vsh/${ this . credentials . thingId } /requestConfig` , {
163178 vshVersion : VSH_VERSION ,
164179 } )
@@ -238,6 +253,7 @@ module.exports = function (RED) {
238253 deviceId,
239254 friendlyName : devices [ deviceId ] [ 'friendlyName' ] ,
240255 template : devices [ deviceId ] [ 'template' ] ,
256+ retrievable : devices [ deviceId ] [ 'retrievable' ] ,
241257 } )
242258 }
243259 }
@@ -260,7 +276,9 @@ module.exports = function (RED) {
260276 shadowDevices [ deviceId ] [ 'template' ] !==
261277 localDevices [ deviceId ] [ 'template' ] ||
262278 shadowDevices [ deviceId ] [ 'friendlyName' ] !==
263- localDevices [ deviceId ] [ 'friendlyName' ]
279+ localDevices [ deviceId ] [ 'friendlyName' ] ||
280+ shadowDevices [ deviceId ] [ 'retrievable' ] !==
281+ localDevices [ deviceId ] [ 'retrievable' ]
264282 ) {
265283 toBeDiscoveredDevices [ deviceId ] = localDevices [ deviceId ]
266284 }
@@ -323,6 +341,17 @@ module.exports = function (RED) {
323341 // },
324342 // }
325343
344+ const isActive = this . execCallbackForOne ( deviceId , 'isActive' )
345+
346+ if ( ! isActive ) {
347+ this . logger (
348+ `ignoring handleReportState for non-active device ID ${ deviceId } ` ,
349+ null ,
350+ 'warn'
351+ )
352+ return
353+ }
354+
326355 const currentState = this . execCallbackForOne ( deviceId , 'getLocalState' )
327356
328357 if ( ! currentState ) {
@@ -366,6 +395,16 @@ module.exports = function (RED) {
366395 // payload: {},
367396 // },
368397 // }
398+ const isActive = this . execCallbackForOne ( deviceId , 'isActive' )
399+
400+ if ( ! isActive ) {
401+ // this.logger(
402+ // `ignoring handleDirectiveFromAlexa for non-active device ID ${deviceId}`,
403+ // null,
404+ // 'warn'
405+ // )
406+ return
407+ }
369408
370409 // get current device state
371410 const oldState = this . execCallbackForOne ( deviceId , 'getLocalState' )
@@ -439,6 +478,27 @@ module.exports = function (RED) {
439478 } )
440479 }
441480
481+ this . handleOverrideConfig = function ( message ) {
482+ this . publish ( `$aws/things/${ this . credentials . thingId } /shadow/get` , { } )
483+
484+ if ( message . msgRateLimiter ) {
485+ const config = message . msgRateLimiter
486+ this . rater . overrideConfig ( config )
487+ }
488+ if ( message . userIdToken ) {
489+ this . userIdToken = message . userIdToken
490+ }
491+ if ( message . allowedDeviceCount ) {
492+ this . allowedDeviceCount = message . allowedDeviceCount
493+ this . disableUnallowedDevices ( message . allowedDeviceCount )
494+ }
495+
496+ node . plan = message . plan
497+
498+ this . isRequestConfigCompleted = true
499+ this . refreshChildrenNodeStatus ( )
500+ }
501+
442502 this . handleRestart = function ( { semverExpr } ) {
443503 if ( semverExpr && ! semver . satisfies ( VSH_VERSION , semverExpr ) ) {
444504 return
@@ -448,6 +508,8 @@ module.exports = function (RED) {
448508
449509 this . disconnect ( )
450510
511+ this . execCallbackForAll ( 'setActive' , true )
512+
451513 setTimeout ( ( ) => {
452514 this . connectAndSubscribe ( )
453515 const requestConfigJob = ( ) => {
@@ -488,21 +550,7 @@ module.exports = function (RED) {
488550 this . handlePing ( message )
489551 break
490552 case 'overrideConfig' :
491- this . publish ( `$aws/things/${ this . credentials . thingId } /shadow/get` , { } )
492-
493- if ( message . msgRateLimiter ) {
494- const config = message . msgRateLimiter
495- this . rater . overrideConfig ( config )
496- }
497- if ( message . userIdToken ) {
498- this . userIdToken = message . userIdToken
499- }
500- if ( message . allowedDeviceCount ) {
501- this . allowedDeviceCount = message . allowedDeviceCount
502- this . unrigisterUnallowedDevices ( message . allowedDeviceCount )
503- }
504- this . isRequestConfigCompleted = true
505- this . refreshChildrenNodeStatus ( )
553+ this . handleOverrideConfig ( message )
506554 break
507555 case 'restart' :
508556 this . handleRestart ( message )
@@ -545,18 +593,23 @@ module.exports = function (RED) {
545593 return await response . json ( )
546594 }
547595
548- this . unrigisterUnallowedDevices = function ( allowedDeviceCount ) {
596+ this . disableUnallowedDevices = function ( allowedDeviceCount ) {
549597 let i = 0
550598
551599 for ( const nodeId in this . childNodes ) {
552600 i ++
553601 if ( i > allowedDeviceCount ) {
554- this . execCallbackForOne ( nodeId , 'setStatus' , {
555- shape : 'dot' ,
556- fill : 'gray' ,
557- text : 'device limit reached! Upgrade your VSH subscription to get more devices!' ,
558- } )
559- this . unregisterChildNode ( nodeId )
602+ this . execCallbackForOne ( nodeId , 'setActive' , false )
603+ this . execCallbackForOne (
604+ nodeId ,
605+ 'setStatus' ,
606+ {
607+ shape : 'dot' ,
608+ fill : 'gray' ,
609+ text : 'device limit reached! Upgrade your VSH subscription to get more devices!' ,
610+ } ,
611+ true //force
612+ )
560613 }
561614 }
562615 }
@@ -723,6 +776,8 @@ module.exports = function (RED) {
723776 }
724777
725778 this . isSubscribed = false
779+ this . isRequestConfigCompleted = false
780+ this . isError = false
726781 }
727782
728783 this . on ( 'close' , async function ( removed , done ) {
0 commit comments