alarm triggering / silencing works

This commit is contained in:
Senad Uka
2016-10-23 15:57:04 +02:00
parent 6108d75074
commit d92e208221
6 changed files with 60 additions and 23 deletions

View File

@@ -3,6 +3,12 @@
<div class="hello">
<h1> Temperatura </h1>
<div class="jumbotron text-center center-block" >
{{#with state}}
{{#if alarmTriggered}}
<img src="/images/alarm.gif" class="img-responsive center-block" id="alarm_image" />
<button id="stop_alarm" class="btn btn-danger"> <i class="fa fa-ban"></i> Prekini </button>
{{/if}}
{{/with}}
{{#with last_reading}}
<div class="huge_text"> {{ all_temperatures }}</div>
<div>{{pretty_time created_at}}</div>

View File

@@ -25,14 +25,17 @@ function last_sensor_reading() {
Template.alarm.helpers({
last_reading: last_sensor_reading,
state: function() {
return Meteor.zoblak.client.controller_state().state;
},
pretty_time: function(time) {
return moment(time).format("DD.MM.YYYY, HH:mm")
},
all_temperatures: function() {
var result="";
var result = "";
var temperatures = last_sensor_reading().temperatures;
for(var i in temperatures) {
for (var i in temperatures) {
result += '' + parseFloat(temperatures[i]).toFixed(1) + ' °C ';
}
return result;
@@ -42,6 +45,10 @@ Template.alarm.helpers({
Template.alarm.events({
'click #run_alarm_settings': function() {
Modal.show('alarm_settings');
},
'click #stop_alarm': function() {
let controller_id = Meteor.zoblak.client.controller_state().controller_id;
Meteor.call('stopTheAlarm', controller_id);
}
});

View File

@@ -23,3 +23,19 @@
cursor: pointer;
}
}
@media all and (orientation: portrait) {
#alarm_image {
width: 90%;
cursor: pointer;
}
}
@media all and (orientation: landscape) {
#alarm_image {
width: 40%;
cursor: pointer;
}
}

BIN
app/public/images/alarm.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 590 KiB

View File

@@ -97,20 +97,6 @@ reactToSensorData = function(nextSensorReading) {
}
}
Api.addRoute('alarm/:id/stop', {
authRequired: false
}, {
post: function() {
var state = Meteor.zoblak.server.controller_state(controller_id);
ControllerState.update(state._id, {
'$set': {
'state.alarmTriggered': false,
'state.alarmStopped': new Date()
}
});
}
});
Api.addRoute('state/:id', {
authRequired: false
}, {

View File

@@ -1,4 +1,3 @@
function setOutValveTo(controller_id, nextState) {
var state = Meteor.zoblak.server.controller_state(controller_id);
ControllerState.update(state._id, {
@@ -173,6 +172,12 @@ function saveAlarmSettings(controller_id, minTemperature, maxTemperature, timeou
});
}
// there are three states of alarm:
// 1. normal ( state.alarmTriggered: false, state.alarmStopped: null )
// 2. triggered ( state.alarmTriggered: true, state.alarmStopped: null )
// 3. silenced ( state.alarmTriggered: false, state.alarmStopped: (sometime) )
function reactToAlarmData(controller_id) {
var reading = last_sensor_reading(controller_id);
var state = Meteor.zoblak.server.controller_state(controller_id);
@@ -181,7 +186,7 @@ function reactToAlarmData(controller_id) {
var minTemperature = function(temperatures) {
// if it gets a lot colder than absolute zero we will
// we will have more problems than the bug in this code
if(size(temperatures) <= 0) return -1000;
if (temperatures.length <= 0) return -1000;
var minimal = parseFloat(temperatures[0]);
for (var i in temperatures) {
if (parseFloat(temperatures[i]) < minimal) {
@@ -193,7 +198,7 @@ function reactToAlarmData(controller_id) {
var maxTemperature = function(temperatures) {
// obviously - hell is not supported in this version
if(size(temperatures) <= 0) return 1000;
if (temperatures.length <= 0) return 1000;
var maximal = parseFloat(temperatures[0]);
for (var i in temperatures) {
if (parseFloat(temperatures[i]) > maximal) {
@@ -213,12 +218,17 @@ function reactToAlarmData(controller_id) {
var minutesSinceLastPhoneContact = state.lastPhoneContact ? moment(state.lastPhoneContact).diff(moment(new Date()), 'minutes') : -1;
var phoneSilent = config.timeoutPhone && minutesSinceLastPhoneContact > config.timeoutPhone;
if (tooCold || tooHot || boxSilent || phoneSilent) {
soundTheAlarm(tooCold, tooHot, boxSilent, phoneSilent);
var alarmSilenced = !!state.state.alarmStopped;
if (!alarmSilenced) soundTheAlarm(controller_id, tooCold, tooHot, boxSilent, phoneSilent);
} else {
stopTheAlarm(controller_id, true);
}
}
function soundTheAlarm(tooCold, tooHot, boxSilent, phoneSilent) {
function soundTheAlarm(controller_id, tooCold, tooHot, boxSilent, phoneSilent) {
var state = Meteor.zoblak.server.controller_state(controller_id);
var reason = {
tooHot: tooHot,
@@ -262,7 +272,18 @@ function sendAlarmingSms(reason, numbers) {
}
}
function stopTheAlarm(controller_id, everythingIsBackToNormal = false) {
// time of alarm stopped is reset so that scheduled job can raise the alarm
// again
var timeOfStopping = (everythingIsBackToNormal) ? null : new Date();
var state = Meteor.zoblak.server.controller_state(controller_id);
ControllerState.update(state._id, {
'$set': {
'state.alarmTriggered': false,
'state.alarmStopped': timeOfStopping
}
});
}
function last_sensor_reading(controller_id) {
var result = null;
@@ -293,5 +314,6 @@ Meteor.methods({
clearLog: clearLog,
saveControllerConfig: saveControllerConfig,
requestNewPicture: requestNewPicture,
saveAlarmSettings: saveAlarmSettings
saveAlarmSettings: saveAlarmSettings,
stopTheAlarm: stopTheAlarm
});