Skip to content

Commit

Permalink
Limit device status value
Browse files Browse the repository at this point in the history
  • Loading branch information
tnoborio committed Oct 14, 2016
1 parent 04276cb commit 70ff5c6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function setStatusHandler(valOrFunc) {
}

return function(req, res) {
var val = valFunc(req);
var val = Math.max(Math.min(valFunc(req), 0), 100);
device(req.params.device).setStatus(
val,
function() {
Expand All @@ -95,12 +95,12 @@ function setStatusHandler(valOrFunc) {
};
}

app.get('/:device/on', setStatusHandler("100"));
app.get('/:device/on', setStatusHandler(100));

app.get('/:device/off', setStatusHandler("0"));
app.get('/:device/off', setStatusHandler(0));

app.get('/:device/:value', setStatusHandler(function(req) {
return req.params.value;
return parseInt(req.params.value) || 0;
}));

var server = app.listen(process.env.PORT || 3000, function () {
Expand Down
2 changes: 1 addition & 1 deletion models/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Device.prototype.info = function(callback) {
redisDeviceKey(name, REDIS_KEY.DEVICE_UPDATED_AT)], function(err, vals) {
var code = vals[0];
var resetOnFetchStatus = vals[1] == "1";
var updatedAt = vals[2]? new Date(parseInt(vals[1])).toString(): "";
var updatedAt = vals[2]? new Date(parseInt(vals[2])).toString(): "";
callback({
name: name,
code: code,
Expand Down

0 comments on commit 70ff5c6

Please sign in to comment.