Skip to content

Commit be22251

Browse files
committed
feat(connection): Show initializing status while starting up
1 parent 4940007 commit be22251

File tree

1 file changed

+46
-30
lines changed

1 file changed

+46
-30
lines changed

connection.js

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ module.exports = function (RED) {
4141
this.isConnected = false
4242
this.isDisconnecting = false
4343
this.isSubscribed = false
44+
this.isRequestConfigCompleted = false
4445
this.isError = false
46+
this.errorCode = ''
4547
this.isKilled = false
4648
this.killedStatusText = 'KILLED'
4749
this.allowedDeviceCount = 200
@@ -66,6 +68,34 @@ module.exports = function (RED) {
6668
}
6769
}
6870

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+
6999
this.registerChildNode = function (nodeId, callbacks) {
70100
if (Object.keys(this.childNodes).length >= this.allowedDeviceCount) {
71101
callbacks.setStatus({
@@ -83,13 +113,6 @@ module.exports = function (RED) {
83113
this.connectAndSubscribe()
84114
}
85115

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-
93116
const requestConfigJob = () => {
94117
if (!this.isSubscribed) {
95118
return false
@@ -137,6 +160,9 @@ module.exports = function (RED) {
137160
}
138161

139162
this.requestConfig = function () {
163+
this.isRequestConfigCompleted = false
164+
this.refreshChildrenNodeStatus()
165+
140166
this.publish(`vsh/${this.credentials.thingId}/requestConfig`, {
141167
vshVersion: VSH_VERSION,
142168
})
@@ -145,6 +171,10 @@ module.exports = function (RED) {
145171
this.requestConfigDebounced = debounce(this.requestConfig, 1000)
146172

147173
this.markAsConnected = function () {
174+
if (!this.isConnected) {
175+
return false
176+
}
177+
148178
this.publish(`$aws/things/${this.credentials.thingId}/shadow/update`, {
149179
state: {
150180
reported: {
@@ -422,6 +452,7 @@ module.exports = function (RED) {
422452
console.warn('CONNECTION KILLED! Reason:', reason || 'undefined')
423453
this.isKilled = true
424454
this.killedStatusText = reason ? reason : 'KILLED'
455+
this.isRequestConfigCompleted = true
425456
this.disconnect()
426457
}
427458

@@ -454,6 +485,8 @@ module.exports = function (RED) {
454485
this.allowedDeviceCount = message.allowedDeviceCount
455486
this.unrigisterUnallowedDevices(message.allowedDeviceCount)
456487
}
488+
this.isRequestConfigCompleted = true
489+
this.refreshChildrenNodeStatus()
457490
break
458491
case 'kill':
459492
this.handleKill(message)
@@ -532,11 +565,8 @@ module.exports = function (RED) {
532565
null,
533566
'error'
534567
)
535-
this.execCallbackForAll('setStatus', {
536-
shape: 'dot',
537-
fill: 'gray',
538-
text: updateHint,
539-
})
568+
this.errorCode = updateHint
569+
this.refreshChildrenNodeStatus()
540570
return
541571
}
542572
} catch (e) {
@@ -570,35 +600,21 @@ module.exports = function (RED) {
570600
this.stats.connectionCount++
571601
this.isConnected = true
572602
this.isError = false
573-
this.execCallbackForAll('setStatus', {
574-
shape: 'dot',
575-
fill: 'green',
576-
text: 'online',
577-
})
578-
603+
this.refreshChildrenNodeStatus()
579604
this.markAsConnectedDebounced()
580605
},
581606

582607
onDisconnect: () => {
583608
this.logger('MQTT disconnected')
584609
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()
592611
},
593612

594613
onError: (error) => {
595614
this.isConnected = false
596615
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()
602618
},
603619

604620
onSubscribeSuccess: (subscribeResult) => {

0 commit comments

Comments
 (0)