Skip to content

Commit

Permalink
fix: set retain to false for stateless valueIds (#215)
Browse files Browse the repository at this point in the history
* fix: set retain to false for stateless valueIds

* fix: reset stateless values

* fix: set lower timeout
  • Loading branch information
robertsLando authored Jan 12, 2021
1 parent 07e4232 commit cf71d1f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/Gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ function onValueChanged (valueId, node, changed) {
this.topicValues[topic] = valueId
}

this.mqtt.publish(topic, data)
this.mqtt.publish(topic, data, valueId.stateless ? { retain: false } : null)
}

function onNotification (node, notificationLabel, parameters) {
Expand Down
5 changes: 4 additions & 1 deletion lib/MqttClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,14 @@ MqttClient.prototype.subscribe = function (topic) {
*/
MqttClient.prototype.publish = function (topic, data, options, prefix) {
if (this.client) {
options = options || {
const settingOptions = {
qos: this.config.qos,
retain: this.config.retain
}

// by default use settingsOptions
options = Object.assign(settingOptions, options)

topic = (prefix || this.config.prefix) + '/' + topic

logger.log(
Expand Down
38 changes: 31 additions & 7 deletions lib/ZwaveClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ function ZwaveClient (config, socket) {
this.cfg = config
this.socket = socket

this.statelessTimeouts = {}

this.closed = false
this.driverReady = false
this.scenes = jsonStore.get(store.scenes)
Expand Down Expand Up @@ -469,11 +471,11 @@ function onNodeValueAdded (zwaveNode, args) {
* @param {import('zwave-js/build/lib/node/Types').ZWaveNodeValueNotificationArgs} args
*/
function onNodeValueNotification (zwaveNode, args) {
logger.debug(
'In onNodeValueNotification: Overwriting args.newValue with args.value'
)
// notification hasn't `newValue`
args.newValue = args.value
onNodeValueUpdated.call(this, zwaveNode, args, true)
// specify that this is stateless
args.stateless = true
onNodeValueUpdated.call(this, zwaveNode, args)
}

/**
Expand All @@ -483,11 +485,11 @@ function onNodeValueNotification (zwaveNode, args) {
* @param {import('zwave-js').ZWaveNodeValueUpdatedArgs} args
* @param {boolean} isNotification
*/
function onNodeValueUpdated (zwaveNode, args, isNotification) {
function onNodeValueUpdated (zwaveNode, args) {
updateValue.call(this, zwaveNode, args)
logger.info(
`Node ${zwaveNode.id}: value ${
isNotification ? 'notification' : 'updated'
args.stateless ? 'notification' : 'updated'
}: ${getValueID(args)} ${args.prevValue} => ${args.newValue}`
)

Expand Down Expand Up @@ -794,7 +796,8 @@ function updateValueMetadata (zwaveNode, zwaveValue, zwaveValueMeta) {
writeable: zwaveValueMeta.writeable,
description: zwaveValueMeta.description,
label: zwaveValueMeta.label || zwaveValue.propertyName + ' (property)', // when label is missing, re use propertyName. Usefull for webinterface
default: zwaveValueMeta.default
default: zwaveValueMeta.default,
stateless: false // used for notifications to specify that this should not be persisted (retained)
}

if (zwaveValueMeta.ccSpecific) {
Expand Down Expand Up @@ -906,8 +909,22 @@ function updateValue (zwaveNode, args) {

if (valueId) {
valueId.value = args.newValue
valueId.stateless = !!args.stateless

this.emit('valueChanged', valueId, node, args.prevValue !== args.newValue)

const self = this

if (valueId.stateless) {
if (this.statelessTimeouts[valueId.id]) {
clearTimeout(this.statelessTimeouts[valueId.id])
}

this.statelessTimeouts[valueId.id] = setTimeout(() => {
valueId.value = undefined
self.emit('valueChanged', valueId, node, false)
}, 1000)
}
}
node.lastActive = Date.now()
}
Expand Down Expand Up @@ -1220,6 +1237,13 @@ ZwaveClient.prototype.close = async function () {
this.healTimeout = null
}

if (this.statelessTimeouts) {
for (const k in this.statelessTimeouts) {
clearTimeout(this.statelessTimeouts[k])
delete this.statelessTimeouts[k]
}
}

if (this.driver) {
this.driverReady = false
this.removeAllListeners()
Expand Down

0 comments on commit cf71d1f

Please sign in to comment.