2016-01-24 09:12:10 +01:00
|
|
|
// Global API configuration
|
|
|
|
|
var Api = new Restivus({
|
|
|
|
|
useDefaultAuth: true,
|
|
|
|
|
prettyJson: true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Api.addRoute('sensorData', {
|
|
|
|
|
authRequired: false
|
|
|
|
|
}, {
|
|
|
|
|
post: function() {
|
|
|
|
|
SensorData.insert({
|
|
|
|
|
temperatureValue: parseFloat(this.bodyParams.temperatureValue),
|
|
|
|
|
humidityValue: parseFloat(this.bodyParams.humidityValue),
|
2016-01-25 19:38:21 +01:00
|
|
|
tankFull: this.bodyParams.tankFull,
|
2016-01-24 09:12:10 +01:00
|
|
|
owner: this.bodyParams.owner,
|
|
|
|
|
created_at: new Date()
|
|
|
|
|
});
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Api.addRoute('state/:id', {
|
|
|
|
|
authRequired: false
|
|
|
|
|
}, {
|
|
|
|
|
post: function() {
|
|
|
|
|
console.log("Body params", this.bodyParams);
|
|
|
|
|
return ControllerState.update({
|
|
|
|
|
controller_id: this.urlParams.id
|
|
|
|
|
}, {
|
|
|
|
|
'$set': {
|
|
|
|
|
'state.out_valve': this.bodyParams.out_valve,
|
|
|
|
|
'time': new Date(),
|
|
|
|
|
'set_by': 'client'
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
get: function() {
|
|
|
|
|
return stateOrDefault(this.urlParams.id).state;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function stateOrDefault(id) {
|
|
|
|
|
var stateEntry = ControllerState.findOne({
|
|
|
|
|
controller_id: id,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (stateEntry === undefined) {
|
|
|
|
|
stateEntry = ControllerState.insert({
|
|
|
|
|
controller_id: id,
|
|
|
|
|
state: {
|
|
|
|
|
out_valve: 'closed'
|
|
|
|
|
},
|
|
|
|
|
time: new Date(),
|
|
|
|
|
set_by: 'server'
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
var stateEntry = ControllerState.findOne({
|
|
|
|
|
controller_id: id,
|
|
|
|
|
});
|
|
|
|
|
return stateEntry;
|
|
|
|
|
}
|