Class: Cosmos::JsonPacket
Instance Attribute Summary collapse
-
#cmd_or_tlm ⇒ Object
Returns the value of attribute cmd_or_tlm.
-
#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.
-
#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, 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_from_epoch, stored, json_data) ⇒ JsonPacket
constructor
A new instance of JsonPacket.
-
#read(name, value_type = :CONVERTED) ⇒ Object
Read an item in the packet by name.
-
#read_all(value_type = :CONVERTED, names = nil) ⇒ Object
Read all items in the packet into an array of arrays [[item name, item value], …].
-
#read_all_names ⇒ 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, 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) ⇒ Object
Constructor Details
#initialize(cmd_or_tlm, target_name, packet_name, time_nsec_from_epoch, stored, json_data) ⇒ JsonPacket
Returns a new instance of JsonPacket.
33 34 35 36 37 38 39 40 |
# File 'lib/cosmos/packets/json_packet.rb', line 33 def initialize(cmd_or_tlm, target_name, packet_name, time_nsec_from_epoch, stored, json_data) @cmd_or_tlm = cmd_or_tlm.intern @target_name = target_name @packet_name = packet_name @packet_time = ::Time.from_nsec_from_epoch(time_nsec_from_epoch) @stored = ConfigParser.handle_true_false(stored) @json_hash = JSON.parse(json_data) end |
Instance Attribute Details
#cmd_or_tlm ⇒ Object
Returns the value of attribute cmd_or_tlm.
26 27 28 |
# File 'lib/cosmos/packets/json_packet.rb', line 26 def cmd_or_tlm @cmd_or_tlm end |
#json_hash ⇒ Object
Returns the value of attribute json_hash.
31 32 33 |
# File 'lib/cosmos/packets/json_packet.rb', line 31 def json_hash @json_hash end |
#packet_name ⇒ Object
Returns the value of attribute packet_name.
28 29 30 |
# File 'lib/cosmos/packets/json_packet.rb', line 28 def packet_name @packet_name end |
#packet_time ⇒ Object
Returns the value of attribute packet_time.
29 30 31 |
# File 'lib/cosmos/packets/json_packet.rb', line 29 def packet_time @packet_time end |
#stored ⇒ Object
Returns the value of attribute stored.
30 31 32 |
# File 'lib/cosmos/packets/json_packet.rb', line 30 def stored @stored end |
#target_name ⇒ Object
Returns the value of attribute target_name.
27 28 29 |
# File 'lib/cosmos/packets/json_packet.rb', line 27 def target_name @target_name end |
Instance Method Details
#formatted(value_type = :CONVERTED, names = nil, indent = 0) ⇒ Object
Create a string that shows the name and value of each item in the packet
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/cosmos/packets/json_packet.rb', line 118 def formatted(value_type = :CONVERTED, names = nil, indent = 0) names = read_all_names() unless names indent_string = ' ' * indent string = '' names.each do |name| value = read(name, value_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) ⇒ Object
Read an item in the packet by name
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/cosmos/packets/json_packet.rb', line 46 def read(name, value_type = :CONVERTED) if value_type == :WITH_UNITS value = @json_hash["#{name}__U"] return value if value end if value_type == :WITH_UNITS or value_type == :FORMATTED value = @json_hash["#{name}__F"] return value if value value = @json_hash["#{name}__C"] return value.to_s if value value = @json_hash[name] return value.to_s if value return nil end if value_type == :CONVERTED value = @json_hash["#{name}__C"] return value if value end value = @json_hash[name] return value if value end |
#read_all(value_type = :CONVERTED, names = nil) ⇒ Object
Read all items in the packet into an array of arrays
[[item name, item value], ...]
82 83 84 85 86 87 88 89 |
# File 'lib/cosmos/packets/json_packet.rb', line 82 def read_all(value_type = :CONVERTED, names = nil) result = {} names = read_all_names() unless names names.each do |name| result[name] = read(name, value_type) end return result end |
#read_all_names ⇒ Object
Read all the names of items in the packet Note: This is not very efficient, ideally only call once for discovery purposes
106 107 108 109 110 111 112 |
# File 'lib/cosmos/packets/json_packet.rb', line 106 def read_all_names result = {} @json_hash.each do |key, value| result[key.split("__")[0]] = true end return result.keys end |
#read_all_with_limits_states(value_type = :CONVERTED, names = nil) ⇒ Object
Read all items in the packet into an array of arrays
[[item name, item value], [item limits state], ...]
95 96 97 98 99 100 101 102 |
# File 'lib/cosmos/packets/json_packet.rb', line 95 def read_all_with_limits_states(value_type = :CONVERTED, names = nil) result = {} names = read_all_names() unless names names.each do |name| result[name] = read_with_limits_state(name, value_type) end return result end |
#read_with_limits_state(name, value_type = :CONVERTED) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/cosmos/packets/json_packet.rb', line 71 def read_with_limits_state(name, value_type = :CONVERTED) value = read(name, value_type) limits_state = @json_hash["#{name}__L"] limits_state.intern if limits_state return [value, limits_state] end |