Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: env var STORE_DIR and renamed OZW_NETWORK_KEY to NETWORK_KEY #308

Merged
merged 7 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
8 changes: 5 additions & 3 deletions docs/guide/env-vars.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Environment variables

> [!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.
This is the list of the actually supported env vars:

- `OZW_NETWORK_KEY`
- `NETWORK_KEY`: Zwave Network key
- `PORT`: The port to listent to for incoming requests. Default is `8091`
- `HOST`: The host address to bind to. Default is `0.0.0.0`
- `STORE_DIR`: The absolute path to the directory where all files will be stored. Default is `<path to your z2m dir>/store`
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`: The controller status
- `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
34 changes: 16 additions & 18 deletions lib/jsonStore.js
Original file line number Diff line number Diff line change
@@ -1,16 +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 utils = reqlib('lib/utils.js')

function getFile (config) {
return new Promise((resolve, reject) => {
jsonfile.readFile(utils.joinPath(true, storeDir, config.file), function (
jsonfile.readFile(utils.joinPath(storeDir, config.file), function (
err,
data
) {
Expand Down Expand Up @@ -62,18 +62,16 @@ 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(utils.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
Loading