Fixed filling logic

This commit is contained in:
Senad Uka
2016-03-26 07:11:52 +01:00
parent 26052a9afc
commit 534497e373
4 changed files with 62 additions and 30 deletions

View File

@@ -46,15 +46,15 @@ Template.state.helpers({
var sensor = last_sensor_reading(); var sensor = last_sensor_reading();
var stateObject = controller_state(); var stateObject = controller_state();
if (sensor) { if (sensor) {
if (sensor.tankFull === 0 && stateObject.state.in_valve === 'open' && stateObject.state.out_valve === 'closed') return "/images/barrellFillingUp.png"; if (parseInt(sensor.tankFull) === 0 && stateObject.state.in_valve === 'open' && stateObject.state.out_valve === 'closed') return "/images/barrellFillingUp.png";
else if (sensor.tankFull === 1 && (stateObject.state.out_valve === 'closed')) return "/images/barrellFull.png"; else if (parseInt(sensor.tankFull) === 1 && (stateObject.state.out_valve === 'closed')) return "/images/barrellFull.png";
else if (sensor.tankFull === 1 && (stateObject.state.out_valve === 'opening')) return "/images/barrellStartWateringFull.png"; else if (parseInt(sensor.tankFull) === 1 && (stateObject.state.out_valve === 'opening')) return "/images/barrellStartWateringFull.png";
else if (sensor.tankFull === 1 && (stateObject.state.out_valve === 'open')) return "/images/barrellWateringFull.png"; else if (parseInt(sensor.tankFull) === 1 && (stateObject.state.out_valve === 'open')) return "/images/barrellWateringFull.png";
else if (sensor.tankFull === 1 && (stateObject.state.out_valve === 'closing')) return "/images/barrellStopWateringFull.png"; else if (parseInt(sensor.tankFull) === 1 && (stateObject.state.out_valve === 'closing')) return "/images/barrellStopWateringFull.png";
else if (sensor.tankFull === 0 && (stateObject.state.out_valve === 'closed')) return "/images/barrellNotFull.png"; else if (parseInt(sensor.tankFull) === 0 && (stateObject.state.out_valve === 'closed')) return "/images/barrellNotFull.png";
else if (sensor.tankFull === 0 && (stateObject.state.out_valve === 'opening')) return "/images/barrellStartWateringNotFull.png"; else if (parseInt(sensor.tankFull) === 0 && (stateObject.state.out_valve === 'opening')) return "/images/barrellStartWateringNotFull.png";
else if (sensor.tankFull === 0 && (stateObject.state.out_valve === 'open')) return "/images/barrellWateringNotFull.png" else if (parseInt(sensor.tankFull) === 0 && (stateObject.state.out_valve === 'open')) return "/images/barrellWateringNotFull.png"
else if (sensor.tankFull === 0 && (stateObject.state.out_valve === 'closing')) return "/images/barrellStopWateringNotFull.png" else if (parseInt(sensor.tankFull) === 0 && (stateObject.state.out_valve === 'closing')) return "/images/barrellStopWateringNotFull.png"
else return "/images/statusAmber.png"; else return "/images/statusAmber.png";
} else return "/images/statusRed.png"; } else return "/images/statusRed.png";
}, },

View File

@@ -24,10 +24,12 @@ Api.addRoute('sensorData', {
function reactToSensorData(nextSensorReading) { function reactToSensorData(nextSensorReading) {
console.log("reacting to sensor");
var controllerId = nextSensorReading.controllerId; var controllerId = nextSensorReading.controllerId;
var state = stateOrDefault(controllerId).state; var state = stateOrDefault(controllerId).state;
var shouldStartPumping = (!state.in_valve || state.in_valve === 'closed') && ((parseInt(nextSensorReading.tankFull) === 0) && (state.out_valve === 'closed' || state.out_valve === 'closing')); console.log(nextSensorReading , state);
var shouldStartPumping = (!state.in_valve || state.in_valve === 'closed' || state.in_valve === 'closing') && ((parseInt(nextSensorReading.tankFull) === 0) && (state.out_valve === 'closed' || state.out_valve === 'closing'));
var shouldStopPumping = (state.in_valve === 'open' || state.in_valve === 'opening') && (parseInt(nextSensorReading.tankFull) === 1 || state.out_valve === 'open' || state.out_valve === 'opening');
if (shouldStartPumping) { if (shouldStartPumping) {
ControllerState.update({ ControllerState.update({
@@ -39,10 +41,7 @@ function reactToSensorData(nextSensorReading) {
'set_by': 'server' 'set_by': 'server'
} }
}); });
} } else if (shouldStopPumping) {
var shouldStopPumping = (state.in_valve === 'open' || state.in_valve === 'opening') && (parseInt(nextSensorReading.tankFull) === 1 || state.out_valve === 'open' || state.out_valve === 'opening');
if (shouldStopPumping) {
ControllerState.update({ ControllerState.update({
controller_id: controllerId controller_id: controllerId
}, { }, {
@@ -102,3 +101,6 @@ function stateOrDefault(id) {
}); });
return stateEntry; return stateEntry;
} }
Meteor.sharedFunctions = Meteor.sharedFunctions || {};
Meteor.sharedFunctions.reactToSensorData = reactToSensorData;

View File

@@ -40,7 +40,9 @@ function openOutValve(controller_id) {
closeOutValve(controller_id); closeOutValve(controller_id);
} }
}); });
console.log("Finished adding cron ", controller_id); console.log(Meteor.sharedFunctions);
Meteor.sharedFunctions.reactToSensorData(last_sensor_reading(controller_id));
} }
function closeOutValve(controller_id) { function closeOutValve(controller_id) {
@@ -51,6 +53,10 @@ function closeOutValve(controller_id) {
SyncedCron.remove(jobName); SyncedCron.remove(jobName);
setOutValveTo(controller_id, 'closing'); setOutValveTo(controller_id, 'closing');
console.log("Finished clearing cron ", controller_id); console.log("Finished clearing cron ", controller_id);
console.log(Meteor.sharedFunctions);
Meteor.sharedFunctions.reactToSensorData(last_sensor_reading(controller_id));
} }
function clearLog() { function clearLog() {
@@ -88,6 +94,28 @@ function saveControllerConfig(controller_id, time, days) {
}); });
} }
function last_sensor_reading(controller_id) {
var result = null;
if (controller_id) {
result = SensorData.find({
controllerId: controller_id
}, {
sort: {
created_at: -1
},
limit: 1
});
}
if (result && result.count() > 0) {
return result.fetch()[0];
} else {
return {}
}
}
Meteor.methods({ Meteor.methods({
openOutValve: openOutValve, openOutValve: openOutValve,
closeOutValve: closeOutValve, closeOutValve: closeOutValve,

View File

@@ -5,26 +5,28 @@ if (Meteor.isServer) {
}); });
// Global API configuration // Global API configuration
var Api = new Restivus({ var Api = new Restivus({
useDefaultAuth: true, useDefaultAuth: true,
prettyJson: true prettyJson: true
}); });
Api.addRoute('sensorData', { // Api.addRoute('sensorData', {
authRequired: false // authRequired: false
}, { // }, {
post: function() { // post: function() {
SensorData.insert({ // SensorData.insert({
temperatureValue: parseFloat(this.bodyParams.temperatureValue), // temperatureValue: parseFloat(this.bodyParams.temperatureValue),
humidityValue: parseFloat(this.bodyParams.humidityValue), // humidityValue: parseFloat(this.bodyParams.humidityValue),
tankFull: this.bodyParams.tankFull, // tankFull: this.bodyParams.tankFull,
owner: this.bodyParams.owner, // owner: this.bodyParams.owner,
created_at: new Date() // created_at: new Date()
}); // });
return []; // return [];
} // }
}); // });
} }