|
var logger = require('winston');
var rpio = require("rpio")
var relay_status = [
{
code: "L01",
status: true,
pin: 35
}
]
module.exports.start = function(router, channel){
logger.info("Starting Relay controller module");
rpio.open(relay_status[0].pin, rpio.OUTPUT, rpio.LOW);
router.post('/switch_relay', function (req, res) {
logger.info("Received request for switching relays", req.body);
if(req.body != undefined){
var relay = req.body;
if(relay.code != undefined && relay.status != undefined){
var done = switchRelay(relay.code, relay.status);
if(done){
channel.emit('relay_switch', { code: relay.code, status: relay.status });
}
}
}
res.sendStatus(200);
});
}
module.exports.monitor = function(socket, channel){
socket.on('relay_switch_update', function (data) {
logger.info("relay_switch_update", data);
socket.emit("relay_switch", relay_status[0]);
});
}
var switchRelay = function(code, status){
switch (code) {
case "L01":
// switch on light relay
if(status == true) {
rpio.write(relay_status[0].pin, rpio.HIGH);
logger.debug("Switched L01 ON");
} else {
rpio.write(relay_status[0].pin, rpio.LOW);
logger.debug("Switched L01 OFF");
}
relay_status[0].status = status;
return true;
break;
default:
return false;
}
}
|