From 6a8a895f6997109836b253a253ef4d677c4ffc4e Mon Sep 17 00:00:00 2001 From: d-popov Date: Tue, 9 Jun 2020 00:38:35 +0300 Subject: [PATCH] cummulative changes --- dht.js | 53 ++++++++++++++++++-- package-lock.json | 114 ++++++++++++++++++++++++++++++------------ package.json | 14 +++--- src/devices/ir.js | 15 ++++-- views/chartGarden.ejs | 98 +++++++++++++++++++++++------------- 5 files changed, 211 insertions(+), 83 deletions(-) diff --git a/dht.js b/dht.js index f8b0d0e..040d0ed 100644 --- a/dht.js +++ b/dht.js @@ -212,6 +212,7 @@ mqtt_client.on('connect', function () { // mqtt_client.subscribe('dht'); // mqtt_client.subscribe('ir'); + mqtt_client.subscribe('ESP_Easy/+'); mqtt_client.subscribe('ESP_Easy/+/+'); mqtt_client.publish('tasmota', 'controller connected') }); @@ -246,11 +247,36 @@ mqtt_client.on('message', function (topic, message) { } handled = true; } - if(topic === "ESP_Easy/Water/Moisture") + if(topic === "ESP_Easy/status/LWT") { - console.log("MANGO> " + message + "% moisture"); + console.log("ESP_Easy >'" + message + "'"); + var stat = {}; + var pairs = message.toString().split(';'); + stat.event = pairs[0]; + for (var i = 0; i < pairs.length; i++) + { + var pair = pairs[i].split(':'); + if(pair.length == 2){ + stat[pair[0].trim()] = pair[1].trim() || ''; + //console.log( "'"+pair[0] +"' >'" + pair[1] + "'"); + } + } + 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!");} + handled = true; + } + if(topic === "ESP_Easy/Soil/RH") + { + console.log("Garden > soil " + message + "% moisture"); dht.Soil = message.toString(); - SaveDhtIf(); + if(dht.Soil > 10){ + SaveDhtIf(); + } else { + 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); } @@ -278,6 +304,15 @@ mqtt_client.on('message', function (topic, message) { SaveDhtIf(); handled = true; } + if(topic === "ESP_Easy/Water/start" || topic === "ESP_Easy/Water/stop") + { + var j = JSON.parse(message); + console.log("Garden > Finished watering with " + j.soil + "% soil RH"); + dht.Soil = j.soil; + SaveDhtIf(); + handled = true; + } + if(!handled){ console.log(topic + " > " + message ); } @@ -285,11 +320,19 @@ mqtt_client.on('message', function (topic, message) { function SaveDhtIf(){ if(dht.Temp && dht.Hum && dht.Pres && dht.Soil) { - console.log("Writing to DB > " + JSON.stringify(dht)); + if(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);} else { console.log("error: " + err); } }); dht = {}; + }else{ + console.log("Missing all data to write to DB !"); } -} \ No newline at end of file +} diff --git a/package-lock.json b/package-lock.json index e3036bb..bee3eb0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -922,9 +922,9 @@ "integrity": "sha1-bYCcnNDPe7iVLYD8hPoT1H3bEwg=" }, "deasync": { - "version": "0.1.19", - "resolved": "https://registry.npmjs.org/deasync/-/deasync-0.1.19.tgz", - "integrity": "sha512-oh3MRktfnPlLysCPpBpKZZzb4cUC/p0aA3SyRGp15lN30juJBTo/CiD0d4fR+f1kBtUQoJj1NE9RPNWQ7BQ9Mg==", + "version": "0.1.20", + "resolved": "https://registry.npmjs.org/deasync/-/deasync-0.1.20.tgz", + "integrity": "sha512-E1GI7jMI57hL30OX6Ht/hfQU8DO4AuB9m72WFm4c38GNbUD4Q03//XZaOIHZiY+H1xUaomcot5yk2q/qIZQkGQ==", "requires": { "bindings": "^1.5.0", "node-addon-api": "^1.7.1" @@ -1101,9 +1101,12 @@ "integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=" }, "ejs": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/ejs/-/ejs-3.0.2.tgz", - "integrity": "sha512-IncmUpn1yN84hy2shb0POJ80FWrfGNY0cxO9f4v+/sG7qcBvAtVWUA1IdzY/8EYUmOVhoKJVdJjNd3AZcnxOjA==" + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/ejs/-/ejs-3.1.2.tgz", + "integrity": "sha512-zFuywxrAWtX5Mk2KAuoJNkXXbfezpNA0v7i+YC971QORguPekpjpAgeOv99YWSdKXwj7JxI2QAWDeDkE8fWtXw==", + "requires": { + "jake": "^10.6.1" + } }, "emoji-regex": { "version": "7.0.3", @@ -1420,9 +1423,9 @@ } }, "express-session": { - "version": "1.17.0", - "resolved": "https://registry.npmjs.org/express-session/-/express-session-1.17.0.tgz", - "integrity": "sha512-t4oX2z7uoSqATbMfsxWMbNjAL0T5zpvcJCk3Z9wnPPN7ibddhnmDZXHfEcoBMG2ojKXZoCyPMc5FbtK+G7SoDg==", + "version": "1.17.1", + "resolved": "https://registry.npmjs.org/express-session/-/express-session-1.17.1.tgz", + "integrity": "sha512-UbHwgqjxQZJiWRTMyhvWGvjBQduGCSBDhhZXYenziMFjxst5rMV+aJZ6hKPHZnPyHGsrqRICxtX8jtEbm/z36Q==", "requires": { "cookie": "0.4.0", "cookie-signature": "1.0.6", @@ -1583,6 +1586,14 @@ "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", "integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==" }, + "filelist": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/filelist/-/filelist-1.0.1.tgz", + "integrity": "sha512-8zSK6Nu0DQIC08mUC46sWGXi+q3GGpKydAG36k+JDba6VRpkevvOWUW5a/PhShij4+vHT9M+ghgG7eM+a9JDUQ==", + "requires": { + "minimatch": "^3.0.4" + } + }, "finalhandler": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.1.2.tgz", @@ -1973,6 +1984,11 @@ "integrity": "sha1-ZxRKUmDDT8PMpnfQQdr1L+e3iy8=", "optional": true }, + "has-flag": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", + "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=" + }, "has-symbols": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.1.tgz", @@ -2373,6 +2389,42 @@ "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz", "integrity": "sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo=" }, + "jake": { + "version": "10.6.1", + "resolved": "https://registry.npmjs.org/jake/-/jake-10.6.1.tgz", + "integrity": "sha512-pHUK3+V0BjOb1XSi95rbBksrMdIqLVC9bJqDnshVyleYsET3H0XAq+3VB2E3notcYvv4wRdRHn13p7vobG+wfQ==", + "requires": { + "async": "0.9.x", + "chalk": "^2.4.2", + "filelist": "^1.0.1", + "minimatch": "^3.0.4" + }, + "dependencies": { + "async": { + "version": "0.9.2", + "resolved": "https://registry.npmjs.org/async/-/async-0.9.2.tgz", + "integrity": "sha1-rqdNXmHB+JlhO/ZL2mbUx48v0X0=" + }, + "chalk": { + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", + "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", + "requires": { + "ansi-styles": "^3.2.1", + "escape-string-regexp": "^1.0.5", + "supports-color": "^5.3.0" + } + }, + "supports-color": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", + "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", + "requires": { + "has-flag": "^3.0.0" + } + } + } + }, "js-beautify": { "version": "1.11.0", "resolved": "https://registry.npmjs.org/js-beautify/-/js-beautify-1.11.0.tgz", @@ -2961,9 +3013,9 @@ "integrity": "sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==" }, "moment": { - "version": "2.24.0", - "resolved": "https://registry.npmjs.org/moment/-/moment-2.24.0.tgz", - "integrity": "sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg==" + "version": "2.25.3", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.25.3.tgz", + "integrity": "sha512-PuYv0PHxZvzc15Sp8ybUCoQ+xpyPWvjOuK72a5ovzp2LI32rJXOiIfyoFoYvG3s6EwwrdkMyWuRiEHSZRLJNdg==" }, "moment-timezone": { "version": "0.5.28", @@ -2974,12 +3026,12 @@ } }, "mongodb": { - "version": "3.5.5", - "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.5.5.tgz", - "integrity": "sha512-GCjDxR3UOltDq00Zcpzql6dQo1sVry60OXJY3TDmFc2SWFY6c8Gn1Ardidc5jDirvJrx2GC3knGOImKphbSL3A==", + "version": "3.5.7", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.5.7.tgz", + "integrity": "sha512-lMtleRT+vIgY/JhhTn1nyGwnSMmJkJELp+4ZbrjctrnBxuLbj6rmLuJFz8W2xUzUqWmqoyVxJLYuC58ZKpcTYQ==", "requires": { "bl": "^2.2.0", - "bson": "^1.1.1", + "bson": "^1.1.4", "denque": "^1.4.1", "require_optional": "^1.0.1", "safe-buffer": "^5.1.2", @@ -3005,15 +3057,15 @@ } }, "mongoose": { - "version": "5.9.7", - "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.9.7.tgz", - "integrity": "sha512-WJOBh9WMvivqBK8my9HFtSzSySKdUxJPNGAwswEakAasWUcPXJl3yHMtZ4ngGnKbwTT9KnAr75xamlt/PouR9w==", + "version": "5.9.13", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.9.13.tgz", + "integrity": "sha512-MsFdJAaCTVbDA3gYskUEpUN1kThL7sp4zh8N9rGt0+9vYMn28q92NLK90vGssM9qjOGWp8HqLeT1fBgfMZDnKA==", "requires": { - "bson": "~1.1.1", + "bson": "^1.1.4", "kareem": "2.3.1", - "mongodb": "3.5.5", + "mongodb": "3.5.7", "mongoose-legacy-pluralize": "1.0.2", - "mpath": "0.6.0", + "mpath": "0.7.0", "mquery": "3.2.2", "ms": "2.1.2", "regexp-clone": "1.0.0", @@ -3245,9 +3297,9 @@ "integrity": "sha1-mxnDdpeOIblF7Xd2eO2VTUt7VHU=" }, "mpath": { - "version": "0.6.0", - "resolved": "https://registry.npmjs.org/mpath/-/mpath-0.6.0.tgz", - "integrity": "sha512-i75qh79MJ5Xo/sbhxrDrPSEG0H/mr1kcZXJ8dH6URU5jD/knFxCVqVC/gVSW7GIXL/9hHWlT9haLbCXWOll3qw==" + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/mpath/-/mpath-0.7.0.tgz", + "integrity": "sha512-Aiq04hILxhz1L+f7sjGyn7IxYzWm1zLNNXcfhDtx04kZ2Gk7uvFdgZ8ts1cWa/6d0TQmag2yR8zSGZUmp0tFNg==" }, "mqtt": { "version": "1.14.1", @@ -4592,9 +4644,9 @@ "integrity": "sha1-1WgS4cAXpuTnw+Ojeh2m143TyT4=" }, "sequelize": { - "version": "5.21.6", - "resolved": "https://registry.npmjs.org/sequelize/-/sequelize-5.21.6.tgz", - "integrity": "sha512-RsgEpP2PP7txeoTWxoLLoe3xX8R2WYQAO7LNba2Ok3/pV5EFfKZry4fJXH56DUHJB909msMCHg0CJKDsQVbjcQ==", + "version": "5.21.8", + "resolved": "https://registry.npmjs.org/sequelize/-/sequelize-5.21.8.tgz", + "integrity": "sha512-UOxGMJ7eSnProTMWX9wRr0T9TcuW0YedFmU7s+YBZT9/RmfJeANYPdJXjXg4E+Yg+dJl4WWvBEu9r2oe+d/1/Q==", "requires": { "bluebird": "^3.5.0", "cls-bluebird": "^2.1.0", @@ -5513,9 +5565,9 @@ "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" }, "ws": { - "version": "7.2.3", - "resolved": "https://registry.npmjs.org/ws/-/ws-7.2.3.tgz", - "integrity": "sha512-HTDl9G9hbkNDk98naoR/cHDws7+EyYMOdL1BmjsZXRUjf7d+MficC4B7HLUPlSiho0vg+CWKrGIt/VJBd1xunQ==" + "version": "7.3.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.3.0.tgz", + "integrity": "sha512-iFtXzngZVXPGgpTlP1rBqsUK82p9tKqsWRPg5L56egiljujJT3vGAYnHANvFxBieXrTFavhzhxW52jnaWV+w2w==" }, "x-xss-protection": { "version": "1.3.0", diff --git a/package.json b/package.json index b26dbba..1a94d58 100644 --- a/package.json +++ b/package.json @@ -22,17 +22,17 @@ "cookie-auth": "^2.4.2", "cookie-parser": "^1.4.5", "cors": "2.8.5", - "deasync": "^0.1.19", - "ejs": "^3.0.2", + "deasync": "^0.1.20", + "ejs": "^3.1.2", "express": "^4.8.7", "express-ejs-layouts": "^2.5.0", "express-namespace": "^0.1.1", - "express-session": "^1.17.0", + "express-session": "^1.17.1", "got": "^10.7.0", "helmet": "3.21.3", - "moment": "^2.24.0", + "moment": "^2.25.3", "moment-timezone": "^0.5.13", - "mongoose": "^5.9.7", + "mongoose": "^5.9.13", "morgan": "^1.10.0", "mosca": "^2.8.3", "mysql": "^2.18.1", @@ -45,11 +45,11 @@ "passport-local": "^1.0.0", "plaintextparser": "^1.0.3", "request": "^2.88.2", - "sequelize": "^5.21.6", + "sequelize": "^5.21.8", "sequelize-cli": "^5.5.1", "swagger-ui-express": "^2.0.13", "sync-request": "^4.0.2", "vash": "^0.13.0", - "ws": "^7.2.3" + "ws": "^7.3.0" } } diff --git a/src/devices/ir.js b/src/devices/ir.js index 0fd7932..770e60f 100644 --- a/src/devices/ir.js +++ b/src/devices/ir.js @@ -6,10 +6,11 @@ const got = require('got'); const request = require('request'); function GetDht() { + try {//? var result; (async () => { try { - var ret = await got('http://192.168.1.126/cm?cmnd=status%2010'); + var ret = await got('http://192.168.1.126/cm?cmnd=status%2010&user=admin&password=vlado555'); console.log("DHT: "+ util.inspect(ret.body)); var j = JSON.parse(ret.body); console.log("JSON> " + util.inspect(j)); @@ -26,16 +27,20 @@ function GetDht() { console.log(error); } })(); - while(result === undefined) { - require('deasync').runLoopOnce(); - } + while(result === undefined) { + require('deasync').runLoopOnce(); + } + return result; + } catch(error){ + console.log(error); } +} exports.SendCmd = function (url, cmd) { - url = url +"/cm?cmnd=irsend%200,"+cmd; + url = url +"/cm?cmnd=irsend%200,"+cmd+"&user=admin&password=vlado555"; console.log("IR_SEND_RAW:" + url); (async () => { try { diff --git a/views/chartGarden.ejs b/views/chartGarden.ejs index ee1dc62..4f9c850 100644 --- a/views/chartGarden.ejs +++ b/views/chartGarden.ejs @@ -1,13 +1,15 @@
+
getting inside conditions...
<%- contentFor('head') %> \ No newline at end of file