add logging and ignore logs
This commit is contained in:
		
							parent
							
								
									b0ef1fe89a
								
							
						
					
					
						commit
						b2f6c7c64a
					
				
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@ -1,2 +1,4 @@
 | 
			
		||||
node_modules
 | 
			
		||||
.vscode/launch.json
 | 
			
		||||
logger.lock
 | 
			
		||||
logs
 | 
			
		||||
@ -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')
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										14
									
								
								lib/logger.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								lib/logger.js
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -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');
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										5
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										5
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							@ -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",
 | 
			
		||||
 | 
			
		||||
@ -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"
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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)
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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)
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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)
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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,11 +17,14 @@ 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) {
 | 
			
		||||
            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
 | 
			
		||||
@ -26,13 +33,18 @@ module.exports = function (app) {
 | 
			
		||||
                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')
 | 
			
		||||
            }
 | 
			
		||||
        } 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)
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										18
									
								
								server.js
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								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");
 | 
			
		||||
});
 | 
			
		||||
							
								
								
									
										9
									
								
								test.js
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								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");
 | 
			
		||||
});
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user