From cca43644036a4e7c08f85c9ad3faab62834c2d4e Mon Sep 17 00:00:00 2001 From: Cornelius Suermann Date: Tue, 3 Oct 2023 15:05:45 +0200 Subject: [PATCH] fix(connection): Ensure current plan gets displayed --- connection.js | 7 ++++--- virtual-device.html | 29 ++++++++++++++++------------- virtual-device.js | 16 +++++++++------- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/connection.js b/connection.js index 8a20923..8725bbb 100644 --- a/connection.js +++ b/connection.js @@ -31,7 +31,8 @@ module.exports = function (RED) { `/vsh-connection/${node.id}`, RED.auth.needsPermission('vsh-virtual-device.read'), function (req, res) { - res.json({ plan: node.getPlan() }) + const connectionNode = RED.nodes.getNode(node.id) + res.json({ plan: connectionNode.getPlan() }) } ) @@ -512,7 +513,7 @@ module.exports = function (RED) { return } - console.warn('RECEIVED REQUEST TO RESTART VSH...') + this.logger('RECEIVED REQUEST TO RESTART VSH...') this.disconnect() @@ -656,7 +657,7 @@ module.exports = function (RED) { return } } catch (e) { - this.logger('connection failed. Retrying in 5 seconds...') + this.logger('connection failed. Retrying in 30 seconds...') this.errorCode = 'connection failed. Retrying every 30 sec...' this.isError = true this.refreshChildrenNodeStatus() diff --git a/virtual-device.html b/virtual-device.html index dcfbf7b..cb34e88 100644 --- a/virtual-device.html +++ b/virtual-device.html @@ -6,20 +6,23 @@ return } - $.getJSON('vsh-connection/' + vshConnectionNodeId, function ({ plan }) { - $('#vsh-current-plan') - .text(plan.toUpperCase()) - .addClass('vsh-plan-' + plan) - - if (plan === 'free') { - setTimeout(() => $('#subscription-hint').fadeIn('slow'), 1500) - } - - if (plan === 'pro') { - $('#node-input-retrievable').prop('disabled', false) - $('#retrievable-label').removeClass('retrievable-option-disabled') + $.getJSON( + 'vsh-connection/' + vshConnectionNodeId + '?_=' + new Date().getTime(), + function ({ plan }) { + $('#vsh-current-plan') + .text(plan.toUpperCase()) + .addClass('vsh-plan-' + plan) + + if (plan === 'free') { + setTimeout(() => $('#subscription-hint').fadeIn('slow'), 1500) + } + + if (plan === 'pro') { + $('#node-input-retrievable').prop('disabled', false) + $('#retrievable-label').removeClass('retrievable-option-disabled') + } } - }) + ) } RED.nodes.registerType('vsh-virtual-device', { diff --git a/virtual-device.js b/virtual-device.js index 1ca1c66..246c619 100644 --- a/virtual-device.js +++ b/virtual-device.js @@ -14,7 +14,9 @@ module.exports = function (RED) { const deviceId = 'vshd-' + node.id.replace('.', '') - const connectionNode = RED.nodes.getNode(config.connection) + const getConnectionNode = () => { + return RED.nodes.getNode(config.connection) + } const validators = getValidators(config.template) const decorator = getDecorator(config.template, config.diff) @@ -79,10 +81,10 @@ module.exports = function (RED) { return approvedState } - if (connectionNode) { + if (getConnectionNode()) { //connection is configured //register callbacks. This way connectionNode can communicate with us: - connectionNode.registerChildNode(deviceId, { + getConnectionNode().registerChildNode(deviceId, { setStatus: (status, force = false) => { if (isActive || force) { node.status(status) @@ -106,7 +108,7 @@ module.exports = function (RED) { } node.on('input', function (msg, send, done) { - if (!connectionNode || !isActive) { + if (!getConnectionNode() || !isActive) { console.log( `ignoring inbound msg for non-active device ID ${deviceId}'` ) @@ -152,7 +154,7 @@ module.exports = function (RED) { const confirmedNewLocalState = setLocalState(newLocalState) if (!deepEql(oldLocalState, newLocalState)) { - connectionNode.handleLocalDeviceStateChange({ + getConnectionNode().handleLocalDeviceStateChange({ deviceId, oldState: oldLocalState, newState: confirmedNewLocalState, @@ -169,8 +171,8 @@ module.exports = function (RED) { }) node.on('close', async function (removed, done) { - if (connectionNode) { - await connectionNode.unregisterChildNode(deviceId) + if (getConnectionNode()) { + await getConnectionNode().unregisterChildNode(deviceId) } node.status({}) return done()