Class: OpenC3::Limits

Inherits:
Object show all
Defined in:
lib/openc3/packets/limits.rb

Overview

Limits uses PacketConfig to parse the command and telemetry configuration files. It provides the API layer which other classes use to access information about and manipulate limits. This includes getting, setting and checking individual limit items as well as manipulating limits groups.

Constant Summary collapse

LATEST_PACKET_NAME =
'LATEST'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Limits



34
35
36
# File 'lib/openc3/packets/limits.rb', line 34

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject



28
29
30
# File 'lib/openc3/packets/limits.rb', line 28

def config
  @config
end

Instance Method Details

#disable(target_name, packet_name, item_name) ⇒ Object

Disables limit checking for the specified item



72
73
74
# File 'lib/openc3/packets/limits.rb', line 72

def disable(target_name, packet_name, item_name)
  _get_packet(target_name, packet_name).disable_limits(item_name)
end

#enable(target_name, packet_name, item_name) ⇒ Object

Enables limit checking for the specified item



65
66
67
# File 'lib/openc3/packets/limits.rb', line 65

def enable(target_name, packet_name, item_name)
  _get_packet(target_name, packet_name).enable_limits(item_name)
end

#enabled?(target_name, packet_name, item_name) ⇒ Boolean

Checks whether the limits are enabled for the specified item



58
59
60
# File 'lib/openc3/packets/limits.rb', line 58

def enabled?(target_name, packet_name, item_name)
  _get_packet(target_name, packet_name).get_item(item_name).limits.enabled
end

#get(target_name, packet_name, item_name, limits_set = nil) ⇒ Array<limits_set, persistence, enabled, red_low, yellow_low, red_high, yellow_high, green_low (optional), green_high (optional)] Limits information

Get the limits for a telemetry item



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/openc3/packets/limits.rb', line 83

def get(target_name, packet_name, item_name, limits_set = nil)
  limits = _get_packet(target_name, packet_name).get_item(item_name).limits
  if limits.values
    if limits_set
      limits_set = limits_set.to_s.upcase.intern
    else
      limits_set = System.limits_set
    end
    limits_for_set = limits.values[limits_set]
    if limits_for_set
      return [limits_set, limits.persistence_setting, limits.enabled, limits_for_set[0], limits_for_set[1], limits_for_set[2], limits_for_set[3], limits_for_set[4], limits_for_set[5]]
    else
      return [nil, nil, nil, nil, nil, nil, nil, nil, nil]
    end
  else
    return [nil, nil, nil, nil, nil, nil, nil, nil, nil]
  end
end

#groupsHash(String, Array)



49
50
51
# File 'lib/openc3/packets/limits.rb', line 49

def groups
  return @config.limits_groups
end

#set(target_name, packet_name, item_name, red_low, yellow_low, yellow_high, red_high, green_low = nil, green_high = nil, limits_set = :CUSTOM, persistence = nil, enabled = true) ⇒ Array<limits_set, persistence, enabled, red_low, yellow_low, red_high, yellow_high, green_low (optional), green_high (optional)] Limits information

Set the limits for a telemetry item



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/openc3/packets/limits.rb', line 117

def set(target_name, packet_name, item_name, red_low, yellow_low, yellow_high, red_high, green_low = nil, green_high = nil, limits_set = :CUSTOM, persistence = nil, enabled = true)
  packet = _get_packet(target_name, packet_name)
  item = packet.get_item(item_name)
  limits = item.limits
  if limits_set
    limits_set = limits_set.to_s.upcase.intern
  else
    limits_set = System.limits_set
  end
  if !limits.values
    if limits_set == :DEFAULT
      limits.values = { :DEFAULT => [] }
    else
      raise "DEFAULT limits must be defined for #{target_name} #{packet_name} #{item_name} before setting limits set #{limits_set}"
    end
  end
  limits_for_set = limits.values[limits_set]
  unless limits_for_set
    limits.values[limits_set] = []
    limits_for_set = limits.values[limits_set]
  end
  limits_for_set[0] = red_low.to_f
  limits_for_set[1] = yellow_low.to_f
  limits_for_set[2] = yellow_high.to_f
  limits_for_set[3] = red_high.to_f
  limits_for_set.delete_at(5) if limits_for_set[5]
  limits_for_set.delete_at(4) if limits_for_set[4]
  if green_low && green_high
    limits_for_set[4] = green_low.to_f
    limits_for_set[5] = green_high.to_f
  end
  limits.enabled = enabled if not enabled.nil?
  limits.persistence_setting = Integer(persistence) if persistence
  packet.update_limits_items_cache(item)
  @config.limits_sets << limits_set
  @config.limits_sets.uniq!
  return [limits_set, limits.persistence_setting, limits.enabled, limits_for_set[0], limits_for_set[1], limits_for_set[2], limits_for_set[3], limits_for_set[4], limits_for_set[5]]
end

#set_state_color(target_name, packet_name, item_name, state_name, color) ⇒ Symbol?

Set the color for a telemetry item state



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/openc3/packets/limits.rb', line 165

def set_state_color(target_name, packet_name, item_name, state_name, color)
  packet = _get_packet(target_name, packet_name)
  item = packet.get_item(item_name)
  raise "Item '#{target_name} #{packet_name} #{item_name}' does not have any states" unless item.states
  state_name = state_name.to_s.upcase
  raise "State '#{state_name}' does not exist for item '#{target_name} #{packet_name} #{item_name}'" unless item.states.key?(state_name)
  if color.nil?
    # Clear the state color while keeping state_colors as a Hash so limits
    # checking (which indexes state_colors by value) continues to work.
    item.state_colors.delete(state_name) if item.state_colors
    packet.update_limits_items_cache(item)
    return nil
  end
  color = color.to_s.upcase.intern
  raise "Invalid state color '#{color}'. Must be one of #{PacketItem::STATE_COLORS.join(', ')}." unless PacketItem::STATE_COLORS.include?(color)
  item.state_colors ||= {}
  item.state_colors[state_name] = color
  item.limits.enabled = true
  packet.update_limits_items_cache(item)
  return color
end

#setsArray<Symbol>



44
45
46
# File 'lib/openc3/packets/limits.rb', line 44

def sets
  return @config.limits_sets
end

#warningsArray<String>



39
40
41
# File 'lib/openc3/packets/limits.rb', line 39

def warnings
  return @config.warnings
end