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