diff --git a/.gitignore b/.gitignore index d5e5c0d..42cbc3a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.config.json dist node_modules -*.log \ No newline at end of file +*.log diff --git a/README.md b/README.md index 5e6af71..48d7940 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ This module contains both HTTP server and MQTT broker, so it can be used as a si You can run this `node.js` module in a variety of ways. First, clone this repo and run ``` -npm i && npm run build +npm i ``` You can now start the process with @@ -55,6 +55,21 @@ To watch logs in real time, start your pm2 process and run tail -f ``` +### Configuration + +The config parmaeters set in `config.js` are referenced when starting the application. These come with defaults and +also look at a local file `.config.json` (if it exists). This local `.config.json` is not tracked in git and it is +where you can define params that are inspected in `config.js`. + +*Sample `.config.json`:* + +``` +{ + "LOG_DEST": "./lattice.log", + "MQTT_PASSWORD": "superdupersecretpassword" +} +``` + ### Running with Docker > **NOTE**: The Docker scripts are written using the default ports (specified in `config.js`: 1883 for MQTT, 3000 for http). If you want to change these ports, please also update the `Dockerfile` and the `docker-run` script in `package.json`. diff --git a/config.js b/config.js index b4c5514..01960ee 100644 --- a/config.js +++ b/config.js @@ -1,14 +1,14 @@ +const local = require('./.config.json'); + module.exports = { - APP_HOST: '0.0.0.0', - APP_PORT: 3000, - LOG_DEST: '/tmp/lattice-connector.log', - LOG_LEVEL: 'error', // trace, debug, info, warn, error - MQTT: { - CLIENT_ID: 'lattice-connector-endpoint', - USERNAME: 'connector', - PASSWORD: 'connectorpasswordpleasechangeme', - BROKER_PORT: 1883, - }, - TIMEOUT_ITER_MS: 500, - TIMEOUT_TOTAL_MS: 60000, -} \ No newline at end of file + APP_HOST: local.APP_HOST || '0.0.0.0', + APP_PORT: local.APP_PORT || 3000, + LOG_DEST: local.LOG_DEST || '/tmp/lattice-connector.log', + LOG_LEVEL: local.LOG_LEVEL || 'error', // trace, debug, info, warn, error + MQTT_CLIENT_ID: local.MQTT_CLIENT_ID || 'lattice-connector-endpoint', + MQTT_USERNAME: local.MQTT_USERNAME || 'connector', + MQTT_PASSWORD: local.MQTT_PASSWORD || 'connectorpasswordpleasechangeme', + MQTT_BROKER_PORT: local.MQTT_BROKER_POR || 1883, + TIMEOUT_ITER_MS: local.TIMEOUT_ITER_MSG || 500, + TIMEOUT_TOTAL_MS: local.TIMEOUT_TOTAL_MSG || 60000, +}; diff --git a/package-lock.json b/package-lock.json index 9b75551..4faf694 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { - "name": "lattice-connector-endpoint", - "version": "0.1.0", + "name": "lattice-connect", + "version": "0.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index e655309..87f0076 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { "name": "lattice-connect", - "version": "0.1.1", + "version": "0.2.0", "description": "A small HTTP server + MQTT broker designed to bridge the web with Lattices in the field", "main": "dist/index.js", "scripts": { "build": "babel src -d dist", "lint": "eslint src", - "start": "npx pm2 start dist/index.js --name lattice-connect --watch", + "start": "npm run build && npx pm2 start dist/index.js --name lattice-connect --watch", "stop": "npx pm2 stop lattice-connect", "rm": "npx pm2 delete lattice-connect && pkill node", "logs": "npx pm2 logs lattice-connect", diff --git a/src/broker.js b/src/broker.js index 5e82778..99b98fa 100644 --- a/src/broker.js +++ b/src/broker.js @@ -2,24 +2,32 @@ // connect to this broker. import logger from './logger'; -const aedes = require('aedes')(); +const aedes = require('aedes'); const net = require('net'); -aedes.on('client', (_client) => { - logger.debug(`BROKER: New client (${_client.id}) attempting connection.`); +const instance = aedes(); +let connCount = 0; + +instance.on('client', (_client) => { + logger.debug(`BROKER (conns=${connCount}): New client (${_client.id}) attempting connection.`); +}); + +instance.on('clientReady', (_client) => { + connCount += 1; + logger.info(`BROKER (conns=${connCount}): Client (${_client.id}) connected.`); }); -aedes.on('clientReady', (_client) => { - logger.debug(`BROKER: Client (${_client.id}) connected.`); +instance.on('clientError', (_client, error) => { + logger.error(`BROKER (conns=${connCount}): Error from client ${_client.id}: ${error.message}`); }); -aedes.on('subscribe', (_subscriptions, _client) => { - logger.debug(`BROKER: Client (${_client.id}) subscribed to topics: ${JSON.stringify(_subscriptions)}`); +instance.on('subscribe', (_subscriptions, _client) => { + logger.debug(`BROKER (conns=${connCount}): Client (${_client.id}) subscribed to topics: ${JSON.stringify(_subscriptions)}`); }); -aedes.on('publish', (_packet, _client) => { - logger.trace(`BROKER: Client (${_client}) published message: ${JSON.stringify(_packet)}`) +instance.on('publish', (_packet, _client) => { + logger.trace(`BROKER (conns=${connCount}): Client (${_client}) published message: ${JSON.stringify(_packet)}`); }); -const broker = net.createServer(aedes.handle); +const broker = net.createServer(instance.handle); export default broker; diff --git a/src/client.js b/src/client.js index 75281f3..7fa58b7 100644 --- a/src/client.js +++ b/src/client.js @@ -6,12 +6,12 @@ import logger from './logger'; const config = require('cconfig')(); const connectOptions = { - clientId: config.MQTT.CLIENT_ID, - username: config.MQTT.USERNAME, - password: config.MQTT.PASSWORD, + clientId: config.MQTT_CLIENT_ID, + username: config.MQTT_USERNAME, + password: config.MQTT_PASSWORD, }; -const brokerURI = `mqtt://${config.APP_HOST}:${config.MQTT.BROKER_PORT}`; +const brokerURI = `mqtt://${config.APP_HOST}:${config.MQTT_BROKER_PORT}`; const client = mqtt.connect(brokerURI, connectOptions); client.on('connect', () => { diff --git a/src/index.js b/src/index.js index f918790..5be8712 100644 --- a/src/index.js +++ b/src/index.js @@ -39,11 +39,11 @@ process.on('unhandledRejection', (reason, promise) => { //---------------------------------- function startBroker() { logger.info('Starting broker', broker) - broker.listen(config.MQTT.BROKER_PORT, () => { - logger.info('MQTT broker server started on port ', config.MQTT.BROKER_PORT); + broker.listen(config.MQTT_BROKER_PORT, () => { + logger.info('MQTT broker server started on port ', config.MQTT_BROKER_PORT); }); } -logger.info('broker?', broker); + if (broker.closed === false) { broker.close(() => { logger.info('closed?'); @@ -54,10 +54,9 @@ if (broker.closed === false) { startBroker(); } - // 2. Create the REST server //---------------------------------- -logger.info('app', app) +logger.info('app', app); app.listen(config.APP_PORT, config.APP_HOST, () => { logger.info(`signing-api-proxy started listening on ${config.APP_PORT}`); });