! :) hot reload of Garden IP setting when new IP advertised by garden controller over MQTT.
This commit is contained in:
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)!'}
|
||||||
|
// ]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
30
src/mqtt.js
30
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");
|
||||||
@@ -178,22 +183,17 @@ mqtt_client.on("message", function (topic, message) {
|
|||||||
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
|
|
||||||
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));
|
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);
|
} catch (e) {
|
||||||
|
console.log("MQTT> error processing /ping message: " + e);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!handled) {
|
if (!handled) {
|
||||||
|
|||||||
Reference in New Issue
Block a user