diff --git a/config/config.json b/config/config.json index 5a5a974..e496297 100644 --- a/config/config.json +++ b/config/config.json @@ -4,28 +4,28 @@ "password": "!iot_popovi_nh", "database": "iot", "host": "192.168.0.10", - "port":"3306", + "port": "3306", "dialect": "mysql", "operatorsAliases": false, - "logFile":"/dht.log", - "gardenIP":"192.168.0.42", + "logFile": "/dht.log", + "gardenIP": "192.168.0.42", "mqttIP": "192.168.0.10", "mqttPortHttp": "1883", "mqttPortHttps": "1884", "irHost": "192.168.0.10", - "mqttEspName":"Garden", - "mqttWebappName":"iot.garden.dev" + "mqttEspName": "Garden", + "mqttWebappName": "iot.garden.dev" }, "mitko": { "host": "node-mysql", "database": "iot", "username": "root", "password": "mitko2021", - "port":"3306", + "port": "3306", "dialect": "mysql", "operatorsAliases": false, - "logFile":"/dht.log", - "gardenIP":"192.168.0.187", + "logFile": "/dht.log", + "gardenIP": "192.168.0.187", "mqttIP": "192.168.0.10", "mqttPortHttp": "1884", "mqttPortHttps": "8444", @@ -36,14 +36,14 @@ "password": "!iot_popovi_nh", "database": "iot", "host": "192.168.0.10", - "port":"3304", + "port": "3304", "dialect": "mysql", "operatorsAliases": false, - "logFile":"/var/www/dht.log", - "gardenIP":"192.168.0.187", + "logFile": "/var/www/dht.log", + "gardenIP": "192.168.0.187", "mqttIP": "192.168.0.10", "mqttPortHttp": "1884", "mqttPortHttps": "8444", "irHost": "192.168.0.10" } -} +} \ No newline at end of file diff --git a/index.js b/index.js index 3491714..d54943f 100644 --- a/index.js +++ b/index.js @@ -41,9 +41,10 @@ config.gardenIP = process.env.GARDEN_IP || config.gardenIP // defining HTTP endpoints app.get('/', (req, res) => { - res.send([ - {title: 'Hello, world (again)!'} - ]); + res.redirect('/garden'); + // res.send([ + // {title: 'Hello, world (again)!'} + // ]); }); diff --git a/src/mqtt.js b/src/mqtt.js index ca59a3b..ba14b90 100644 --- a/src/mqtt.js +++ b/src/mqtt.js @@ -3,6 +3,7 @@ var db = require("./db"); const env = process.env.NODE_ENV || "development"; console.log("ENV: %s", env); +var fs = require("fs"); const path = require('path'); const configPath = path.join(__dirname, '/../config/config.json'); const config = require(configPath)[env]; @@ -43,10 +44,13 @@ console.log("MQTT: mqtt_settings.port : %s", mqtt_settings.port); // mqttServer.on('clientConnected', function(){ // console.log("MQTT client connected !"); // }); -var EspDeviceName = process.env.MQTT_ESP_NAME; +var EspDeviceName = process.env.MQTT_ESP_NAME || config.mqttEspName; var mqtt = require("mqtt"); -var mqtt_client = mqtt.connect("mqtt://" + mqtt_settings.host); +var mqtt_client = mqtt.connect("mqtt://" + mqtt_settings.host, + { + clientId: process.env.MQTT_WEBAPP_NAME || config.mqttWebappName || "iot.garden.dev.local" + }); mqtt_client.on("connect", function () { console.log("MQTT connected to " + mqtt_settings.host + ". subscribing to topics"); mqtt_client.subscribe("tele/tasmota/STATE"); @@ -60,6 +64,7 @@ mqtt_client.on("connect", function () { // mqtt_client.subscribe('ir'); // ESP_Easy_0 = garden + console.log("subscribing to topics '" + EspDeviceName + "/'"); mqtt_client.subscribe(EspDeviceName + "/+"); mqtt_client.subscribe(EspDeviceName + "/+/+"); mqtt_client.publish("tasmota", "controller connected"); @@ -173,27 +178,22 @@ mqtt_client.on("message", function (topic, message) { console.log("MQTT> unexpected json payload at /Water/stop:" + message); } } - if(topic === EspDeviceName + "/ping"){ - try { + if (topic === EspDeviceName + "/ping") { + try { console.log("MQTT> processing /ping message. Payload:" + context); var data = context.replace(/^\{|\}$/g, '').split('|'); - var ip = data[4].replace(/"/g, ''); - console.log("got reporded garden controller IP: " + ip); - console.log("MQTT> /ping got us " + data.length + "data items"); - config.gardenIP = ip; - - - // Make a copy of config and change that -let newConfig = Object.assign({}, config); -newConfig.gardenIP = ip; - -// Write the new config back to the original file -fs.writeFileSync(configPath, JSON.stringify(newConfig, null, 2)); - + var ip = data[4].replace(/"/g, ''); + if (ip != config.gardenIP) { + //console.log("MQTT> /ping got us " + data.length + " data items"); + config.gardenIP = ip; + let newConfig = require(configPath); + newConfig[env].gardenIP = ip; + fs.writeFileSync(configPath, JSON.stringify(newConfig, null, 2)); + console.log("Got NEW reporded garden controller IP: " + ip + ". new value stored in persistent settings storage"); + } } catch (e) { - console.log("MQTT> error at /ping: " + e); + console.log("MQTT> error processing /ping message: " + e); } - } if (!handled) {