Fixed filling logic
This commit is contained in:
@@ -46,15 +46,15 @@ Template.state.helpers({
|
||||
var sensor = last_sensor_reading();
|
||||
var stateObject = controller_state();
|
||||
if (sensor) {
|
||||
if (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 (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 (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 (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 (sensor.tankFull === 0 && (stateObject.state.out_valve === 'closing')) return "/images/barrellStopWateringNotFull.png"
|
||||
if (parseInt(sensor.tankFull) === 0 && stateObject.state.in_valve === 'open' && stateObject.state.out_valve === 'closed') return "/images/barrellFillingUp.png";
|
||||
else if (parseInt(sensor.tankFull) === 1 && (stateObject.state.out_valve === 'closed')) return "/images/barrellFull.png";
|
||||
else if (parseInt(sensor.tankFull) === 1 && (stateObject.state.out_valve === 'opening')) return "/images/barrellStartWateringFull.png";
|
||||
else if (parseInt(sensor.tankFull) === 1 && (stateObject.state.out_valve === 'open')) return "/images/barrellWateringFull.png";
|
||||
else if (parseInt(sensor.tankFull) === 1 && (stateObject.state.out_valve === 'closing')) return "/images/barrellStopWateringFull.png";
|
||||
else if (parseInt(sensor.tankFull) === 0 && (stateObject.state.out_valve === 'closed')) return "/images/barrellNotFull.png";
|
||||
else if (parseInt(sensor.tankFull) === 0 && (stateObject.state.out_valve === 'opening')) return "/images/barrellStartWateringNotFull.png";
|
||||
else if (parseInt(sensor.tankFull) === 0 && (stateObject.state.out_valve === 'open')) return "/images/barrellWateringNotFull.png"
|
||||
else if (parseInt(sensor.tankFull) === 0 && (stateObject.state.out_valve === 'closing')) return "/images/barrellStopWateringNotFull.png"
|
||||
else return "/images/statusAmber.png";
|
||||
} else return "/images/statusRed.png";
|
||||
},
|
||||
|
||||
@@ -24,10 +24,12 @@ Api.addRoute('sensorData', {
|
||||
|
||||
|
||||
function reactToSensorData(nextSensorReading) {
|
||||
console.log("reacting to sensor");
|
||||
var controllerId = nextSensorReading.controllerId;
|
||||
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) {
|
||||
ControllerState.update({
|
||||
@@ -39,10 +41,7 @@ function reactToSensorData(nextSensorReading) {
|
||||
'set_by': 'server'
|
||||
}
|
||||
});
|
||||
}
|
||||
var shouldStopPumping = (state.in_valve === 'open' || state.in_valve === 'opening') && (parseInt(nextSensorReading.tankFull) === 1 || state.out_valve === 'open' || state.out_valve === 'opening');
|
||||
|
||||
if (shouldStopPumping) {
|
||||
} else if (shouldStopPumping) {
|
||||
ControllerState.update({
|
||||
controller_id: controllerId
|
||||
}, {
|
||||
@@ -102,3 +101,6 @@ function stateOrDefault(id) {
|
||||
});
|
||||
return stateEntry;
|
||||
}
|
||||
|
||||
Meteor.sharedFunctions = Meteor.sharedFunctions || {};
|
||||
Meteor.sharedFunctions.reactToSensorData = reactToSensorData;
|
||||
|
||||
@@ -40,7 +40,9 @@ function openOutValve(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) {
|
||||
@@ -51,6 +53,10 @@ function closeOutValve(controller_id) {
|
||||
SyncedCron.remove(jobName);
|
||||
setOutValveTo(controller_id, 'closing');
|
||||
console.log("Finished clearing cron ", controller_id);
|
||||
|
||||
console.log(Meteor.sharedFunctions);
|
||||
|
||||
Meteor.sharedFunctions.reactToSensorData(last_sensor_reading(controller_id));
|
||||
}
|
||||
|
||||
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({
|
||||
openOutValve: openOutValve,
|
||||
closeOutValve: closeOutValve,
|
||||
|
||||
@@ -5,26 +5,28 @@ if (Meteor.isServer) {
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
// 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),
|
||||
tankFull: this.bodyParams.tankFull,
|
||||
owner: this.bodyParams.owner,
|
||||
created_at: new Date()
|
||||
});
|
||||
return [];
|
||||
}
|
||||
});
|
||||
// Api.addRoute('sensorData', {
|
||||
// authRequired: false
|
||||
// }, {
|
||||
// post: function() {
|
||||
// SensorData.insert({
|
||||
// temperatureValue: parseFloat(this.bodyParams.temperatureValue),
|
||||
// humidityValue: parseFloat(this.bodyParams.humidityValue),
|
||||
// tankFull: this.bodyParams.tankFull,
|
||||
// owner: this.bodyParams.owner,
|
||||
// created_at: new Date()
|
||||
// });
|
||||
// return [];
|
||||
// }
|
||||
// });
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user