Class: OpenC3::TriggerGroupModel

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

Overview

INPUT:

{
  "name": "FOOBAR",
  "color": "#000000",
}

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

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

Constructor Details

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

Returns a new instance of TriggerGroupModel.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/openc3/models/trigger_group_model.rb', line 75

def initialize(name:, scope:, color: nil, updated_at: nil)
  if name.nil? || scope.nil?
    raise GroupTriggerInputError.new "name, or scope must not be nil"
  end
  unless name.is_a?(String)
    raise TriggerGroupInputError.new "invalid name: '#{name}'"
  end
  if name.include?('_')
    raise TriggerGroupInputError.new "invalid name: '#{name}' can not include an underscore '_'"
  end
  super("#{scope}#{PRIMARY_KEY}", name: name, scope: scope)
  @microservice_name = "#{scope}__TRIGGER_GROUP__#{name}"
  update_color(color: color)
  @updated_at = updated_at
end

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color.



73
74
75
# File 'lib/openc3/models/trigger_group_model.rb', line 73

def color
  @color
end

#nameObject (readonly)

Returns the value of attribute name.



73
74
75
# File 'lib/openc3/models/trigger_group_model.rb', line 73

def name
  @name
end

#scopeObject (readonly)

Returns the value of attribute scope.



73
74
75
# File 'lib/openc3/models/trigger_group_model.rb', line 73

def scope
  @scope
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



73
74
75
# File 'lib/openc3/models/trigger_group_model.rb', line 73

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



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

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

.delete(name:, scope:) ⇒ Object

Check dependents before delete.



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/openc3/models/trigger_group_model.rb', line 59

def self.delete(name:, scope:)
  model = self.get(name: name, scope: scope)
  if model.nil?
    raise TriggerGroupInputError.new "invalid group: #{name} not found"
  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 "failed to delete #{name} triggers: #{triggers}"
  end
end

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

Returns Model generated from the passed JSON.

Returns:



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

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

Return the object with the name at

Returns:

  • (GroupModel)

    Return the object with the name at



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

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



54
55
56
# File 'lib/openc3/models/trigger_group_model.rb', line 54

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



127
128
129
130
131
132
133
134
# File 'lib/openc3/models/trigger_group_model.rb', line 127

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

#createObject



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

def create
  unless Store.hget(@primary_key, @name).nil?
    raise TriggerGroupInputError.new "exsisting TriggerGroup found: #{@name}"
  end
  @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



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/openc3/models/trigger_group_model.rb', line 157

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



173
174
175
176
177
178
179
# File 'lib/openc3/models/trigger_group_model.rb', line 173

def deploy
  topics = ["#{@scope}__openc3_autonomic"]
  if MicroserviceModel.get_model(name: @microservice_name, scope: @scope).nil?
    AutonomicTopic.initialize_streams(topics)
    create_microservice(topics: topics)
  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



146
147
148
149
150
151
152
153
154
155
# File 'lib/openc3/models/trigger_group_model.rb', line 146

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 TriggerGroupModel.

Returns:

  • (String)

    generated from the TriggerGroupModel



122
123
124
# File 'lib/openc3/models/trigger_group_model.rb', line 122

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

#undeployObject



181
182
183
184
185
186
# File 'lib/openc3/models/trigger_group_model.rb', line 181

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

#updateObject



115
116
117
118
119
# File 'lib/openc3/models/trigger_group_model.rb', line 115

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

#update_color(color: nil) ⇒ Object



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

def update_color(color: nil)
  if color.nil?
    color = '#%06x' % (rand * 0xffffff)
  end
  valid_color = color =~ /(#*)([0-9,a-f,A-f]{6})/
  if valid_color.nil?
    raise TriggerGroupInputError.new "invalid color must be in hex format. #FF0000"
  end

  unless color.start_with?('#')
    color = "##{color}"
  end
  @color = color
end