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?, filter, find_all_by_plugin, get_all_models, get_model, handle_config, set, store, #update

Constructor Details

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

Returns a new instance of TriggerGroupModel.



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/openc3/models/trigger_group_model.rb', line 69

def initialize(name:, scope:, 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}"
  @updated_at = updated_at
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



67
68
69
# File 'lib/openc3/models/trigger_group_model.rb', line 67

def name
  @name
end

#scopeObject (readonly)

Returns the value of attribute scope.



67
68
69
# File 'lib/openc3/models/trigger_group_model.rb', line 67

def scope
  @scope
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



67
68
69
# File 'lib/openc3/models/trigger_group_model.rb', line 67

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



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

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

.delete(name:, scope:) ⇒ Object

Check dependents before delete.



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

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:



101
102
103
104
105
# File 'lib/openc3/models/trigger_group_model.rb', line 101

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



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

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



48
49
50
# File 'lib/openc3/models/trigger_group_model.rb', line 48

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



92
93
94
95
96
97
98
# File 'lib/openc3/models/trigger_group_model.rb', line 92

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

#create(update: false) ⇒ Object



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

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

#create_microservice(topics:) ⇒ Object



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

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/lib/openc3/microservices',
    options: [],
    topics: topics,
    target_names: [],
    plugin: nil,
    scope: @scope
  )
  microservice.create
end

#deployObject



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

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



108
109
110
111
112
113
114
115
116
117
# File 'lib/openc3/models/trigger_group_model.rb', line 108

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

#to_sString

Returns generated from the TriggerModel.

Returns:

  • (String)

    generated from the TriggerModel



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

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

#undeployObject



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

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