Class: OpenC3::CvtModel

Inherits:
Object show all
Defined in:
lib/openc3/models/cvt_model.rb

Constant Summary collapse

VALUE_TYPES =
[:RAW, :CONVERTED, :FORMATTED]
@@packet_cache =
{}
@@override_cache =
{}

Class Method Summary collapse

Class Method Details

._get_overrides(now, tgt_pkt_key, overrides, target_name, packet_name, cache_timeout:, scope:) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/openc3/models/cvt_model.rb', line 340

def self._get_overrides(now, tgt_pkt_key, overrides, target_name, packet_name, cache_timeout:, scope:)
  if cache_timeout
    cache_time, hash = @@override_cache[tgt_pkt_key]
    if hash and (now - cache_time) < cache_timeout
      overrides[tgt_pkt_key] = hash
      return hash
    end
  end
  override_data = store_for_target(target_name, scope: scope).hget("#{scope}__override__#{target_name}", packet_name)
  if override_data
    hash = JSON.parse(override_data, allow_nan: true, create_additions: true)
    overrides[tgt_pkt_key] = hash
  else
    hash = {}
    overrides[tgt_pkt_key] = {}
  end
  @@override_cache[tgt_pkt_key] = [now, hash] # always update
  return hash
end

._handle_item_override(target_name, packet_name, item_name, type:, cache_timeout:, scope: $openc3_scope) ⇒ Object

PRIVATE METHODS



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/openc3/models/cvt_model.rb', line 317

def self._handle_item_override(target_name, packet_name, item_name, type:, cache_timeout:, scope: $openc3_scope)
  override_key = item_name
  types = []
  case type
  when :FORMATTED, :WITH_UNITS
    types = ["#{item_name}__F", "#{item_name}__C", item_name]
    override_key = "#{item_name}__F"
  when :CONVERTED
    types = ["#{item_name}__C", item_name]
    override_key = "#{item_name}__C"
  when :RAW
    types = [item_name]
  else
    raise "Unknown type '#{type}' for #{target_name} #{packet_name} #{item_name}"
  end

  tgt_pkt_key = "#{scope}__tlm__#{target_name}__#{packet_name}"
  overrides = _get_overrides(Time.now, tgt_pkt_key, {}, target_name, packet_name, cache_timeout: cache_timeout, scope: scope)
  result = overrides[override_key]
  return result, types if result
  return nil, types
end

._parse_item(now, lookups, overrides, item, cache_timeout:, scope:) ⇒ Object

parse item and update lookups with packet_name and target_name and keys return an ordered array of hash with keys



362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/openc3/models/cvt_model.rb', line 362

def self._parse_item(now, lookups, overrides, item, cache_timeout:, scope:)
  target_name, packet_name, item_name, value_type = item
  # They will all be nil when item is a nil value
  if item_name.nil?
    lookups << nil
    return
  end

  # We build lookup keys by including all the less formatted types to gracefully degrade lookups
  # This allows the user to specify FORMATTED and if there is no conversions it will simply return the RAW value
  case value_type.to_s
  when 'RAW'
    keys = [item_name]
  when 'CONVERTED'
    keys = ["#{item_name}__C", item_name]
  when 'FORMATTED', 'WITH_UNITS'
    keys = ["#{item_name}__F", "#{item_name}__C", item_name]
  else
    raise "Unknown value type '#{value_type}'"
  end

  # Check the overrides cache for this target / packet
  tgt_pkt_key = "#{scope}__tlm__#{target_name}__#{packet_name}"
  _get_overrides(now, tgt_pkt_key, overrides, target_name, packet_name, cache_timeout: cache_timeout, scope: scope) unless overrides[tgt_pkt_key]

  # Set the result as a Hash to distinguish it from the key array and from an overridden Array value
  value = overrides[tgt_pkt_key][keys[0]]
  keys = {'value' => value} if value

  lookups << [tgt_pkt_key, target_name, packet_name, keys]
end

.build_json_from_packet(packet) ⇒ Object



29
30
31
# File 'lib/openc3/models/cvt_model.rb', line 29

def self.build_json_from_packet(packet)
  packet.decom
end

.del(target_name:, packet_name:, scope: $openc3_scope) ⇒ Object

Delete the current value table for a target



44
45
46
47
48
49
# File 'lib/openc3/models/cvt_model.rb', line 44

def self.del(target_name:, packet_name:, scope: $openc3_scope)
  key = "#{scope}__tlm__#{target_name}"
  tgt_pkt_key = key + "__#{packet_name}"
  @@packet_cache[tgt_pkt_key] = nil
  store_for_target(target_name, scope: scope).hdel(key, packet_name)
end

.determine_latest_packet_for_item(target_name, item_name, cache_timeout: nil, scope: $openc3_scope) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/openc3/models/cvt_model.rb', line 294

def self.determine_latest_packet_for_item(target_name, item_name, cache_timeout: nil, scope: $openc3_scope)
  item_map = TargetModel.get_item_to_packet_map(target_name, scope: scope)
  packet_names = item_map[item_name]
  raise "Item '#{target_name} LATEST #{item_name}' does not exist for scope: #{scope}" unless packet_names

  latest = -1
  latest_packet_name = nil
  packet_names.each do |packet_name|
    hash = get(target_name: target_name, packet_name: packet_name, cache_timeout: cache_timeout, scope: scope)
    if hash['PACKET_TIMESECONDS'] && hash['PACKET_TIMESECONDS'] > latest
      latest = hash['PACKET_TIMESECONDS']
      latest_packet_name = packet_name
    end
  end
  # Return the first packet name if no packets have been received
  if latest == -1
    latest_packet_name = packet_names[0]
  end
  return latest_packet_name
end

.get(target_name:, packet_name:, cache_timeout: nil, scope: $openc3_scope) ⇒ Object

Get the hash for packet in the CVT Note: Does not apply overrides



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/openc3/models/cvt_model.rb', line 80

def self.get(target_name:, packet_name:, cache_timeout: nil, scope: $openc3_scope)
  key = "#{scope}__tlm__#{target_name}"
  tgt_pkt_key = key + "__#{packet_name}"
  now = Time.now
  if cache_timeout
    cache_time, hash = @@packet_cache[tgt_pkt_key]
    return hash if hash and (now - cache_time) < cache_timeout
  end
  packet = store_for_target(target_name, scope: scope).hget(key, packet_name)
  raise "Packet '#{target_name} #{packet_name}' does not exist" unless packet
  hash = JSON.parse(packet, allow_nan: true, create_additions: true)
  @@packet_cache[tgt_pkt_key] = [now, hash]
  hash
end

.get_item(target_name, packet_name, item_name, type:, cache_timeout: nil, scope: $openc3_scope) ⇒ Object

Get an item from the current value table



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

def self.get_item(target_name, packet_name, item_name, type:, cache_timeout: nil, scope: $openc3_scope)
  result, types = self._handle_item_override(target_name, packet_name, item_name, type: type, cache_timeout: cache_timeout, scope: scope)
  return result if result
  hash = get(target_name: target_name, packet_name: packet_name, cache_timeout: cache_timeout, scope: scope)
  hash.values_at(*types).each do |cvt_value|
    if cvt_value
      if type == :FORMATTED or type == :WITH_UNITS
        return cvt_value.to_s
      end
      return cvt_value
    end
  end
  # RECEIVED_COUNT is a special case where it is 0 if it doesn't exist
  # This allows scripts to check against the value to see if the packet was ever received
  if item_name == "RECEIVED_COUNT"
    return 0
  else
    return nil
  end
end

.get_tlm_values(items, stale_time: 30, cache_timeout: nil, start_time: nil, end_time: nil, scope: $openc3_scope) ⇒ Array

Return all item values and limit state from the CVT

Parameters:

  • items (Array<String>)

    Items to return. Must be formatted as TGT__PKT__ITEM__TYPE

  • stale_time (Integer) (defaults to: 30)

    Time in seconds from Time.now that value will be marked stale

Returns:

  • (Array)

    Array of values



146
147
148
149
150
151
152
153
154
155
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/openc3/models/cvt_model.rb', line 146

def self.get_tlm_values(items, stale_time: 30, cache_timeout: nil, start_time: nil, end_time: nil, scope: $openc3_scope)
  now = Time.now
  results = []
  lookups = []
  packet_lookup = {}
  overrides = {}

  # If a start_time is passed we're doing a QuestDB lookup and directly return the results
  # TODO: This currently does NOT support the override values
  if start_time
    return tsdb_lookup(items, start_time: start_time, end_time: end_time, scope: scope)
  end

  # First generate a lookup hash of all the items represented so we can query the CVT
  items.each { |item| _parse_item(now, lookups, overrides, item, cache_timeout: cache_timeout, scope: scope) }

  now = now.to_f
  lookups.each do |target_packet_key, target_name, packet_name, value_keys|
    if target_packet_key.nil?
      results << [nil, nil]
      next
    end
    unless packet_lookup[target_packet_key]
      packet_lookup[target_packet_key] = get(target_name: target_name, packet_name: packet_name, cache_timeout: cache_timeout, scope: scope)
    end
    hash = packet_lookup[target_packet_key]
    item_result = []
    if value_keys.is_a?(Hash) # Set in _parse_item to indicate override
      item_result[0] = value_keys['value']
    else
      value_keys.each do |key|
        item_result[0] = hash[key]
        break if item_result[0] # We want the first value
      end
      # If we were able to find a value, try to get the limits state
      if item_result[0]
        if now - hash['RECEIVED_TIMESECONDS'] > stale_time
          item_result[1] = :STALE
        else
          # The last key is simply the name (RAW) so we can append __L
          # If there is no limits then it returns nil which is acceptable
          item_result[1] = hash["#{value_keys[-1]}__L"]
          item_result[1] = item_result[1].intern if item_result[1] # Convert to symbol
        end
      else
        # We didn't find a value but the packet hash contains the key so the item exists
        # Thus set the result to nil so it comes back like a normal item
        if hash.key?(value_keys[-1])
          item_result[1] = nil
        else
          item_result[0] = nil
          item_result[1] = nil
        end
      end
    end
    results << item_result
  end
  results
end

.normalize(target_name, packet_name, item_name, type: :ALL, scope: $openc3_scope) ⇒ Object

Normalize a current value table item such that it returns the actual value



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/openc3/models/cvt_model.rb', line 264

def self.normalize(target_name, packet_name, item_name, type: :ALL, scope: $openc3_scope)
  hash = store_for_target(target_name, scope: scope).hget("#{scope}__override__#{target_name}", packet_name)
  hash = JSON.parse(hash, allow_nan: true, create_additions: true) if hash
  hash ||= {} # In case the above didn't create anything
  case type
  when :ALL
    hash.delete(item_name)
    hash.delete("#{item_name}__C")
    hash.delete("#{item_name}__F")
  when :RAW
    hash.delete(item_name)
  when :CONVERTED
    hash.delete("#{item_name}__C")
  when :FORMATTED, :WITH_UNITS
    hash.delete("#{item_name}__F")
  else
    raise "Unknown type '#{type}' for #{target_name} #{packet_name} #{item_name}"
  end

  tgt_pkt_key = "#{scope}__tlm__#{target_name}__#{packet_name}"
  store = store_for_target(target_name, scope: scope)
  if hash.empty?
    @@override_cache.delete(tgt_pkt_key)
    store.hdel("#{scope}__override__#{target_name}", packet_name)
  else
    @@override_cache[tgt_pkt_key] = [Time.now, hash]
    store.hset("#{scope}__override__#{target_name}", packet_name, JSON.generate(hash.as_json, allow_nan: true))
  end
end

.override(target_name, packet_name, item_name, value, type: :ALL, scope: $openc3_scope) ⇒ Object

Override a current value table item such that it always returns the same value for the given type



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/openc3/models/cvt_model.rb', line 239

def self.override(target_name, packet_name, item_name, value, type: :ALL, scope: $openc3_scope)
  hash = store_for_target(target_name, scope: scope).hget("#{scope}__override__#{target_name}", packet_name)
  hash = JSON.parse(hash, allow_nan: true, create_additions: true) if hash
  hash ||= {} # In case the above didn't create anything
  case type
  when :ALL
    hash[item_name] = value
    hash["#{item_name}__C"] = value
    hash["#{item_name}__F"] = value.to_s
  when :RAW
    hash[item_name] = value
  when :CONVERTED
    hash["#{item_name}__C"] = value
  when :FORMATTED, :WITH_UNITS
    hash["#{item_name}__F"] = value.to_s # Always a String
  else
    raise "Unknown type '#{type}' for #{target_name} #{packet_name} #{item_name}"
  end

  tgt_pkt_key = "#{scope}__tlm__#{target_name}__#{packet_name}"
  @@override_cache[tgt_pkt_key] = [Time.now, hash]
  store_for_target(target_name, scope: scope).hset("#{scope}__override__#{target_name}", packet_name, JSON.generate(hash.as_json, allow_nan: true))
end

.overrides(scope: $openc3_scope) ⇒ Object

Return all the overrides Note: Does not use cache to benefit from hgetall



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/openc3/models/cvt_model.rb', line 208

def self.overrides(scope: $openc3_scope)
  overrides = []
  TargetModel.names(scope: scope).each do |target_name|
    all = store_for_target(target_name, scope: scope).hgetall("#{scope}__override__#{target_name}")
    next if all.nil? or all.empty?
    all.each do |packet_name, hash|
      items = JSON.parse(hash, allow_nan: true, create_additions: true)
      items.each do |key, value|
        item = {}
        item['target_name'] = target_name
        item['packet_name'] = packet_name
        item_name, value_type_key = key.split('__')
        item['item_name'] = item_name
        case value_type_key
        when 'F', 'U'
          item['value_type'] = 'FORMATTED'
        when 'C'
          item['value_type'] = 'CONVERTED'
        else
          item['value_type'] = 'RAW'
        end
        item['value'] = value
        overrides << item
      end
    end
  end
  overrides
end

.set(hash, target_name:, packet_name:, queued: false, scope: $openc3_scope) ⇒ Object

Set the current value table for a target, packet



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/openc3/models/cvt_model.rb', line 52

def self.set(hash, target_name:, packet_name:, queued: false, scope: $openc3_scope)
  packet_json = JSON.generate(hash.as_json, allow_nan: true)
  key = "#{scope}__tlm__#{target_name}"
  tgt_pkt_key = key + "__#{packet_name}"
  @@packet_cache[tgt_pkt_key] = [Time.now, hash]
  if queued
    store_queued_for_target(target_name, scope: scope).hset(key, packet_name, packet_json)
  else
    store_for_target(target_name, scope: scope).hset(key, packet_name, packet_json)
  end
end

.set_item(target_name, packet_name, item_name, value, type:, queued: false, scope: $openc3_scope) ⇒ Object

Set an item in the current value table



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/openc3/models/cvt_model.rb', line 96

def self.set_item(target_name, packet_name, item_name, value, type:, queued: false, scope: $openc3_scope)
  hash = get(target_name: target_name, packet_name: packet_name, cache_timeout: nil, scope: scope)
  case type
  when :FORMATTED, :WITH_UNITS
    hash["#{item_name}__F"] = value.to_s # FORMATTED should always be a string
  when :CONVERTED
    hash["#{item_name}__C"] = value
  when :RAW
    hash[item_name] = value
  when :ALL
    hash["#{item_name}__F"] = value.to_s # FORMATTED should always be a string
    hash["#{item_name}__C"] = value
    hash[item_name] = value
  else
    raise "Unknown type '#{type}' for #{target_name} #{packet_name} #{item_name}"
  end
  set(hash, target_name: target_name, packet_name: packet_name, queued: queued, scope: scope)
end

.set_json(packet_json, hash, target_name:, packet_name:, queued: false, scope: $openc3_scope) ⇒ Object

Set the current value table with pre-serialized JSON (avoids double as_json call)

Parameters:

  • packet_json (String)

    Pre-serialized JSON string

  • hash (Hash)

    The JSON-safe hash (already had as_json called)



67
68
69
70
71
72
73
74
75
76
# File 'lib/openc3/models/cvt_model.rb', line 67

def self.set_json(packet_json, hash, target_name:, packet_name:, queued: false, scope: $openc3_scope)
  key = "#{scope}__tlm__#{target_name}"
  tgt_pkt_key = key + "__#{packet_name}"
  @@packet_cache[tgt_pkt_key] = [Time.now, hash]
  if queued
    store_queued_for_target(target_name, scope: scope).hset(key, packet_name, packet_json)
  else
    store_for_target(target_name, scope: scope).hset(key, packet_name, packet_json)
  end
end

.store_for_target(target_name, scope:) ⇒ Object

Get a Store instance routed to the correct db_shard for a target



34
35
36
# File 'lib/openc3/models/cvt_model.rb', line 34

def self.store_for_target(target_name, scope:)
  Store.instance(db_shard: Store.db_shard_for_target(target_name, scope: scope))
end

.store_queued_for_target(target_name, scope:) ⇒ Object

Get a StoreQueued instance routed to the correct db_shard for a target



39
40
41
# File 'lib/openc3/models/cvt_model.rb', line 39

def self.store_queued_for_target(target_name, scope:)
  StoreQueued.instance(db_shard: Store.db_shard_for_target(target_name, scope: scope))
end

.tsdb_lookup(items, start_time:, end_time: nil, scope: $openc3_scope) ⇒ Object



137
138
139
# File 'lib/openc3/models/cvt_model.rb', line 137

def self.tsdb_lookup(items, start_time:, end_time: nil, scope: $openc3_scope)
  QuestDBClient.tsdb_lookup(items, start_time: start_time, end_time: end_time, scope: scope)
end