Class: OpenC3::JsonPacket
Instance Attribute Summary collapse
-
#cmd_or_tlm ⇒ Object
Returns the value of attribute cmd_or_tlm.
-
#extra ⇒ Object
Returns the value of attribute extra.
-
#json_hash ⇒ Object
Returns the value of attribute json_hash.
-
#packet_name ⇒ Object
Returns the value of attribute packet_name.
-
#packet_time ⇒ Object
Returns the value of attribute packet_time.
-
#received_time ⇒ Object
Returns the value of attribute received_time.
-
#stored ⇒ Object
Returns the value of attribute stored.
-
#target_name ⇒ Object
Returns the value of attribute target_name.
Instance Method Summary collapse
-
#formatted(value_type = :CONVERTED, reduced_type = nil, names = nil, indent = 0) ⇒ Object
Create a string that shows the name and value of each item in the packet.
-
#initialize(cmd_or_tlm, target_name, packet_name, time_nsec_since_epoch, stored, json_data, key_map = nil, received_time_nsec_since_epoch: nil, extra: nil) ⇒ JsonPacket
constructor
A new instance of JsonPacket.
-
#read(name, value_type = :CONVERTED, reduced_type = nil) ⇒ Object
Read an item in the packet by name.
-
#read_all(value_type = :CONVERTED, reduced_type = nil, names = nil) ⇒ Object
Read all items in the packet into an array of arrays [[item name, item value], …].
-
#read_all_names(value_type = nil, reduced_type = nil) ⇒ Object
Read all the names of items in the packet Note: This is not very efficient, ideally only call once for discovery purposes.
-
#read_all_with_limits_states(value_type = :CONVERTED, reduced_type = nil, names = nil) ⇒ Object
Read all items in the packet into an array of arrays [[item name, item value], [item limits state], …].
- #read_with_limits_state(name, value_type = :CONVERTED, reduced_type = nil) ⇒ Object
Constructor Details
#initialize(cmd_or_tlm, target_name, packet_name, time_nsec_since_epoch, stored, json_data, key_map = nil, received_time_nsec_since_epoch: nil, extra: nil) ⇒ JsonPacket
Returns a new instance of JsonPacket.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/openc3/packets/json_packet.rb', line 38 def initialize(cmd_or_tlm, target_name, packet_name, time_nsec_since_epoch, stored, json_data, key_map = nil, received_time_nsec_since_epoch: nil, extra: nil) @cmd_or_tlm = cmd_or_tlm.intern @target_name = target_name @packet_name = packet_name @packet_time = ::Time.from_nsec_from_epoch(time_nsec_since_epoch) @stored = ConfigParser.handle_true_false(stored) if received_time_nsec_since_epoch @received_time = ::Time.from_nsec_from_epoch(received_time_nsec_since_epoch) else @received_time = @packet_time end @extra = extra @json_hash = json_data @json_hash = JSON.parse(json_data, :allow_nan => true, :create_additions => true) if String === json_data if key_map uncompressed = {} @json_hash.each do |key, value| uncompressed_key = key_map[key] uncompressed_key = key unless uncompressed_key uncompressed[uncompressed_key] = value end @json_hash = uncompressed end end |
Instance Attribute Details
#cmd_or_tlm ⇒ Object
Returns the value of attribute cmd_or_tlm.
29 30 31 |
# File 'lib/openc3/packets/json_packet.rb', line 29 def cmd_or_tlm @cmd_or_tlm end |
#extra ⇒ Object
Returns the value of attribute extra.
36 37 38 |
# File 'lib/openc3/packets/json_packet.rb', line 36 def extra @extra end |
#json_hash ⇒ Object
Returns the value of attribute json_hash.
34 35 36 |
# File 'lib/openc3/packets/json_packet.rb', line 34 def json_hash @json_hash end |
#packet_name ⇒ Object
Returns the value of attribute packet_name.
31 32 33 |
# File 'lib/openc3/packets/json_packet.rb', line 31 def packet_name @packet_name end |
#packet_time ⇒ Object
Returns the value of attribute packet_time.
32 33 34 |
# File 'lib/openc3/packets/json_packet.rb', line 32 def packet_time @packet_time end |
#received_time ⇒ Object
Returns the value of attribute received_time.
35 36 37 |
# File 'lib/openc3/packets/json_packet.rb', line 35 def received_time @received_time end |
#stored ⇒ Object
Returns the value of attribute stored.
33 34 35 |
# File 'lib/openc3/packets/json_packet.rb', line 33 def stored @stored end |
#target_name ⇒ Object
Returns the value of attribute target_name.
30 31 32 |
# File 'lib/openc3/packets/json_packet.rb', line 30 def target_name @target_name end |
Instance Method Details
#formatted(value_type = :CONVERTED, reduced_type = nil, names = nil, indent = 0) ⇒ Object
Create a string that shows the name and value of each item in the packet
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/openc3/packets/json_packet.rb', line 231 def formatted(value_type = :CONVERTED, reduced_type = nil, names = nil, indent = 0) names = read_all_names() unless names indent_string = ' ' * indent string = '' names.each do |name| value = read(name, value_type, reduced_type) if String === value and value =~ File::NON_ASCII_PRINTABLE string << "#{indent_string}#{name}:\n" string << value.formatted(1, 16, ' ', indent + 2) else string << "#{indent_string}#{name}: #{value}\n" end end return string end |
#read(name, value_type = :CONVERTED, reduced_type = nil) ⇒ Object
Read an item in the packet by name
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/openc3/packets/json_packet.rb', line 67 def read(name, value_type = :CONVERTED, reduced_type = nil) value = nil array_index = nil # Check for array index to handle array items but also make sure there # isn't a REAL item that has brackets in the name if name[-1] == ']' and !@json_hash.include?(name) open_bracket_index = name.index('[') if open_bracket_index array_index = name[(open_bracket_index + 1)..-2].to_i name = name[0..(open_bracket_index - 1)] end end if reduced_type raise "Reduced types only support RAW or CONVERTED value types: #{value_type} unsupported" if value_type == :WITH_UNITS or value_type == :FORMATTED if value_type == :CONVERTED case reduced_type when :AVG value = @json_hash["#{name}__CA"] when :STDDEV value = @json_hash["#{name}__CS"] when :MIN value = @json_hash["#{name}__CN"] when :MAX value = @json_hash["#{name}__CX"] end if value value = value[array_index] if array_index return value end end case reduced_type when :AVG value = @json_hash["#{name}__A"] when :STDDEV value = @json_hash["#{name}__S"] when :MIN value = @json_hash["#{name}__N"] when :MAX value = @json_hash["#{name}__X"] end if value value = value[array_index] if array_index return value end end if value_type == :WITH_UNITS value = @json_hash["#{name}__U"] if value value = value[array_index] if array_index return value end end if value_type == :WITH_UNITS or value_type == :FORMATTED value = @json_hash["#{name}__F"] if value value = value[array_index] if array_index return value end value = @json_hash["#{name}__C"] if value value = value[array_index] if array_index return value.to_s end value = @json_hash[name] if value value = value[array_index] if array_index return value.to_s end return nil end if value_type == :CONVERTED value = @json_hash["#{name}__C"] if value value = value[array_index] if array_index return value end end value = @json_hash[name] if value value = value[array_index] if array_index return value end return nil end |
#read_all(value_type = :CONVERTED, reduced_type = nil, names = nil) ⇒ Object
Read all items in the packet into an array of arrays
[[item name, item value], ...]
168 169 170 171 172 173 174 175 |
# File 'lib/openc3/packets/json_packet.rb', line 168 def read_all(value_type = :CONVERTED, reduced_type = nil, names = nil) result = {} names = read_all_names() unless names names.each do |name| result[name] = read(name, value_type, reduced_type) end return result end |
#read_all_names(value_type = nil, reduced_type = nil) ⇒ Object
Read all the names of items in the packet Note: This is not very efficient, ideally only call once for discovery purposes
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/openc3/packets/json_packet.rb', line 192 def read_all_names(value_type = nil, reduced_type = nil) result = {} if value_type case value_type when :RAW postfix = '' when :CONVERTED postfix = 'C' when :FORMATTED postfix = 'F' when :WITH_UNITS postfix = 'U' end case reduced_type when :MIN postfix << 'N' when :MAX postfix << 'X' when :AVG postfix << 'A' when :STDDEV postfix << 'S' else postfix = nil if value_type == :RAW end @json_hash.each do |key, _value| key_split = key.split("__") result[key_split[0]] = true if key_split[1] == postfix end else @json_hash.each { |key, _value| result[key.split("__")[0]] = true } end return result.keys end |
#read_all_with_limits_states(value_type = :CONVERTED, reduced_type = nil, names = nil) ⇒ Object
Read all items in the packet into an array of arrays
[[item name, item value], [item limits state], ...]
181 182 183 184 185 186 187 188 |
# File 'lib/openc3/packets/json_packet.rb', line 181 def read_all_with_limits_states(value_type = :CONVERTED, reduced_type = nil, names = nil) result = {} names = read_all_names() unless names names.each do |name| result[name] = read_with_limits_state(name, value_type, reduced_type) end return result end |
#read_with_limits_state(name, value_type = :CONVERTED, reduced_type = nil) ⇒ Object
155 156 157 158 159 160 161 162 |
# File 'lib/openc3/packets/json_packet.rb', line 155 def read_with_limits_state(name, value_type = :CONVERTED, reduced_type = nil) value = read(name, value_type, reduced_type) limits_state = @json_hash["#{name}__L"] if limits_state limits_state = limits_state.intern end return [value, limits_state] end |