From e0c06ed53d31247876614daa6c08e1b388d09930 Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Tue, 25 Aug 2020 21:30:56 +0300 Subject: [PATCH] changes from remote --- dht.js | 158 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 87 insertions(+), 71 deletions(-) diff --git a/dht.js b/dht.js index 60f1b18..1cce07e 100644 --- a/dht.js +++ b/dht.js @@ -161,7 +161,7 @@ var wws; if(credentials){ var httpsServer = https.createServer(credentials, app); httpsServer.listen(8443, () => { - console.log('HTTPS server listening on port 8443'); + console.log('\r\n\r\nHTTPS server listening on port 8443'); }); wss = new WebSocket.Server({ server: httpsServer }); } @@ -242,7 +242,7 @@ var dht = {}; //https://stevessmarthomeguide.com/setting-up-the-sonoff-tasmota-mqtt-switch/ mqtt_client.on('message', function (topic, message) { var context = message.toString(); - console.log("MMQT> " + topic + " : " + context); + //console.log("MQTT> " + topic + " : " + context); var handled = false; if(topic === "tele/tasmota/SENSOR") { @@ -269,7 +269,7 @@ mqtt_client.on('message', function (topic, message) { } if(topic === "ESP_Easy/status/LWT") { - console.log("ESP_Easy >'" + message + "'"); + //!console.log("ESP_Easy >'" + message + "'"); var stat = {}; var pairs = message.toString().split(';'); stat.event = pairs[0]; @@ -283,13 +283,13 @@ mqtt_client.on('message', function (topic, message) { } dht.ssid=stat["SSID"]; dht.rssi=stat["RSSI"]; - if(stat.event ==='ON'){console.log("Device is ONLINE! <" + dht.ssid + "> " + dht.rssi + "dB" );} - if(stat.event ==='OFF'){console.log("Device went ofline!");} + //if(stat.event ==='ON'){console.log(formatDate(new Date()) + "Device is ONLINE! <" + dht.ssid + "> " + dht.rssi + "dB" );} + //if(stat.event ==='OFF'){console.log(formatDate(new Date()) + "Device went ofline!");} handled = true; } if(topic === "ESP_Easy/Soil/RH") { - console.log("Garden > soil " + message + "% moisture"); + //console.log("Garden > soil " + message + "% moisture"); dht.Soil = message.toString(); if(dht.Soil > 10){ SaveDhtIf(); @@ -297,10 +297,6 @@ mqtt_client.on('message', function (topic, message) { console.log("Got suspicious soil RH value:" + dht.Soil); dht.Soil = null; } - // db.devicemessages.insert(1, "A23_Garden_Humidity", message, function (err, data) { - // if (!err) { console.log("success: "+ data);} - // else { console.log("error: " + err); } - // }); handled = true; } if(topic === "ESP_Easy/bmp/Temperature") @@ -330,7 +326,7 @@ mqtt_client.on('message', function (topic, message) { dht.Soil = j.soil; SaveDhtIf(); handled = true; - console.log("Garden > Started watering with " + j.soil + "% soil RH"); + console.log(formatDate(new Date()) + "Garden > Started watering at "+j.time+"(GMT) with " + j.soil + "% soil RH"); } if(topic === "ESP_Easy/Water/stop") { @@ -338,9 +334,9 @@ mqtt_client.on('message', function (topic, message) { dht.Soil = j.soil; SaveDhtIf(); handled = true; - console.log("Garden > Finished watering with " + j.soil + "% soil RH"); + console.log(formatDate(new Date()) + "Garden > Finished watering at "+j.time+"(GMT) with " + j.soil + "% soil RH"); } - + if(!handled){ console.log(topic + " > " + message ); } @@ -348,83 +344,103 @@ mqtt_client.on('message', function (topic, message) { function SaveDhtIf(){ if(dht.Temp && dht.Hum && dht.Pres)// && dht.Soil { - if(dht.Soil < 5) + if(dht.Soil && dht.Soil < 5) { console.log("Soil reported below 5%! Probable loose sensor wire. Record discarded" ); dht = {}; return; } - console.log("Writing to DB > " + JSON.stringify(dht)+"; SSID: '"+dht.ssid+"' RSSI:"+ dht.rssi + "dB"); - db.devicemessages.insert(1, "A23_Garden_dht", JSON.stringify(dht), function (err, data) { - if (!err) { console.log("success: "+ data);} + var msg = JSON.stringify(dht); + db.devicemessages.insert(1, "A23_Garden_dht", msg, function (err, data) { + if (!err) { + console.log("Saved to DB > ["+ data.id +"]:" + msg);//+"; SSID: '"+dht.ssid+"' RSSI:"+ dht.rssi + "dB"); + } else { console.log("error: " + err); } }); dht = {}; }else{ - if(dht.Temp ){console.log("have Temp");} - if(dht.Hum ){console.log("have Hum");} - if(dht.Pres ){console.log("have Pres");} - console.log("Missing all data to write to DB !"); + // if(dht.Temp ){console.log("have Temp");} + // if(dht.Hum ){console.log("have Hum");} + // if(dht.Pres ){console.log("have Pres");} + // console.log("Missing all data to write to DB !"); } } -//(function() { - // var exLog = console.log; - // console.log = function(msg) { - // exLog.apply(this, arguments); - // fs.writeFileSync('./log.txt', msg); - // } -//})(); +(function() { + var exLog = console.log; + console.log = function(msg) { + exLog.apply(this, arguments); + fs.appendFileSync('/var/www/dht.log', msg +"\r\n"); + } +})(); -const {keys} = Object; -const {Console} = console; +// const {keys} = Object; +// const {Console} = console; -/** - * Redirect console to a file. Call without path or with false-y - * value to restore original behavior. - * @param {string} [path] - */ -function file(path) { - const con = path ? new Console(fs.createWriteStream(path)) : null; +// /** +// * Redirect console to a file. Call without path or with false-y +// * value to restore original behavior. +// * @param {string} [path] +// */ +// function file(path) { +// const con = path ? new Console(fs.createWriteStream(path)) : null; - keys(Console.prototype).forEach(key => { - if (path) { - this[key] = (...args) => con[key](...args); - } else { - delete this[key]; - } - }); -}; +// keys(Console.prototype).forEach(key => { +// if (path) { +// this[key] = (...args) => con[key](...args); +// } else { +// delete this[key]; +// } +// }); +// }; -// ///---patch global console object and export -module.exports = console.file = file; +// // ///---patch global console object and export +// module.exports = console.file = file; -console.file(__dirname + '/../log.txt');//__dirname + '/debug.log' log.txt +// console.file(__dirname + '/../log.txt');//__dirname + '/debug.log' log.txt -var log4js = require("log4js"); -log4js.configure({ - appenders: { - file: { - type: 'file', - filename: '/../log4.txt', - maxLogSize: 10 * 1024 * 1024, // = 10Mb - backups: 5, // keep five backup files - compress: true, // compress the backups - encoding: 'utf-8', - mode: 0o0640, - flags: 'w+' - }, - console: { type: 'console' } - },categories: { - default: { appenders: ['console','file'], level: 'trace' } - } -}); +// var log4js = require("log4js"); +// log4js.configure({ +// appenders: { +// file: { +// type: 'file', +// filename: '/var/www/log4.txt', +// maxLogSize: 10 * 1024 * 1024, // = 10Mb +// backups: 5, // keep five backup files +// compress: true, // compress the backups +// encoding: 'utf-8', +// mode: 0o0640, +// flags: 'w+' +// }, +// console: { type: 'console' } +// },categories: { +// default: { appenders: ['console','file'], level: 'trace' } +// } +// }); -var logger = log4js.getLogger(); -logger.level = "debug"; -logger.debug("Some debug messages"); -console.log = (msg) => logger.trace(msg); -console.log("Console MSG"); +// var logger = log4js.getLogger(); +// logger.level = "debug"; +// console.log("Some debug messages"); +// //console.log = (msg) => console.log("C>" + msg); +// console.log("Console MSG"); + + +function formatDate (date) { + var hour = date.getHours(); + var minutes = date.getMinutes(); + var seconds = date.getSeconds(); + var milliseconds = date.getMilliseconds(); + + return '[' + + ((hour < 10) ? '0' + hour: hour) + + ':' + + ((minutes < 10) ? '0' + minutes: minutes) + + ':' + + ((seconds < 10) ? '0' + seconds: seconds) + + '.' + + ('00' + milliseconds).slice(-3) + + '] '; +} \ No newline at end of file