From c7aa6e5d926d6425aa93d5bd6b2dd0ccb224a77f Mon Sep 17 00:00:00 2001 From: Aart van Halteren Date: Sun, 23 Oct 2022 12:23:08 +0200 Subject: [PATCH] Email tariffs daily --- app/helpers/TariffsMailer.rb | 46 ++++++++++++++++++++++++++++++++++++ app/models/cost.rb | 2 +- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 app/helpers/TariffsMailer.rb diff --git a/app/helpers/TariffsMailer.rb b/app/helpers/TariffsMailer.rb new file mode 100644 index 0000000..3d1fc8a --- /dev/null +++ b/app/helpers/TariffsMailer.rb @@ -0,0 +1,46 @@ +require "mail" + +class TariffsMailer + + SSL_OPTS = { + :openssl_verify_mode => OpenSSL::SSL::VERIFY_NONE, + } + + + # + # 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") + + # Create png file + c = Cost.new + c.easy_energy_tariff_barplot(Date.today) + + mail = Mail.new do + delivery_method :smtp, smtp_opts + to 'a.t.van.halteren@vu.nl' + from 'SmartMeter ' + subject "EasyEnergy tariffs for #{date}" + + text_part do + body "Tariffs for #{date}\n" + end + + html_part do + content_type 'text/html; charset=UTF-8' + body "

Tariffs for #{date}" + end + + # add attachment + filename = "easy_tariff_%s.png" % date.strftime("%F") + add_file :filename => filename, :content => File.read("plots/%s" % filename) + end + + mail.deliver! + end + end +end \ No newline at end of file diff --git a/app/models/cost.rb b/app/models/cost.rb index 44c1967..b2369c3 100644 --- a/app/models/cost.rb +++ b/app/models/cost.rb @@ -90,7 +90,7 @@ class Cost title = "Tarief per kwH (incl. belastingen en BTW) - %s" % date.strftime("%A, %e %B %Y") xlabel = "uur" ylabel = "EUR" - GR.savefig("easy_tariff_%s.png" % date.strftime("%F"), title: title, xlabel: xlabel, ylabel: ylabel) + GR.savefig("plots/easy_tariff_%s.png" % date.strftime("%F"), title: title, xlabel: xlabel, ylabel: ylabel) end def easy_energy_cost_barplot(date)