Files
old-tfm/app/server/api.js

71 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

// Global API configuration
var Api = new Restivus({
2016-02-14 16:34:11 +01:00
version: 'v1.0',
useDefaultAuth: true,
prettyJson: true
});
Api.addRoute('sensorData', {
authRequired: false
}, {
post: function() {
2016-02-14 16:34:11 +01:00
console.log("Body params", this.bodyParams);
SensorData.insert({
temperatureValue: parseFloat(this.bodyParams.temperatureValue),
humidityValue: parseFloat(this.bodyParams.humidityValue),
tankFull: this.bodyParams.tankFull,
owner: this.bodyParams.owner,
controllerId: this.bodyParams.controllerId,
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(),
2016-02-14 10:34:54 +01:00
config: {
2016-02-14 10:52:26 +01:00
draining_period_amount: 5,
2016-02-14 10:34:54 +01:00
draining_period_unit: 'minutes'
},
set_by: 'server'
});
};
var stateEntry = ControllerState.findOne({
controller_id: id,
});
return stateEntry;
}