Skip to content

Commit

Permalink
fix: read/parse buffers as hex strings (#797)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando authored Mar 2, 2021
1 parent d4f7780 commit 521d2d2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
20 changes: 18 additions & 2 deletions lib/ZwaveClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -1092,15 +1092,27 @@ function updateValue (zwaveNode, args) {
const valueId = node.values[getValueID(args)]

if (valueId) {
valueId.value = args.newValue
let newValue = args.newValue
if (Buffer.isBuffer(newValue)) {
// encode Buffers as HEX strings
newValue = utils.buffer2hex(newValue)
}

let prevValue = args.prevValue
if (Buffer.isBuffer(prevValue)) {
// encode Buffers as HEX strings
prevValue = utils.buffer2hex(prevValue)
}

valueId.value = newValue
valueId.stateless = !!args.stateless

// ensure duration is never undefined
if (valueId.type === 'duration' && valueId.value === undefined) {
valueId.value = new Duration(undefined, 'seconds')
}

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

const self = this

Expand Down Expand Up @@ -2508,6 +2520,10 @@ ZwaveClient.prototype.writeValue = async function (valueId, value) {
value = value.substr(1)
}

if (typeof value === 'string' && utils.isBufferAsHex(value)) {
value = utils.bufferFromHex(value)
}

try {
const zwaveNode = await this.getNode(valueId.nodeId)

Expand Down
36 changes: 35 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,37 @@ async function verifyPsw (password, hash) {
})
}

/**
* Checks if a string is a hex buffer
*
* @param {string} str
* @returns {boolean}
*/
function isBufferAsHex (str) {
return /^0x([a-fA-F0-9]{2})+$/.test(str)
}

/**
* Parses a buffer from a string has the form 0x[a-f0-9]+
*
* @param {string} hex
* @returns {Buffer} the parsed Buffer
*/
function bufferFromHex (hex) {
return Buffer.from(hex.substr(2), 'hex')
}

/**
* Converts a buffer to an hex string
*
* @param {Buffer} buffer
* @returns {string}
*/
function buffer2hex (buffer) {
if (buffer.length === 0) return ''
return `0x${buffer.toString('hex')}`
}

module.exports = {
getPath,
joinPath,
Expand All @@ -209,5 +240,8 @@ module.exports = {
hasProperty,
humanSize,
hashPsw,
verifyPsw
verifyPsw,
isBufferAsHex,
bufferFromHex,
buffer2hex
}
6 changes: 1 addition & 5 deletions src/components/ValueId.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
></v-text-field>

<v-text-field
v-if="!value.list && (value.type === 'any' || value.type === 'buffer')"
v-if="!value.list && value.type === 'any'"
:append-outer-icon="!disable_send ? 'send' : null"
:suffix="value.unit"
persistent-hint
Expand Down Expand Up @@ -264,17 +264,13 @@ export default {
get: function () {
if (this.value.type === 'any') {
return JSON.stringify(this.value.newValue)
} else if (this.value.type === 'buffer') {
return this.value.newValue && this.value.newValue.toString('hex')
}
return this.value.newValue
},
set: function (v) {
try {
if (this.value.type === 'any') {
this.value.newValue = JSON.parse(v)
} else if (this.value.type === 'buffer') {
this.value.newValue = Buffer.from(v, 'hex')
} else {
this.value.newValue = v
}
Expand Down

0 comments on commit 521d2d2

Please sign in to comment.