Skip to content

Commit

Permalink
fix: extract firmware update
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando committed Nov 24, 2020
1 parent 17280f5 commit af55b9b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
32 changes: 30 additions & 2 deletions lib/ZwaveClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@

// eslint-disable-next-line one-var
const reqlib = require('app-root-path').require
const { Driver, NodeStatus, InterviewStage } = require('zwave-js')
const {
Driver,
NodeStatus,
InterviewStage,
FirmwareFileFormat,
extractFirmware
} = require('zwave-js')
const utils = reqlib('/lib/utils.js')
const EventEmitter = require('events')
const jsonStore = reqlib('/lib/jsonStore.js')
Expand Down Expand Up @@ -1651,8 +1657,22 @@ ZwaveClient.prototype.refreshInfo = async function (nodeId) {
throw Error('Driver is closed')
}

function guessFirmwareFormat (filename, firmware) {
if (
(filename.endsWith('.exe') || filename.endsWith('.ex_')) &&
firmware.includes(Buffer.from('Aeon Labs', 'utf8'))
) {
return 'aeotec'
} else if (/\.(hex|ota|otz)$/.test(filename)) {
return FirmwareFileFormat[filename.slice(-3)]
} else {
throw new Error('could not detect firmware format')
}
}

ZwaveClient.prototype.beginFirmwareUpdate = async function (
nodeId,
fileName,
data,
target = 0
) {
Expand All @@ -1667,7 +1687,15 @@ ZwaveClient.prototype.beginFirmwareUpdate = async function (
throw Error('Data must be a buffer')
}

return zwaveNode.beginFirmwareUpdate(data, target)
let actualFirmware
try {
const format = guessFirmwareFormat(fileName, data)
actualFirmware = extractFirmware(data, format)
} catch (e) {
throw Error('Unable to extract firmware from file: ' + e.message)
}

return zwaveNode.beginFirmwareUpdate(actualFirmware, target)
}

throw Error('Driver is closed')
Expand Down
2 changes: 1 addition & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export default {
if (err) {
reject(err)
} else {
resolve(data)
resolve({ data, file })
}
})
Expand Down
18 changes: 10 additions & 8 deletions src/components/ControlPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ export default {
)
) {
try {
var data = await this.$listeners.import('json')
var { data } = await this.$listeners.import('json')
var response = await ConfigApis.importConfig({ data: data })
this.showSnackbar(response.message)
} catch (error) {
Expand Down Expand Up @@ -979,9 +979,9 @@ export default {
)
) {
try {
var scenes = await this.$listeners.import('json')
if (scenes instanceof Array) {
this.apiRequest('_setScenes', [scenes])
var { data } = await this.$listeners.import('json')
if (data instanceof Array) {
this.apiRequest('_setScenes', [data])
} else {
this.showSnackbar('Imported file not valid')
}
Expand Down Expand Up @@ -1226,8 +1226,9 @@ export default {
}
} else if (this.cnt_action === 'beginFirmwareUpdate') {
try {
var dataBuffer = await this.$listeners.import('buffer')
args.push(dataBuffer)
var { data, file } = await this.$listeners.import('buffer')
args.push(file.name)
args.push(data)
} catch (error) {
return
}
Expand All @@ -1250,8 +1251,9 @@ export default {
if (this.node_action === 'beginFirmwareUpdate') {
try {
var dataBuffer = await this.$listeners.import('buffer')
args.push(dataBuffer)
var { data, file } = await this.$listeners.import('buffer')
args.push(file.name)
args.push(data)
} catch (error) {
return
}
Expand Down
6 changes: 3 additions & 3 deletions src/components/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,9 @@ export default {
},
async importSettings () {
try {
var settings = await this.$listeners.import('json')
if (settings.zwave && settings.mqtt && settings.gateway) {
this.$store.dispatch('import', settings)
var { data } = await this.$listeners.import('json')
if (data.zwave && data.mqtt && data.gateway) {
this.$store.dispatch('import', data)
this.showSnackbar('Configuration imported successfully')
} else {
this.showSnackbar('Imported settings not valid')
Expand Down

0 comments on commit af55b9b

Please sign in to comment.