From 3ce0d3c45ae068e2a7741ef1e60e72933a379b83 Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Thu, 4 Sep 2014 19:24:33 -0700 Subject: [PATCH] Adding door alert emails (test mode) and adjusting door log graph view --- app/controllers/space_api_controller.rb | 25 +++++++++++++++++++++++-- app/mailers/door_mailer.rb | 12 ++++++++++++ app/views/door_logs/index.html.erb | 6 +++--- app/views/door_mailer/alert.html.erb | 5 +++++ config/routes.rb | 1 + 5 files changed, 44 insertions(+), 5 deletions(-) create mode 100755 app/mailers/door_mailer.rb create mode 100755 app/views/door_mailer/alert.html.erb diff --git a/app/controllers/space_api_controller.rb b/app/controllers/space_api_controller.rb index 30e75aa..6a76d91 100755 --- a/app/controllers/space_api_controller.rb +++ b/app/controllers/space_api_controller.rb @@ -1,8 +1,8 @@ class SpaceApiController < ApplicationController # Individually remove authorizing stuff since there is no SpaceApi model - authorize_resource :except => [:index, :access, :access_post] + authorize_resource :except => [:index, :access, :access_post, :alert_if_not_status] # User auth here happens via params, instead of form. - before_filter :authenticate_user!, :except => [:index, :access, :access_post] + before_filter :authenticate_user!, :except => [:index, :access, :access_post, :alert_if_not_status] def index @json = JSON.parse(Setting.space_api_json_template) @@ -102,4 +102,25 @@ class SpaceApiController < ApplicationController end + # Expect status to be "open" or "closed" + def alert_if_not_status + @expected_status = params['status'] + @status = DoorLog.show_status + + if !["open","closed"].include?(@expected_status) + @output = "USAGE: Specify an expected status (/alert_if_not/open or /alert_if_not/closed). Alert emails will be sent if status doesn't match." + elsif @expected_status.to_s == "open" && @status[:unlocked] == true + @output = "Unlocked Status is OK." + elsif @expected_status.to_s == "closed" && @status[:unlocked] == false + @output = "Unlocked Status is OK." + else + @output = "Unlocked Status is NOT OK. Alerting." + @output += " - Mail result: " + @output += DoorMailer.alert(@status).deliver.inspect + end + + response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate" + render :json => {response: @output, status: @status} + end + end diff --git a/app/mailers/door_mailer.rb b/app/mailers/door_mailer.rb new file mode 100755 index 0000000..22b4bee --- /dev/null +++ b/app/mailers/door_mailer.rb @@ -0,0 +1,12 @@ +class DoorMailer < ActionMailer::Base + default :from => "no-reply@heatsynclabs.org" + + def alert(status) + @url = "http://members.heatsynclabs.org" + @status = status + + mail(:to => 'will@heatsynclabs.org', + :subject => "HSL Doors") + end + +end diff --git a/app/views/door_logs/index.html.erb b/app/views/door_logs/index.html.erb index 183e878..63c29ee 100755 --- a/app/views/door_logs/index.html.erb +++ b/app/views/door_logs/index.html.erb @@ -12,8 +12,8 @@ $(function() { $( "#graph-dialog" ).dialog({ autoOpen: false, - height: 480, - width: 640, + height: 700, + width: 825, modal: true, }); @@ -130,7 +130,7 @@ function generatePlotBands(start,end){
-
+
<%= form_tag(nil, :method => :get) do %>