From 185c3ebb5e718ab3c5070aa6eaf7e47f7af96499 Mon Sep 17 00:00:00 2001 From: Ty Eggen Date: Thu, 21 Apr 2022 11:53:14 -0700 Subject: [PATCH] Various updates - refactored code order - dotenv - convict - debug - cleaned up code --- index.js | 131 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 93 insertions(+), 38 deletions(-) diff --git a/index.js b/index.js index b2dd0b9..e8d508c 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,10 @@ +const convict = require('convict') const MQTT = require('async-mqtt') +const debug = require('debug')('sergeant') +debug('Starting...') + +// Configurables const quietTimes = { '0': { 10: 'green', 18: 'orange', 20: 'red' }, '1': { 8: 'green', 18: 'orange', 20: 'red' }, @@ -10,26 +15,81 @@ const quietTimes = { '6': { 10: 'green', 18: 'orange', 20: 'red' } } -var curDate = new Date() -var dayOfWeek = 0 -var hourOfDay = 0 +// Config +const config = convict({ + env: { + doc: 'The application environment.', + format: ['production', 'development', 'test'], + default: 'development', + env: 'NODE_ENV' + }, + mqtt_uri: { + doc: 'The MQTT uri to connect to.', + format: (s) => { + try { + // eslint-disable-next-line no-new + new URL(s) + return true + } catch (err) { + return false + } + }, + default: 'tcp://test.mosquitto.org:1883', + env: 'MQTT_URI' + }, + mqtt_sub: { + doc: 'The MQTT topics to subscribe and proxy to: /topic:status[;/topic:status]', + format: String, + default: '/test/vanhack/sergeant/events/phone/ring:phone', + env: 'MQTT_SUB' + }, + mqtt_topic: { + doc: 'The MQTT topic to write to: /topic', + format: String, + default: '/test/vanhack/sergeant/status/space/sergeant', + env: 'MQTT_TOPIC' + }, + mqtt_interval: { + doc: 'The MQTT update interval.', + format: 'int', + default: 60000, + env: 'MQTT_INTERVAL' + } +}) + +// Variables +let curDate = new Date() +let dayOfWeek = 0 +let hourOfDay = 0 + +let superStatus = 0 -var superStatus = 0 +let newStatus = 'red' +let lastStatus = 'red' +let lastUpdateSent = 0 + +let mqttInterval = 0 + +const topics = {} // Functions +const customLog = (name, ...log) => { + debug.extend(name)(...log) +} + const handleError = (msg) => { - console.log(msg) + customLog('handleError', msg) process.exit() } const updateStatus = async (overrideStatus) => { - console.log('updateStatus', 'Running ...') + customLog('updateStatus', 'Running ...') try { - let forceUpdate = ((Date.now() - lastUpdateSent) > MQTT_INTERVAL) + let forceUpdate = ((Date.now() - lastUpdateSent) > mqttInterval) if (overrideStatus !== undefined) { - console.log('updateStatus', 'Overriding status to:', overrideStatus) + customLog('updateStatus', 'Overriding status to:', overrideStatus) newStatus = overrideStatus superStatus = 1 } @@ -44,25 +104,25 @@ const updateStatus = async (overrideStatus) => { } if (forceUpdate) { - console.log('updateStatus', 'Checking window...') + customLog('updateStatus', 'Checking window...') newStatus = 'red' curDate = new Date() dayOfWeek = curDate.getDay() hourOfDay = curDate.getHours() - Object.keys(quietTimes[dayOfWeek]).forEach(function (slot) { - if (hourOfDay >= slot) { - console.log('updateStatus', 'Found window for:', hourOfDay, '/', slot) + Object.keys(quietTimes[dayOfWeek]).forEach((slot) => { + if (hourOfDay >= parseInt(slot)) { + customLog('updateStatus', 'Found window for:', hourOfDay, '/', slot) newStatus = quietTimes[dayOfWeek][slot] } }) } if (newStatus !== lastStatus || forceUpdate) { - console.log('updateStatus', 'Updating from', lastStatus, 'to', newStatus, '-', forceUpdate) + customLog('updateStatus', 'Updating from', lastStatus, 'to', newStatus, '-', forceUpdate) - await client.publish(MQTT_TOPIC, newStatus) + await client.publish(config.get('mqtt_topic'), newStatus) lastStatus = newStatus lastUpdateSent = Date.now() @@ -73,52 +133,47 @@ const updateStatus = async (overrideStatus) => { } const onConnect = async () => { - console.log('onConnect', 'Online') - console.log('onConnect', 'Subscribing to topics:') - console.log('onConnect', MQTT_SUB) + customLog('onConnect', 'Online') - MQTT_SUB.split(';').forEach((subscription) => { + const subTopics = config.get('mqtt_sub').split(';') + + customLog('onConnect', 'Subscribing to topics:') + customLog('onConnect', config.get('mqtt_sub')) + + subTopics.forEach((subscription) => { let subscriptionDetails = subscription.split(':') - console.log('onConnect', 'Subscribing to', subscriptionDetails[0], '=>', subscriptionDetails[1]) + customLog('onConnect', 'Subscribing to', subscriptionDetails[0], '=>', subscriptionDetails[1]) topics[subscriptionDetails[0]] = subscriptionDetails[1] client.subscribe(subscriptionDetails[0]) }) - console.log('onConnect', 'Subscribed to topics:', topics) + customLog('onConnect', 'Subscribed to topics:', topics) - console.log('onConnect', 'Forcing initial update') + customLog('onConnect', 'Forcing initial update') updateStatus() + + mqttInterval = (config.get('mqtt_interval') < 1000) ? (config.get('mqtt_interval') * 1000) : config.get('mqtt_interval') + + setInterval(updateStatus, 1000) } const onMessage = async (topic, message, packet) => { - console.log('onMessage', 'Handling', topic, '=>', message) + customLog('onMessage', 'Handling', topic, '=>', message) if (topics[topic] !== undefined) { updateStatus(topics[topic]) } else { - console.log('onMessage', 'Missing topic', topic) + customLog('onMessage', 'Missing topic', topic) } } // Main -const MQTT_URI = process.env.MQTT_URI || handleError('Missing MQTT_URI') -const MQTT_SUB = process.env.MQTT_SUB || handleError('Missing MQTT_SUB') -const MQTT_TOPIC = process.env.MQTT_TOPIC || handleError('Missing MQTT_TOPIC') -const MQTT_INTERVAL = process.env.MQTT_INTERVAL !== undefined && parseInt(process.env.MQTT_INTERVAL) ? (parseInt(process.env.MQTT_INTERVAL) < 1000) ? parseInt(process.env.MQTT_INTERVAL) * 1000 : parseInt(process.env.MQTT_INTERVAL) : 60000 - -var newStatus = 'red' -var lastStatus = 'red' -var lastUpdateSent = (Date.now() - MQTT_INTERVAL - 1) - -var topics = {} - -const client = MQTT.connect(MQTT_URI) +customLog('main', 'Starting...') +customLog('main', 'Connecting to', config.get('mqtt_uri')) +const client = MQTT.connect(config.get('mqtt_uri')) client.on('connect', onConnect) client.on('message', onMessage) - -// Start -setInterval(updateStatus, 15000)