Class: OpenC3::TriggerModel
- Defined in:
- lib/openc3/models/trigger_model.rb
Overview
INPUT:
{
"group": "someGroup",
"left": {
"type": "item",
"target": "INST",
"packet": "ADCS",
"item": "POSX",
"valueType": "RAW",
},
"operator": ">",
"right": {
"type": "value",
"value": 690000,
}
}
Constant Summary collapse
- PRIMARY_KEY =
'__TRIGGERS__'.freeze
- ITEM_TYPE =
'item'.freeze
- LIMIT_TYPE =
'limit'.freeze
- FLOAT_TYPE =
'float'.freeze
- STRING_TYPE =
'string'.freeze
- REGEX_TYPE =
'regex'.freeze
- TRIGGER_TYPE =
'trigger'.freeze
Instance Attribute Summary collapse
-
#dependents ⇒ Object
readonly
Returns the value of attribute dependents.
-
#enabled ⇒ Object
readonly
Returns the value of attribute enabled.
-
#group ⇒ Object
readonly
Returns the value of attribute group.
-
#label ⇒ Object
Returns the value of attribute label.
-
#left ⇒ Object
Returns the value of attribute left.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#operator ⇒ Object
Returns the value of attribute operator.
-
#right ⇒ Object
Returns the value of attribute right.
-
#roots ⇒ Object
readonly
Returns the value of attribute roots.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#state ⇒ Object
Returns the value of attribute state.
Attributes inherited from Model
Class Method Summary collapse
-
.all(group:, scope:) ⇒ Array<Hash>
All the Key, Values stored under the name key.
- .create_unique_name(group:, scope:) ⇒ Object
-
.delete(name:, group:, scope:) ⇒ Object
Check dependents before delete.
-
.from_json(json, name:, scope:) ⇒ TriggerModel
Model generated from the passed JSON.
-
.get(name:, group:, scope:) ⇒ TriggerModel
Return the object with the name at.
-
.names(group:, scope:) ⇒ Array<String>
All the uuids stored under the name key.
Instance Method Summary collapse
-
#as_json(*a) ⇒ Hash
Generated from the TriggerModel.
-
#commit_roots(models) ⇒ Object
Persist dependent changes to root triggers.
- #create ⇒ Object
- #disable ⇒ Object
-
#generate_topics ⇒ Object
[“#@scope__DECOM__#{@target}__#@packet”].
-
#initialize(name:, scope:, group:, left:, operator:, right:, state: false, enabled: true, dependents: nil, label: nil, updated_at: nil) ⇒ TriggerModel
constructor
A new instance of TriggerModel.
-
#notify(kind:) ⇒ Object
-
update the redis stream / trigger topic that something has changed.
-
- #notify_disable ⇒ Object
- #notify_enable ⇒ Object
-
#to_s ⇒ String
Generated from the TriggerModel.
- #update ⇒ Object
- #update_dependents(dependent:, remove: false) ⇒ Object
- #validate_operand(operand:, right: false) ⇒ Object
- #validate_operator(operator:) ⇒ Object
-
#validate_roots ⇒ Object
Validate that all root triggers exist, but do not persist dependent changes yet.
Methods inherited from Model
#check_disable_erb, #deploy, #destroy, #destroyed?, #diff, filter, find_all_by_plugin, get_all_models, get_model, handle_config, set, store, store_queued, #undeploy
Constructor Details
#initialize(name:, scope:, group:, left:, operator:, right:, state: false, enabled: true, dependents: nil, label: nil, updated_at: nil) ⇒ TriggerModel
Returns a new instance of TriggerModel.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/openc3/models/trigger_model.rb', line 104 def initialize( name:, scope:, group:, left:, operator:, right:, state: false, enabled: true, dependents: nil, label: nil, updated_at: nil ) super("#{scope}#{PRIMARY_KEY}#{group}", name: name, scope: scope) @roots = [] @group = group @state = state @enabled = enabled @left = validate_operand(operand: left) @operator = validate_operator(operator: operator) @right = validate_operand(operand: right, right: true) @dependents = dependents @label = label @updated_at = updated_at selected_group = TriggerGroupModel.get(name: @group, scope: @scope) if selected_group.nil? raise TriggerInputError.new "failed to find group: '#{@group}'" end end |
Instance Attribute Details
#dependents ⇒ Object (readonly)
Returns the value of attribute dependents.
101 102 103 |
# File 'lib/openc3/models/trigger_model.rb', line 101 def dependents @dependents end |
#enabled ⇒ Object (readonly)
Returns the value of attribute enabled.
101 102 103 |
# File 'lib/openc3/models/trigger_model.rb', line 101 def enabled @enabled end |
#group ⇒ Object (readonly)
Returns the value of attribute group.
101 102 103 |
# File 'lib/openc3/models/trigger_model.rb', line 101 def group @group end |
#label ⇒ Object
Returns the value of attribute label.
102 103 104 |
# File 'lib/openc3/models/trigger_model.rb', line 102 def label @label end |
#left ⇒ Object
Returns the value of attribute left.
101 102 103 |
# File 'lib/openc3/models/trigger_model.rb', line 101 def left @left end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
101 102 103 |
# File 'lib/openc3/models/trigger_model.rb', line 101 def name @name end |
#operator ⇒ Object
Returns the value of attribute operator.
101 102 103 |
# File 'lib/openc3/models/trigger_model.rb', line 101 def operator @operator end |
#right ⇒ Object
Returns the value of attribute right.
101 102 103 |
# File 'lib/openc3/models/trigger_model.rb', line 101 def right @right end |
#roots ⇒ Object (readonly)
Returns the value of attribute roots.
101 102 103 |
# File 'lib/openc3/models/trigger_model.rb', line 101 def roots @roots end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
101 102 103 |
# File 'lib/openc3/models/trigger_model.rb', line 101 def scope @scope end |
#state ⇒ Object
Returns the value of attribute state.
101 102 103 |
# File 'lib/openc3/models/trigger_model.rb', line 101 def state @state end |
Class Method Details
.all(group:, scope:) ⇒ Array<Hash>
Returns All the Key, Values stored under the name key.
74 75 76 |
# File 'lib/openc3/models/trigger_model.rb', line 74 def self.all(group:, scope:) super("#{scope}#{PRIMARY_KEY}#{group}") end |
.create_unique_name(group:, scope:) ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/openc3/models/trigger_model.rb', line 54 def self.create_unique_name(group:, scope:) trigger_names = self.names(group: group, scope: scope) num = 1 # Users count with 1 unless trigger_names.empty? # Extract numeric suffixes and find the max to avoid lexicographic sort issues max_num = trigger_names.map { |name| name[4..-1].to_i }.max num = max_num + 1 end return "TRIG#{num}" end |
.delete(name:, group:, scope:) ⇒ Object
Check dependents before delete.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/openc3/models/trigger_model.rb', line 84 def self.delete(name:, group:, scope:) model = self.get(name: name, group: group, scope: scope) if model.nil? raise TriggerInputError.new "trigger #{group}:#{name} does not exist" end unless model.dependents.empty? raise TriggerError.new "#{group}:#{name} has dependents: #{model.dependents}" end model.roots.each do | trigger | trigger_model = self.get(name: trigger, group: group, scope: scope) trigger_model.update_dependents(dependent: name, remove: true) trigger_model.update() end Store.hdel("#{scope}#{PRIMARY_KEY}#{group}", name) # No notification as this is only called via trigger_controller which already notifies end |
.from_json(json, name:, scope:) ⇒ TriggerModel
Returns Model generated from the passed JSON.
292 293 294 295 296 |
# File 'lib/openc3/models/trigger_model.rb', line 292 def self.from_json(json, name:, scope:) json = JSON.parse(json, allow_nan: true, create_additions: true) if String === json raise "json data is nil" if json.nil? self.new(**json.transform_keys(&:to_sym), name: name, scope: scope) end |
.get(name:, group:, scope:) ⇒ TriggerModel
Return the object with the name at
66 67 68 69 70 71 |
# File 'lib/openc3/models/trigger_model.rb', line 66 def self.get(name:, group:, scope:) json = super("#{scope}#{PRIMARY_KEY}#{group}", name: name) unless json.nil? self.from_json(json, name: name, scope: scope) end end |
.names(group:, scope:) ⇒ Array<String>
Returns All the uuids stored under the name key.
79 80 81 |
# File 'lib/openc3/models/trigger_model.rb', line 79 def self.names(group:, scope:) super("#{scope}#{PRIMARY_KEY}#{group}") end |
Instance Method Details
#as_json(*a) ⇒ Hash
Returns generated from the TriggerModel.
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/openc3/models/trigger_model.rb', line 275 def as_json(*a) return { 'name' => @name, 'scope' => @scope, 'state' => @state, 'enabled' => @enabled, 'group' => @group, 'dependents' => @dependents, 'left' => @left, 'operator' => @operator, 'right' => @right, 'label' => @label, 'updated_at' => @updated_at, } end |
#commit_roots(models) ⇒ Object
Persist dependent changes to root triggers
202 203 204 |
# File 'lib/openc3/models/trigger_model.rb', line 202 def commit_roots(models) models.each { |model| model.update() } end |
#create ⇒ Object
206 207 208 209 210 211 212 213 214 215 |
# File 'lib/openc3/models/trigger_model.rb', line 206 def create unless Store.hget(@primary_key, @name).nil? raise TriggerInputError.new "existing trigger found: '#{@name}'" end models = validate_roots() @updated_at = Time.now.to_nsec_from_epoch Store.hset(@primary_key, @name, JSON.generate(as_json, allow_nan: true)) commit_roots(models) notify(kind: 'created') end |
#disable ⇒ Object
243 244 245 246 247 |
# File 'lib/openc3/models/trigger_model.rb', line 243 def disable notify_disable() @updated_at = Time.now.to_nsec_from_epoch Store.hset(@primary_key, @name, JSON.generate(as_json, allow_nan: true)) end |
#generate_topics ⇒ Object
- “#@scope__DECOM__#{@target}__#@packet”
250 251 252 253 254 255 256 257 258 259 |
# File 'lib/openc3/models/trigger_model.rb', line 250 def generate_topics topics = {} if @left['type'] == ITEM_TYPE topics["#{@scope}__DECOM__{#{left['target']}}__#{left['packet']}"] = 1 end if @right and @right['type'] == ITEM_TYPE topics["#{@scope}__DECOM__{#{right['target']}}__#{right['packet']}"] = 1 end return topics.keys end |
#notify(kind:) ⇒ Object
Returns [] update the redis stream / trigger topic that something has changed.
299 300 301 302 303 304 305 306 |
# File 'lib/openc3/models/trigger_model.rb', line 299 def notify(kind:) notification = { 'kind' => kind, 'type' => 'trigger', 'data' => JSON.generate(as_json, allow_nan: true), } AutonomicTopic.write_notification(notification, scope: @scope) end |
#notify_disable ⇒ Object
237 238 239 240 241 |
# File 'lib/openc3/models/trigger_model.rb', line 237 def notify_disable @enabled = false @state = false notify(kind: 'disabled') end |
#notify_enable ⇒ Object
232 233 234 235 |
# File 'lib/openc3/models/trigger_model.rb', line 232 def notify_enable @enabled = true notify(kind: 'enabled') end |
#to_s ⇒ String
Returns generated from the TriggerModel.
270 271 272 |
# File 'lib/openc3/models/trigger_model.rb', line 270 def to_s return "OpenC3::TriggerModel:#{@scope}:#{group}:#{@name})" end |
#update ⇒ Object
217 218 219 220 221 222 223 |
# File 'lib/openc3/models/trigger_model.rb', line 217 def update models = validate_roots() @updated_at = Time.now.to_nsec_from_epoch Store.hset(@primary_key, @name, JSON.generate(as_json, allow_nan: true)) commit_roots(models) # No notification as this is only called via trigger_controller which already notifies end |
#update_dependents(dependent:, remove: false) ⇒ Object
261 262 263 264 265 266 267 |
# File 'lib/openc3/models/trigger_model.rb', line 261 def update_dependents(dependent:, remove: false) if remove @dependents.delete(dependent) elsif @dependents.index(dependent).nil? @dependents << dependent end end |
#validate_operand(operand:, right: false) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/openc3/models/trigger_model.rb', line 145 def validate_operand(operand:, right: false) return operand if right and @operator.include?('CHANGE') unless operand.is_a?(Hash) raise TriggerInputError.new "invalid operand: #{operand}" end operand_types = [ITEM_TYPE, LIMIT_TYPE, FLOAT_TYPE, STRING_TYPE, REGEX_TYPE, TRIGGER_TYPE] unless operand_types.include?(operand['type']) raise TriggerInputError.new "invalid operand, type '#{operand['type']}' must be one of #{operand_types}" end if operand[operand['type']].nil? raise TriggerInputError.new "invalid operand, type value '#{operand['type']}' must be a key: #{operand}" end case operand['type'] when ITEM_TYPE # We don't need to check for 'item' because the above check already does it if operand['target'].nil? || operand['packet'].nil? || operand['valueType'].nil? raise TriggerInputError.new "invalid operand, must contain target, packet, item and valueType: #{operand}" end when TRIGGER_TYPE @roots << operand[operand['type']] end return operand end |
#validate_operator(operator:) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/openc3/models/trigger_model.rb', line 169 def validate_operator(operator:) operators = ['>', '<', '>=', '<=', '==', '!=', 'CHANGES', 'DOES NOT CHANGE'] trigger_operators = ['AND', 'OR'] if @roots.empty? && operators.include?(operator) return operator elsif !@roots.empty? && trigger_operators.include?(operator) return operator elsif operators.include?(operator) raise TriggerInputError.new "invalid operator for triggers: '#{operator}' must be one of #{trigger_operators}" else raise TriggerInputError.new "invalid operator: '#{operator}' must be one of #{operators}" end end |
#validate_roots ⇒ Object
Validate that all root triggers exist, but do not persist dependent changes yet. Returns the list of root trigger models that need updating.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/openc3/models/trigger_model.rb', line 185 def validate_roots @dependents = [] if @dependents.nil? models_to_update = [] @roots.each do | trigger | model = TriggerModel.get(name: trigger, group: @group, scope: @scope) if model.nil? raise TriggerInputError.new "failed to find dependent trigger: '#{@group}:#{trigger}'" end unless model.dependents.include?(@name) model.update_dependents(dependent: @name) models_to_update << model end end models_to_update end |