-
Notifications
You must be signed in to change notification settings - Fork 44
Simple restify example
bigkevmcd edited this page Oct 16, 2012
·
2 revisions
This provides a simple API...
To turn on a light with device id 0x101010/1 you can visit (or access using curl or wget).
http://localhost:3000/light/0x101010/1/on
and to turn it off...
http://localhost:3000/light/0x101010/1/off
var rfxcom = require('rfxcom'),
restify = require('restify'),
rfxtrx = new rfxcom.RfxCom('/dev/ttyUSB0', {debug: true}),
lightwave = new rfxcom.LightwaveRf(rfxtrx),
server = restify.createServer({
name: 'lightwave',
version: '0.0.1'
});
server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser());
server.get('/light/:device/:code/:option/', function (req, res, next) {
var deviceId = req.params.device + '/' + req.params.code,
statusCode = 200;
switch (req.params.option) {
case 'on':
lightwave.switchOn(deviceId);
console.log('Turning on light %s', deviceId);
break;
case 'off':
lightwave.switchOff(deviceId);
console.log('Turning off light %s', deviceId);
break;
default:
console.log('Unknown option', req.params.option);
statusCode = 400;
break;
}
res.send(statusCode);
return next();
});
rfxtrx.initialise(function (error) {
if (error) {
throw new Error("Unable to initialise the rfx device");
};
});
server.listen(8080, function () {
console.log('%s listening at %s', server.name, server.url);
});