Class: OpenC3::CvtModel
Constant Summary collapse
- VALUE_TYPES =
[:RAW, :CONVERTED, :FORMATTED, :WITH_UNITS]
Class Method Summary collapse
-
._parse_item(lookups, item) ⇒ Object
PRIVATE METHODS.
- .build_json_from_packet(packet) ⇒ Object
-
.del(target_name:, packet_name:, scope:) ⇒ Object
Delete the current value table for a target.
-
.get_item(target_name, packet_name, item_name, type:, scope:) ⇒ Object
Get an item from the current value table.
-
.get_tlm_values(items, scope: $openc3_scope) ⇒ Array
Return all item values and limit state from the CVT.
-
.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.
-
.override(target_name, packet_name, item_name, value, type:, scope: $openc3_scope) ⇒ Object
Override a current value table item such that it always returns the same value for the given type.
-
.set(hash, target_name:, packet_name:, scope:) ⇒ Object
Set the current value table for a target, packet.
-
.set_item(target_name, packet_name, item_name, value, type:, scope:) ⇒ Object
Set an item in the current value table.
Class Method Details
._parse_item(lookups, item) ⇒ Object
PRIVATE METHODS
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/openc3/models/cvt_model.rb', line 152 def self._parse_item(lookups, item) # parse item and update lookups with packet_name and target_name and keys # # return an ordered array of hash with keys target_name, packet_name, item_name, value_type = item.split('__') raise ArgumentError, "items must be formatted as TGT__PKT__ITEM__TYPE" if target_name.nil? || packet_name.nil? || item_name.nil? || value_type.nil? # We build lookup keys by including all the less formatted types to gracefully degrade lookups # This allows the user to specify WITH_UNITS and if there is no conversions it will simply return the RAW value case value_type.upcase when 'RAW' keys = [item_name] when 'CONVERTED' keys = ["#{item_name}__C", item_name] when 'FORMATTED' keys = ["#{item_name}__F", "#{item_name}__C", item_name] when 'WITH_UNITS' keys = ["#{item_name}__U", "#{item_name}__F", "#{item_name}__C", item_name] else raise "Unknown value type #{value_type}" end lookups << ["#{target_name}__#{packet_name}", target_name, packet_name, keys] end |
.build_json_from_packet(packet) ⇒ Object
28 29 30 |
# File 'lib/openc3/models/cvt_model.rb', line 28 def self.build_json_from_packet(packet) packet.decom end |
.del(target_name:, packet_name:, scope:) ⇒ Object
Delete the current value table for a target
33 34 35 |
# File 'lib/openc3/models/cvt_model.rb', line 33 def self.del(target_name:, packet_name:, scope:) Store.hdel("#{scope}__tlm__#{target_name}", packet_name) end |
.get_item(target_name, packet_name, item_name, type:, scope:) ⇒ Object
Get an item from the current value table
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/openc3/models/cvt_model.rb', line 62 def self.get_item(target_name, packet_name, item_name, type:, scope:) if @overrides["#{target_name}__#{packet_name}__#{item_name}__#{type}"] return @overrides["#{target_name}__#{packet_name}__#{item_name}__#{type}"] end types = [] case type when :WITH_UNITS types = ["#{item_name}__U", "#{item_name}__F", "#{item_name}__C", item_name] when :FORMATTED types = ["#{item_name}__F", "#{item_name}__C", item_name] when :CONVERTED types = ["#{item_name}__C", item_name] when :RAW types = [item_name] else raise "Unknown type '#{type}' for #{target_name} #{packet_name} #{item_name}" end hash = JSON.parse(Store.hget("#{scope}__tlm__#{target_name}", packet_name), :allow_nan => true, :create_additions => true) hash.values_at(*types).each do |result| return result if result end return nil end |
.get_tlm_values(items, scope: $openc3_scope) ⇒ Array
Return all item values and limit state from the CVT
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/openc3/models/cvt_model.rb', line 91 def self.get_tlm_values(items, scope: $openc3_scope) results = [] lookups = [] packet_lookup = {} # First generate a lookup hash of all the items represented so we can query the CVT items.each { |item| _parse_item(lookups, item) } lookups.each do |target_packet_key, target_name, packet_name, packet_values| unless packet_lookup[target_packet_key] packet = Store.hget("#{scope}__tlm__#{target_name}", packet_name) raise "Packet '#{target_name} #{packet_name}' does not exist" unless packet packet_lookup[target_packet_key] = JSON.parse(packet, :allow_nan => true, :create_additions => true) end hash = packet_lookup[target_packet_key] item_result = [] packet_values.each do |value| item_result[0] = hash[value] 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] # 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["#{packet_values[-1]}__L"] item_result[1] = item_result[1].intern if item_result[1] # Convert to symbol else raise "Item '#{target_name} #{packet_name} #{packet_values[-1]}' does not exist" unless hash.key?(packet_values[-1]) item_result[1] = nil 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
136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/openc3/models/cvt_model.rb', line 136 def self.normalize(target_name, packet_name, item_name, type: :ALL, scope: $openc3_scope) if type == :ALL VALUE_TYPES.each do |type| @overrides.delete("#{target_name}__#{packet_name}__#{item_name}__#{type}") end else if VALUE_TYPES.include?(type) @overrides.delete("#{target_name}__#{packet_name}__#{item_name}__#{type}") else raise "Unknown type '#{type}' for #{target_name} #{packet_name} #{item_name}" end end end |
.override(target_name, packet_name, item_name, value, type:, scope: $openc3_scope) ⇒ Object
Override a current value table item such that it always returns the same value for the given type
127 128 129 130 131 132 133 |
# File 'lib/openc3/models/cvt_model.rb', line 127 def self.override(target_name, packet_name, item_name, value, type:, scope: $openc3_scope) if VALUE_TYPES.include?(type) @overrides["#{target_name}__#{packet_name}__#{item_name}__#{type}"] = value else raise "Unknown type '#{type}' for #{target_name} #{packet_name} #{item_name}" end end |
.set(hash, target_name:, packet_name:, scope:) ⇒ Object
Set the current value table for a target, packet
38 39 40 |
# File 'lib/openc3/models/cvt_model.rb', line 38 def self.set(hash, target_name:, packet_name:, scope:) Store.hset("#{scope}__tlm__#{target_name}", packet_name, JSON.generate(hash.as_json(:allow_nan => true))) end |
.set_item(target_name, packet_name, item_name, value, type:, scope:) ⇒ Object
Set an item in the current value table
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/openc3/models/cvt_model.rb', line 43 def self.set_item(target_name, packet_name, item_name, value, type:, scope:) case type when :WITH_UNITS field = "#{item_name}__U" when :FORMATTED field = "#{item_name}__F" when :CONVERTED field = "#{item_name}__C" when :RAW field = item_name else raise "Unknown type '#{type}' for #{target_name} #{packet_name} #{item_name}" end hash = JSON.parse(Store.hget("#{scope}__tlm__#{target_name}", packet_name), :allow_nan => true, :create_additions => true) hash[field] = value Store.hset("#{scope}__tlm__#{target_name}", packet_name, JSON.generate(hash.as_json(:allow_nan => true))) end |