finished scheduled close after opening

This commit is contained in:
Senad Uka
2016-02-14 10:34:54 +01:00
parent 5a4dd87e4f
commit f203119656
7 changed files with 99 additions and 25 deletions

View File

@@ -24,3 +24,4 @@ nimble:restivus
momentjs:moment
selaias:meteor-simpleweather
u2622:persistent-session
percolate:synced-cron

View File

@@ -57,6 +57,7 @@ nimble:restivus@0.8.4
npm-mongo@1.4.39_1
observe-sequence@1.0.7
ordered-dict@1.0.4
percolate:synced-cron@1.3.0
promise@0.5.1
random@1.0.5
rate-limit@1.0.0

View File

@@ -11,8 +11,8 @@
</div>
<div>
<button id="water_now">Zalij sada</button>
<button id="stop_water_now">Prekini zalijevanje</button>
<button id="water_now" class="{{ water_now_button_class }}">Zalij sada</button>
<button id="stop_water_now" class="{{ stop_button_class }}">Prekini zalijevanje</button>
</div>
</template>

View File

@@ -22,30 +22,36 @@ if (Meteor.isClient) {
if (state === 'opening') return "Otvara se";
if (state === 'closing') return "Zatvara se";
if (state === 'closed') return "Zatvoren";
},
water_now_button_class: function () {
var stateObject = controller_state();
if (stateObject.state && ( stateObject.state.out_valve === 'open' || stateObject.state.out_valve === 'opening' )) {
return 'hidden';
} else {
return '';
}
},
stop_button_class: function () {
var stateObject = controller_state();
if (stateObject.state && ( stateObject.state.out_valve === 'closed' || stateObject.state.out_valve === 'closing' )) {
return 'hidden';
} else {
return '';
}
}
});
Template.state.events({
'click #water_now': function() {
var state = controller_state();
ControllerState.update(state._id, {
'$set': {
'state.out_valve': 'opening',
'time': new Date(),
'set_by': 'server'
}
});
var controller_id = Session.get('controller_id');
Meteor.call('openOutValve',controller_id )
},
'click #stop_water_now': function() {
var state = controller_state();
ControllerState.update(state._id, {
'$set': {
'state.out_valve': 'closing',
'time': new Date(),
'set_by': 'server'
}
});
var controller_id = Session.get('controller_id');
Meteor.call('closeOutValve',controller_id )
}
});

View File

@@ -52,6 +52,11 @@ function stateOrDefault(id) {
out_valve: 'closed'
},
time: new Date(),
config: {
draining_period_amount: 1,
draining_period_unit: 'minutes'
},
set_by: 'server'
});
};

65
app/server/methods.js Normal file
View File

@@ -0,0 +1,65 @@
function controller_state(controller_id) {
var result = {}
if (controller_id) {
result = ControllerState.findOne({
controller_id: controller_id
});
}
if (!result) {
result = {}
};
return result;
};
function setOutValveTo(controller_id, nextState) {
var state = controller_state(controller_id);
ControllerState.update(state._id, {
'$set': {
'state.out_valve': nextState,
'time': new Date(),
'set_by': 'server'
}
});
}
function openOutValve(controller_id) {
setOutValveTo(controller_id, 'opening');
var state = controller_state(controller_id);
var config = state.config;
var jobName = "Close out valve " + state.controller_id + " after draining";
console.log("Opening valve ", controller_id, jobName);
SyncedCron.remove(jobName);
SyncedCron.add({
name: jobName,
schedule: function(parser) {
var time = moment().add(config.draining_period_amount, config.draining_period_unit).toDate();
return parser.recur().on(time).fullDate();
},
job: function() {
closeOutValve(controller_id);
}
});
console.log("Finished adding cron ", controller_id);
}
function closeOutValve(controller_id) {
var state = controller_state(controller_id);
var jobName = "Close out valve " + state.controller_id + " after draining";
console.log("Closing valve ", controller_id, jobName);
SyncedCron.remove(jobName);
setOutValveTo(controller_id, 'closing');
console.log("Finished clearing cron ", controller_id);
}
function clearLog() {
console.log("Removing sensor data");
SensorData.remove({});
}
Meteor.methods({
openOutValve: openOutValve,
closeOutValve: closeOutValve,
clearLog: clearLog
});

View File

@@ -1,14 +1,10 @@
if (Meteor.isServer) {
Meteor.startup(function() {
// code to run on server at startup
return Meteor.methods({
clearLog: function() {
return SensorData.remove({});
}
});
SyncedCron.start();
});
// Global API configuration
var Api = new Restivus({
useDefaultAuth: true,
@@ -30,5 +26,5 @@ if (Meteor.isServer) {
}
});
}