From 46ec5dd410c026e06859d3797e73977f94fd998e Mon Sep 17 00:00:00 2001 From: Karl Date: Sun, 10 May 2020 21:13:16 +0100 Subject: [PATCH] update logger --- lib/common.js | 4 ++-- lib/post.js | 12 +++++++----- package-lock.json | 39 +++++++++++++++++++++++++++++++++++++++ package.json | 1 + routes/heating_get.js | 14 +++++++------- routes/heating_post.js | 8 ++++---- routes/root_get copy.js | 13 +++++++++++++ routes/water_get.js | 18 +++++++++--------- routes/water_post.js | 32 ++++++++++++++++++++++++-------- test.js | 4 ++-- 10 files changed, 108 insertions(+), 37 deletions(-) create mode 100644 routes/root_get copy.js diff --git a/lib/common.js b/lib/common.js index 8ad54f2..e5821af 100644 --- a/lib/common.js +++ b/lib/common.js @@ -7,7 +7,7 @@ module.exports = { mrt = mrt[0] + "." + mrt[1] return mrt } catch (error) { - logger.info('Error') + logger.debug('Error') } }, @@ -21,7 +21,7 @@ module.exports = { } return on } catch (error) { - logger.info('Error') + logger.debug('Error') } }, diff --git a/lib/post.js b/lib/post.js index d09c43a..d870331 100644 --- a/lib/post.js +++ b/lib/post.js @@ -1,6 +1,8 @@ const got = require('got') const token = require('basic-auth-token'); let logger = require('perfect-logger') +var cacheManager = require('cache-manager'); +var memoryCache = cacheManager.caching({ store: 'memory', max: 100, ttl: 200/*seconds*/ }); module.exports = { postRequest: async (body, url) => { @@ -14,16 +16,16 @@ module.exports = { } } try { - const response = await got('https://public.wcs.schneider-electric.ws/rpc/public_genie/' + url, options); + response = await got('https://public.wcs.schneider-electric.ws/rpc/public_genie/' + url, options); const res = JSON.parse(response.body) - logger.info(url + " - ZONES - " + res.updateData.zones.length) - if (res.updateData.zones.length <2){ - logger.info(JSON.stringify(res)) + logger.debug(url + " - ZONES - " + res.updateData.zones.length) + if (res.updateData.zones.length < 2) { + logger.debug(JSON.stringify(res)) } return res } catch (error) { logger.warn('-- post error --'); - logger.crit(error) + logger.crit(JSON.stringify(response)) logger.warn('-- post error --'); } } diff --git a/package-lock.json b/package-lock.json index 7618508..ec386e7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -68,6 +68,11 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" }, + "async": { + "version": "1.5.2", + "resolved": "https://registry.npmjs.org/async/-/async-1.5.2.tgz", + "integrity": "sha1-7GphrlZIDAw8skHJVhjiCJL5Zyo=" + }, "basic-auth-token": { "version": "0.4.2", "resolved": "https://registry.npmjs.org/basic-auth-token/-/basic-auth-token-0.4.2.tgz", @@ -95,6 +100,16 @@ "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz", "integrity": "sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg==" }, + "cache-manager": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/cache-manager/-/cache-manager-3.3.0.tgz", + "integrity": "sha512-BH7pfWWZ6BYnkcj6759uWnTCwMz24LXI0PzribTP8WqpydHo9Jk6EM9itmG+rBXAJHbNOwo1ES49SIn8hyhW2A==", + "requires": { + "async": "1.5.2", + "lodash": "^4.17.15", + "lru-cache": "4.0.0" + } + }, "cacheable-lookup": { "version": "4.3.0", "resolved": "https://registry.npmjs.org/cacheable-lookup/-/cacheable-lookup-4.3.0.tgz", @@ -361,11 +376,25 @@ "json-buffer": "3.0.1" } }, + "lodash": { + "version": "4.17.15", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", + "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==" + }, "lowercase-keys": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-2.0.0.tgz", "integrity": "sha512-tqNXrS78oMOE73NMxK4EMLQsQowWf8jKooH9g7xPavRT706R6bkQJ6DY2Te7QukaZsulxa30wQ7bk0pm4XiHmA==" }, + "lru-cache": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.0.0.tgz", + "integrity": "sha1-tcvwFVbBaWb+vlTO7A+03JDfbCg=", + "requires": { + "pseudomap": "^1.0.1", + "yallist": "^2.0.0" + } + }, "media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", @@ -464,6 +493,11 @@ "ipaddr.js": "1.9.1" } }, + "pseudomap": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz", + "integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM=" + }, "pump": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz", @@ -603,6 +637,11 @@ "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" + }, + "yallist": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz", + "integrity": "sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI=" } } } diff --git a/package.json b/package.json index b5d17b1..1bd242b 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "dependencies": { "basic-auth-token": "^0.4.2", "body-parser": "^1.19.0", + "cache-manager": "^3.3.0", "express": "^4.17.1", "fs": "0.0.1-security", "got": "^11.1.2", diff --git a/routes/heating_get.js b/routes/heating_get.js index 9671993..e598d48 100644 --- a/routes/heating_get.js +++ b/routes/heating_get.js @@ -4,17 +4,17 @@ let logger = require('perfect-logger') module.exports = function (app) { app.get("/heating", async function (req, res, next) { - logger.info('GET - /heating - START') + logger.debug('GET - /heating - START') const response = await post.postRequest('{}', 'poll') const heating = response.updateData.zones[0] - logger.info('GET - /heating - END') + logger.debug('GET - /heating - END') res.json(heating) }); app.get("/heating/status", async function (req, res, next) { - logger.info('GET - /heating/status - START') + logger.debug('GET - /heating/status - START') const response = await post.postRequest('{}', 'poll') - // logger.info(response.updateData) + // logger.debug(response.updateData) const heating = response.updateData.zones[0].status const mrt = await common.updateTemp(heating) const on = await common.heatingOn(heating) @@ -28,15 +28,15 @@ module.exports = function (app) { "lastTimerSetPoint": lsp, "lastTimerDurationMinutes": JSON.stringify(heating.lastTimerDurationMinutes) } - logger.info('GET - /heating/status - END') + logger.debug('GET - /heating/status - END') res.json(heatingStatus) }); app.get("/heating/config", async function (req, res, next) { - logger.info('GET - /heating/config - START') + logger.debug('GET - /heating/config - START') const response = await post.postRequest('{}', 'poll') const heating = response.updateData.zones[0].config - logger.info('GET - /heating/config - END') + logger.debug('GET - /heating/config - END') res.json(heating) }); diff --git a/routes/heating_post.js b/routes/heating_post.js index 90fcb85..dc7b3bb 100644 --- a/routes/heating_post.js +++ b/routes/heating_post.js @@ -5,15 +5,15 @@ let logger = require('perfect-logger') module.exports = function (app) { app.post("/heating", async function (req, res, next) { - logger.info('POST - /heating - START') + logger.debug('POST - /heating - START') const response = await post.postRequest('{}') const heating = response.updateData.zones[0] - logger.info('POST - /heating - END') + logger.debug('POST - /heating - END') res.json(heating) }); app.post("/heating/status", async function (req, res, next) { - logger.info('POST - /heating/status - START') + logger.debug('POST - /heating/status - START') const response = await post.postRequest('{}', 'poll') const heating = response.updateData.zones[0].status const mrt = await common.updateTemp(heating) @@ -28,7 +28,7 @@ module.exports = function (app) { "lastTimerSetPoint": lsp, "lastTimerDurationMinutes": JSON.stringify(heating.lastTimerDurationMinutes) } - logger.info('POST - /heating/status - END') + logger.debug('POST - /heating/status - END') res.json(heatingStatus) }); diff --git a/routes/root_get copy.js b/routes/root_get copy.js new file mode 100644 index 0000000..02d4678 --- /dev/null +++ b/routes/root_get copy.js @@ -0,0 +1,13 @@ +const post = require('../lib/post') +let logger = require('perfect-logger') + +module.exports = function (app) { + app.get("/", async function (req, res, next) { + logger.debug('GET - / - START') + const response = await post.postRequest('{}', 'poll') + const heating = response + logger.debug('GET - / - END') + res.json(heating) + }); + +} \ No newline at end of file diff --git a/routes/water_get.js b/routes/water_get.js index a9f4313..12c7a5d 100644 --- a/routes/water_get.js +++ b/routes/water_get.js @@ -4,15 +4,15 @@ let logger = require('perfect-logger') module.exports = function (app) { app.get("/water", async function (req, res, next) { - logger.info('GET - /water - START') + logger.debug('GET - /water - START') const response = await post.postRequest('{}', 'poll') const water = response.updateData.zones[1] - logger.info('GET - /water - END') + logger.debug('GET - /water - END') res.json(water) }); app.get("/water/status", async function (req, res, next) { - logger.info('GET - /water/status - START') + logger.debug('GET - /water/status - START') const response = await post.postRequest('{}', 'poll') const water = response.updateData.zones[1].status const on = await common.heatingOn(water) @@ -23,20 +23,20 @@ module.exports = function (app) { "lastTimerSetPoint": JSON.stringify(water.lastTimerSetPoint), "lastTimerDurationMinutes": JSON.stringify(water.lastTimerDurationMinutes) } - logger.info('GET - /water/status - END') + logger.debug('GET - /water/status - END') res.json(waterStatus) }); app.get("/water/config", async function (req, res, next) { - logger.info('GET - /water/config - START') + logger.debug('GET - /water/config - START') const response = await post.postRequest('{}', 'poll') const water = response.updateData.zones[1].config - logger.info('GET - /water/config - END') + logger.debug('GET - /water/config - END') res.json(water) }); app.get("/water/switch", async function (req, res, next) { - logger.info('GET - /water/switch - START') + logger.debug('GET - /water/switch - START') const response = await post.postRequest('{}', 'poll') const water = response.updateData.zones[1].status let on = await common.heatingOn(water) @@ -49,8 +49,8 @@ module.exports = function (app) { let switchStatus = { "switch": on } - logger.info(switchStatus) - logger.info('GET - /water/switch - END') + logger.debug(switchStatus) + logger.debug('GET - /water/switch - END') res.json(switchStatus) }); diff --git a/routes/water_post.js b/routes/water_post.js index d6c9e8f..50d8437 100644 --- a/routes/water_post.js +++ b/routes/water_post.js @@ -5,9 +5,9 @@ let logger = require('perfect-logger') module.exports = function (app) { app.post("/water/switch", async function (req, res, next) { - logger.info('POST - /water/switch - START') + logger.debug('POST - /water/switch - START') if (req.body.switch === 1) { - logger.info('POST - /water/switch - ON - START') + logger.debug('POST - /water/switch - ON - START') try { await post.postRequest('{"zoneId":[1],"setPoint": 255,"durationMinutes": 90}', 'apply_timer') const response = await post.postRequest('{}', 'poll') @@ -17,14 +17,14 @@ module.exports = function (app) { let waterStatus = { "waterOn": JSON.stringify(on), } - logger.info('POST - /water/switch - ON - COMPLETE') + logger.debug('POST - /water/switch - ON - COMPLETE') res.json(waterStatus) } catch (error) { logger.warn('POST - /water/switch - ON - FAILED') } } else if (req.body.switch === 0) { try { - logger.info('POST - /water/switch - OFF - START') + logger.debug('POST - /water/switch - OFF - START') await post.postRequest('{"zoneId":[1]}', 'cancel_timer') const response = await post.postRequest('{}', 'poll') const water = response.updateData.zones[1].status @@ -33,18 +33,34 @@ module.exports = function (app) { let waterStatus = { "waterOn": JSON.stringify(on), } - logger.info('POST - /water/switch - OFF - COMPLETE') + logger.debug('POST - /water/switch - OFF - COMPLETE') res.json(waterStatus) } catch (error) { + const response = await post.postRequest('{}', 'poll') + const water = response.updateData.zones[1].status + const on = await common.heatingOn(water) + + let waterStatus = { + "waterOn": JSON.stringify(on), + } logger.warn('POST - /water/switch - OFF - FAILED') + res.json(waterStatus) } } else { - logger.warn('POST - /water/switch - FAILED') + const response = await post.postRequest('{}', 'poll') + const water = response.updateData.zones[1].status + const on = await common.heatingOn(water) + + let waterStatus = { + "waterOn": JSON.stringify(on), + } + logger.warn('POST - /water/switch - OFF - FAILED') + res.json(waterStatus) } }); app.post("/water/status", async function (req, res, next) { - logger.info('POST - /water/status - START') + logger.debug('POST - /water/status - START') const response = await post.postRequest('{}', 'poll') const water = response.updateData.zones[1].status const on = await common.heatingOn(water) @@ -55,7 +71,7 @@ module.exports = function (app) { "lastTimerSetPoint": JSON.stringify(water.lastTimerSetPoint), "lastTimerDurationMinutes": JSON.stringify(water.lastTimerDurationMinutes) } - logger.info('POST - /water/status - END') + logger.debug('POST - /water/status - END') res.json(waterStatus) }); } \ No newline at end of file diff --git a/test.js b/test.js index d61cca6..d1f0207 100644 --- a/test.js +++ b/test.js @@ -15,9 +15,9 @@ app.use( app.use(bodyParser.json()) app.post('/endpoint', (req, res) => { - logger.info(req.body) + logger.debug(req.body) }) app.listen(2020, function () { - logger.info("Server running on port 2020"); + logger.debug("Server running on port 2020"); }); \ No newline at end of file