diff --git a/.gitignore b/.gitignore index 654189c..d4e3a96 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ node_modules .vscode/launch.json +logger.lock +logs \ No newline at end of file diff --git a/lib/common.js b/lib/common.js index f43b9fd..8ad54f2 100644 --- a/lib/common.js +++ b/lib/common.js @@ -7,7 +7,7 @@ module.exports = { mrt = mrt[0] + "." + mrt[1] return mrt } catch (error) { - console.log('Error') + logger.info('Error') } }, @@ -21,7 +21,7 @@ module.exports = { } return on } catch (error) { - console.log('Error') + logger.info('Error') } }, diff --git a/lib/logger.js b/lib/logger.js new file mode 100644 index 0000000..60d84ed --- /dev/null +++ b/lib/logger.js @@ -0,0 +1,14 @@ +// Load the perfect-logger module +let logger = require('perfect-logger'); + +module.exports = { + logger: () => { + // Configure Settings + logger.initialize('FrontEndDriver', { + logLevelFile: 0, // Log level for file + logLevelConsole: 0, // Log level for STDOUT/STDERR + logDirectory: 'logs/', // Log directory + customBannerHeaders: 'This is a custom banner' // Custom Log Banner + }); + } +} \ No newline at end of file diff --git a/lib/post.js b/lib/post.js index 729378b..01db451 100644 --- a/lib/post.js +++ b/lib/post.js @@ -1,5 +1,6 @@ const got = require('got') const token = require('basic-auth-token'); +let logger = require('perfect-logger') module.exports = { postRequest: async (body, url) => { @@ -15,9 +16,14 @@ module.exports = { try { const 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)) + } return res } catch (error) { - console.log('post error'); + logger.info(error) + logger.info('post error'); } } } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 8a86b90..1dc190e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -450,6 +450,11 @@ "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" }, + "perfect-logger": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/perfect-logger/-/perfect-logger-2.0.1.tgz", + "integrity": "sha512-MGjZ4KcKFJ0w2LOvO2kSILZMU2KUOESaIJnI4sJ6qAumEmrRHiJVPx088WgAYnZMnys6gFI2ZP2YbNTL308xkA==" + }, "proxy-addr": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.6.tgz", diff --git a/package.json b/package.json index 6d88356..8f5ab29 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "body-parser": "^1.19.0", "express": "^4.17.1", "fs": "0.0.1-security", - "got": "^11.1.1" + "got": "^11.1.1", + "perfect-logger": "^2.0.1" } } diff --git a/routes/heating_get.js b/routes/heating_get.js index 6f33b6c..9671993 100644 --- a/routes/heating_get.js +++ b/routes/heating_get.js @@ -1,16 +1,20 @@ const post = require('../lib/post') const common = require('../lib/common') +let logger = require('perfect-logger') module.exports = function (app) { app.get("/heating", async function (req, res, next) { + logger.info('GET - /heating - START') const response = await post.postRequest('{}', 'poll') const heating = response.updateData.zones[0] + logger.info('GET - /heating - END') res.json(heating) }); app.get("/heating/status", async function (req, res, next) { + logger.info('GET - /heating/status - START') const response = await post.postRequest('{}', 'poll') - // console.log(response.updateData) + // logger.info(response.updateData) const heating = response.updateData.zones[0].status const mrt = await common.updateTemp(heating) const on = await common.heatingOn(heating) @@ -24,12 +28,15 @@ module.exports = function (app) { "lastTimerSetPoint": lsp, "lastTimerDurationMinutes": JSON.stringify(heating.lastTimerDurationMinutes) } + logger.info('GET - /heating/status - END') res.json(heatingStatus) }); app.get("/heating/config", async function (req, res, next) { + logger.info('GET - /heating/config - START') const response = await post.postRequest('{}', 'poll') const heating = response.updateData.zones[0].config + logger.info('GET - /heating/config - END') res.json(heating) }); diff --git a/routes/heating_post.js b/routes/heating_post.js index e274163..90fcb85 100644 --- a/routes/heating_post.js +++ b/routes/heating_post.js @@ -1,14 +1,19 @@ const post = require('../lib/post') const common = require('../lib/common') +let logger = require('perfect-logger') + module.exports = function (app) { app.post("/heating", async function (req, res, next) { + logger.info('POST - /heating - START') const response = await post.postRequest('{}') const heating = response.updateData.zones[0] + logger.info('POST - /heating - END') res.json(heating) }); app.post("/heating/status", async function (req, res, next) { + logger.info('POST - /heating/status - START') const response = await post.postRequest('{}', 'poll') const heating = response.updateData.zones[0].status const mrt = await common.updateTemp(heating) @@ -23,6 +28,7 @@ module.exports = function (app) { "lastTimerSetPoint": lsp, "lastTimerDurationMinutes": JSON.stringify(heating.lastTimerDurationMinutes) } + logger.info('POST - /heating/status - END') res.json(heatingStatus) }); diff --git a/routes/water_get.js b/routes/water_get.js index ec8f10b..a9f4313 100644 --- a/routes/water_get.js +++ b/routes/water_get.js @@ -1,14 +1,18 @@ const post = require('../lib/post') const common = require('../lib/common') +let logger = require('perfect-logger') module.exports = function (app) { app.get("/water", async function (req, res, next) { + logger.info('GET - /water - START') const response = await post.postRequest('{}', 'poll') const water = response.updateData.zones[1] + logger.info('GET - /water - END') res.json(water) }); app.get("/water/status", async function (req, res, next) { + logger.info('GET - /water/status - START') const response = await post.postRequest('{}', 'poll') const water = response.updateData.zones[1].status const on = await common.heatingOn(water) @@ -19,18 +23,21 @@ module.exports = function (app) { "lastTimerSetPoint": JSON.stringify(water.lastTimerSetPoint), "lastTimerDurationMinutes": JSON.stringify(water.lastTimerDurationMinutes) } + logger.info('GET - /water/status - END') res.json(waterStatus) }); app.get("/water/config", async function (req, res, next) { + logger.info('GET - /water/config - START') const response = await post.postRequest('{}', 'poll') const water = response.updateData.zones[1].config + logger.info('GET - /water/config - END') res.json(water) }); app.get("/water/switch", async function (req, res, next) { + logger.info('GET - /water/switch - START') const response = await post.postRequest('{}', 'poll') - console.log('switch status start') const water = response.updateData.zones[1].status let on = await common.heatingOn(water) if (on == true) { @@ -42,8 +49,8 @@ module.exports = function (app) { let switchStatus = { "switch": on } - console.log(switchStatus) - console.log('switch status end') + logger.info(switchStatus) + logger.info('GET - /water/switch - END') res.json(switchStatus) }); diff --git a/routes/water_post.js b/routes/water_post.js index 331161b..ee916f0 100644 --- a/routes/water_post.js +++ b/routes/water_post.js @@ -1,9 +1,13 @@ const post = require('../lib/post') const common = require('../lib/common') +let logger = require('perfect-logger') + module.exports = function (app) { app.post("/water/switch", async function (req, res, next) { + logger.info('POST - /water/switch - START') if (req.body.switch === 1) { + logger.info('POST - /heating/switch - ON - START') try { await post.postRequest('{"zoneId":[1],"setPoint": 255,"durationMinutes": 90}', 'apply_timer') const response = await post.postRequest('{}', 'poll') @@ -13,26 +17,34 @@ module.exports = function (app) { let waterStatus = { "waterOn": JSON.stringify(on), } + logger.info('POST - /heating/switch - ON - COMPLETE') res.json(waterStatus) } catch (error) { - + logger.info('POST - /heating/switch - ON - FAILED') } } else if (req.body.switch === 0) { - await post.postRequest('{"zoneId":[1]}', 'cancel_timer') - const response = await post.postRequest('{}', 'poll') - const water = response.updateData.zones[1].status - const on = await common.heatingOn(water) + try { + logger.info('POST - /heating/switch - OFF - START') + await post.postRequest('{"zoneId":[1]}', 'cancel_timer') + 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), + let waterStatus = { + "waterOn": JSON.stringify(on), + } + logger.info('POST - /heating/switch - OFF - COMPLETE') + res.json(waterStatus) + } catch (error) { + logger.info('POST - /heating/switch - OFF - FAILED') } - res.json(waterStatus) } else { - res.json({ "switch": "break" }) + logger.info('POST - /heating/switch - FAILED') } }); app.post("/water/status", async function (req, res, next) { + logger.info('POST - /heating/status - START') const response = await post.postRequest('{}', 'poll') const water = response.updateData.zones[1].status const on = await common.heatingOn(water) @@ -43,6 +55,7 @@ module.exports = function (app) { "lastTimerSetPoint": JSON.stringify(water.lastTimerSetPoint), "lastTimerDurationMinutes": JSON.stringify(water.lastTimerDurationMinutes) } + logger.info('POST - /heating/status - END') res.json(waterStatus) }); } \ No newline at end of file diff --git a/server.js b/server.js index b7f1eef..c9aaaef 100644 --- a/server.js +++ b/server.js @@ -1,13 +1,23 @@ var express = require("express"); var app = express(); const bodyParser = require('body-parser'); +let logger = require('perfect-logger') -app.use(bodyParser.urlencoded({ extended: true })); - -app.use(bodyParser.json()) +logger.initialize('FrontEndDriver', { + logLevelFile: 0, // Log level for file + logLevelConsole: 0, // Log level for STDOUT/STDERR + logDirectory: 'logs/', // Log directory + customBannerHeaders: 'This is a custom banner' // Custom Log Banner +}); +app.use( + bodyParser.urlencoded({ + extended: true + }), + bodyParser.json(), +) require('./routes')(app); app.listen(2020, function () { - console.log("Server running on port 2020"); + logger.info("Server running on port 2020"); }); \ No newline at end of file diff --git a/test.js b/test.js index 5673f9f..d61cca6 100644 --- a/test.js +++ b/test.js @@ -3,18 +3,21 @@ var app = express(); const bodyParser = require('body-parser') +const logger = require('./lib/logger') + app.use( bodyParser.urlencoded({ extended: true - }) + }), + logger.logger ) app.use(bodyParser.json()) app.post('/endpoint', (req, res) => { - console.log(req.body) + logger.info(req.body) }) app.listen(2020, function () { - console.log("Server running on port 2020"); + logger.info("Server running on port 2020"); }); \ No newline at end of file