Module: RSMP::Components

Included in:
Site, SiteProxy
Defined in:
lib/rsmp/component/components.rb

Overview

Things shared between sites and site proxies

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#componentsObject (readonly)

Returns the value of attribute components.



4
5
6
# File 'lib/rsmp/component/components.rb', line 4

def components
  @components
end

#mainObject (readonly)

Returns the value of attribute main.



4
5
6
# File 'lib/rsmp/component/components.rb', line 4

def main
  @main
end

Instance Method Details

#add_component(component) ⇒ Object



40
41
42
# File 'lib/rsmp/component/components.rb', line 40

def add_component(component)
  @components[component.c_id] = component
end

#aggregated_status_changed(component, options = {}) ⇒ Object



11
# File 'lib/rsmp/component/components.rb', line 11

def aggregated_status_changed(component, options = {}); end

#check_main_component(settings) ⇒ Object

Raises:



33
34
35
36
37
38
# File 'lib/rsmp/component/components.rb', line 33

def check_main_component(settings)
  raise ConfigurationError, 'main component must be defined' unless settings['main'] && settings['main'].size >= 1
  return unless settings['main'].size > 1

  raise ConfigurationError, "only one main component can be defined, found #{settings['main'].keys.join(', ')}"
end

#clear_alarm_timestampsObject



79
80
81
# File 'lib/rsmp/component/components.rb', line 79

def clear_alarm_timestamps
  @components.each_value(&:clear_alarm_timestamps)
end

#component_listObject



50
51
52
53
54
55
56
57
58
# File 'lib/rsmp/component/components.rb', line 50

def component_list
  @components.values.sort_by { |component| natural_sort_key(component.c_id) }.map do |component|
    {
      'id' => component.c_id,
      'type' => component.component_type || 'component',
      'name' => component.name || component.c_id
    }
  end
end

#find_component(component_id, build: true) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rsmp/component/components.rb', line 64

def find_component(component_id, build: true)
  component = @components[component_id]
  return component if component

  return unless build

  inferred_type = infer_component_type component_id
  component = inferred_type.new node: self, id: component_id
  @components[component_id] = component
  class_name = component.class.name.split('::').last
  class_name << ' component' unless %w[Component ComponentProxy].include?(class_name)
  log "Added component #{component_id} with the inferred type #{class_name}", level: :debug
  component
end

#infer_component_type(component_id) ⇒ Object

Raises:



60
61
62
# File 'lib/rsmp/component/components.rb', line 60

def infer_component_type(component_id)
  raise UnknownComponent, "Component #{component_id} mising and cannot infer type"
end

#initialize_componentsObject



6
7
8
9
# File 'lib/rsmp/component/components.rb', line 6

def initialize_components
  @components = {}
  @main = nil
end

#natural_sort_key(value) ⇒ Object



44
45
46
47
48
# File 'lib/rsmp/component/components.rb', line 44

def natural_sort_key(value)
  value.to_s.split(/(\d+)/).map do |part|
    part.match?(/\A\d+\z/) ? part.to_i : part
  end
end

#setup_components(settings) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rsmp/component/components.rb', line 13

def setup_components(settings)
  return unless settings

  check_main_component settings
  settings.each_pair do |type, components_by_type|
    next unless components_by_type

    components_by_type.each_pair do |id, component_settings|
      component_settings ||= {}
      component = build_component(id: id, type: type, settings: component_settings)
      component.(
        type: component_settings['type'] || type,
        name: component_settings['name'] || id
      )
      @components[id] = component
      @main = @components[id] if type == 'main'
    end
  end
end