Files
smartmeter/app/helpers/ReadingsMailer.rb
2023-02-20 18:13:50 +01:00

74 lines
3.0 KiB
Ruby

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)
# Calculate costs for oxxio and easy energy
c = Cost.new
oxxio_cost = c.oxxio_energy_cost(date.to_s,usage_today[:total_kwh_consumed_high]-usage_today[:total_kwh_produced_high], usage_today[:total_kwh_consumed_low]-usage_today[:total_kwh_produced_low])
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 <aart@van-halteren.net>'
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 "<h1>Summary for #{date}</h1>" +
"<p>Total kWH electricity consumed: #{usage_today[:total_kwh_consumed_high] + usage_today[:total_kwh_consumed_low]}</p>" +
"<p>Total kWH electricity produced: #{usage_today[:total_kwh_produced_high] + usage_today[:total_kwh_produced_low]}</p>" +
"<p>Total m3 gas consumed: #{usage_today[:total_m3_gas_consumed]}</p>" +
"</br>" +
"<p>kWH cost (Oxxio): <b>EUR #{ oxxio_cost} </b></p>" +
"<p>kWH cost (EasyEnergy): <b>EUR #{ easy_cost} </b></p>"
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
end
end