Class: OpenC3::TriggerModel
- Defined in:
- lib/openc3/models/trigger_model.rb
Overview
INPUT:
{
"group": "someGroup",
"left": {
"type": "item",
"target": "INST",
"packet": "ADCS",
"item": "POSX",
},
"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
- TRIGGER_TYPE =
'trigger'.freeze
Instance Attribute Summary collapse
-
#active ⇒ Object
readonly
Returns the value of attribute active.
-
#dependents ⇒ Object
readonly
Returns the value of attribute dependents.
-
#group ⇒ Object
readonly
Returns the value of attribute group.
-
#left ⇒ Object
readonly
Returns the value of attribute left.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#operator ⇒ Object
readonly
Returns the value of attribute operator.
-
#right ⇒ Object
readonly
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
readonly
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_mini_id ⇒ 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
- #activate ⇒ Object
-
#as_json(*a) ⇒ Hash
Generated from the TriggerModel.
- #create ⇒ Object
- #deactivate ⇒ Object
- #disable ⇒ Object
- #enable ⇒ Object
-
#generate_topics ⇒ Object
[“#@scope__DECOM__#{@target}__#@packet”].
-
#initialize(name:, scope:, group:, left:, operator:, right:, state: false, active: true, description: nil, dependents: nil, updated_at: nil) ⇒ TriggerModel
constructor
A new instance of TriggerModel.
- #log(kind:, message: nil) ⇒ Object
- #modify ⇒ Object
-
#notify(kind:) ⇒ Object
-
update the redis stream / trigger topic that something has changed.
-
-
#to_s ⇒ String
Generated from the TriggerModel.
- #update ⇒ Object
- #update_dependents(dependent:, remove: false) ⇒ Object
- #validate_description(description:) ⇒ Object
- #validate_operand(operand:) ⇒ Object
- #validate_operator(operator:) ⇒ Object
- #verify_triggers ⇒ Object
Methods inherited from Model
#deploy, #destroy, #destroyed?, filter, find_all_by_plugin, get_all_models, get_model, handle_config, set, store, #undeploy
Constructor Details
#initialize(name:, scope:, group:, left:, operator:, right:, state: false, active: true, description: nil, dependents: nil, updated_at: nil) ⇒ TriggerModel
Returns a new instance of TriggerModel.
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/openc3/models/trigger_model.rb', line 156 def initialize( name:, scope:, group:, left:, operator:, right:, state: false, active: true, description: nil, dependents: nil, updated_at: nil ) if name.nil? || scope.nil? || group.nil? || left.nil? || operator.nil? || right.nil? raise TriggerInputError.new "#{name}, #{scope}, #{group}, #{left}, #{operator}, or #{right} must not be nil" end super("#{scope}#{PRIMARY_KEY}#{group}", name: name, scope: scope) @roots = [] @group = group @state = state @active = active @left = validate_operand(operand: left) @right = validate_operand(operand: right) @operator = validate_operator(operator: operator) @description = validate_description(description: description) @dependents = dependents @updated_at = updated_at end |
Instance Attribute Details
#active ⇒ Object (readonly)
Returns the value of attribute active.
153 154 155 |
# File 'lib/openc3/models/trigger_model.rb', line 153 def active @active end |
#dependents ⇒ Object (readonly)
Returns the value of attribute dependents.
153 154 155 |
# File 'lib/openc3/models/trigger_model.rb', line 153 def dependents @dependents end |
#group ⇒ Object (readonly)
Returns the value of attribute group.
153 154 155 |
# File 'lib/openc3/models/trigger_model.rb', line 153 def group @group end |
#left ⇒ Object (readonly)
Returns the value of attribute left.
153 154 155 |
# File 'lib/openc3/models/trigger_model.rb', line 153 def left @left end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
153 154 155 |
# File 'lib/openc3/models/trigger_model.rb', line 153 def name @name end |
#operator ⇒ Object (readonly)
Returns the value of attribute operator.
153 154 155 |
# File 'lib/openc3/models/trigger_model.rb', line 153 def operator @operator end |
#right ⇒ Object (readonly)
Returns the value of attribute right.
153 154 155 |
# File 'lib/openc3/models/trigger_model.rb', line 153 def right @right end |
#roots ⇒ Object (readonly)
Returns the value of attribute roots.
153 154 155 |
# File 'lib/openc3/models/trigger_model.rb', line 153 def roots @roots end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
153 154 155 |
# File 'lib/openc3/models/trigger_model.rb', line 153 def scope @scope end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
153 154 155 |
# File 'lib/openc3/models/trigger_model.rb', line 153 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_mini_id ⇒ Object
56 57 58 59 60 61 |
# File 'lib/openc3/models/trigger_model.rb', line 56 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 "TV0-#{key}" 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 "invalid operation group: #{group} trigger: #{name} not found" end unless model.dependents.empty? raise TriggerError.new "failed to delete #{name} 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) model.notify(kind: 'deleted') end |
.from_json(json, name:, scope:) ⇒ TriggerModel
Returns Model generated from the passed JSON.
302 303 304 305 306 307 308 |
# File 'lib/openc3/models/trigger_model.rb', line 302 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:, 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
#activate ⇒ Object
240 241 242 243 244 245 |
# File 'lib/openc3/models/trigger_model.rb', line 240 def activate @active = true @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 TriggerModel.
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/openc3/models/trigger_model.rb', line 285 def as_json(*a) return { 'name' => @name, 'scope' => @scope, 'state' => @state, 'active' => @active, 'group' => @group, 'description' => @description, 'dependents' => @dependents, 'left' => @left, 'operator' => @operator, 'right' => @right, 'updated_at' => @updated_at, } end |
#create ⇒ Object
209 210 211 212 213 214 215 216 217 |
# File 'lib/openc3/models/trigger_model.rb', line 209 def create unless Store.hget(@primary_key, @name).nil? raise TriggerInputError.new "exsisting 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 |
#deactivate ⇒ Object
247 248 249 250 251 252 253 |
# File 'lib/openc3/models/trigger_model.rb', line 247 def deactivate @active = false @state = 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 |
#disable ⇒ Object
233 234 235 236 237 238 |
# File 'lib/openc3/models/trigger_model.rb', line 233 def disable @state = false @updated_at = Time.now.to_nsec_from_epoch Store.hset(@primary_key, @name, JSON.generate(as_json(:allow_nan => true))) notify(kind: 'disabled') end |
#enable ⇒ Object
226 227 228 229 230 231 |
# File 'lib/openc3/models/trigger_model.rb', line 226 def enable @state = true @updated_at = Time.now.to_nsec_from_epoch Store.hset(@primary_key, @name, JSON.generate(as_json(:allow_nan => true))) notify(kind: 'enabled') end |
#generate_topics ⇒ Object
- “#@scope__DECOM__#{@target}__#@packet”
260 261 262 263 264 265 266 267 268 269 |
# File 'lib/openc3/models/trigger_model.rb', line 260 def generate_topics topics = Hash.new if @left['type'] == ITEM_TYPE topics["#{@scope}__DECOM__{#{left['target']}}__#{left['packet']}"] = 1 end if @right['type'] == ITEM_TYPE topics["#{@scope}__DECOM__{#{right['target']}}__#{right['packet']}"] = 1 end return topics.keys end |
#log(kind:, message: nil) ⇒ Object
322 323 324 325 326 327 328 329 330 331 |
# File 'lib/openc3/models/trigger_model.rb', line 322 def log(kind:, message: nil) notification = { 'kind' => kind, 'type' => 'log', 'time' => Time.now.to_i, 'name' => @name, } notification['message'] = unless .nil? AutonomicTopic.write_notification(notification, scope: @scope) end |
#modify ⇒ Object
255 256 257 |
# File 'lib/openc3/models/trigger_model.rb', line 255 def modify raise "TODO" end |
#notify(kind:) ⇒ Object
Returns [] update the redis stream / trigger topic that something has changed.
311 312 313 314 315 316 317 318 |
# File 'lib/openc3/models/trigger_model.rb', line 311 def notify(kind:) notification = { 'kind' => kind, 'type' => 'trigger', 'data' => JSON.generate(as_json(:allow_nan => true)), } AutonomicTopic.write_notification(notification, scope: @scope) end |
#to_s ⇒ String
Returns generated from the TriggerModel.
280 281 282 |
# File 'lib/openc3/models/trigger_model.rb', line 280 def to_s return "(TriggerModel :: #{@name} :: #{group} :: #{@description})" end |
#update ⇒ Object
219 220 221 222 223 224 |
# File 'lib/openc3/models/trigger_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 |
#update_dependents(dependent:, remove: false) ⇒ Object
271 272 273 274 275 276 277 |
# File 'lib/openc3/models/trigger_model.rb', line 271 def update_dependents(dependent:, remove: false) if remove @dependents.delete(dependent) elsif @dependents.index(dependent).nil? @dependents << dependent end end |
#validate_description(description:) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/openc3/models/trigger_model.rb', line 141 def validate_description(description:) if description.nil? left_type = @left['type'] right_type = @right['type'] return "#{@left[left_type]} #{@operator} #{@right[right_type]}" end unless description.is_a?(String) raise TriggerInputError.new "invalid description: #{description}" end return description end |
#validate_operand(operand:) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/openc3/models/trigger_model.rb', line 99 def validate_operand(operand:) unless operand.is_a?(Hash) raise TriggerInputError.new "invalid operand: #{operand}" end operand_types = [ITEM_TYPE, LIMIT_TYPE, FLOAT_TYPE, STRING_TYPE, TRIGGER_TYPE] unless operand_types.include?(operand['type']) raise TriggerInputError.new "invalid operand type: #{operand['type']} must be of type: #{operand_types}" end if operand[operand['type']].nil? raise TriggerInputError.new "invalid operand must contain type: #{operand}" end case operand['type'] when ITEM_TYPE if operand['target'].nil? || operand['packet'].nil? || operand['raw'].nil? raise TriggerInputError.new "invalid operand must contain target, packet, item, and raw: #{operand}" end when TRIGGER_TYPE @roots << operand[operand['type']] end return operand end |
#validate_operator(operator:) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/openc3/models/trigger_model.rb', line 121 def validate_operator(operator:) unless operator.is_a?(String) raise TriggerInputError.new "invalid operator: #{operator}" end operators = ['>', '<', '>=', '<='] match_operators = ['==', '!='] trigger_operators = ['AND', 'OR'] if @roots.empty? && operators.include?(operator) return operator elsif @roots.empty? && match_operators.include?(operator) return operator elsif @roots.size() == 2 && trigger_operators.include?(operator) return operator elsif operators.include?(operator) raise TriggerInputError.new "invalid operator pair: '#{operator}' must be of type: #{trigger_operators}" else raise TriggerInputError.new "invalid operator: '#{operator}' must be of type: #{operators}" end end |
#verify_triggers ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/openc3/models/trigger_model.rb', line 185 def verify_triggers unless @group.is_a?(String) raise TriggerInputError.new "invalid group: #{@group}" end selected_group = OpenC3::TriggerGroupModel.get(name: @group, scope: @scope) if selected_group.nil? raise TriggerGroupInputError.new "failed to find group: #{@group}" end @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: #{trigger}" end if model.group != @group raise TriggerInputError.new "failed group dependent trigger: #{trigger}" end unless model.dependents.include?(@name) model.update_dependents(dependent: @name) model.update() end end end |