Skip to content

Commit

Permalink
fix(connection): Ensure current plan gets displayed
Browse files Browse the repository at this point in the history
  • Loading branch information
csuermann committed Oct 3, 2023
1 parent c8f7d7b commit cca4364
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
7 changes: 4 additions & 3 deletions connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() })
}
)

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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()
Expand Down
29 changes: 16 additions & 13 deletions virtual-device.html
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down
16 changes: 9 additions & 7 deletions virtual-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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}'`
)
Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand Down

0 comments on commit cca4364

Please sign in to comment.