@@ -41,7 +41,9 @@ module.exports = function (RED) {
41
41
this . isConnected = false
42
42
this . isDisconnecting = false
43
43
this . isSubscribed = false
44
+ this . isRequestConfigCompleted = false
44
45
this . isError = false
46
+ this . errorCode = ''
45
47
this . isKilled = false
46
48
this . killedStatusText = 'KILLED'
47
49
this . allowedDeviceCount = 200
@@ -66,6 +68,34 @@ module.exports = function (RED) {
66
68
}
67
69
}
68
70
71
+ this . refreshChildrenNodeStatus = ( ) => {
72
+ let fill , text
73
+
74
+ if ( ! this . isRequestConfigCompleted ) {
75
+ fill = 'yellow'
76
+ text = 'initializing...'
77
+ } else if ( this . isConnected ) {
78
+ fill = 'green'
79
+ text = 'online'
80
+ } else {
81
+ fill = 'red'
82
+
83
+ if ( this . isError ) {
84
+ text = this . errorCode
85
+ } else if ( this . isKilled ) {
86
+ text = this . killedStatusText
87
+ } else {
88
+ text = 'offline'
89
+ }
90
+ }
91
+
92
+ this . execCallbackForAll ( 'setStatus' , {
93
+ shape : 'dot' ,
94
+ fill,
95
+ text,
96
+ } )
97
+ }
98
+
69
99
this . registerChildNode = function ( nodeId , callbacks ) {
70
100
if ( Object . keys ( this . childNodes ) . length >= this . allowedDeviceCount ) {
71
101
callbacks . setStatus ( {
@@ -83,13 +113,6 @@ module.exports = function (RED) {
83
113
this . connectAndSubscribe ( )
84
114
}
85
115
86
- //immediately push most relevant state to new subscriber
87
- this . execCallbackForOne ( nodeId , 'setStatus' , {
88
- shape : 'dot' ,
89
- fill : this . isConnected ? 'green' : 'red' ,
90
- text : this . isConnected ? 'online' : 'offline' ,
91
- } )
92
-
93
116
const requestConfigJob = ( ) => {
94
117
if ( ! this . isSubscribed ) {
95
118
return false
@@ -137,6 +160,9 @@ module.exports = function (RED) {
137
160
}
138
161
139
162
this . requestConfig = function ( ) {
163
+ this . isRequestConfigCompleted = false
164
+ this . refreshChildrenNodeStatus ( )
165
+
140
166
this . publish ( `vsh/${ this . credentials . thingId } /requestConfig` , {
141
167
vshVersion : VSH_VERSION ,
142
168
} )
@@ -145,6 +171,10 @@ module.exports = function (RED) {
145
171
this . requestConfigDebounced = debounce ( this . requestConfig , 1000 )
146
172
147
173
this . markAsConnected = function ( ) {
174
+ if ( ! this . isConnected ) {
175
+ return false
176
+ }
177
+
148
178
this . publish ( `$aws/things/${ this . credentials . thingId } /shadow/update` , {
149
179
state : {
150
180
reported : {
@@ -422,6 +452,7 @@ module.exports = function (RED) {
422
452
console . warn ( 'CONNECTION KILLED! Reason:' , reason || 'undefined' )
423
453
this . isKilled = true
424
454
this . killedStatusText = reason ? reason : 'KILLED'
455
+ this . isRequestConfigCompleted = true
425
456
this . disconnect ( )
426
457
}
427
458
@@ -454,6 +485,8 @@ module.exports = function (RED) {
454
485
this . allowedDeviceCount = message . allowedDeviceCount
455
486
this . unrigisterUnallowedDevices ( message . allowedDeviceCount )
456
487
}
488
+ this . isRequestConfigCompleted = true
489
+ this . refreshChildrenNodeStatus ( )
457
490
break
458
491
case 'kill' :
459
492
this . handleKill ( message )
@@ -532,11 +565,8 @@ module.exports = function (RED) {
532
565
null ,
533
566
'error'
534
567
)
535
- this . execCallbackForAll ( 'setStatus' , {
536
- shape : 'dot' ,
537
- fill : 'gray' ,
538
- text : updateHint ,
539
- } )
568
+ this . errorCode = updateHint
569
+ this . refreshChildrenNodeStatus ( )
540
570
return
541
571
}
542
572
} catch ( e ) {
@@ -570,35 +600,21 @@ module.exports = function (RED) {
570
600
this . stats . connectionCount ++
571
601
this . isConnected = true
572
602
this . isError = false
573
- this . execCallbackForAll ( 'setStatus' , {
574
- shape : 'dot' ,
575
- fill : 'green' ,
576
- text : 'online' ,
577
- } )
578
-
603
+ this . refreshChildrenNodeStatus ( )
579
604
this . markAsConnectedDebounced ( )
580
605
} ,
581
606
582
607
onDisconnect : ( ) => {
583
608
this . logger ( 'MQTT disconnected' )
584
609
this . isConnected = false
585
- if ( ! this . isError ) {
586
- this . execCallbackForAll ( 'setStatus' , {
587
- shape : 'dot' ,
588
- fill : 'red' ,
589
- text : this . isKilled ? this . killedStatusText : 'offline' ,
590
- } )
591
- }
610
+ this . refreshChildrenNodeStatus ( )
592
611
} ,
593
612
594
613
onError : ( error ) => {
595
614
this . isConnected = false
596
615
this . isError = true
597
- this . execCallbackForAll ( 'setStatus' , {
598
- shape : 'dot' ,
599
- fill : 'red' ,
600
- text : error . code ,
601
- } )
616
+ this . errorCode = error . code
617
+ this . refreshChildrenNodeStatus ( )
602
618
} ,
603
619
604
620
onSubscribeSuccess : ( subscribeResult ) => {
0 commit comments