Class: OpenC3::TriggerModel
- 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
Instance Attribute Summary collapse
-
#dependents ⇒ Object
readonly
Returns the value of attribute dependents.
-
#enabled ⇒ Object
readonly
Returns the value of attribute enabled.
-
#group ⇒ Object
readonly
Returns the value of attribute group.
-
#left ⇒ Object
Returns the value of attribute left.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#operator ⇒ Object
Returns the value of attribute operator.
-
#right ⇒ Object
Returns the value of attribute right.
-
#roots ⇒ Object
readonly
Returns the value of attribute roots.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#state ⇒ Object
Returns the value of attribute state.
Attributes inherited from Model
Class Method Summary collapse
-
.all(group:, scope:) ⇒ Array<Hash>
All the Key, Values stored under the name key.
- .create_unique_name(group:, scope:) ⇒ Object
-
.delete(name:, group:, scope:) ⇒ Object
Check dependents before delete.
-
.from_json(json, name:, scope:) ⇒ TriggerModel
Model generated from the passed JSON.
-
.get(name:, group:, scope:) ⇒ TriggerModel
Return the object with the name at.
-
.names(group:, scope:) ⇒ Array<String>
All the uuids stored under the name key.
Instance Method Summary collapse
-
#as_json(*a) ⇒ Hash
Generated from the TriggerModel.
- #create ⇒ Object
- #disable ⇒ Object
-
#generate_topics ⇒ Object
[“#@scope__DECOM__#{@target}__#@packet”].
-
#initialize(name:, scope:, group:, left:, operator:, right:, state: false, enabled: true, dependents: nil, updated_at: nil) ⇒ TriggerModel
constructor
A new instance of TriggerModel.
-
#notify(kind:) ⇒ Object
-
update the redis stream / trigger topic that something has changed.
-
- #notify_disable ⇒ Object
- #notify_enable ⇒ Object
-
#to_s ⇒ String
Generated from the TriggerModel.
- #update ⇒ Object
- #update_dependents(dependent:, remove: false) ⇒ Object
- #validate_operand(operand:, right: false) ⇒ Object
- #validate_operator(operator:) ⇒ Object
- #verify_triggers ⇒ Object
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:, state: false, enabled: true, dependents: nil, updated_at: nil) ⇒ TriggerModel
Returns a new instance of TriggerModel.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/openc3/models/trigger_model.rb', line 101 def initialize( name:, scope:, group:, left:, operator:, right:, state: false, enabled: true, dependents: 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 @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
#dependents ⇒ Object (readonly)
Returns the value of attribute dependents.
99 100 101 |
# File 'lib/openc3/models/trigger_model.rb', line 99 def dependents @dependents end |
#enabled ⇒ Object (readonly)
Returns the value of attribute enabled.
99 100 101 |
# File 'lib/openc3/models/trigger_model.rb', line 99 def enabled @enabled end |
#group ⇒ Object (readonly)
Returns the value of attribute group.
99 100 101 |
# File 'lib/openc3/models/trigger_model.rb', line 99 def group @group end |
#left ⇒ Object
Returns the value of attribute left.
99 100 101 |
# File 'lib/openc3/models/trigger_model.rb', line 99 def left @left end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
99 100 101 |
# File 'lib/openc3/models/trigger_model.rb', line 99 def name @name end |
#operator ⇒ Object
Returns the value of attribute operator.
99 100 101 |
# File 'lib/openc3/models/trigger_model.rb', line 99 def operator @operator end |
#right ⇒ Object
Returns the value of attribute right.
99 100 101 |
# File 'lib/openc3/models/trigger_model.rb', line 99 def right @right end |
#roots ⇒ Object (readonly)
Returns the value of attribute roots.
99 100 101 |
# File 'lib/openc3/models/trigger_model.rb', line 99 def roots @roots end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
99 100 101 |
# File 'lib/openc3/models/trigger_model.rb', line 99 def scope @scope end |
#state ⇒ Object
Returns the value of attribute state.
99 100 101 |
# File 'lib/openc3/models/trigger_model.rb', line 99 def state @state end |
Class Method Details
.all(group:, scope:) ⇒ Array<Hash>
Returns All the Key, Values stored under the name key.
72 73 74 |
# File 'lib/openc3/models/trigger_model.rb', line 72 def self.all(group:, scope:) super("#{scope}#{PRIMARY_KEY}#{group}") end |
.create_unique_name(group:, scope:) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/openc3/models/trigger_model.rb', line 54 def self.create_unique_name(group:, scope:) trigger_names = self.names(group: group, scope: scope) # comes back sorted num = 1 # Users count with 1 if trigger_names[-1] num = trigger_names[-1][4..-1].to_i + 1 end return "TRIG#{num}" end |
.delete(name:, group:, scope:) ⇒ Object
Check dependents before delete.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/openc3/models/trigger_model.rb', line 82 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.
275 276 277 278 279 |
# File 'lib/openc3/models/trigger_model.rb', line 275 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
64 65 66 67 68 69 |
# File 'lib/openc3/models/trigger_model.rb', line 64 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.
77 78 79 |
# File 'lib/openc3/models/trigger_model.rb', line 77 def self.names(group:, scope:) super("#{scope}#{PRIMARY_KEY}#{group}") end |
Instance Method Details
#as_json(*a) ⇒ Hash
Returns generated from the TriggerModel.
259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/openc3/models/trigger_model.rb', line 259 def as_json(*a) return { 'name' => @name, 'scope' => @scope, 'state' => @state, 'enabled' => @enabled, 'group' => @group, 'dependents' => @dependents, 'left' => @left, 'operator' => @operator, 'right' => @right, 'updated_at' => @updated_at, } end |
#create ⇒ Object
192 193 194 195 196 197 198 199 200 |
# File 'lib/openc3/models/trigger_model.rb', line 192 def create unless Store.hget(@primary_key, @name).nil? raise TriggerInputError.new "existing trigger 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 |
#disable ⇒ Object
227 228 229 230 231 |
# File 'lib/openc3/models/trigger_model.rb', line 227 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_topics ⇒ Object
- “#@scope__DECOM__#{@target}__#@packet”
234 235 236 237 238 239 240 241 242 243 |
# File 'lib/openc3/models/trigger_model.rb', line 234 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.
282 283 284 285 286 287 288 289 |
# File 'lib/openc3/models/trigger_model.rb', line 282 def notify(kind:) notification = { 'kind' => kind, 'type' => 'trigger', 'data' => JSON.generate(as_json, allow_nan: true), } AutonomicTopic.write_notification(notification, scope: @scope) end |
#notify_disable ⇒ Object
221 222 223 224 225 |
# File 'lib/openc3/models/trigger_model.rb', line 221 def notify_disable @enabled = false @state = false notify(kind: 'disabled') end |
#notify_enable ⇒ Object
216 217 218 219 |
# File 'lib/openc3/models/trigger_model.rb', line 216 def notify_enable @enabled = true notify(kind: 'enabled') end |
#to_s ⇒ String
Returns generated from the TriggerModel.
254 255 256 |
# File 'lib/openc3/models/trigger_model.rb', line 254 def to_s return "OpenC3::TriggerModel:#{@scope}:#{group}:#{@name})" end |
#update ⇒ Object
202 203 204 205 206 207 |
# File 'lib/openc3/models/trigger_model.rb', line 202 def update verify_triggers() @updated_at = Time.now.to_nsec_from_epoch Store.hset(@primary_key, @name, JSON.generate(as_json, allow_nan: true)) # No notification as this is only called via trigger_controller which already notifies end |
#update_dependents(dependent:, remove: false) ⇒ Object
245 246 247 248 249 250 251 |
# File 'lib/openc3/models/trigger_model.rb', line 245 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
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/openc3/models/trigger_model.rb', line 140 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
164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/openc3/models/trigger_model.rb', line 164 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 |
#verify_triggers ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/openc3/models/trigger_model.rb', line 178 def verify_triggers @dependents = [] if @dependents.nil? @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) model.update() end end end |