Class: OpenC3::TriggerBase
- 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
-
#autonomic_topic ⇒ Object
readonly
Returns the value of attribute autonomic_topic.
-
#triggers ⇒ Object
readonly
Returns the value of attribute triggers.
Instance Method Summary collapse
-
#add(trigger:) ⇒ Object
Add a trigger from TriggerBase, must only be called once per trigger.
-
#enabled_triggers ⇒ Object
returns a Hash of ALL enabled Trigger objects.
-
#get_triggers(topic:) ⇒ Object
Get triggers to evaluate based on the topic.
-
#initialize(scope:) ⇒ TriggerBase
constructor
A new instance of TriggerBase.
-
#rebuild(triggers:) ⇒ Object
Rebuild the database lookup of all triggers in the group.
-
#remove(trigger:) ⇒ Object
remove a trigger from TriggerBase.
-
#topics ⇒ Object
get all topics group is working with.
-
#triggers_from(topic:) ⇒ Object
returns an Array of enabled Trigger objects that use a topic.
-
#triggers_with_roots ⇒ Object
returns an Array of enabled Trigger objects that have roots to other triggers.
-
#update(trigger:) ⇒ Object
update a trigger from TriggerBase.
-
#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).
Constructor Details
#initialize(scope:) ⇒ TriggerBase
Returns a new instance of TriggerBase.
83 84 85 86 87 88 89 90 |
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 83 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_topic ⇒ Object (readonly)
Returns the value of attribute autonomic_topic.
81 82 83 |
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 81 def autonomic_topic @autonomic_topic end |
#triggers ⇒ Object (readonly)
Returns the value of attribute triggers.
81 82 83 |
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 81 def triggers @triggers end |
Instance Method Details
#add(trigger:) ⇒ Object
Add a trigger from TriggerBase, must only be called once per trigger
194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 194 def add(trigger:) @triggers_mutex.synchronize do @triggers[trigger['name']] = Marshal.load( Marshal.dump(trigger) ) end trigger = TriggerModel.from_json(trigger, name: trigger['name'], scope: trigger['scope']) @lookup_mutex.synchronize do trigger.generate_topics.each do | topic | @lookup[topic] ||= [] @lookup[topic] << trigger.name end end end |
#enabled_triggers ⇒ Object
returns a Hash of ALL enabled Trigger objects
124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 124 def enabled_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.enabled end return ret end |
#get_triggers(topic:) ⇒ Object
Get triggers to evaluate based on the topic. If the topic is equal to the autonomic topic it will return only triggers with roots
95 96 97 98 99 100 101 |
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 95 def get_triggers(topic:) if @autonomic_topic == topic return triggers_with_roots() else return triggers_from(topic: topic) end end |
#rebuild(triggers:) ⇒ Object
Rebuild the database lookup of all triggers in the group
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 177 def rebuild(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 | @lookup[topic] ||= [] @lookup[topic] << trigger.name end end end end |
#remove(trigger:) ⇒ Object
remove a trigger from TriggerBase
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 217 def remove(trigger:) topics = [] @triggers_mutex.synchronize do @triggers.delete(trigger['name']) model = TriggerModel.from_json(trigger, name: trigger['name'], scope: trigger['scope']) topics = model.generate_topics() TriggerModel.delete(name: trigger['name'], group: trigger['group'], scope: trigger['scope']) end @lookup_mutex.synchronize do topics.each do | topic | unless @lookup[topic].nil? @lookup[topic].delete(trigger['name']) @lookup.delete(topic) if @lookup[topic].empty? end end end end |
#topics ⇒ Object
get all topics group is working with
170 171 172 173 174 |
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 170 def topics @lookup_mutex.synchronize do return Marshal.load( Marshal.dump(@lookup.keys()) ) end end |
#triggers_from(topic:) ⇒ Object
returns an Array of enabled Trigger objects that use a topic
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 152 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 | data = Marshal.load( Marshal.dump(@triggers[trigger_name]) ) trigger = TriggerModel.from_json(data, name: data['name'], scope: data['scope']) ret << trigger if trigger.enabled end end return ret end |
#triggers_with_roots ⇒ Object
returns an Array of enabled Trigger objects that have roots to other triggers
138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 138 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.enabled && ! trigger.roots.empty? end return ret end |
#update(trigger:) ⇒ Object
update a trigger from TriggerBase
208 209 210 211 212 213 214 |
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 208 def update(trigger:) @triggers_mutex.synchronize do model = TriggerModel.from_json(trigger, name: trigger['name'], scope: trigger['scope']) model.update() @triggers[trigger['name']] = model.as_json(:allow_nan => true) 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)
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/openc3/microservices/trigger_group_microservice.rb', line 107 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.enabled trigger.disable() elsif value == 1 && trigger.state == false trigger.state = true elsif value == 0 && trigger.state == true trigger.state = false end @triggers[name] = trigger.as_json(:allow_nan => true) end end |