Method: OpenC3::LimitsEventTopic.write

Defined in:
lib/openc3/topics/limits_event_topic.rb

.write(event, scope:) ⇒ Object



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
# File 'lib/openc3/topics/limits_event_topic.rb', line 29

def self.write(event, scope:)
  case event[:type]
  when :LIMITS_CHANGE
    # The current_limits hash keeps only the current limits state of items
    # It is used by the API to determine the overall limits state
    field = "#{event[:target_name]}__#{event[:packet_name]}__#{event[:item_name]}"
    Store.hset("#{scope}__current_limits", field, event[:new_limits_state])

  when :LIMITS_SETTINGS
    # Limits updated in limits_api.rb to avoid circular reference to TargetModel
    unless sets(scope: scope).has_key?(event[:limits_set])
      Store.hset("#{scope}__limits_sets", event[:limits_set], 'false')
    end

  when :LIMITS_SET
    sets = sets(scope: scope)
    raise "Set '#{event[:set]}' does not exist!" unless sets.key?(event[:set])

    # Set all existing sets to "false"
    sets = sets.transform_values! { |_key, _value| "false" }
    sets[event[:set]] = "true" # Enable the requested set
    Store.hmset("#{scope}__limits_sets", *sets)
  else
    raise "Invalid limits event type '#{event[:type]}'"
  end

  Topic.write_topic("#{scope}__openc3_limits_events", event, '*', 1000)
end