Class: Item::Materials::Checks::ControlCapacity

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/item/materials/checks/control_capacity.rb

Defined Under Namespace

Classes: Result

Constant Summary collapse

CAPACITY_TOLERANCE =
1.05

Instance Method Summary collapse

Instance Method Details

#has_controls_and_heating_elements?(container) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'app/services/item/materials/checks/control_capacity.rb', line 14

def has_controls_and_heating_elements?(container)
  ( container.line_items.controls.present? || container.line_items.power_modules.present? || container.line_items.integration_kits.present? ) && container.line_items.heating_elements.present?
end

#process(container:, options: {}) ⇒ Object



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
61
62
# File 'app/services/item/materials/checks/control_capacity.rb', line 18

def process(container:, options:{})
  return Result.new(status: :skipped) unless qualify?(container)
  material_alerts = []

  # yes, so test if system load exceeds system control capacity (plus 5% fudge factor)
  tot_amps = container.calculate_total_amps
  if tot_amps > (container.control_capacity * CAPACITY_TOLERANCE)
    alert_name = <<-EOS
      Room #{container.reference_number}: total current load (#{tot_amps.round(1)} A) exceeds controls capacity (#{container.control_capacity.round(1)} A),
      please add relays or power modules and/or remove heating elements.
    EOS
    material_alerts << Item::Materials::Alert.new(name: alert_name, container: container)
  end
  # test if heating elements voltage is compatible with control voltage and/or with each other
  qty_mismatch = 0
  if container.control_voltage.present? # this will be 120 or 240v, nil for dual voltage
    qty_mismatch = container.line_items.heating_elements.to_a.sum{|li| (li.item.voltage.to_f != container.control_voltage.to_f) ? li.quantity : 0 }
    mismatch_snippet = "control voltage (#{container.control_voltage.round} V)"
  else # dual voltage, see if heating elements are at least all the same voltage
    num_pms = container.line_items.power_modules.to_a.sum{|li| li.quantity} # power modules allow mixing voltages, one voltage per power module
    num_rps = container.line_items.relay_panels.to_a.sum{|li| li.quantity} # relay panels also allow this, any relay panel will support at least 4 voltages, which covers all of 120, 240, 277 or more
    volts_arr = container.line_items.heating_elements.collect{|li| li.item.voltage}.uniq
    qtys_arr = volts_arr.map{|v| container.line_items.heating_elements.with_product_specification(:voltage, v).to_a.sum{|li| li.quantity}}.sort_by{|q| q}
    largest_qty = qtys_arr.last
    if num_rps == 0
      if num_pms > 0
        if volts_arr.length > num_pms
          qty_mismatch = container.line_items.heating_elements.to_a.sum{|li| li.quantity} - largest_qty
          mismatch_snippet = "the voltage of the other heating elements. That is, there are more heating element voltages than power modules"
        end
      else
        qty_mismatch = container.line_items.heating_elements.to_a.sum{|li| li.quantity} - largest_qty
        mismatch_snippet = "the voltage of the other heating elements"
      end
    end
  end
  alert_name = <<-EOS
    Room #{container.reference_number}: #{qty_mismatch} heating element(s) do not match #{mismatch_snippet}.
    Please ensure that heating element voltages are compatible in this system.
  EOS
  material_alerts << Item::Materials::Alert.new(name: alert_name, container: container) if qty_mismatch > 0


  Result.new(status: :ok, alerts: material_alerts)
end

#qualify?(container) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'app/services/item/materials/checks/control_capacity.rb', line 10

def qualify?(container)
  container.is_a?(RoomConfiguration) && has_controls_and_heating_elements?(container)
end