Class: OpenC3::TriggerBase

Inherits:
Object show all
Defined in:
lib/openc3/microservices/trigger_group_microservice.rb

Overview

Stored in the TriggerGroupShare this should be a thread safe hash that triggers will be added, updated, and removed from.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope:) ⇒ TriggerBase

Returns a new instance of TriggerBase.



78
79
80
81
82
83
84
85
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 78

def initialize(scope:)
  @scope = scope
  @autonomic_topic = "#{@scope}__openc3_autonomic".freeze
  @triggers_mutex = Mutex.new
  @triggers = Hash.new
  @lookup_mutex = Mutex.new
  @lookup = Hash.new
end

Instance Attribute Details

#autonomic_topicObject (readonly)

Returns the value of attribute autonomic_topic.



76
77
78
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 76

def autonomic_topic
  @autonomic_topic
end

Instance Method Details

#add(trigger:) ⇒ Object

add a trigger from TriggerBase



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 192

def add(trigger:)
  @triggers_mutex.synchronize do
    @triggers[trigger['name']] = Marshal.load( Marshal.dump(trigger) )
  end
  t = TriggerModel.from_json(trigger, name: trigger['name'], scope: trigger['scope'])
  @lookup_mutex.synchronize do
    t.generate_topics.each do | topic |
      if @lookup[topic].nil?
        @lookup[topic] = { t.name => 1 }
      else
        @lookup[topic][t.name] = 1
      end
    end
  end
end

#get_triggers(topic:) ⇒ Object

Get triggers to evaluate based on the topic. IF the topic is the equal to the autonomic topic it will return only triggers with roots



90
91
92
93
94
95
96
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 90

def get_triggers(topic:)
  if @autonomic_topic == topic
    return triggers_with_roots()
  else
    return triggers_from(topic: topic)
  end
end

#remove(trigger:) ⇒ Object

remove a trigger from TriggerBase



209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 209

def remove(trigger:)
  @triggers_mutex.synchronize do
    @triggers.delete(trigger['name'])
  end
  t = TriggerModel.from_json(trigger, name: trigger['name'], scope: trigger['scope'])
  @lookup_mutex.synchronize do
    t.generate_topics.each do | topic |
      unless @lookup[topic].nil?
        @lookup[topic].delete(t.name)
      end
    end
  end
end

#topicsObject

get all topics group is working with



165
166
167
168
169
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 165

def topics
  @lookup_mutex.synchronize do
    return Marshal.load( Marshal.dump(@lookup.keys()) )
  end
end

#triggersObject

returns a Hash of ALL active Trigger objects



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 119

def triggers
  val = nil
  @triggers_mutex.synchronize do
    val = Marshal.load( Marshal.dump(@triggers) )
  end
  ret = Hash.new
  val.each do | name, data |
    trigger = TriggerModel.from_json(data, name: data['name'], scope: data['scope'])
    ret[name] = trigger if trigger.active
  end
  return ret
end

#triggers_from(topic:) ⇒ Object

returns an Array of active Trigger objects that use a topic



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 147

def triggers_from(topic:)
  val = nil
  @lookup_mutex.synchronize do
    val = Marshal.load( Marshal.dump(@lookup[topic]) )
  end
  return [] if val.nil?
  ret = []
  @triggers_mutex.synchronize do
    val.each do | trigger_name, _v |
      data = Marshal.load( Marshal.dump(@triggers[trigger_name]) )
      trigger = TriggerModel.from_json(data, name: data['name'], scope: data['scope'])
      ret << trigger if trigger.active
    end
  end
  return ret
end

#triggers_with_rootsObject

returns an Array of active Trigger objects that have roots to other triggers



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 133

def triggers_with_roots
  val = nil
  @triggers_mutex.synchronize do
    val = Marshal.load( Marshal.dump(@triggers) )
  end
  ret = []
  val.each do | _name, data |
    trigger = TriggerModel.from_json(data, name: data['name'], scope: data['scope'])
    ret << trigger if trigger.active && ! trigger.roots.empty?
  end
  return ret
end

#update(triggers:) ⇒ Object

database update of all triggers in the group



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 172

def update(triggers:)
  @triggers_mutex.synchronize do
    @triggers = Marshal.load( Marshal.dump(triggers) )
  end
  @lookup_mutex.synchronize do
    @lookup = {@autonomic_topic => {}}
    triggers.each do | _name, data |
      trigger = TriggerModel.from_json(data, name: data['name'], scope: data['scope'])
      trigger.generate_topics.each do | topic |
        if @lookup[topic].nil?
          @lookup[topic] = { trigger.name => 1 }
        else
          @lookup[topic][trigger.name] = 1
        end
      end
    end
  end
end

#update_state(name:, value:) ⇒ Object

update trigger state after evaluated -1 (the value is considered an error used to disable the trigger)

0 (the value is considered as a false value)
1 (the value is considered as a true value)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 102

def update_state(name:, value:)
  @triggers_mutex.synchronize do
    data = @triggers[name]
    return unless data
    trigger = TriggerModel.from_json(data, name: data['name'], scope: data['scope'])
    if value == -1 && trigger.active
      trigger.deactivate()
    elsif value == 1 && trigger.state == false
      trigger.enable()
    elsif value == 0 && trigger.state == true
      trigger.disable()
    end
    @triggers[name] = trigger.as_json(:allow_nan => true)
  end
end