Merge pull request #5 from senaduka/scheduled_close_after_opening
finished scheduled close after opening
This commit is contained in:
@@ -24,3 +24,4 @@ nimble:restivus
|
|||||||
momentjs:moment
|
momentjs:moment
|
||||||
selaias:meteor-simpleweather
|
selaias:meteor-simpleweather
|
||||||
u2622:persistent-session
|
u2622:persistent-session
|
||||||
|
percolate:synced-cron
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ nimble:restivus@0.8.4
|
|||||||
npm-mongo@1.4.39_1
|
npm-mongo@1.4.39_1
|
||||||
observe-sequence@1.0.7
|
observe-sequence@1.0.7
|
||||||
ordered-dict@1.0.4
|
ordered-dict@1.0.4
|
||||||
|
percolate:synced-cron@1.3.0
|
||||||
promise@0.5.1
|
promise@0.5.1
|
||||||
random@1.0.5
|
random@1.0.5
|
||||||
rate-limit@1.0.0
|
rate-limit@1.0.0
|
||||||
|
|||||||
@@ -11,8 +11,8 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<button id="water_now">Zalij sada</button>
|
<button id="water_now" class="{{ water_now_button_class }}">Zalij sada</button>
|
||||||
<button id="stop_water_now">Prekini zalijevanje</button>
|
<button id="stop_water_now" class="{{ stop_button_class }}">Prekini zalijevanje</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -22,30 +22,36 @@ if (Meteor.isClient) {
|
|||||||
if (state === 'opening') return "Otvara se";
|
if (state === 'opening') return "Otvara se";
|
||||||
if (state === 'closing') return "Zatvara se";
|
if (state === 'closing') return "Zatvara se";
|
||||||
if (state === 'closed') return "Zatvoren";
|
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({
|
Template.state.events({
|
||||||
'click #water_now': function() {
|
'click #water_now': function() {
|
||||||
var state = controller_state();
|
var controller_id = Session.get('controller_id');
|
||||||
ControllerState.update(state._id, {
|
Meteor.call('openOutValve',controller_id )
|
||||||
'$set': {
|
|
||||||
'state.out_valve': 'opening',
|
|
||||||
'time': new Date(),
|
|
||||||
'set_by': 'server'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
'click #stop_water_now': function() {
|
'click #stop_water_now': function() {
|
||||||
var state = controller_state();
|
var controller_id = Session.get('controller_id');
|
||||||
ControllerState.update(state._id, {
|
Meteor.call('closeOutValve',controller_id )
|
||||||
'$set': {
|
|
||||||
'state.out_valve': 'closing',
|
|
||||||
'time': new Date(),
|
|
||||||
'set_by': 'server'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -52,6 +52,11 @@ function stateOrDefault(id) {
|
|||||||
out_valve: 'closed'
|
out_valve: 'closed'
|
||||||
},
|
},
|
||||||
time: new Date(),
|
time: new Date(),
|
||||||
|
config: {
|
||||||
|
draining_period_amount: 1,
|
||||||
|
draining_period_unit: 'minutes'
|
||||||
|
|
||||||
|
},
|
||||||
set_by: 'server'
|
set_by: 'server'
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
65
app/server/methods.js
Normal file
65
app/server/methods.js
Normal 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
|
||||||
|
});
|
||||||
@@ -1,14 +1,10 @@
|
|||||||
if (Meteor.isServer) {
|
if (Meteor.isServer) {
|
||||||
Meteor.startup(function() {
|
Meteor.startup(function() {
|
||||||
// code to run on server at startup
|
// code to run on server at startup
|
||||||
|
SyncedCron.start();
|
||||||
return Meteor.methods({
|
|
||||||
clearLog: function() {
|
|
||||||
return SensorData.remove({});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// Global API configuration
|
// Global API configuration
|
||||||
var Api = new Restivus({
|
var Api = new Restivus({
|
||||||
useDefaultAuth: true,
|
useDefaultAuth: true,
|
||||||
|
|||||||
Reference in New Issue
Block a user