Class: HeatingSystemCalculator::SnowMeltingControlsFinder

Inherits:
BaseItemFinder
  • Object
show all
Defined in:
app/services/heating_system_calculator/snow_melting_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) ⇒ SnowMeltingControlsFinder

Returns a new instance of SnowMeltingControlsFinder.



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

def initialize(options)
  @control_set = options[:control_set]
  @sensor_set = options[:sensor_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/snow_melting_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/snow_melting_controls_finder.rb', line 3

def error
  @error
end

Instance Method Details

#add_bundled_relay_panels?(sku, consolidate) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/services/heating_system_calculator/snow_melting_controls_finder.rb', line 55

def add_bundled_relay_panels?(sku, consolidate)
  return false if consolidate

  smc_bundle_hash = ItemConstants::SNOWMELT_CONTROL_BUNDLES.detect { |s| s[:sku] == sku }
  unless smc_bundle_hash
    # Log warning - SKU not in bundles may indicate outdated ItemConstants
    Rails.logger.warn("SnowMeltingControlsFinder: SKU '#{sku}' not found in SNOWMELT_CONTROL_BUNDLES")
    return false
  end

  is_manual_or_value = %w[Manual Value].include?(smc_bundle_hash[:name]) # Fixed typo: was 'Manuel'
  return false if is_manual_or_value

  true
end

#find_controls_for_elements(elements, volts) ⇒ Object



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
# File 'app/services/heating_system_calculator/snow_melting_controls_finder.rb', line 11

def find_controls_for_elements(elements, volts)
  @controls = []
  relay_panels = []
  total_amps = get_total_from_solution(elements, 'amps')
  # handle relay panels, number of poles is the number of parallel circuits, one per element, but need two when 208/240 v elements, since all controls are 120 v
  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 = { error_status: :relay_panel_not_found, error_message: "Required relay panel with SKU #{sku} not found." }
      return self
    end
    relay_panels << rp.merge({ 'qty' => qty }) if qty > 0
  end
  # Here if total amps > 16 or elements voltage is 240V, we add these to all control groups so just extract as non-optional
  total_amps = get_total_from_solution(elements, 'amps')
  consolidate_relay_panels = (total_amps > 16.0 or volts >= 240)
  relay_panels.each { |rp| @controls << rp } if consolidate_relay_panels
  # ok, now controls
  @control_set.each do |control|
    additional_control_hash = { 'bundle_sku' => control['original_bundle_sku'], 'exclusive_group_type' => 'snow_melting_controls',
                                'exclusive_group_name' => 'Snow Melting Controls' }
    @controls << control.merge(additional_control_hash.merge('qty' => 1))
    sensor = get_sensor_for_control_sku(control['original_bundle_sku'])
    @controls << sensor.merge(additional_control_hash.merge('qty' => 1)) if sensor
    relay_panels.each { |rp| controls << rp.merge(additional_control_hash) } if add_bundled_relay_panels?(control['original_bundle_sku'], consolidate_relay_panels)
  end
  self
end

#get_sensor_for_control_sku(sku) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'app/services/heating_system_calculator/snow_melting_controls_finder.rb', line 44

def get_sensor_for_control_sku(sku)
  sensor = nil
  smc_bundle_hash = ItemConstants::SNOWMELT_CONTROL_BUNDLES.detect { |s| s[:sku] == sku }
  if smc_bundle_hash and sensor_sku = smc_bundle_hash[:sensor_sku]
    sensor = @sensor_set.detect { |p| p['sku'] == sensor_sku }
    # flag error if expected sensor not present
    @error = { error_status: :sensor_not_found, error_message: "Required sensor with SKU #{sensor_sku} not found." } unless sensor.present?
  end
  sensor
end