Class: OpenC3::TriggerModel

Inherits:
Model show all
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
ITEM_VALUE_TYPES =
['RAW', 'CONVERTED', 'FORMATTED', 'WITH_UNITS'].freeze

Instance Attribute Summary collapse

Attributes inherited from Model

#plugin, #updated_at

Class Method Summary collapse

Instance Method Summary collapse

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: nil, state: false, enabled: true, dependents: nil, label: nil, updated_at: nil) ⇒ TriggerModel

Returns a new instance of TriggerModel.



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
133
# File 'lib/openc3/models/trigger_model.rb', line 105

def initialize(
  name:,
  scope:,
  group:,
  left:,
  operator:,
  right: nil,
  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

#dependentsObject (readonly)

Returns the value of attribute dependents.



102
103
104
# File 'lib/openc3/models/trigger_model.rb', line 102

def dependents
  @dependents
end

#enabledObject (readonly)

Returns the value of attribute enabled.



102
103
104
# File 'lib/openc3/models/trigger_model.rb', line 102

def enabled
  @enabled
end

#groupObject (readonly)

Returns the value of attribute group.



102
103
104
# File 'lib/openc3/models/trigger_model.rb', line 102

def group
  @group
end

#labelObject

Returns the value of attribute label.



103
104
105
# File 'lib/openc3/models/trigger_model.rb', line 103

def label
  @label
end

#leftObject

Returns the value of attribute left.



102
103
104
# File 'lib/openc3/models/trigger_model.rb', line 102

def left
  @left
end

#nameObject (readonly)

Returns the value of attribute name.



102
103
104
# File 'lib/openc3/models/trigger_model.rb', line 102

def name
  @name
end

#operatorObject

Returns the value of attribute operator.



102
103
104
# File 'lib/openc3/models/trigger_model.rb', line 102

def operator
  @operator
end

#rightObject

Returns the value of attribute right.



102
103
104
# File 'lib/openc3/models/trigger_model.rb', line 102

def right
  @right
end

#rootsObject (readonly)

Returns the value of attribute roots.



102
103
104
# File 'lib/openc3/models/trigger_model.rb', line 102

def roots
  @roots
end

#scopeObject (readonly)

Returns the value of attribute scope.



102
103
104
# File 'lib/openc3/models/trigger_model.rb', line 102

def scope
  @scope
end

#stateObject

Returns the value of attribute state.



102
103
104
# File 'lib/openc3/models/trigger_model.rb', line 102

def state
  @state
end

Class Method Details

.all(group:, scope:) ⇒ Array<Hash>

Returns All the Key, Values stored under the name key.

Returns:

  • (Array<Hash>)

    All the Key, Values stored under the name key



75
76
77
# File 'lib/openc3/models/trigger_model.rb', line 75

def self.all(group:, scope:)
  super("#{scope}#{PRIMARY_KEY}#{group}")
end

.create_unique_name(group:, scope:) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/openc3/models/trigger_model.rb', line 55

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.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/openc3/models/trigger_model.rb', line 85

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.

Returns:



312
313
314
315
316
# File 'lib/openc3/models/trigger_model.rb', line 312

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

Returns:



67
68
69
70
71
72
# File 'lib/openc3/models/trigger_model.rb', line 67

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.

Returns:

  • (Array<String>)

    All the uuids stored under the name key



80
81
82
# File 'lib/openc3/models/trigger_model.rb', line 80

def self.names(group:, scope:)
  super("#{scope}#{PRIMARY_KEY}#{group}")
end

Instance Method Details

#as_json(*a) ⇒ Hash

Returns generated from the TriggerModel.

Returns:

  • (Hash)

    generated from the TriggerModel



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/openc3/models/trigger_model.rb', line 295

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



221
222
223
# File 'lib/openc3/models/trigger_model.rb', line 221

def commit_roots(models)
  models.each { |model| model.update() }
end

#createObject



225
226
227
228
229
230
231
232
233
234
235
# File 'lib/openc3/models/trigger_model.rb', line 225

def create
  unless Store.hget(@primary_key, @name).nil?
    raise TriggerInputError.new "existing trigger found: '#{@name}'"
  end
  validate_trigger_items()
  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

#disableObject



263
264
265
266
267
# File 'lib/openc3/models/trigger_model.rb', line 263

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_topicsObject

["#@scope__DECOM__#{@target}__#@packet"]



270
271
272
273
274
275
276
277
278
279
# File 'lib/openc3/models/trigger_model.rb', line 270

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.

Returns:

  • [] update the redis stream / trigger topic that something has changed



319
320
321
322
323
324
325
326
# File 'lib/openc3/models/trigger_model.rb', line 319

def notify(kind:)
  notification = {
    'kind' => kind,
    'type' => 'trigger',
    'data' => JSON.generate(as_json, allow_nan: true),
  }
  AutonomicTopic.write_notification(notification, scope: @scope)
end

#notify_disableObject



257
258
259
260
261
# File 'lib/openc3/models/trigger_model.rb', line 257

def notify_disable
  @enabled = false
  @state = false
  notify(kind: 'disabled')
end

#notify_enableObject



252
253
254
255
# File 'lib/openc3/models/trigger_model.rb', line 252

def notify_enable
  @enabled = true
  notify(kind: 'enabled')
end

#to_sString

Returns generated from the TriggerModel.

Returns:

  • (String)

    generated from the TriggerModel



290
291
292
# File 'lib/openc3/models/trigger_model.rb', line 290

def to_s
  return "OpenC3::TriggerModel:#{@scope}:#{group}:#{@name})"
end

#updateObject



237
238
239
240
241
242
243
# File 'lib/openc3/models/trigger_model.rb', line 237

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



281
282
283
284
285
286
287
# File 'lib/openc3/models/trigger_model.rb', line 281

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



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/openc3/models/trigger_model.rb', line 146

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



170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/openc3/models/trigger_model.rb', line 170

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_rootsObject

Validate that all root triggers exist, but do not persist dependent changes yet. Returns the list of root trigger models that need updating.



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/openc3/models/trigger_model.rb', line 204

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

#validate_trigger_itemsObject

Validate the items hold a legal valueType and reference telemetry items which actually exist. Only called on create and update, never from initialize. Anything which can reject an already persisted trigger must live here rather than in validate_operand, because validate_operand runs on every from_json



188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/openc3/models/trigger_model.rb', line 188

def validate_trigger_items
  {'left' => @left, 'right' => @right}.each do |side, operand|
    next unless operand.is_a?(Hash) and operand['type'] == ITEM_TYPE
    unless ITEM_VALUE_TYPES.include?(operand['valueType'])
      raise TriggerInputError.new "invalid #{side} operand, valueType '#{operand['valueType']}' must be one of #{ITEM_VALUE_TYPES}"
    end
    begin
      TargetModel.packet_item(operand['target'], operand['packet'], operand['item'], scope: @scope)
    rescue StandardError => e
      raise TriggerInputError.new "invalid #{side} trigger: #{e.message}"
    end
  end
end