diff --git a/app/.meteor/packages b/app/.meteor/packages index 26572e0..ee2b624 100644 --- a/app/.meteor/packages +++ b/app/.meteor/packages @@ -29,3 +29,4 @@ mfpierre:chartist-js standard-minifier-css standard-minifier-js iron:router +accolver:twilio-meteor diff --git a/app/.meteor/versions b/app/.meteor/versions index 7e24b5d..97783e0 100644 --- a/app/.meteor/versions +++ b/app/.meteor/versions @@ -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 diff --git a/app/client/log.js b/app/client/log.js index ac9afd6..68837ad 100644 --- a/app/client/log.js +++ b/app/client/log.js @@ -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; } }); diff --git a/app/client/sensorData.html b/app/client/sensorData.html index 4d0b6ff..f42fc9b 100644 --- a/app/client/sensorData.html +++ b/app/client/sensorData.html @@ -1,3 +1,3 @@ diff --git a/app/client/startup.js b/app/client/startup.js index 8610e56..96f74fd 100644 --- a/app/client/startup.js +++ b/app/client/startup.js @@ -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') + } }); diff --git a/app/server/api.js b/app/server/api.js index a36b172..49c999c 100644 --- a/app/server/api.js +++ b/app/server/api.js @@ -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"); diff --git a/app/server/methods.js b/app/server/methods.js index 847d028..696d40b 100644 --- a/app/server/methods.js +++ b/app/server/methods.js @@ -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 } }); }