Method: OpenC3::LimitsEventTopic.write

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

.write(event, scope:) ⇒ Object



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/openc3/topics/limits_event_topic.rb', line 33

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

    field = "#{event[:target_name]}__#{event[:packet_name]}__#{event[:item_name]}"
    limits_settings = Store.hget("#{scope}__current_limits_settings", field)
    if limits_settings
      limits_settings = JSON.parse(limits_settings, :allow_nan => true, :create_additions => true)
    else
      limits_settings = {}
    end
    limits = {}
    limits['red_low'] = event[:red_low]
    limits['yellow_low'] = event[:yellow_low]
    limits['yellow_high'] = event[:yellow_high]
    limits['red_high'] = event[:red_high]
    limits['green_low'] = event[:green_low] if event[:green_low] && event[:green_high]
    limits['green_high'] = event[:green_high] if event[:green_low] && event[:green_high]
    limits_settings[event[:limits_set]] = limits
    limits_settings['persistence_setting'] = event[:persistence] if event[:persistence]
    limits_settings['enabled'] = event[:enabled] if not event[:enabled].nil?
    Store.hset("#{scope}__current_limits_settings", field, JSON.generate(limits_settings, :allow_nan => true))

  when :LIMITS_ENABLE_STATE
    field = "#{event[:target_name]}__#{event[:packet_name]}__#{event[:item_name]}"
    limits_settings = Store.hget("#{scope}__current_limits_settings", field)
    if limits_settings
      limits_settings = JSON.parse(limits_settings, :allow_nan => true, :create_additions => true)
    else
      limits_settings = {}
    end
    limits_settings['enabled'] = event[:enabled]
    Store.hset("#{scope}__current_limits_settings", field, JSON.generate(limits_settings, :allow_nan => true))

  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: JSON.generate(event, :allow_nan => true)}, '*', 1000)
end