Skip to content

Commit

Permalink
fix: Watch for file changes when using customDevices.json
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando committed Jan 30, 2020
1 parent 44b06f3 commit ba5f150
Showing 1 changed file with 52 additions and 48 deletions.
100 changes: 52 additions & 48 deletions lib/Gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

const fs = require('fs')
const reqlib = require('app-root-path').require
const utils = reqlib('/lib/utils.js');
const utils = reqlib('/lib/utils.js')
const EventEmitter = require('events')
const Constants = reqlib('/lib/Constants.js')
const debug = reqlib('/lib/debug')('Gateway')
Expand All @@ -13,22 +13,56 @@ const hassCfg = reqlib('/hass/configurations.js')
const hassDevices = reqlib('/hass/devices.js')
const version = reqlib('package.json').version

const CUSTOM_DEVICES = utils.joinPath(true, reqlib('config/app.js').storeDir, 'customDevices')
const NODE_PREFIX = 'nodeID_'
// const GW_TYPES = ['valueID', 'named', 'manual']
// const PY_TYPES = ['time_value', 'zwave_value', 'just_value']

// Continue supporting customDevices.js for now
if (fs.existsSync(CUSTOM_DEVICES + '.js')) {
const devices = reqlib(CUSTOM_DEVICES + '.js')
for (const key in devices) {
hassDevices[key] = devices[key]
}
debug('Loaded', Object.keys(devices).length, 'custom js hass devices configurations')
}
var CUSTOM_DEVICES = reqlib('config/app.js').storeDir + '/customDevices'
var allDevices = {} // will contain customDevices + hassDevices

debug.color = 2

const NODE_PREFIX = 'nodeID_'
// const GW_TYPES = ['valueID', 'named', 'manual']
// const PY_TYPES = ['time_value', 'zwave_value', 'just_value']
// util function to watch for file changes, prevents the watch to be called twice
const watch = (path, fn) => {
var lock = false
fs.watch(path, function () {
if (lock) return

lock = true
fn()
setTimeout(() => { lock = false }, 1000)
})
}

function loadCustomDevices (devices) {
allDevices = {
...hassDevices,
...devices
}
debug('Loaded', Object.keys(devices).length, 'custom Hass devices configurations')
}

try {
const devices = reqlib(CUSTOM_DEVICES)
loadCustomDevices(devices)
CUSTOM_DEVICES = utils.joinPath(true, CUSTOM_DEVICES)
if (fs.existsSync(CUSTOM_DEVICES + '.json')) {
watch(CUSTOM_DEVICES + '.json', function () {
try {
const fileContent = fs.readFileSync(CUSTOM_DEVICES + '.json')
loadCustomDevices(JSON.parse(fileContent))
} catch (error) {
debug('Error while reading', CUSTOM_DEVICES + '.json', error.message)
}
})
}
} catch (error) {
if (error.message.indexOf('Cannot find module') >= 0) {
debug('No customDevices file found')
} else {
debug('Error while parsing customDevices file:', error.message)
}
}

/**
* The constructor
Expand Down Expand Up @@ -58,8 +92,6 @@ function init (config, zwave, mqtt) {

this.discovered = {}

this.customDevicesLength = Object.keys(hassDevices).length

if (mqtt) {
mqtt.on('writeRequest', onWriteRequest.bind(this))
mqtt.on('broadcastRequest', onBroadRequest.bind(this))
Expand Down Expand Up @@ -202,13 +234,6 @@ function onNodeStatus (node) {
}
}

// discover node devices
const allDevices = this.loadDevices()
const deviceKeys = Object.keys(allDevices)
if (deviceKeys.length !== this.customDevicesLength) {
this.customDevicesLength = deviceKeys.length
debug(`Loaded ${this.customDevicesLength} custom hass device configurations: ${deviceKeys.join(', ')}`)
}
const nodeDevices = allDevices[node.device_id] || []
nodeDevices.forEach(device => this.discoverDevice(node, device))

Expand Down Expand Up @@ -639,11 +664,11 @@ Gateway.prototype.rediscoverAll = function () {

var nodes = this.zwave ? this.zwave.nodes : []
for (let i = 0; i < nodes.length; i++) {
const hassDevices = nodes[i] && nodes[i].hassDevices ? nodes[i].hassDevices : {}
for (const id in hassDevices) {
const hassDevice = hassDevices[id]
if (hassDevice && hassDevice.discoveryTopic && hassDevice.discovery_payload) {
this.publishDiscovery(hassDevice, i)
const devices = nodes[i] && nodes[i].hassDevices ? nodes[i].hassDevices : {}
for (const id in devices) {
const d = devices[id]
if (d && d.discoveryTopic && d.discovery_payload) {
this.publishDiscovery(d, i)
}
} // end foreach hassdevice
} // end foreach node
Expand Down Expand Up @@ -936,25 +961,4 @@ Gateway.prototype.discoverValue = function (node, valueId) {
}
}


Gateway.prototype.loadDevices = function () {
try {
// Should probably use a cache and file stating to get mtime
// before reading the entire file again
const filepath = CUSTOM_DEVICES + '.json'
if (fs.existsSync(filepath)) {
const fileContent = fs.readFileSync(filepath)
const devices = JSON.parse(fileContent)
const keys = Object.keys(devices)
return {
...hassDevices,
...devices
}
}
} catch (error) {
debug('Error while loading store/customDevices', error.message)
}
return hassDevices
}

module.exports = Gateway

0 comments on commit ba5f150

Please sign in to comment.