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

@@ -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
}
});
}