Class: OpenC3::TriggerGroupModel

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

Constant Summary collapse

PRIMARY_KEY =
'__TRIGGER__GROUP'.freeze

Instance Attribute Summary collapse

Attributes inherited from Model

#plugin

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#check_disable_erb, #destroy, #destroyed?, #diff, filter, find_all_by_plugin, get_all_models, get_model, handle_config, set, store, store_queued, #update

Constructor Details

#initialize(name:, scope:, shard: 0, updated_at: nil) ⇒ TriggerGroupModel

Returns a new instance of TriggerGroupModel.



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/openc3/models/trigger_group_model.rb', line 64

def initialize(name:, scope:, shard: 0, updated_at: nil)
  unless name.is_a?(String)
    raise TriggerGroupInputError.new "invalid group name: '#{name}'"
  end
  if name.include?('_')
    raise TriggerGroupInputError.new "group name '#{name}' can not include an underscore"
  end
  super("#{scope}#{PRIMARY_KEY}", name: name, scope: scope)
  @microservice_name = "#{scope}__TRIGGER_GROUP__#{name}"
  @shard = shard.to_i # to_i to handle nil
  @updated_at = updated_at
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



62
63
64
# File 'lib/openc3/models/trigger_group_model.rb', line 62

def name
  @name
end

#scopeObject (readonly)

Returns the value of attribute scope.



62
63
64
# File 'lib/openc3/models/trigger_group_model.rb', line 62

def scope
  @scope
end

#shardObject (readonly)

Returns the value of attribute shard.



62
63
64
# File 'lib/openc3/models/trigger_group_model.rb', line 62

def shard
  @shard
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



62
63
64
# File 'lib/openc3/models/trigger_group_model.rb', line 62

def updated_at
  @updated_at
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



38
39
40
# File 'lib/openc3/models/trigger_group_model.rb', line 38

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

.delete(name:, scope:) ⇒ Object

Check dependents before delete.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/openc3/models/trigger_group_model.rb', line 48

def self.delete(name:, scope:)
  model = self.get(name: name, scope: scope)
  if model.nil?
    raise TriggerGroupInputError.new "group '#{name}' does not exist"
  end
  triggers = TriggerModel.names(scope: scope, group: name)
  if triggers.empty?
    Store.hdel("#{scope}#{PRIMARY_KEY}", name)
    model.notify(kind: 'deleted')
  else
    raise TriggerGroupError.new "group '#{name}' has dependent triggers: #{triggers}"
  end
end

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

Returns Model generated from the passed JSON.

Returns:



98
99
100
101
102
# File 'lib/openc3/models/trigger_group_model.rb', line 98

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:, scope:) ⇒ GroupModel

Return the object with the name at

Returns:

  • (GroupModel)

    Return the object with the name at



30
31
32
33
34
35
# File 'lib/openc3/models/trigger_group_model.rb', line 30

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



43
44
45
# File 'lib/openc3/models/trigger_group_model.rb', line 43

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

Instance Method Details

#as_json(*a) ⇒ Hash

Returns generated from the TriggerGroupModel.

Returns:

  • (Hash)

    generated from the TriggerGroupModel



88
89
90
91
92
93
94
95
# File 'lib/openc3/models/trigger_group_model.rb', line 88

def as_json(*a)
  return {
    'name' => @name,
    'scope' => @scope,
    'shard' => @shard,
    'updated_at' => @updated_at,
  }
end

#create(update: false) ⇒ Object



77
78
79
80
# File 'lib/openc3/models/trigger_group_model.rb', line 77

def create(update: false)
  super(update: update)
  notify(kind: 'created')
end

#create_microservice(topics:) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/openc3/models/trigger_group_model.rb', line 116

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

#deployObject



133
134
135
136
137
138
139
# File 'lib/openc3/models/trigger_group_model.rb', line 133

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

#notify(kind:, error: nil) ⇒ Object

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

Returns:

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



105
106
107
108
109
110
111
112
113
114
# File 'lib/openc3/models/trigger_group_model.rb', line 105

def notify(kind:, error: nil)
  data = as_json()
  data['error'] = error unless error.nil?
  notification = {
    'kind' => kind,
    'type' => 'group',
    'data' => JSON.generate(data, allow_nan: true),
  }
  AutonomicTopic.write_notification(notification, scope: @scope)
end

#to_sString

Returns generated from the TriggerModel.

Returns:

  • (String)

    generated from the TriggerModel



83
84
85
# File 'lib/openc3/models/trigger_group_model.rb', line 83

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

#undeployObject



141
142
143
144
145
146
147
148
149
# File 'lib/openc3/models/trigger_group_model.rb', line 141

def undeploy
  if TriggerModel.names(scope: scope, group: name).empty?
    model = MicroserviceModel.get_model(name: @microservice_name, scope: @scope)
    if model
      model.destroy
      notify(kind: 'undeployed')
    end
  end
end