require "mail" class ReadingsMailer 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(date) # 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) consumption_today, production_today = Reading.consumed_and_produced_for_diff(usage_today) net_consumption_high, net_consumption_low = Reading.net_consumed_high_and_low_for_diff(usage_today) # Calculate costs for oxxio and easy energy c = Cost.new oxxio_normaal_cost, oxxio_dal_cost = c.oxxio_energy_cost(date.to_s,net_consumption_high,net_consumption_low) oxxio_cost = oxxio_normaal_cost+oxxio_dal_cost easy_cost = c.easy_energy_cost_barplot(date) # side effect: generates a PNG # rounding oxxio_cost = oxxio_cost.round(2) easy_cost = easy_cost.round(2) mail = Mail.new do delivery_method :smtp, smtp_opts to 'a.t.van.halteren@vu.nl' from 'SmartMeter ' subject "SmartMeter report for #{date}" text_part do body "Summary for #{date}\n -------------------------------\n\n Total kWH electricity consumed: #{consumption_today}\n Total kWH electricity produced: #{production_today}\n Total m3 gas consumed: #{usage_today[:total_m3_gas_consumed]}\n\n kWH cost (Oxxio): EUR #{ oxxio_cost }\n kWH cost (EasyEnergy): EUR #{ easy_cost }\n " end html_part do content_type 'text/html; charset=UTF-8' body "

Summary for #{date}

" + "

Total kWH electricity consumed: #{consumption_today}

" + "

Total kWH electricity produced: #{production_today}

" + "

Total m3 gas consumed: #{usage_today[:total_m3_gas_consumed]}

" + "
" + "

kWH cost (Oxxio): EUR #{ oxxio_cost}

" + "

kWH cost (EasyEnergy): EUR #{ easy_cost}

" end # add attachment filename = "easy_cost_%s.png" % date.strftime("%F") add_file :filename => filename, :content => File.read("plots/%s" % filename) end mail.deliver! end # default is current year def deliver_for_month(month, year=nil) return if (month <1 || month >12) year = Date.today.year unless year date = Date.parse("%s%.2d01" % [year,month]) date_str = date.strftime("%B %Y") # 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_month = Reading.diff_in_month(date) consumption_month, production_month = Reading.consumed_and_produced_for_diff(usage_month) net_consumption_normaal, net_consumption_dal = Reading.net_consumed_high_and_low_for_diff(usage_month) # Calculate costs for oxxio and easy energy c = Cost.new vat = 1 + c.vat_at(date) # cost with VAT, but without energiebelasting. oxxio_raw_cost = c.oxxio_energy_cost(date.to_s,net_consumption_normaal, net_consumption_dal, false).sum.round(2) easy_energy_raw_cost = c.easy_energy_raw_cost_in_month(date).compact.sum.round(2) # cost with VAT and with energiebelasting oxxio_cost = c.oxxio_energy_cost(date.to_s,net_consumption_normaal, net_consumption_dal).sum.round(2) easy_energy_cost = c.easy_energy_cost_in_month(date).compact.sum.round(2) # opslag Easy Energy easy_energy_opslag = (c.easy_energy_rate(date.to_s)*vat*(net_consumption_normaal + net_consumption_dal)).round(2) mail = Mail.new do delivery_method :smtp, smtp_opts to 'a.t.van.halteren@vu.nl' from 'SmartMeter ' subject "SmartMeter Month report for #{date_str}" text_part do body "Summary for #{date_str}\n -------------------------------\n\n Total kWH electricity consumed: #{consumption_month}\n Total kWH electricity produced: #{production_month}\n Total m3 gas consumed: #{usage_month[:total_m3_gas_consumed]}\n\n Levering kWH cost (Oxxio) : EUR #{ oxxio_raw_cost }\n Levering kWH cost (EasyEnergy): EUR #{ easy_energy_raw_cost }\n Total kWH cost (Oxxio) : EUR #{ oxxio_cost }\n Total kWH cost (EasyEnergy): EUR #{ easy_energy_cost }, inclusief opslag van EUR #{ easy_energy_opslag }\n " end html_part do content_type 'text/html; charset=UTF-8' body "

Summary for #{date_str}

" + "

Total kWH electricity consumed: #{consumption_month}

" + "

Total kWH electricity produced: #{production_month}

" + "

Total m3 gas consumed: #{usage_month[:total_m3_gas_consumed]}

" + "
" + "

Levering kWH cost (Oxxio): EUR #{ oxxio_raw_cost}

" + "

Levering kWH cost (EasyEnergy): EUR #{ easy_energy_raw_cost}

" + "

Total kWH cost (Oxxio): EUR #{ oxxio_cost}

" + "

Total kWH cost (EasyEnergy): EUR #{ easy_energy_cost} , inclusief opslag van EUR #{ easy_energy_opslag }

" end end mail.deliver! end end end