Class: HeatingSystemCalculator::FloorHeatingControlsFinder

Inherits:
BaseItemFinder
  • Object
show all
Defined in:
app/services/heating_system_calculator/floor_heating_controls_finder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseItemFinder

#get_number_of_poles_from_consolidated_elements, #get_total_from_solution

Constructor Details

#initialize(options) ⇒ FloorHeatingControlsFinder

Returns a new instance of FloorHeatingControlsFinder.



5
6
7
8
# File 'app/services/heating_system_calculator/floor_heating_controls_finder.rb', line 5

def initialize(options)
  @control_set = options[:control_set]
  @power_set = options[:power_set]
end

Instance Attribute Details

#controlsObject (readonly)

Returns the value of attribute controls.



3
4
5
# File 'app/services/heating_system_calculator/floor_heating_controls_finder.rb', line 3

def controls
  @controls
end

#errorObject (readonly)

Returns the value of attribute error.



3
4
5
# File 'app/services/heating_system_calculator/floor_heating_controls_finder.rb', line 3

def error
  @error
end

Instance Method Details

#calculate_qty_of_power_components_from_elements(elements, power_capacity_amps) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/services/heating_system_calculator/floor_heating_controls_finder.rb', line 62

def calculate_qty_of_power_components_from_elements(elements, power_capacity_amps)
  # first need to explode out elements into an array of qty 1
  # DO NOT USE .dup on the array !!!! It will not so what you expect, see GOTCHA here: https://gist.github.com/boone/1010901
  single_elements = []
  elements.each do |e|
    single_elements << e.dup
  end
  additional_elements = []
  single_elements.each do |e|
    next unless (additional = (e['qty'] - 1)) > 0

    e['qty'] = 1
    additional.times do
      additional_elements << e
    end
  end
  single_elements += additional_elements
  # then, sort by amps
  single_elements.sort_by! { |e| e['amps'] }
  # now iterate and count the number of elements whose 'amps' load 'fit' within the power_capacity_amps
  qty = 0
  amps = 0.0
  single_elements.each do |e|
    if (amps + e['amps']) > power_capacity_amps
      qty += 1
      amps = e['amps']
    else
      amps += e['amps']
    end
  end
  qty
end

#find_controls_for_elements(elements, volts) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/services/heating_system_calculator/floor_heating_controls_finder.rb', line 10

def find_controls_for_elements(elements, volts)
  @controls = []
  # get all qualifying controls (tstats and timers minus power module master tstats)
  # get the dual voltage controls
  control_set_dual = @control_set.select { |c| c['is_dual_voltage'] }
  # now get the controls of the selected voltage
  control_set_by_voltage = @control_set.select { |c| c['volts'] == volts }
  # concatenate these together
  working_control_set = []
  working_control_set.concat(control_set_dual)
  working_control_set.concat(control_set_by_voltage)
  working_control_set.uniq! # in case someone enters a volts value for dual voltage controls
  if working_control_set.length < 1
    @error = { error_status: :no_controls_found, error_message: 'No matching controls found.' }
    return self
  else
    # check if we require nJoin power modules to handle high current system
    amps = get_total_from_solution(elements, 'amps')
    power_module_capacity = 15.0
    njoins_qty_threshold_to_use_relays = 4
    relay_panel_qty = 0
    njoins_qty = 0
    if 1.01 * amps >= power_module_capacity
      # at this stage we only need the nJoin for the amount over 15 amps
      power_module = @power_set.detect{|p| p['sku'].in?(ItemConstants::POWER_MODULE_SKUS)}
      relays = @power_set.select{|p| ItemConstants::RELAY_PANEL_SKUS.include?(p['sku'])}
      njoins_qty = calculate_qty_of_power_components_from_elements(elements, power_module_capacity) if power_module
      # here we consolidate the njoins outside of the control bundles
      if power_module.nil?
        @error = { error_status: :power_module_not_found, error_message: 'Required nJoin power module not found.' }
      elsif njoins_qty > 0 && njoins_qty < njoins_qty_threshold_to_use_relays
        @controls << power_module.merge({ 'qty' => njoins_qty, 'bundle_sku' => nil, 'exclusive_group_type' => 'thermostat', 'exclusive_group_name' => 'Thermostat' })
      elsif njoins_qty >= njoins_qty_threshold_to_use_relays
        njoins_qty = 0
        res = get_relay_panels_from_elements(elements)
        if res[:error_flag]
          @error = res[:error]
          return self
        else
          res[:relay_panels].each { |rp| @controls << rp }
        end
      end
    end
    working_control_set.each do |control|
      unless control['sku'].to_s.upcase.index('MYSA') && njoins_qty >= 1
        @controls << control.merge({ 'qty' => 1, 'bundle_sku' => control['sku'], 'exclusive_group_type' => 'thermostat', 'exclusive_group_name' => 'Thermostat' })
      end # prevent MYSA if njoins are required
    end
  end
  self
end

#get_relay_panels_from_elements(elements) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/services/heating_system_calculator/floor_heating_controls_finder.rb', line 95

def get_relay_panels_from_elements(elements)
  relay_panels = []
  error_flag = false
  error = nil
  poles = get_number_of_poles_from_consolidated_elements(elements)
  # get proper relay panels based on number of poles
  relay_panels_sku_qty = HeatingElementProductLineOption.get_relay_panels_from_poles(poles)
  relay_panels_sku_qty.each do |sku, qty|
    rp = @power_set.detect { |p| p['sku'] == sku }
    # flag error if expected relay panel not present
    if qty.positive? and !rp.present?
      error_flag = true
      error = { error_status: :relay_panel_not_found, error_message: "Required relay panel with SKU #{sku} not found." }
    else
      relay_panels << rp.merge({ 'qty' => qty }) if qty > 0
    end
  end
  {relay_panels: relay_panels, error_flag: error_flag, error: error}
end