Class: RoomConfiguration::OperatingCostsCalculator

Inherits:
Object
  • Object
show all
Defined in:
app/services/room_configuration/operating_costs_calculator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(room_configuration, options = {}) ⇒ OperatingCostsCalculator

Returns a new instance of OperatingCostsCalculator.

Parameters:

  • room_configuration (RoomConfiguration)

    the room configuration to calculate operating costs for

  • options (Hash) (defaults to: {})

    configurable options

Options Hash (options):

  • logger (Logger)

    logger to use (defaults to Rails.logger)



24
25
26
27
28
# File 'app/services/room_configuration/operating_costs_calculator.rb', line 24

def initialize(room_configuration, options = {})
  @options = options
  @room_configuration = room_configuration
  @logger = options[:logger] || Rails.logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



3
4
5
# File 'app/services/room_configuration/operating_costs_calculator.rb', line 3

def logger
  @logger
end

#resultsObject (readonly)

Returns the value of attribute results.



3
4
5
# File 'app/services/room_configuration/operating_costs_calculator.rb', line 3

def results
  @results
end

#room_configurationObject (readonly)

Returns the value of attribute room_configuration.



3
4
5
# File 'app/services/room_configuration/operating_costs_calculator.rb', line 3

def room_configuration
  @room_configuration
end

Class Method Details

.get_monthly_operation_kwh(hl_avg_comfort, hl_avg_economy) ⇒ Object

res[:avg_num_hrs_per_month] = (RoomConfigurationConstants::AVG_DAYS_PER_MONTH[:week_day] + RoomConfigurationConstants::AVG_DAYS_PER_MONTH[:week_end_day]).to_f*24.0
res[:avg_num_comfort_hrs_per_month] = RoomConfigurationConstants::AVG_DAYS_PER_MONTH[:week_day]*RoomConfigurationConstants::HOURS_OF_OPERATION_PER_DAY[:comfort][:week_day] + RoomConfigurationConstants::AVG_DAYS_PER_MONTH[:week_end_day]*RoomConfigurationConstants::HOURS_OF_OPERATION_PER_DAY[:comfort][:week_end_day]
res[:avg_num_economy_hrs_per_month] = RoomConfigurationConstants::AVG_DAYS_PER_MONTH[:week_days]*RoomConfigurationConstants::HOURS_OF_OPERATION_PER_DAY[:economy][:week_day] + RoomConfigurationConstants::AVG_DAYS_PER_MONTH[:week_end_day]*RoomConfigurationConstants::HOURS_OF_OPERATION_PER_DAY[:economy][:week_end_day]



8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/services/room_configuration/operating_costs_calculator.rb', line 8

def self.get_monthly_operation_kwh(hl_avg_comfort, hl_avg_economy)
  # assumptions:
  # use average number of weekdays vs weekends per month, derived from averaging actual number in 4 year cycle
  monthly_kwh = 0.0
  %i[week_day week_end_day].each do |days_type|
    { comfort: hl_avg_comfort, economy: hl_avg_economy }.each do |k, hl|
      monthly_kwh += RoomConfigurationConstants::AVG_DAYS_PER_MONTH[days_type] *
                     RoomConfigurationConstants::HOURS_OF_OPERATION_PER_DAY[k][days_type] * (hl / RoomConfiguration::BTU_PER_HOUR_PER_WATT) / 1000.0
    end
  end
  monthly_kwh
end

Instance Method Details

#calculate_operating_costs_without_hlcObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/services/room_configuration/operating_costs_calculator.rb', line 45

def calculate_operating_costs_without_hlc
  if @room_configuration.heating_elements.to_a.any? && @room_configuration.installation_postal_code.present?
    temp_data = AverageMonthlyTemperature.get_from_postal_code(@room_configuration.installation_postal_code)
    return { status: 'error', message: "can't calculate operating costs, temperatures error: #{temp_data[:message]}" } if temp_data[:status] != :ok

    # get utility rate $ cost per kwH using room's location
    rate_data = ElectricityRate.get_from_postal_code(@room_configuration.installation_postal_code)
    return { status: 'error', message: "can't calculate operating costs, electricity rate error: #{rate_data[:message]}" } if rate_data[:status] != :ok

    avg_cost_per_kwh = rate_data[:average_rate]
    kw = @room_configuration.calculate_total_kilowatts
    operating_costs = []
    operating_hrs = []
    # here we assume a linear relationship temp diff of outside to desired ambient divided by desired ambient where desired comfort and economy is from the average and low in TEMPERATURE_OPTIONS
    avg_comfort_temp = RoomConfiguration::TEMPERATURE_OPTIONS.find { |to| to.first == 'Average' }.last
    avg_economy_temp = RoomConfiguration::TEMPERATURE_OPTIONS.find { |to| to.first == 'Low' }.last
    12.times do |month|
      tdiffcom = avg_comfort_temp - temp_data[:average_temperatures][month]
      tdiffcom = 0.0 if tdiffcom < 0.0
      tdiffcom = avg_comfort_temp if tdiffcom > avg_comfort_temp
      factorcom = tdiffcom / avg_comfort_temp
      tdiffeco = avg_economy_temp - temp_data[:average_temperatures][month]
      tdiffeco = 0.0 if tdiffeco < 0.0
      tdiffeco = avg_economy_temp if tdiffeco > avg_economy_temp
      factoreco = tdiffeco / avg_economy_temp
      hrs = (RoomConfigurationConstants::AVERAGE_HOURS[:avg_num_comfort_hrs_per_month] * factorcom) + (RoomConfigurationConstants::AVERAGE_HOURS[:avg_num_economy_hrs_per_month] * factoreco)
      operating_hrs << hrs
      operating_costs << (hrs * kw * avg_cost_per_kwh)
    end
    { status: 'ok', operating_cost_by_month: operating_costs, operating_cost_annual: operating_costs.sum.round(2), operating_cost_by_month_average: (operating_costs.sum / 12.0).round(2),
operating_cost_by_coldest_month: operating_costs.max.round(2), message: "assuming default SmartStat settings, yielding average operation of #{(operating_hrs.sum / 365.0).round(1)} hours per day annually, and coldest month operation of #{(operating_hrs.max / 30.4).round(1)} hours per day, based on average monthly temperatures and electricity rates in your area." }
  else
    { status: 'error', message: 'can\'t calculate operating costs' }
  end
end

#get_operating_costsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/services/room_configuration/operating_costs_calculator.rb', line 30

def get_operating_costs
  operating_cost_by_hlc = false
  operating_cost_by_hlc = true if @room_configuration.last_heat_loss && @room_configuration.last_heat_loss['operating_cost']
  if @room_configuration.allow_heat_loss_calculation? && @room_configuration.can_calculate_heat_loss? && @room_configuration.floor_type&.is_indoors?
    @room_configuration.calculate_heat_loss
    operating_cost_by_hlc = true if @room_configuration.last_heat_loss && @room_configuration.last_heat_loss['operating_cost']
  end
  if operating_cost_by_hlc
    { status: 'ok', operating_cost_by_month: @room_configuration.last_heat_loss['operating_cost'], operating_cost_annual: @room_configuration.last_heat_loss['operating_cost'].sum.round(2),
operating_cost_by_month_average: (@room_configuration.last_heat_loss['operating_cost'].sum / 12.0).round(2), operating_cost_by_coldest_month: @room_configuration.last_heat_loss['operating_cost'].max.round(2), message: "assuming default thermostat settings, and taking account the details entered for this room for insulation and openings, yielding average operation of #{(@room_configuration.last_heat_loss['operating_hrs'].sum / 365.0).round(1)} hours per day annually, and coldest month operation of #{(@room_configuration.last_heat_loss['operating_hrs'].max / 30.4).round(1)} hours per day, based on average monthly temperatures and electricity rates in your area." }
  else
    calculate_operating_costs_without_hlc
  end
end