server side done

This commit is contained in:
Senad Uka
2016-10-29 13:07:08 +02:00
parent d92e208221
commit 7afe902f3f
7 changed files with 61 additions and 16 deletions

View File

@@ -29,3 +29,4 @@ mfpierre:chartist-js
standard-minifier-css
standard-minifier-js
iron:router
accolver:twilio-meteor

View File

@@ -1,3 +1,4 @@
accolver:twilio-meteor@1.10.1
accounts-base@1.2.5
accounts-password@1.1.7
allow-deny@1.0.3

View File

@@ -23,5 +23,14 @@ Template.log.events({
Template.sensorData.helpers({
created_at_formatted: function() {
return moment(this.created_at).format("DD.MM.YYYY, HH:mm")
},
all_temperatures: function(temperatures) {
var result = '';
if (temperatures.length > 0) {
for (var i in temperatures) {
result += '' + parseFloat(temperatures[i]).toFixed(1) + ' °C ';
}
}
return result;
}
});

View File

@@ -1,3 +1,3 @@
<template name="sensorData">
<li>{{owner}} / <strong>{{temperatureValue}}°C</strong> / <strong>{{humidityValue}}%</strong> / Bačva puna: <strong>{{tankFull}}</strong> (L0:{{tankLevel0}}-L1:{{tankLevel1}}-L2:{{tankLevel2}}-L3:{{tankLevel3}}-L4:{{tankLevel4}}-full:{{tankFull}}) / {{created_at_formatted}}</li>
<li>{{owner}} / <strong>{{temperatureValue}}°C {{all_temperatures temperatures}}</strong> / <strong>{{humidityValue}}%</strong> / Bačva puna: <strong>{{tankFull}}</strong> (L0:{{tankLevel0}}-L1:{{tankLevel1}}-L2:{{tankLevel2}}-L3:{{tankLevel3}}-L4:{{tankLevel4}}-full:{{tankFull}}) / {{created_at_formatted}}</li>
</template>

View File

@@ -16,5 +16,9 @@ Router.route('/', function() {
});
Router.route('/farmalarm', function() {
if (Meteor.zoblak.client.accessible('alarm')) {
Session.set('templateName', 'alarm');
} else {
Session.set('templateName', 'no_access')
}
});

View File

@@ -24,6 +24,7 @@ Api.addRoute('sensorData', {
stopPumpingAt: this.bodyParams.stopPumpingAt,
owner: this.bodyParams.owner,
controllerId: this.bodyParams.controllerId,
lastBoxContact: new Date(),
created_at: new Date()
};
SensorData.insert(sensorObject);
@@ -31,6 +32,20 @@ Api.addRoute('sensorData', {
}
});
Api.addRoute('alarm/:id/phonePing', {
authRequired: false
}, {
post: function() {
return ControllerState.update({
controller_id: this.urlParams.id
}, {
'$set': {
'lastPhoneContact': new Date(),
}
});
}
});
reactToSensorData = function(nextSensorReading) {
console.log("reacting to sensor");

View File

@@ -212,12 +212,15 @@ function reactToAlarmData(controller_id) {
var tooHot = config.maxTemperature && (maxTemperature(reading.temperatures) > config.maxTemperature);
var minutesSinceLastBoxContact = state.lastBoxContact ? moment(state.lastBoxContact).diff(moment(new Date()), 'minutes') : -1;
var minutesSinceLastBoxContact = reading.lastBoxContact ? moment(new Date()).diff(moment(reading.lastBoxContact), 'minutes') : -1;
var boxSilent = config.timeoutBox && minutesSinceLastBoxContact > config.timeoutBox;
var minutesSinceLastPhoneContact = state.lastPhoneContact ? moment(state.lastPhoneContact).diff(moment(new Date()), 'minutes') : -1;
var minutesSinceLastPhoneContact = state.lastPhoneContact ? moment(new Date()).diff(moment(state.lastPhoneContact), 'minutes') : -1;
var phoneSilent = config.timeoutPhone && minutesSinceLastPhoneContact > config.timeoutPhone;
console.log("lpc", state.lastPhoneContact);
console.log("mslpc", minutesSinceLastPhoneContact);
console.log("phoneSilent", phoneSilent);
if (tooCold || tooHot || boxSilent || phoneSilent) {
@@ -236,19 +239,30 @@ function soundTheAlarm(controller_id, tooCold, tooHot, boxSilent, phoneSilent) {
boxSilent: boxSilent,
phoneSilent: phoneSilent
};
console.log("Alarmiram", reason);
var firstTime = {};
if (!state.state.alarmTriggered) {
ControllerState.update(state._id, {
'$set': {
'state.alarmTriggered': true,
'state.alarmStarted': new Date(),
'state.alarmStopped': null,
'state.alarmReasons': reason
}
});
if (boxSilent || phoneSilent) {
sendAlarmingSms(reason, state.config.smsNumbers)
firstTime = {
'state.alarmStarted': new Date()
}
};
var smsSent = !!state.state.alarmSmsSent;
var needsToSendSms = !smsSent && (boxSilent || phoneSilent);
var sendSmsPart = needsToSendSms ? { 'state.alarmSmsSent': true } : {};
ControllerState.update(state._id, {
'$set': Object.assign({
'state.alarmTriggered': true,
'state.alarmStopped': null,
'state.alarmReasons': reason
}, firstTime, sendSmsPart)
});
if (needsToSendSms) {
sendAlarmingSms(reason, state.config.smsNumbers)
}
}
@@ -258,7 +272,7 @@ function sendAlarmingSms(reason, numbers) {
twilio = Twilio('AC10d7ed0bf54c1be4b1cd7133130e63f4', 'e133d3f02a69b79e93ad9ca1d73517d1');
twilio.sendSms({
to: number, // Any number Twilio can deliver to
from: '+43676800104260', // A number you bought from Twilio and can use for outbound communication
from: '+447481345235', // A number you bought from Twilio and can use for outbound communication
body: 'Zoblak alarm! Pokrenite aplikaciju! HITNO!' // body of the SMS message
}, function(err, responseData) { //this function is executed when a response is received from Twilio
if (!err) { // "err" is an error received during the request, if any
@@ -280,7 +294,8 @@ function stopTheAlarm(controller_id, everythingIsBackToNormal = false) {
ControllerState.update(state._id, {
'$set': {
'state.alarmTriggered': false,
'state.alarmStopped': timeOfStopping
'state.alarmStopped': timeOfStopping,
'state.alarmSmsSent': false
}
});
}