Skip to content

Commit cca4364

Browse files
committed
fix(connection): Ensure current plan gets displayed
1 parent c8f7d7b commit cca4364

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

connection.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ module.exports = function (RED) {
3131
`/vsh-connection/${node.id}`,
3232
RED.auth.needsPermission('vsh-virtual-device.read'),
3333
function (req, res) {
34-
res.json({ plan: node.getPlan() })
34+
const connectionNode = RED.nodes.getNode(node.id)
35+
res.json({ plan: connectionNode.getPlan() })
3536
}
3637
)
3738

@@ -512,7 +513,7 @@ module.exports = function (RED) {
512513
return
513514
}
514515

515-
console.warn('RECEIVED REQUEST TO RESTART VSH...')
516+
this.logger('RECEIVED REQUEST TO RESTART VSH...')
516517

517518
this.disconnect()
518519

@@ -656,7 +657,7 @@ module.exports = function (RED) {
656657
return
657658
}
658659
} catch (e) {
659-
this.logger('connection failed. Retrying in 5 seconds...')
660+
this.logger('connection failed. Retrying in 30 seconds...')
660661
this.errorCode = 'connection failed. Retrying every 30 sec...'
661662
this.isError = true
662663
this.refreshChildrenNodeStatus()

virtual-device.html

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,23 @@
66
return
77
}
88

9-
$.getJSON('vsh-connection/' + vshConnectionNodeId, function ({ plan }) {
10-
$('#vsh-current-plan')
11-
.text(plan.toUpperCase())
12-
.addClass('vsh-plan-' + plan)
13-
14-
if (plan === 'free') {
15-
setTimeout(() => $('#subscription-hint').fadeIn('slow'), 1500)
16-
}
17-
18-
if (plan === 'pro') {
19-
$('#node-input-retrievable').prop('disabled', false)
20-
$('#retrievable-label').removeClass('retrievable-option-disabled')
9+
$.getJSON(
10+
'vsh-connection/' + vshConnectionNodeId + '?_=' + new Date().getTime(),
11+
function ({ plan }) {
12+
$('#vsh-current-plan')
13+
.text(plan.toUpperCase())
14+
.addClass('vsh-plan-' + plan)
15+
16+
if (plan === 'free') {
17+
setTimeout(() => $('#subscription-hint').fadeIn('slow'), 1500)
18+
}
19+
20+
if (plan === 'pro') {
21+
$('#node-input-retrievable').prop('disabled', false)
22+
$('#retrievable-label').removeClass('retrievable-option-disabled')
23+
}
2124
}
22-
})
25+
)
2326
}
2427

2528
RED.nodes.registerType('vsh-virtual-device', {

virtual-device.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ module.exports = function (RED) {
1414

1515
const deviceId = 'vshd-' + node.id.replace('.', '')
1616

17-
const connectionNode = RED.nodes.getNode(config.connection)
17+
const getConnectionNode = () => {
18+
return RED.nodes.getNode(config.connection)
19+
}
1820

1921
const validators = getValidators(config.template)
2022
const decorator = getDecorator(config.template, config.diff)
@@ -79,10 +81,10 @@ module.exports = function (RED) {
7981
return approvedState
8082
}
8183

82-
if (connectionNode) {
84+
if (getConnectionNode()) {
8385
//connection is configured
8486
//register callbacks. This way connectionNode can communicate with us:
85-
connectionNode.registerChildNode(deviceId, {
87+
getConnectionNode().registerChildNode(deviceId, {
8688
setStatus: (status, force = false) => {
8789
if (isActive || force) {
8890
node.status(status)
@@ -106,7 +108,7 @@ module.exports = function (RED) {
106108
}
107109

108110
node.on('input', function (msg, send, done) {
109-
if (!connectionNode || !isActive) {
111+
if (!getConnectionNode() || !isActive) {
110112
console.log(
111113
`ignoring inbound msg for non-active device ID ${deviceId}'`
112114
)
@@ -152,7 +154,7 @@ module.exports = function (RED) {
152154
const confirmedNewLocalState = setLocalState(newLocalState)
153155

154156
if (!deepEql(oldLocalState, newLocalState)) {
155-
connectionNode.handleLocalDeviceStateChange({
157+
getConnectionNode().handleLocalDeviceStateChange({
156158
deviceId,
157159
oldState: oldLocalState,
158160
newState: confirmedNewLocalState,
@@ -169,8 +171,8 @@ module.exports = function (RED) {
169171
})
170172

171173
node.on('close', async function (removed, done) {
172-
if (connectionNode) {
173-
await connectionNode.unregisterChildNode(deviceId)
174+
if (getConnectionNode()) {
175+
await getConnectionNode().unregisterChildNode(deviceId)
174176
}
175177
node.status({})
176178
return done()

0 commit comments

Comments
 (0)