adds constants and class variables

This commit is contained in:
Aart van Halteren
2022-01-09 19:42:11 +01:00
parent 6db550c11f
commit feb26239f6

View File

@@ -1,7 +1,14 @@
require 'open-uri'
UNKNOWN_READING = { :total_kwh_consumed_high => nil, :total_kwh_consumed_low => nil, :total_kwh_produced_high => nil, :total_kwh_produced_low => nil, :total_m3_gas_consumed => nil}
EASY_ENERGY_TARIFFS = {}
BATTERY_MAX_KWH = 10.0
MAX_CHARGE_KWH = 5.0
class Reading < ActiveRecord::Base
@@battery_kwh = 0.0
def eql_reading?(reading)
self.total_kwh_consumed_high == reading.total_kwh_consumed_high &&
self.total_kwh_consumed_low == reading.total_kwh_consumed_low &&
@@ -66,9 +73,6 @@ class Reading < ActiveRecord::Base
Reading.where("created_at > :begin AND created_at < :end", { :begin => date.to_date.beginning_of_day, :end => date.to_date.end_of_day})
end
EASY_ENERGY_TARIFFS = {}
# returns a hash with keys formatted "yyyy-mm-dd-hr" and values [usage, return]
# e.g. { 2021-12-16-06"=>[0.36058, 0.298] }
def easy_energy_tariffs(date)
@@ -166,46 +170,38 @@ class Reading < ActiveRecord::Base
end
end
UNKNOWN_READING = { :total_kwh_consumed_high => nil, :total_kwh_consumed_low => nil, :total_kwh_produced_high => nil, :total_kwh_produced_low => nil, :total_m3_gas_consumed => nil}
def format_cost(cost)
cost ? "EUR %0.03f" % cost : "EUR ?"
end
@battery_kwh = 0.0
BATTERY_MAX_KWH = 10.0
def charge_battery(kwh)
return 0.0 if kwh.nil?
if @battery_kwh + kwh <= BATTERY_MAX_KWH
@battery_kwh += kwh.to_f
p "Battery is now at %s kwh" % @battery_kwh
if @@battery_kwh + kwh <= BATTERY_MAX_KWH
@@battery_kwh += kwh.to_f
p "Battery is now at %s kwh" % @@battery_kwh
return kwh
else
old_kwh = @battery_kwh
@battery_kwh = BATTERY_MAX_KWH
p "Battery is now at %s kwh" % @battery_kwh
@@battery_kwh = BATTERY_MAX_KWH
p "Battery is now at %s kwh" % @@battery_kwh
return (BATTERY_MAX_KWH-old_kwh)
end
end
def discharge_battery(kwh)
return 0.0 if kwh.nil?
if @battery_kwh > kwh.to_f
@battery_kwh -= kwh.to_f
p "Battery is now at %s kwh" % @battery_kwh
if @@battery_kwh > kwh.to_f
@@battery_kwh -= kwh.to_f
p "Battery is now at %s kwh" % @@battery_kwh
return kwh
else
old_kwh = @battery_kwh
@battery_kwh = 0.0
p "Battery is now at %s kwh" % @battery_kwh
old_kwh = @@battery_kwh
@@battery_kwh = 0.0
p "Battery is now at %s kwh" % @@battery_kwh
return old_kwh
end
end
MAX_CHARGE_KWH = 7.0
def hours(date, year_shift=0)
hour_start = date.beginning_of_day
day_end = hour_start.advance(days: 1)