Class: OpenC3::ReactionModel

Inherits:
Model show all
Defined in:
lib/openc3/models/reaction_model.rb

Overview

{

  "description": "POSX greater than 200",
  "snooze": 300,
  "review": true,
  "triggers": [
    {
      "name": "TV0-1234",
      "group": "foo",
    }
  ],
  "actions": [
    {
      "type": "command",
      "value": "INST CLEAR",
    }
  ]
}

Constant Summary collapse

PRIMARY_KEY =
'__openc3__reaction'.freeze
COMMAND_REACTION =
'command'.freeze
SCRIPT_REACTION =
'script'.freeze

Instance Attribute Summary collapse

Attributes inherited from Model

#plugin, #updated_at

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#destroy, #destroyed?, filter, find_all_by_plugin, get_all_models, get_model, handle_config, set, store

Constructor Details

#initialize(name:, scope:, description:, snooze:, actions:, triggers:, active: true, review: true, snoozed_until: nil, username: nil, updated_at: nil) ⇒ ReactionModel

Returns a new instance of ReactionModel.



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/openc3/models/reaction_model.rb', line 162

def initialize(
  name:,
  scope:,
  description:,
  snooze:,
  actions:,
  triggers:,
  active: true,
  review: true,
  snoozed_until: nil,
  username: nil,
  updated_at: nil
)
  if name.nil? || scope.nil? || description.nil? || snooze.nil? || triggers.nil? || actions.nil?
    raise ReactionInputError.new "#{name}, #{scope}, #{description}, #{snooze}, #{triggers}, or #{actions} must not be nil"
  end
  super("#{scope}#{PRIMARY_KEY}", name: name, scope: scope)
  @microservice_name = "#{scope}__OPENC3__REACTION"
  @active = active
  @review = review
  @description = description
  @snoozed_until = snoozed_until
  @snooze = validate_snooze(snooze: snooze)
  @actions = validate_actions(actions: actions)
  @triggers = validate_triggers(triggers: triggers)
  @username = username
  @updated_at = updated_at
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



159
160
161
# File 'lib/openc3/models/reaction_model.rb', line 159

def actions
  @actions
end

#activeObject (readonly)

Returns the value of attribute active.



159
160
161
# File 'lib/openc3/models/reaction_model.rb', line 159

def active
  @active
end

#descriptionObject (readonly)

Returns the value of attribute description.



159
160
161
# File 'lib/openc3/models/reaction_model.rb', line 159

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



159
160
161
# File 'lib/openc3/models/reaction_model.rb', line 159

def name
  @name
end

#reviewObject (readonly)

Returns the value of attribute review.



159
160
161
# File 'lib/openc3/models/reaction_model.rb', line 159

def review
  @review
end

#scopeObject (readonly)

Returns the value of attribute scope.



159
160
161
# File 'lib/openc3/models/reaction_model.rb', line 159

def scope
  @scope
end

#snoozeObject (readonly)

Returns the value of attribute snooze.



159
160
161
# File 'lib/openc3/models/reaction_model.rb', line 159

def snooze
  @snooze
end

#snoozed_untilObject (readonly)

Returns the value of attribute snoozed_until.



159
160
161
# File 'lib/openc3/models/reaction_model.rb', line 159

def snoozed_until
  @snoozed_until
end

#triggersObject (readonly)

Returns the value of attribute triggers.



159
160
161
# File 'lib/openc3/models/reaction_model.rb', line 159

def triggers
  @triggers
end

#usernameObject

Returns the value of attribute username.



160
161
162
# File 'lib/openc3/models/reaction_model.rb', line 160

def username
  @username
end

Class Method Details

.all(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



82
83
84
# File 'lib/openc3/models/reaction_model.rb', line 82

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

.create_mini_idObject



55
56
57
58
59
60
# File 'lib/openc3/models/reaction_model.rb', line 55

def self.create_mini_id
  time = (Time.now.to_f * 10_000_000).to_i
  jitter = rand(10_000_000)
  key = "#{jitter}#{time}".to_i.to_s(36)
  return "RV0-#{key}"
end

.delete(name:, scope:, force: false) ⇒ Object

Check dependents before delete.



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/openc3/models/reaction_model.rb', line 92

def self.delete(name:, scope:, force: false)
  model = self.get(name: name, scope: scope)
  if model.nil?
    raise ReactionInputError.new "failed to find reaction: #{name}"
  end
  model.triggers.each do | trigger |
    trigger_model = TriggerModel.get(name: trigger['name'], group: trigger['group'], scope: scope)
    trigger_model.update_dependents(dependent: name, remove: true)
    trigger_model.update()
  end
  Store.hdel("#{scope}#{PRIMARY_KEY}", name)
  model.notify(kind: 'deleted')
end

.from_json(json, name:, scope:) ⇒ ReactionModel

Returns Model generated from the passed JSON.

Returns:



278
279
280
281
282
283
284
# File 'lib/openc3/models/reaction_model.rb', line 278

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?

  json.transform_keys!(&:to_sym)
  self.new(**json, name: name, scope: scope)
end

.get(name:, scope:) ⇒ ReactionModel

Return the object with the name at

Returns:



74
75
76
77
78
79
# File 'lib/openc3/models/reaction_model.rb', line 74

def self.get(name:, scope:)
  json = super("#{scope}#{PRIMARY_KEY}", name: name)
  unless json.nil?
    self.from_json(json, name: name, scope: scope)
  end
end

.names(scope:) ⇒ Array<String>

Returns All the uuids stored under the name key.

Returns:

  • (Array<String>)

    All the uuids stored under the name key



87
88
89
# File 'lib/openc3/models/reaction_model.rb', line 87

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

.reactions(scope:) ⇒ Array<ReactionModel>

Returns:



63
64
65
66
67
68
69
70
71
# File 'lib/openc3/models/reaction_model.rb', line 63

def self.reactions(scope:)
  reactions = Array.new
  Store.hgetall("#{scope}#{PRIMARY_KEY}").each do |key, value|
    data = JSON.parse(value, :allow_nan => true, :create_additions => true)
    reaction = self.from_json(data, name: data['name'], scope: data['scope'])
    reactions << reaction if reaction.active
  end
  return reactions
end

Instance Method Details

#activateObject



226
227
228
229
230
231
232
# File 'lib/openc3/models/reaction_model.rb', line 226

def activate
  @active = true
  @snoozed_until = nil if @snoozed_until && @snoozed_until < Time.now.to_i
  @updated_at = Time.now.to_nsec_from_epoch
  Store.hset(@primary_key, @name, JSON.generate(as_json(:allow_nan => true)))
  notify(kind: 'activated')
end

#as_json(*a) ⇒ Hash

Returns generated from the ReactionModel.

Returns:

  • (Hash)

    generated from the ReactionModel



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/openc3/models/reaction_model.rb', line 261

def as_json(*a)
  return {
    'name' => @name,
    'scope' => @scope,
    'active' => @active,
    'review' => @review,
    'description' => @description,
    'snooze' => @snooze,
    'snoozed_until' => @snoozed_until,
    'triggers' => @triggers,
    'actions' => @actions,
    'username' => @username,
    'updated_at' => @updated_at
  }
end

#awakenObject



248
249
250
251
252
253
# File 'lib/openc3/models/reaction_model.rb', line 248

def awaken
  @snoozed_until = nil
  @updated_at = Time.now.to_nsec_from_epoch
  Store.hset(@primary_key, @name, JSON.generate(as_json(:allow_nan => true)))
  notify(kind: 'awaken')
end

#createObject



209
210
211
212
213
214
215
216
217
# File 'lib/openc3/models/reaction_model.rb', line 209

def create
  unless Store.hget(@primary_key, @name).nil?
    raise ReactionInputError.new "exsisting Reaction found: #{@name}"
  end
  verify_triggers()
  @updated_at = Time.now.to_nsec_from_epoch
  Store.hset(@primary_key, @name, JSON.generate(as_json(:allow_nan => true)))
  notify(kind: 'created')
end

#create_microservice(topics:) ⇒ Object



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

def create_microservice(topics:)
  # reaction Microservice
  microservice = MicroserviceModel.new(
    name: @microservice_name,
    folder_name: nil,
    cmd: ['ruby', 'reaction_microservice.rb', @microservice_name],
    work_dir: '/openc3/lib/openc3/microservices',
    options: [],
    topics: topics,
    target_names: [],
    plugin: nil,
    scope: @scope
  )
  microservice.create
end

#deactivateObject



234
235
236
237
238
239
# File 'lib/openc3/models/reaction_model.rb', line 234

def deactivate
  @active = false
  @updated_at = Time.now.to_nsec_from_epoch
  Store.hset(@primary_key, @name, JSON.generate(as_json(:allow_nan => true)))
  notify(kind: 'deactivated')
end

#deployObject



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

def deploy
  topics = ["#{@scope}__openc3_autonomic"]
  if MicroserviceModel.get_model(name: @microservice_name, scope: @scope).nil?
    create_microservice(topics: topics)
  end
end

#notify(kind:) ⇒ Object

Returns [] update the redis stream / reaction topic that something has changed.

Returns:

  • update the redis stream / reaction topic that something has changed



287
288
289
290
291
292
293
294
# File 'lib/openc3/models/reaction_model.rb', line 287

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

#sleepObject



241
242
243
244
245
246
# File 'lib/openc3/models/reaction_model.rb', line 241

def sleep
  @snoozed_until = Time.now.to_i + @snooze
  @updated_at = Time.now.to_nsec_from_epoch
  Store.hset(@primary_key, @name, JSON.generate(as_json(:allow_nan => true)))
  notify(kind: 'sleep')
end

#to_sString

Returns generated from the TriggerModel.

Returns:

  • (String)

    generated from the TriggerModel



256
257
258
# File 'lib/openc3/models/reaction_model.rb', line 256

def to_s
  return "(ReactionModel :: #{@name} :: #{@active} :: #{@review} :: #{@description} :: #{@snooze} :: #{@snoozed_until})"
end

#undeployObject



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

def undeploy
  if ReactionModel.names(scope: @scope).empty?
    model = MicroserviceModel.get_model(name: @microservice_name, scope: @scope)
    model.destroy if model
  end
end

#updateObject



219
220
221
222
223
224
# File 'lib/openc3/models/reaction_model.rb', line 219

def update
  verify_triggers()
  @updated_at = Time.now.to_nsec_from_epoch
  Store.hset(@primary_key, @name, JSON.generate(as_json(:allow_nan => true)))
  notify(kind: 'updated')
end

#validate_actions(actions:) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/openc3/models/reaction_model.rb', line 138

def validate_actions(actions:)
  unless actions.is_a?(Array)
    raise ReactionInputError.new "invalid actions object: #{actions}"
  end
  actions.each do | action |
    unless action.is_a?(Hash)
      raise ReactionInputError.new "invalid action object: #{action}"
    end
    action_type = action['type']
    if action_type.nil?
      raise ReactionInputError.new "reaction action must contain type: #{action_type}"
    elsif action['value'].nil?
      raise ReactionInputError.new "reaction action: #{action} does not contain 'value'"
    end
    unless [COMMAND_REACTION, SCRIPT_REACTION].include?(action_type)
      raise ReactionInputError.new "reaction action contains invalid type: #{action_type}"
    end
  end
  return actions
end

#validate_snooze(snooze:) ⇒ Object



107
108
109
110
111
112
# File 'lib/openc3/models/reaction_model.rb', line 107

def validate_snooze(snooze:)
  unless snooze.is_a?(Integer)
    raise ReactionInputError.new "invalid snooze value: #{snooze}"
  end
  return snooze
end

#validate_triggers(triggers:) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/openc3/models/reaction_model.rb', line 115

def validate_triggers(triggers:)
  unless triggers.is_a?(Array)
    raise ReactionInputError.new "invalid operator: #{operator}"
  end
  trigger_hash = Hash.new()
  triggers.each do | trigger |
    unless trigger.is_a?(Hash)
      raise ReactionInputError.new "invalid trigger object: #{trigger}"
    end
    if trigger['name'].nil? || trigger['group'].nil?
      raise ReactionInputError.new "allowed: #{triggers}"
    end
    trigger_name = trigger['name']
    unless trigger_hash[trigger_name].nil?
      raise ReactionInputError.new "no duplicate triggers allowed: #{triggers}"
    else
      trigger_hash[trigger_name] = 1
    end
  end
  return triggers
end

#verify_triggersObject



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

def verify_triggers
  trigger_models = []
  @triggers.each do | trigger |
    model = TriggerModel.get(name: trigger['name'], group: trigger['group'], scope: @scope)
    if model.nil?
      raise ReactionInputError.new "failed to find trigger: #{trigger}"
    end
    trigger_models << model
  end
  if trigger_models.empty?
    raise ReactionInputError.new "reaction must contain at least one valid trigger: #{@triggers}"
  end
  trigger_models.each do | trigger_model |
    trigger_model.update_dependents(dependent: @name)
    trigger_model.update()
  end
end