|
| 1 | +'use strict' |
| 2 | + |
| 3 | +/** |
| 4 | + * Module Dependencies |
| 5 | + */ |
| 6 | +const config = require('./config'); |
| 7 | +const fs = require('fs'); |
| 8 | +const restify = require('restify'); |
| 9 | +const errors = require('restify-errors'); |
| 10 | +const bunyan = require('bunyan'); |
| 11 | +const winston = require('winston'); |
| 12 | +const bunyanWinston = require('bunyan-winston-adapter'); |
| 13 | +const rcswitch = require('rcswitch'); |
| 14 | +const path = require('path'); |
| 15 | + |
| 16 | +/** |
| 17 | + * Logging |
| 18 | + */ |
| 19 | +// workaround for https://github.com/winstonjs/winston/issues/875 |
| 20 | +if (!fs.existsSync(path.join(__dirname, "logs"))) { |
| 21 | + fs.mkdirSync(path.join(__dirname, "logs")); |
| 22 | +} |
| 23 | + |
| 24 | +global.logger = new winston.Logger({ |
| 25 | + transports: [ |
| 26 | + new winston.transports.File({ |
| 27 | + filename: path.join(__dirname, "logs", "info.log"), |
| 28 | + level: 'info', |
| 29 | + timestamp: true |
| 30 | + }) |
| 31 | + ] |
| 32 | +}) |
| 33 | + |
| 34 | +/** |
| 35 | + * Initialize Server |
| 36 | + */ |
| 37 | +global.server = restify.createServer({ |
| 38 | + name : config.name, |
| 39 | + version : config.version, |
| 40 | + log : bunyanWinston.createAdapter(logger), |
| 41 | +}) |
| 42 | + |
| 43 | +/** |
| 44 | + * Initialize 443mhz Transmitter |
| 45 | + */ |
| 46 | +rcswitch.enableTransmit(config.transmitter_pin); |
| 47 | + |
| 48 | +/** |
| 49 | + * Error Handling |
| 50 | + */ |
| 51 | +server.on('uncaughtException', (req, res, route, err) => { |
| 52 | + logger.error(err.stack) |
| 53 | + res.send(err) |
| 54 | +}); |
| 55 | + |
| 56 | +/** |
| 57 | + * Lift Server & Bind Routes |
| 58 | + */ |
| 59 | +server.listen(config.port, function() { |
| 60 | + logger.info('raspi-rcswitch-api Server listening on port ' + config.port); |
| 61 | + logger.info('Using configuration:'); |
| 62 | + logger.info('config.transmitter_pin: ' + config.transmitter_pin); |
| 63 | + logger.info('config.reties: ' + config.retries); |
| 64 | +}); |
| 65 | + |
| 66 | +server.get('/api/v1/switch/:systemCode/:unitCode/:state', switchFunction); |
| 67 | +server.get('/api/v1/switch/:deviceName/:state', mappedSwitchFunction); |
| 68 | + |
| 69 | +/** |
| 70 | + * Route functions |
| 71 | + */ |
| 72 | +function switchFunction(request, response, next){ |
| 73 | + // Argument validation |
| 74 | + if(request.params.systemCode.length != 5 || !isBinaryString(request.params.systemCode)) { |
| 75 | + return next(new errors.InvalidArgumentError("The systen code have to be a 5 character long binary string")); |
| 76 | + } |
| 77 | + |
| 78 | + if(isNaN(request.params.unitCode) || parseInt(request.params.unitCode) < 1 || parseInt(request.params.unitCode) > 4) { |
| 79 | + return next(new errors.InvalidArgumentError("The unit code have to be an number between 1 and 4.")); |
| 80 | + } |
| 81 | + |
| 82 | + if(!request.params.state || (request.params.state != 'on' && request.params.state != 'off')) { |
| 83 | + return next(new errors.InvalidArgumentError("The new state have to be either 'on' or 'off'.")); |
| 84 | + } |
| 85 | + |
| 86 | + switchUnit(request.params.systemCode, parseInt(request.params.unitCode), request.params.state); |
| 87 | + |
| 88 | + response.send({"systemCode:": request.params.systemCode, "systemCode:": request.params.unitCode, "state": request.params.state}); |
| 89 | + next(); |
| 90 | +} |
| 91 | + |
| 92 | +function mappedSwitchFunction(request, response, next){ |
| 93 | + var switchConfig = JSON.parse(fs.readFileSync('device_config.json', 'utf8')); |
| 94 | + |
| 95 | + // Argument validation |
| 96 | + if(switchConfig[request.params.deviceName]) { |
| 97 | + if(switchConfig[request.params.deviceName].systemCode.length != 5 || !isBinaryString(switchConfig[request.params.deviceName].systemCode)) { |
| 98 | + return next(new errors.InvalidArgumentError("The systen code have to be a 5 character long binary string")); |
| 99 | + } |
| 100 | + |
| 101 | + if(isNaN(switchConfig[request.params.deviceName].unitCode) || parseInt(switchConfig[request.params.deviceName].unitCode) < 1 || parseInt(switchConfig[request.params.deviceName].unitCode) > 4) { |
| 102 | + return next(new errors.InvalidArgumentError("The unit code have to be an number between 1 and 4.")); |
| 103 | + } |
| 104 | + } else { |
| 105 | + return next(new errors.InternalError("Configuration file 'device_config.json' doesn't define such a device or is missing.")); |
| 106 | + } |
| 107 | + |
| 108 | + if(!request.params.state || (request.params.state != 'on' && request.params.state != 'off')) { |
| 109 | + return next(new errors.InvalidArgumentError("The new state have to be either 'on' or 'off'.")); |
| 110 | + } |
| 111 | + |
| 112 | + switchUnit(switchConfig[request.params.deviceName].systemCode, parseInt(switchConfig[request.params.deviceName].unitCode), request.params.state); |
| 113 | + |
| 114 | + response.send('{"systemCode:" '+switchConfig[request.params.deviceName].systemCode+',"systemCode:" '+switchConfig[request.params.deviceName].unitCode+',"state": '+request.params.state+'}'); |
| 115 | + next(); |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * Logic functions |
| 120 | + */ |
| 121 | +function switchUnit(systemCode, unitCode, state) { |
| 122 | + logger.info('switchUnit: systemCode='+systemCode+', unitCode='+unitCode+', state='+state); |
| 123 | + |
| 124 | + var retries = config.retries ? config.retries : 1; |
| 125 | + var currentTry = 0; |
| 126 | + var intervalObj = setInterval((systemCode, unitCode) => { |
| 127 | + logger.debug('switchUnit: systemCode='+systemCode+', unitCode='+unitCode+', state='+state+', try='+currentTry); |
| 128 | + |
| 129 | + if(state === 'on') { |
| 130 | + rcswitch.switchOn(systemCode, unitCode); |
| 131 | + } else if(state === 'off') { |
| 132 | + rcswitch.switchOff(systemCode, unitCode); |
| 133 | + } |
| 134 | + |
| 135 | + if(++currentTry == retries) { |
| 136 | + clearInterval(intervalObj); |
| 137 | + } |
| 138 | + }, 100, systemCode, unitCode); |
| 139 | +} |
| 140 | + |
| 141 | +/** |
| 142 | + * Help functions |
| 143 | + */ |
| 144 | +function isBinaryString(input) { |
| 145 | + for (var i = 0; i < input.length; i++) { |
| 146 | + if(!(input[i] === '1' || input[i] === '0')) { |
| 147 | + return false; |
| 148 | + } |
| 149 | + } |
| 150 | + return true; |
| 151 | +} |
0 commit comments