diff --git a/app/helpers/ReadingsMailer.rb b/app/helpers/ReadingsMailer.rb index 9b66efe..b4ca14d 100644 --- a/app/helpers/ReadingsMailer.rb +++ b/app/helpers/ReadingsMailer.rb @@ -1,38 +1,50 @@ require "mail" class ReadingsMailer - OPTS = { :address => "mail.van-halteren.net", - :port => 465, - :domain => 'van-halteren.net', - :user_name => 'aart@van-halteren.net', - :password => '', - :authentication => 'plain', - :enable_ssl => true} - - OPTS_NO_SSL_VERIFY = { - :openssl_verify_mode => OpenSSL::SSL::VERIFY_NONE, + + SSL_OPTS = { + :openssl_verify_mode => OpenSSL::SSL::VERIFY_NONE, } + + # IMAP_OPTS = { :address => "mail.van-halteren.net", + # :port => 993, + # :user_name => 'aart@van-halteren.net', + # :password => 'XXXXX', + # :openssl_verify_mode => OpenSSL::SSL::VERIFY_NONE, + # :enable_ssl => true + # } # # Class methods # class << self - def deliver - - mail = Mail.new do - delivery_method :smtp, OPTS_NO_SSL_VERIFY - to 'aart@van-halteren.net' + def deliver + # Read SMTP options from smtp.yml + smtp_opts = YAML::load(File.open('config/smtp.yml')).symbolize_keys + smtp_opts.merge!(SSL_OPTS) if smtp_opts[:ssl] && smtp_opts[:ssl_verify_mode].eql?("none") + + # Fetch today's usage + usage_today = Reading.diff_on(Date.today) + + mail = Mail.new do + delivery_method :smtp, smtp_opts + to 'a.t.van.halteren@vu.nl' from 'SmartMeter ' - subject 'First multipart email sent with Mail' + subject 'SmartMeter report' text_part do - body 'This is plain text' + body "Summary for #{Date.today}\n + -------------------------------\n\n + Total kWH electricity consumed: #{usage_today[:total_kwh_consumed_high] + usage_today[:total_kwh_consumed_low]}\n + Total kWH electricity produced: #{usage_today[:total_kwh_produced_high] + usage_today[:total_kwh_produced_low]}\n + Total m3 gas consumed: #{usage_today[:total_m3_gas_consumed]}\n + " end - html_part do - content_type 'text/html; charset=UTF-8' - body '

This is HTML

' - end + # html_part do + # content_type 'text/html; charset=UTF-8' + # body '

This is HTML

' + # end end mail.deliver! diff --git a/app/models/reading.rb b/app/models/reading.rb index eaa8a00..b07260e 100644 --- a/app/models/reading.rb +++ b/app/models/reading.rb @@ -34,11 +34,19 @@ class Reading < ActiveRecord::Base # calculate difference with another reading # return a hash with differences (self - reading) def diff(reading) - { :total_kwh_consumed_high => (self.total_kwh_consumed_high - reading.total_kwh_consumed_high).round(1), - :total_kwh_consumed_low => (self.total_kwh_consumed_low - reading.total_kwh_consumed_low).round(1), - :total_kwh_produced_high => (self.total_kwh_produced_high - reading.total_kwh_produced_high).round(1), - :total_kwh_produced_low => (self.total_kwh_produced_low - reading.total_kwh_produced_low).round(1), - :total_m3_gas_consumed => (self.total_m3_gas_consumed - reading.total_m3_gas_consumed).round(3) } + if reading + { :total_kwh_consumed_high => (self.total_kwh_consumed_high - reading.total_kwh_consumed_high).round(1), + :total_kwh_consumed_low => (self.total_kwh_consumed_low - reading.total_kwh_consumed_low).round(1), + :total_kwh_produced_high => (self.total_kwh_produced_high - reading.total_kwh_produced_high).round(1), + :total_kwh_produced_low => (self.total_kwh_produced_low - reading.total_kwh_produced_low).round(1), + :total_m3_gas_consumed => (self.total_m3_gas_consumed - reading.total_m3_gas_consumed).round(3) } + else + { :total_kwh_consumed_high => self.total_kwh_consumed_high, + :total_kwh_consumed_low => self.total_kwh_consumed_low, + :total_kwh_produced_high => self.total_kwh_produced_high, + :total_kwh_produced_low => self.total_kwh_produced_low, + :total_m3_gas_consumed => self.total_m3_gas_consumed } + end end # @@ -58,7 +66,11 @@ class Reading < ActiveRecord::Base readings_on = day(date) first = readings_on.first last = readings_on.last - last.diff(first) + if last + last.diff(first) + else + { :total_kwh_consumed_high => 0, :total_kwh_consumed_low => 0, :total_kwh_produced_high => 0, :total_kwh_produced_low => 0, :total_m3_gas_consumed => 0 } + end end end end