Skip to content

Commit

Permalink
feat: STORE_DIR env var and renamed OZW_NETWORK_KEY to NETWORK_KEY
Browse files Browse the repository at this point in the history
Fixes #85
  • Loading branch information
robertsLando committed Jan 21, 2021
1 parent 5846763 commit b81219a
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 51 deletions.
3 changes: 1 addition & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ const { inboundEvents, socketEvents } = reqlib('/lib/SocketManager.js')
const utils = reqlib('/lib/utils.js')
const fs = require('fs-extra')
const path = require('path')
const appConfig = reqlib('config/app.js')
const { storeDir } = reqlib('config/app.js')
const renderIndex = reqlib('/lib/renderIndex')
const archiver = require('archiver')
const storeDir = utils.joinPath(true, appConfig.storeDir)

const socketManager = new SocketManager()

Expand Down
3 changes: 2 additions & 1 deletion config/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const { joinPath } = require('../lib/utils')
// config/app.js
module.exports = {
title: 'ZWave To MQTT',
storeDir: 'store',
storeDir: process.env.STORE_DIR || joinPath(true, 'store'),
base: '/',
port: 8091
}
3 changes: 2 additions & 1 deletion docs/guide/env-vars.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
> [!NOTE]
> Each one of the following environment variables corresponds to their respective options in the UI settings and options saved in the UI take presence over these environment variables.
- `OZW_NETWORK_KEY`
- `NETWORK_KEY`: Zwave Network key
- `STORE_DIR`: The absolute path to the directory where all files will be stored
4 changes: 2 additions & 2 deletions docs/guide/mqtt.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ This are the available apis:
- `name`: homeId Hex
- `version`: zwave-js version
- `uptime`: Seconds from when the app process is started. It's the result of `process.uptime()`
- `lastUpdate`: Timestamp of latest event received from OZW
- `lastUpdate`: Timestamp of latest event received
- `status`: Client status. Could be: 'driverReady', 'connected', 'scanDone', 'driverFailed', 'closed'
- `cntStatus`: Controller status received from ozw notifications controller command. If inclusion/exclusion is running it would be `Waiting`
- `cntStatus`: Controller status received from zwavejs notifications controller command. If inclusion/exclusion is running it would be `Waiting`
- `getAssociations(nodeId, groupId)`: get an array of current [associations](https://zwave-js.github.io/node-zwave-js/#/api/controller?id=association-interface) of a specific group
- `addAssociations(nodeId, groupId, associations)`: add a node to the array of specified [associations](https://zwave-js.github.io/node-zwave-js/#/api/controller?id=association-interface)
- `removeAssociations(nodeId, groupId, associations[])`: the opposite of add associations
Expand Down
2 changes: 1 addition & 1 deletion docs/usage/reverse-proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ would look like:
```javascript
module.exports = {
title: 'ZWave to MQTT',
storeDir: 'store',
storeDir: process.env.STORE_DIR || joinPath(true, 'store'),
base: '/zwave/',
port: 8091
}
Expand Down
7 changes: 4 additions & 3 deletions lib/Gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ const inherits = require('util').inherits
const hassCfg = reqlib('/hass/configurations.js')
const hassDevices = reqlib('/hass/devices.js')
const version = reqlib('package.json').version
const { storeDir } = reqlib('config/app.js')

const NODE_PREFIX = 'nodeID_'
// const GW_TYPES = ['valueID', 'named', 'manual']
// const PY_TYPES = ['time_value', 'zwave_value', 'just_value']

const CUSTOM_DEVICES = reqlib('config/app.js').storeDir + '/customDevices'
const CUSTOM_DEVICES = storeDir + '/customDevices'
let allDevices = hassDevices // will contain customDevices + hassDevices

// watcher initiates a watch on a file. if this fails (e.g., because the file
Expand Down Expand Up @@ -57,8 +58,8 @@ const watch = (filename, fn) => {
}
}

const customDevicesJsPath = utils.joinPath(true, CUSTOM_DEVICES) + '.js'
const customDevicesJsonPath = utils.joinPath(true, CUSTOM_DEVICES) + '.json'
const customDevicesJsPath = CUSTOM_DEVICES + '.js'
const customDevicesJsonPath = CUSTOM_DEVICES + '.json'

let lastCustomDevicesLoad = null
// loadCustomDevices attempts to load a custom devices file, preferring `.js`
Expand Down
22 changes: 11 additions & 11 deletions lib/MqttClient.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict'

// eslint-disable-next-line one-var
const reqlib = require('app-root-path').require,
mqtt = require('mqtt'),
utils = reqlib('/lib/utils.js'),
NeDBStore = require('mqtt-nedb-store'),
EventEmitter = require('events'),
storeDir = reqlib('config/app.js').storeDir,
logger = reqlib('/lib/logger.js').module('Mqtt'),
url = require('native-url'),
inherits = require('util').inherits
const reqlib = require('app-root-path').require
const mqtt = require('mqtt')
const { joinPath, sanitizeTopic } = reqlib('/lib/utils.js')
const NeDBStore = require('mqtt-nedb-store')
const EventEmitter = require('events')
const { storeDir } = reqlib('config/app.js')
const logger = reqlib('/lib/logger.js').module('Mqtt')
const url = require('native-url')
const { inherits } = require('util')

const appVersion = reqlib('package.json').version

Expand Down Expand Up @@ -49,7 +49,7 @@ function init (config) {
return
}

this.clientID = utils.sanitizeTopic(NAME_PREFIX + config.name)
this.clientID = sanitizeTopic(NAME_PREFIX + config.name)

const parsed = url.parse(config.host || '')
let protocol = 'mqtt'
Expand Down Expand Up @@ -82,7 +82,7 @@ function init (config) {

if (config.store) {
const COMPACT = { autocompactionInterval: 30000 }
const manager = NeDBStore(utils.joinPath(true, storeDir, 'mqtt'), {
const manager = NeDBStore(joinPath(storeDir, 'mqtt'), {
incoming: COMPACT,
outgoing: COMPACT
})
Expand Down
4 changes: 2 additions & 2 deletions lib/ZwaveClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const EventEmitter = require('events')
const jsonStore = reqlib('/lib/jsonStore.js')
const { socketEvents } = reqlib('/lib/SocketManager.js')
const store = reqlib('config/store.js')
const storeDir = utils.joinPath(true, reqlib('config/app.js').storeDir)
const { storeDir } = reqlib('config/app.js')
const LogManager = require('./logger.js')
const logger = LogManager.module('Zwave')
const inherits = require('util').inherits
Expand Down Expand Up @@ -59,7 +59,7 @@ function ZwaveClient (config, socket) {
this.driverReady = false
this.scenes = jsonStore.get(store.scenes)

config.networkKey = config.networkKey || process.env.OZW_NETWORK_KEY
config.networkKey = config.networkKey || process.env.NETWORK_KEY

this.nodes = []
this.storeNodes = jsonStore.get(store.nodes)
Expand Down
35 changes: 14 additions & 21 deletions lib/jsonStore.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
'use strict'

// eslint-disable-next-line one-var
const jsonfile = require('jsonfile'),
reqlib = require('app-root-path').require,
storeDir = reqlib('config/app.js').storeDir,
Promise = require('bluebird'),
logger = reqlib('/lib/logger.js').module('Store'),
utils = reqlib('lib/utils.js')
const jsonfile = require('jsonfile')
const reqlib = require('app-root-path').require
const { storeDir } = reqlib('config/app.js')
const Promise = require('bluebird')
const logger = reqlib('/lib/logger.js').module('Store')
const { joinPath } = reqlib('lib/utils.js')

function getFile (config) {
return new Promise((resolve, reject) => {
jsonfile.readFile(utils.joinPath(true, storeDir, config.file), function (
err,
data
) {
jsonfile.readFile(joinPath(storeDir, config.file), function (err, data) {
if (err && err.code !== 'ENOENT') {
reject(err)
} else {
Expand Down Expand Up @@ -62,18 +59,14 @@ StorageHelper.prototype.get = function (model) {

StorageHelper.prototype.put = function (model, data) {
return new Promise((resolve, reject) => {
jsonfile.writeFile(
utils.joinPath(true, storeDir, model.file),
data,
function (err) {
if (err) {
reject(err)
} else {
storage_helper.store[model.file] = data
resolve(storage_helper.store[model.file])
}
jsonfile.writeFile(joinPath(storeDir, model.file), data, function (err) {
if (err) {
reject(err)
} else {
storage_helper.store[model.file] = data
resolve(storage_helper.store[model.file])
}
)
})
})
}

Expand Down
8 changes: 2 additions & 6 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const winston = require('winston')
const { format, transports, addColors } = winston
const { combine, timestamp, label, printf, colorize, splat } = format
const utils = require('./utils')
const { joinPath } = require('./utils')
const { storeDir } = require('../config/app.js')

const colorizer = colorize()
Expand All @@ -16,11 +16,7 @@ const defaultLogFile = 'zwavejs2mqtt.log'
*/
const sanitizedConfig = (module, config) => {
config = config || {}
const filePath = utils.joinPath(
true,
storeDir,
config.logFileName || defaultLogFile
)
const filePath = joinPath(storeDir, config.logFileName || defaultLogFile)

return {
module: module || '-',
Expand Down
1 change: 0 additions & 1 deletion test/lib/logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const { storeDir } = require('../../config/app.js')

function checkConfigDefaults (mod, cfg) {
const defaultLogFile = utils.joinPath(
true,
storeDir,
logContainer.__get__('defaultLogFile')
)
Expand Down

0 comments on commit b81219a

Please sign in to comment.