homenode-node
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -12,2 +12,22 @@ var express = require('express'); | ||
function getDeviceIndex(id) { | ||
var idx=-1; | ||
devices.forEach(function(dev,i) { | ||
if( dev.id==id ) { | ||
idx=i; | ||
} | ||
}); | ||
return idx; | ||
} | ||
function getDeviceAction(device,actionname) { | ||
var action; | ||
device.actions.forEach(function(a) { | ||
if( a.name == actionname ) { | ||
action=a; | ||
} | ||
}); | ||
return action; | ||
} | ||
function addDevice(device) { | ||
@@ -19,9 +39,28 @@ devices.push(device); | ||
var getDevice = function(req,res) { | ||
var idx = getDeviceIndex(req.params.deviceid); | ||
if( idx==-1 ) { | ||
res.sendStatus(404); | ||
} else { | ||
res.send(devices[idx]); | ||
} | ||
} | ||
var executeDeviceAction = function(req,res) { | ||
var idx = getDeviceIndex(req.params.deviceid); | ||
if( idx==-1 ) { | ||
res.sendStatus(404); | ||
} else { | ||
var action = getDeviceAction(devices[idx],req.params.actionname); | ||
if( action ) { | ||
action.use(); | ||
res.sendStatus(200); | ||
} else { | ||
res.sendStatus(404); | ||
} | ||
} | ||
} | ||
function removeDevice(id) { | ||
var idx=-1; | ||
devices.forEach(function(dev,i) { | ||
if( dev.id==id ) { | ||
idx=i; | ||
} | ||
}); | ||
var idx=getDeviceIndex(id); | ||
if( idx!=-1 ) { | ||
@@ -64,2 +103,19 @@ devices.splice(idx,1); | ||
}, | ||
device: { | ||
path : "devices/:deviceid", | ||
description : "Gets a device by id", | ||
parameter : { | ||
deviceid : { description : "Device ID", path : true } | ||
}, | ||
use : getDevice | ||
}, | ||
deviceAction: { | ||
path : "devices/:deviceid/:actionname", | ||
description : "Executes a device action", | ||
parameter : { | ||
deviceid : { description : "Device ID", path : true }, | ||
actionname : { description : "Action name", path : true } | ||
}, | ||
use : executeDeviceAction | ||
}, | ||
heartbeat : { | ||
@@ -91,3 +147,3 @@ description : 'Heartbeat endpoint service', | ||
server.listen(port); | ||
log('Http node listener for ""'+name+'"" started on port '+port); | ||
log('Http node listener for "'+name+'" started on port '+port); | ||
@@ -94,0 +150,0 @@ master.init(port, id, name); |
{ | ||
"name": "homenode-node", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Homenode node library", | ||
@@ -5,0 +5,0 @@ "main": "homenode.js", |
9403
340