Class: Cosmos::Packet
- Defined in:
- lib/cosmos/packets/packet.rb,
lib/cosmos/utilities/simulated_target.rb
Overview
Adds features common to all COSMOS packets of data to the Structure class. This includes the additional attributes listed below. The primary behavior Packet adds is the ability to apply formatting to PacketItem values as well as managing PacketItem’s limit states.
Direct Known Subclasses
Constant Summary collapse
- RESERVED_ITEM_NAMES =
['PACKET_TIMESECONDS'.freeze, 'PACKET_TIMEFORMATTED'.freeze, 'RECEIVED_TIMESECONDS'.freeze, 'RECEIVED_TIMEFORMATTED'.freeze, 'RECEIVED_COUNT'.freeze]
- CATCH_ALL_STATE =
'ANY'- VALUE_TYPES =
Valid format types
[:RAW, :CONVERTED, :FORMATTED, :WITH_UNITS]
Constants inherited from Structure
Structure::ASCII_8BIT_STRING, Structure::ZERO_STRING
Instance Attribute Summary collapse
-
#abstract ⇒ Boolean
Whether or not this is a ‘abstract’ packet.
-
#description ⇒ String
Description of the packet.
-
#disabled ⇒ Boolean
Whether or not this is a ‘disabled’ packet.
-
#extra ⇒ Hash
Extra data to be logged/transferred with packet.
-
#given_values ⇒ Hash<Item Name, Value>
Contains the values given by the user for a command (distinguished from defaults) These values should be used within command conversions if present because the order that values are written into the actual packet can vary.
-
#hazardous ⇒ Boolean
Flag indicating if the packet is hazardous (typically for commands).
-
#hazardous_description ⇒ String
Description of why the packet is hazardous.
-
#hidden ⇒ Boolean
Whether or not this is a ‘hidden’ packet.
-
#messages_disabled ⇒ Boolean
Whether or not messages should be printed for this packet.
-
#packet_name ⇒ String
Name of the packet.
-
#packet_rate ⇒ Object
Returns the value of attribute packet_rate.
-
#raw ⇒ Boolean
Whether or not this is a ‘raw’ packet.
-
#received_count ⇒ Integer
Number of times the packet has been received.
-
#received_time ⇒ Time
Time at which the packet was received.
-
#stale ⇒ Boolean
readonly
Flag indicating if the packet is stale (hasn’t been received recently).
-
#stored ⇒ Boolean
Whether or not this was a stored packet.
-
#target_name ⇒ String
Name of the target this packet is associated with.
Attributes inherited from Structure
#default_endianness, #defined_length, #defined_length_bits, #fixed_size, #items, #short_buffer_allowed, #sorted_items
Class Method Summary collapse
-
.next_bit_offset(item) ⇒ Integer
Returns the bit offset of the next item after the current item if items are packed.
Instance Method Summary collapse
-
#append_item(name, bit_size, data_type, array_size = nil, endianness = @default_endianness, overflow = :ERROR, format_string = nil, read_conversion = nil, write_conversion = nil, id_value = nil) ⇒ PacketItem
Define an item at the end of the packet.
-
#buffer=(buffer) ⇒ Object
Set the buffer to be used by the structure.
-
#check_bit_offsets ⇒ Array<String>
Review bit offset to look for overlapping definitions.
-
#check_limits(limits_set = :DEFAULT, ignore_persistence = false) ⇒ Object
Check all the items in the packet against their defined limits.
-
#clone ⇒ Packet
(also: #dup)
Make a light weight clone of this packet.
-
#config_name ⇒ Object
Calculates a unique hashing sum that changes if the parts of the packet configuration change that could affect the “shape” of the packet.
-
#define(item) ⇒ PacketItem
Add an item to the packet by adding it to the items hash.
-
#define_item(name, bit_offset, bit_size, data_type, array_size = nil, endianness = @default_endianness, overflow = :ERROR, format_string = nil, read_conversion = nil, write_conversion = nil, id_value = nil) ⇒ PacketItem
Define an item in the packet.
-
#define_reserved_items ⇒ Object
Define the reserved items on the current telemetry packet.
-
#disable_limits(name) ⇒ Object
Disable limits on an item by name.
-
#enable_limits(name) ⇒ Object
Enable limits on an item by name.
-
#formatted(value_type = :CONVERTED, indent = 0, buffer = @buffer, ignored = nil) ⇒ String
Create a string that shows the name and value of each item in the packet.
-
#get_item(name) ⇒ StructureItem
StructureItem or one of its subclasses.
-
#id_items ⇒ Array<PacketItem>
Id items are used by the identify? method to determine if a raw buffer of data represents this packet.
-
#identified? ⇒ TrueClass or FalseClass
Indicates if the packet has been identified.
-
#identify?(buffer) ⇒ Boolean
Tries to identify if a buffer represents the currently defined packet.
-
#initialize(target_name, packet_name, default_endianness = :BIG_ENDIAN, description = nil, buffer = '', item_class = PacketItem) ⇒ Packet
constructor
Creates a new packet by initalizing the attributes.
-
#limits_change_callback=(limits_change_callback) ⇒ Object
Sets the callback object called when a limits state changes.
-
#limits_items ⇒ Array<PacketItem>
All items with defined limits.
-
#meta ⇒ Hash<Meta Name, Meta Values>
Returns packet specific metadata.
-
#out_of_limits ⇒ Array<Array<String, String, String, Symbol>>
Return an array of arrays indicating all items in the packet that are out of limits [[target name, packet name, item name, item limits state], …].
-
#packed? ⇒ Boolean
Checks if the packet has any gaps or overlapped items.
-
#packet_time ⇒ Object
Returns @received_time unless a packet item called PACKET_TIME exists that returns a Ruby Time object that represents a different timestamp for the packet.
-
#processors ⇒ Hash
Hash of processors associated with this packet.
-
#read(name, value_type = :CONVERTED, buffer = @buffer) ⇒ Object
Read an item in the packet by name.
-
#read_all(value_type = :CONVERTED, buffer = @buffer, top = true) ⇒ Array<Array>
Read all items in the packet into an array of arrays [[item name, item value], …].
-
#read_all_with_limits_states(value_type = :CONVERTED, buffer = @buffer) ⇒ Array<String, Object, Symbol|nil>
Read all items in the packet into an array of arrays [[item name, item value], [item limits state], …].
-
#read_id_values(buffer) ⇒ Array
Reads the values from a buffer at the position of each id_item defined in the packet.
-
#read_item(item, value_type = :CONVERTED, buffer = @buffer) ⇒ Object
Read an item in the packet.
-
#reset ⇒ Object
Reset temporary packet data This includes packet received time, received count, and processor state.
-
#restore_defaults(buffer = @buffer, skip_item_names = nil) ⇒ Object
Restore all items in the packet to their default value.
-
#set_all_limits_states(state) ⇒ Object
Set the limits state for all items to the given state.
-
#set_received_time_fast(received_time) ⇒ Object
Sets the received time of the packet (without cloning).
-
#set_stale ⇒ Object
Sets the overall packet stale state to true and sets each packet item limits state to :STALE.
- #to_config(cmd_or_tlm) ⇒ Object
- #update_id_items(item) ⇒ Object
-
#update_limits_items_cache(item) ⇒ Object
Add an item to the limits items cache if necessary.
-
#write(name, value, value_type = :CONVERTED, buffer = @buffer) ⇒ Object
Write an item in the packet by name.
-
#write_item(item, value, value_type = :CONVERTED, buffer = @buffer) ⇒ Object
Write an item in the packet.
Methods inherited from Structure
#append, #buffer, #defined?, #delete_item, #enable_method_missing, #length, #rename_item, #resize_buffer, #set_item
Constructor Details
#initialize(target_name, packet_name, default_endianness = :BIG_ENDIAN, description = nil, buffer = '', item_class = PacketItem) ⇒ Packet
Creates a new packet by initalizing the attributes.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/cosmos/packets/packet.rb', line 89 def initialize(target_name, packet_name, default_endianness = :BIG_ENDIAN, description = nil, buffer = '', item_class = PacketItem) super(default_endianness, buffer, item_class) self.target_name = target_name self.packet_name = packet_name self.description = description @received_time = nil @received_count = 0 @id_items = nil @hazardous = false @hazardous_description = nil @given_values = nil @limits_items = nil @processors = nil @stale = true @limits_change_callback = nil @read_conversion_cache = nil @raw = nil @messages_disabled = false @meta = nil @hidden = false @disabled = false @stored = false @extra = nil end |
Instance Attribute Details
#abstract ⇒ Boolean
Returns Whether or not this is a ‘abstract’ packet.
68 69 70 |
# File 'lib/cosmos/packets/packet.rb', line 68 def abstract @abstract end |
#description ⇒ String
Returns Description of the packet.
32 33 34 |
# File 'lib/cosmos/packets/packet.rb', line 32 def description @description end |
#disabled ⇒ Boolean
Returns Whether or not this is a ‘disabled’ packet.
62 63 64 |
# File 'lib/cosmos/packets/packet.rb', line 62 def disabled @disabled end |
#extra ⇒ Hash
Returns Extra data to be logged/transferred with packet.
74 75 76 |
# File 'lib/cosmos/packets/packet.rb', line 74 def extra @extra end |
#given_values ⇒ Hash<Item Name, Value>
Contains the values given by the user for a command (distinguished from defaults) These values should be used within command conversions if present because the order that values are written into the actual packet can vary
50 51 52 |
# File 'lib/cosmos/packets/packet.rb', line 50 def given_values @given_values end |
#hazardous ⇒ Boolean
Returns Flag indicating if the packet is hazardous (typically for commands).
41 42 43 |
# File 'lib/cosmos/packets/packet.rb', line 41 def hazardous @hazardous end |
#hazardous_description ⇒ String
Returns Description of why the packet is hazardous.
44 45 46 |
# File 'lib/cosmos/packets/packet.rb', line 44 def hazardous_description @hazardous_description end |
#hidden ⇒ Boolean
Returns Whether or not this is a ‘hidden’ packet.
59 60 61 |
# File 'lib/cosmos/packets/packet.rb', line 59 def hidden @hidden end |
#messages_disabled ⇒ Boolean
Returns Whether or not messages should be printed for this packet.
65 66 67 |
# File 'lib/cosmos/packets/packet.rb', line 65 def @messages_disabled end |
#packet_name ⇒ String
Returns Name of the packet.
29 30 31 |
# File 'lib/cosmos/packets/packet.rb', line 29 def packet_name @packet_name end |
#packet_rate ⇒ Object
Returns the value of attribute packet_rate.
17 18 19 |
# File 'lib/cosmos/utilities/simulated_target.rb', line 17 def packet_rate @packet_rate end |
#raw ⇒ Boolean
Returns Whether or not this is a ‘raw’ packet.
56 57 58 |
# File 'lib/cosmos/packets/packet.rb', line 56 def raw @raw end |
#received_count ⇒ Integer
Returns Number of times the packet has been received.
38 39 40 |
# File 'lib/cosmos/packets/packet.rb', line 38 def received_count @received_count end |
#received_time ⇒ Time
Returns Time at which the packet was received.
35 36 37 |
# File 'lib/cosmos/packets/packet.rb', line 35 def received_time @received_time end |
#stale ⇒ Boolean (readonly)
Returns Flag indicating if the packet is stale (hasn’t been received recently).
53 54 55 |
# File 'lib/cosmos/packets/packet.rb', line 53 def stale @stale end |
#stored ⇒ Boolean
Returns Whether or not this was a stored packet.
71 72 73 |
# File 'lib/cosmos/packets/packet.rb', line 71 def stored @stored end |
#target_name ⇒ String
Returns Name of the target this packet is associated with.
26 27 28 |
# File 'lib/cosmos/packets/packet.rb', line 26 def target_name @target_name end |
Class Method Details
.next_bit_offset(item) ⇒ Integer
Returns the bit offset of the next item after the current item if items are packed
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/cosmos/packets/packet.rb', line 373 def self.next_bit_offset(item) if item.array_size if item.array_size > 0 next_offset = item.bit_offset + item.array_size else next_offset = item.array_size end else next_offset = nil if item.bit_offset > 0 if item.little_endian_bit_field? # Bit offset always refers to the most significant bit of a bitfield bits_remaining_in_last_byte = 8 - (item.bit_offset % 8) if item.bit_size > bits_remaining_in_last_byte next_offset = item.bit_offset + bits_remaining_in_last_byte end end end unless next_offset if item.bit_size > 0 next_offset = item.bit_offset + item.bit_size else next_offset = item.bit_size end end end next_offset end |
Instance Method Details
#append_item(name, bit_size, data_type, array_size = nil, endianness = @default_endianness, overflow = :ERROR, format_string = nil, read_conversion = nil, write_conversion = nil, id_value = nil) ⇒ PacketItem
Define an item at the end of the packet. This creates a new instance of the item_class as given in the constructor and adds it to the items hash. It also resizes the buffer to accomodate the new item.
488 489 490 491 |
# File 'lib/cosmos/packets/packet.rb', line 488 def append_item(name, bit_size, data_type, array_size = nil, endianness = @default_endianness, overflow = :ERROR, format_string = nil, read_conversion = nil, write_conversion = nil, id_value = nil) item = super(name, bit_size, data_type, array_size, endianness, overflow) packet_define_item(item, format_string, read_conversion, write_conversion, id_value) end |
#buffer=(buffer) ⇒ Object
Set the buffer to be used by the structure. The buffer is copied and thus further modifications to the buffer have no effect on the structure items.
269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/cosmos/packets/packet.rb', line 269 def buffer=(buffer) synchronize() do begin internal_buffer_equals(buffer) rescue RuntimeError Logger.instance.error "#{@target_name} #{@packet_name} received with actual packet length of #{buffer.length} but defined length of #{@defined_length}" end @read_conversion_cache.clear if @read_conversion_cache process() end end |
#check_bit_offsets ⇒ Array<String>
Review bit offset to look for overlapping definitions. This will allow gaps in the packet, but not allow the same bits to be used for multiple variables.
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/cosmos/packets/packet.rb', line 339 def check_bit_offsets expected_next_offset = nil previous_item = nil warnings = [] @sorted_items.each do |item| if expected_next_offset and (item.bit_offset < expected_next_offset) and !item.overlap msg = "Bit definition overlap at bit offset #{item.bit_offset} for packet #{@target_name} #{@packet_name} items #{item.name} and #{previous_item.name}" Logger.instance.warn(msg) warnings << msg end expected_next_offset = Packet.next_bit_offset(item) previous_item = item end warnings end |
#check_limits(limits_set = :DEFAULT, ignore_persistence = false) ⇒ Object
Check all the items in the packet against their defined limits. Update their internal limits state and persistence and call the limits_change_callback as necessary.
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 |
# File 'lib/cosmos/packets/packet.rb', line 805 def check_limits(limits_set = :DEFAULT, ignore_persistence = false) # If check_limits is being called, then a new packet has arrived and # this packet is no longer stale # Stored telemetry doesn't affect the current value table and such doesn't affect stale if @stale and !@stored @stale = false set_all_limits_states(nil) end return unless @limits_items @limits_items.each do |item| # Verify limits monitoring is enabled for this item if item.limits.enabled value = read_item(item) # Handle state monitoring and value monitoring differently if item.states handle_limits_states(item, value) elsif item.limits.values handle_limits_values(item, value, limits_set, ignore_persistence) end end end end |
#clone ⇒ Packet Also known as: dup
Make a light weight clone of this packet. This only creates a new buffer of data and clones the processors. The defined packet items are the same.
863 864 865 866 867 868 869 870 871 872 873 874 |
# File 'lib/cosmos/packets/packet.rb', line 863 def clone packet = super() if packet.instance_variable_get("@processors".freeze) packet.instance_variable_set("@processors".freeze, packet.processors.clone) packet.processors.each do |processor_name, processor| packet.processors[processor_name] = processor.clone end end packet.instance_variable_set("@read_conversion_cache".freeze, nil) packet.extra = JSON.parse(packet.extra.to_json) if packet.extra # Deep copy using JSON packet end |
#config_name ⇒ Object
Calculates a unique hashing sum that changes if the parts of the packet configuration change that could affect the “shape” of the packet. This value is cached and that packet should not be changed if this method is being used
254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/cosmos/packets/packet.rb', line 254 def config_name return @config_name if @config_name string = "#{self.target_name} #{self.packet_name}" @sorted_items.each do |item| string << " ITEM #{item.name} #{item.bit_offset} #{item.bit_size} #{item.data_type} #{item.array_size} #{item.endianness} #{item.overflow} #{item.states} #{item.read_conversion ? item.read_conversion.class : 'NO_CONVERSION'}" end # Use the hashing algorithm established by Cosmos::System digest = Digest.const_get(System.hashing_algorithm).send('new') digest << string @config_name = digest.hexdigest @config_name end |
#define(item) ⇒ PacketItem
Add an item to the packet by adding it to the items hash. It also resizes the buffer to accomodate the new item.
466 467 468 469 470 471 |
# File 'lib/cosmos/packets/packet.rb', line 466 def define(item) item = super(item) update_id_items(item) update_limits_items_cache(item) item end |
#define_item(name, bit_offset, bit_size, data_type, array_size = nil, endianness = @default_endianness, overflow = :ERROR, format_string = nil, read_conversion = nil, write_conversion = nil, id_value = nil) ⇒ PacketItem
Define an item in the packet. This creates a new instance of the item_class as given in the constructor and adds it to the items hash. It also resizes the buffer to accomodate the new item.
456 457 458 459 |
# File 'lib/cosmos/packets/packet.rb', line 456 def define_item(name, bit_offset, bit_size, data_type, array_size = nil, endianness = @default_endianness, overflow = :ERROR, format_string = nil, read_conversion = nil, write_conversion = nil, id_value = nil) item = super(name, bit_offset, bit_size, data_type, array_size, endianness, overflow) packet_define_item(item, format_string, read_conversion, write_conversion, id_value) end |
#define_reserved_items ⇒ Object
Define the reserved items on the current telemetry packet
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 |
# File 'lib/cosmos/packets/packet.rb', line 721 def define_reserved_items item = define_item('PACKET_TIMESECONDS', 0, 0, :DERIVED, nil, @default_endianness, :ERROR, '%0.6f', PacketTimeSecondsConversion.new) item.description = 'COSMOS Packet Time (UTC, Floating point, Unix epoch)' item = define_item('PACKET_TIMEFORMATTED', 0, 0, :DERIVED, nil, @default_endianness, :ERROR, nil, PacketTimeFormattedConversion.new) item.description = 'COSMOS Packet Time (Local time zone, Formatted string)' item = define_item('RECEIVED_TIMESECONDS', 0, 0, :DERIVED, nil, @default_endianness, :ERROR, '%0.6f', ReceivedTimeSecondsConversion.new) item.description = 'COSMOS Received Time (UTC, Floating point, Unix epoch)' item = define_item('RECEIVED_TIMEFORMATTED', 0, 0, :DERIVED, nil, @default_endianness, :ERROR, nil, ReceivedTimeFormattedConversion.new) item.description = 'COSMOS Received Time (Local time zone, Formatted string)' item = define_item('RECEIVED_COUNT', 0, 0, :DERIVED, nil, @default_endianness, :ERROR, nil, ReceivedCountConversion.new) item.description = 'COSMOS packet received count' end |
#disable_limits(name) ⇒ Object
Disable limits on an item by name
749 750 751 752 753 754 755 756 757 |
# File 'lib/cosmos/packets/packet.rb', line 749 def disable_limits(name) item = get_item(name) item.limits.enabled = false unless item.limits.state == :STALE old_limits_state = item.limits.state item.limits.state = nil @limits_change_callback.call(self, item, old_limits_state, nil, false) if @limits_change_callback end end |
#enable_limits(name) ⇒ Object
Enable limits on an item by name
742 743 744 |
# File 'lib/cosmos/packets/packet.rb', line 742 def enable_limits(name) get_item(name).limits.enabled = true end |
#formatted(value_type = :CONVERTED, indent = 0, buffer = @buffer, ignored = nil) ⇒ String
Create a string that shows the name and value of each item in the packet
705 706 707 |
# File 'lib/cosmos/packets/packet.rb', line 705 def formatted(value_type = :CONVERTED, indent = 0, buffer = @buffer, ignored = nil) return super(value_type, indent, buffer, ignored) end |
#get_item(name) ⇒ StructureItem
Returns StructureItem or one of its subclasses.
494 495 496 497 498 |
# File 'lib/cosmos/packets/packet.rb', line 494 def get_item(name) super(name) rescue ArgumentError raise "Packet item '#{@target_name} #{@packet_name} #{name.upcase}' does not exist" end |
#id_items ⇒ Array<PacketItem>
Id items are used by the identify? method to determine if a raw buffer of data represents this packet.
405 406 407 |
# File 'lib/cosmos/packets/packet.rb', line 405 def id_items @id_items ||= [] end |
#identified? ⇒ TrueClass or FalseClass
Indicates if the packet has been identified
427 428 429 |
# File 'lib/cosmos/packets/packet.rb', line 427 def identified? !@target_name.nil? && !@packet_name.nil? end |
#identify?(buffer) ⇒ Boolean
Tries to identify if a buffer represents the currently defined packet. It does this by iterating over all the packet items that were created with an ID value and checking whether that ID value is present at the correct location in the buffer.
Incorrectly sized buffers will still positively identify if there is enough data to match the ID values. This is to allow incorrectly sized packets to still be processed as well as possible given the incorrectly sized data.
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/cosmos/packets/packet.rb', line 204 def identify?(buffer) return false unless buffer return true unless @id_items @id_items.each do |item| begin value = read_item(item, :RAW, buffer) rescue Exception value = nil end return false if item.id_value != value end true end |
#limits_change_callback=(limits_change_callback) ⇒ Object
Sets the callback object called when a limits state changes
325 326 327 328 329 330 331 332 |
# File 'lib/cosmos/packets/packet.rb', line 325 def limits_change_callback=(limits_change_callback) if limits_change_callback raise ArgumentError, "limits_change_callback must respond to call" unless limits_change_callback.respond_to?(:call) @limits_change_callback = limits_change_callback else @limits_change_callback = nil end end |
#limits_items ⇒ Array<PacketItem>
Returns All items with defined limits.
410 411 412 |
# File 'lib/cosmos/packets/packet.rb', line 410 def limits_items @limits_items ||= [] end |
#meta ⇒ Hash<Meta Name, Meta Values>
Returns packet specific metadata
421 422 423 |
# File 'lib/cosmos/packets/packet.rb', line 421 def @meta ||= {} end |
#out_of_limits ⇒ Array<Array<String, String, String, Symbol>>
Return an array of arrays indicating all items in the packet that are out of limits
[[target name, packet name, item name, item limits state], ...]
778 779 780 781 782 783 784 785 786 787 788 |
# File 'lib/cosmos/packets/packet.rb', line 778 def out_of_limits items = [] return items unless @limits_items @limits_items.each do |item| if (item.limits.enabled && item.limits.state && PacketItemLimits::OUT_OF_LIMITS_STATES.include?(item.limits.state)) items << [@target_name, @packet_name, item.name, item.limits.state] end end return items end |
#packed? ⇒ Boolean
Checks if the packet has any gaps or overlapped items
358 359 360 361 362 363 364 365 366 367 |
# File 'lib/cosmos/packets/packet.rb', line 358 def packed? expected_next_offset = nil @sorted_items.each do |item| if expected_next_offset and item.bit_offset != expected_next_offset return false end expected_next_offset = Packet.next_bit_offset(item) end true end |
#packet_time ⇒ Object
Returns @received_time unless a packet item called PACKET_TIME exists that returns a Ruby Time object that represents a different timestamp for the packet
243 244 245 246 247 248 249 250 |
# File 'lib/cosmos/packets/packet.rb', line 243 def packet_time item = @items['PACKET_TIME'.freeze] if item return read_item(item, :CONVERTED, @buffer) else return @received_time end end |
#processors ⇒ Hash
Returns Hash of processors associated with this packet.
415 416 417 |
# File 'lib/cosmos/packets/packet.rb', line 415 def processors @processors ||= {} end |
#read(name, value_type = :CONVERTED, buffer = @buffer) ⇒ Object
Read an item in the packet by name
655 656 657 |
# File 'lib/cosmos/packets/packet.rb', line 655 def read(name, value_type = :CONVERTED, buffer = @buffer) return super(name, value_type, buffer) end |
#read_all(value_type = :CONVERTED, buffer = @buffer, top = true) ⇒ Array<Array>
Read all items in the packet into an array of arrays
[[item name, item value], ...]
676 677 678 |
# File 'lib/cosmos/packets/packet.rb', line 676 def read_all(value_type = :CONVERTED, buffer = @buffer, top = true) return super(value_type, buffer, top) end |
#read_all_with_limits_states(value_type = :CONVERTED, buffer = @buffer) ⇒ Array<String, Object, Symbol|nil>
Read all items in the packet into an array of arrays
[[item name, item value], [item limits state], ...]
688 689 690 691 692 693 694 695 696 |
# File 'lib/cosmos/packets/packet.rb', line 688 def read_all_with_limits_states(value_type = :CONVERTED, buffer = @buffer) result = nil synchronize_allow_reads(true) do result = read_all(value_type, buffer, false).map! do |array| array << @items[array[0]].limits.state end end return result end |
#read_id_values(buffer) ⇒ Array
Reads the values from a buffer at the position of each id_item defined in the packet.
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/cosmos/packets/packet.rb', line 225 def read_id_values(buffer) return [] unless buffer return [] unless @id_items values = [] @id_items.each do |item| begin values << read_item(item, :RAW, buffer) rescue Exception values << nil end end values end |
#read_item(item, value_type = :CONVERTED, buffer = @buffer) ⇒ Object
Read an item in the packet
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 |
# File 'lib/cosmos/packets/packet.rb', line 509 def read_item(item, value_type = :CONVERTED, buffer = @buffer) value = super(item, :RAW, buffer) derived_raw = false if item.data_type == :DERIVED && value_type == :RAW value_type = :CONVERTED derived_raw = true end case value_type when :RAW # Done above when :CONVERTED, :FORMATTED, :WITH_UNITS if item.read_conversion using_cached_value = false check_cache = buffer.equal?(@buffer) if check_cache and @read_conversion_cache synchronize_allow_reads() do if @read_conversion_cache[item] value = @read_conversion_cache[item] # Make sure cached value is not modified by anyone by creating # a deep copy if String === value value = value.clone elsif Array === value value = Marshal.load(Marshal.dump(value)) end using_cached_value = true end end end unless using_cached_value if item.array_size value.map! do |val, index| item.read_conversion.call(val, self, buffer) end else value = item.read_conversion.call(value, self, buffer) end if check_cache synchronize_allow_reads() do @read_conversion_cache ||= {} @read_conversion_cache[item] = value # Make sure cached value is not modified by anyone by creating # a deep copy if String === value value = value.clone elsif Array === value value = Marshal.load(Marshal.dump(value)) end end end end end # Derived raw values perform read_conversions but nothing else return value if derived_raw # Convert from value to state if possible if item.states if Array === value value = value.map do |val, index| if item.states.key(val) item.states.key(val) elsif item.states.values.include?(CATCH_ALL_STATE) item.states.key(CATCH_ALL_STATE) else apply_format_string_and_units(item, val, value_type) end end else state_value = item.states.key(value) if state_value value = state_value elsif item.states.values.include?(CATCH_ALL_STATE) value = item.states.key(CATCH_ALL_STATE) else value = apply_format_string_and_units(item, value, value_type) end end else if Array === value value = value.map do |val, index| apply_format_string_and_units(item, val, value_type) end else value = apply_format_string_and_units(item, value, value_type) end end else raise ArgumentError, "Unknown value type on read: #{value_type}" end return value end |
#reset ⇒ Object
Reset temporary packet data This includes packet received time, received count, and processor state
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 |
# File 'lib/cosmos/packets/packet.rb', line 839 def reset # The SYSTEM META packet is a special case that does not get reset return if @target_name == 'SYSTEM' && @packet_name == 'META' @received_time = nil @received_count = 0 @stored = false @extra = nil if @read_conversion_cache synchronize() do @read_conversion_cache.clear end end return unless @processors @processors.each do |processor_name, processor| processor.reset end end |
#restore_defaults(buffer = @buffer, skip_item_names = nil) ⇒ Object
Restore all items in the packet to their default value
713 714 715 716 717 718 |
# File 'lib/cosmos/packets/packet.rb', line 713 def restore_defaults(buffer = @buffer, skip_item_names = nil) upcase_skip_item_names = skip_item_names.map(&:upcase) if skip_item_names @sorted_items.each do |item| write_item(item, item.default, :CONVERTED, buffer) unless (skip_item_names and upcase_skip_item_names.include?(item.name)) end end |
#set_all_limits_states(state) ⇒ Object
Set the limits state for all items to the given state
793 794 795 |
# File 'lib/cosmos/packets/packet.rb', line 793 def set_all_limits_states(state) @sorted_items.each {|item| item.limits.state = state} end |
#set_received_time_fast(received_time) ⇒ Object
Sets the received time of the packet (without cloning)
284 285 286 287 288 289 290 291 292 |
# File 'lib/cosmos/packets/packet.rb', line 284 def set_received_time_fast(received_time) @received_time = received_time @received_time.freeze if @received_time if @read_conversion_cache synchronize() do @read_conversion_cache.clear end end end |
#set_stale ⇒ Object
Sets the overall packet stale state to true and sets each packet item limits state to :STALE.
832 833 834 835 |
# File 'lib/cosmos/packets/packet.rb', line 832 def set_stale @stale = true set_all_limits_states(:STALE) end |
#to_config(cmd_or_tlm) ⇒ Object
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 |
# File 'lib/cosmos/packets/packet.rb', line 897 def to_config(cmd_or_tlm) config = '' if cmd_or_tlm == :TELEMETRY config << "TELEMETRY #{self.target_name.to_s.quote_if_necessary} #{self.packet_name.to_s.quote_if_necessary} #{@default_endianness} \"#{self.description}\"\n" else config << "COMMAND #{self.target_name.to_s.quote_if_necessary} #{self.packet_name.to_s.quote_if_necessary} #{@default_endianness} \"#{self.description}\"\n" end config << " ALLOW_SHORT\n" if self.short_buffer_allowed config << " HAZARDOUS #{self.hazardous_description.to_s.quote_if_necessary}\n" if self.hazardous config << " DISABLE_MESSAGES\n" if self. if self.disabled config << " DISABLED\n" elsif self.hidden config << " HIDDEN\n" end if @processors @processors.each do |processor_name, processor| config << processor.to_config end end if @meta @meta.each do |key, values| config << " META #{key.to_s.quote_if_necessary} #{values.map {|a| a..to_s.quote_if_necessary}.join(" ")}\n" end end # Items with derived items last self.sorted_items.each do |item| if item.data_type != :DERIVED config << item.to_config(cmd_or_tlm, @default_endianness) end end self.sorted_items.each do |item| if item.data_type == :DERIVED unless RESERVED_ITEM_NAMES.include?(item.name) config << item.to_config(cmd_or_tlm, @default_endianness) end end end config end |
#update_id_items(item) ⇒ Object
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 |
# File 'lib/cosmos/packets/packet.rb', line 877 def update_id_items(item) if item.id_value @id_items ||= [] # Add to Id Items unless @id_items.empty? last_item = @id_items[-1] @id_items << item # If the current item or last item have a negative offset then we have # to re-sort. We also re-sort if the current item is less than the last # item because we are inserting. if last_item.bit_offset <= 0 or item.bit_offset <= 0 or item.bit_offset < last_item.bit_offset @id_items = @id_items.sort end else @id_items << item end end item end |
#update_limits_items_cache(item) ⇒ Object
Add an item to the limits items cache if necessary. You MUST call this after adding limits to an item This is an optimization so we don’t have to iterate through all the items when checking for limits.
763 764 765 766 767 768 769 770 771 772 |
# File 'lib/cosmos/packets/packet.rb', line 763 def update_limits_items_cache(item) if item.limits.values || item.state_colors @limits_items ||= [] @limits_items_hash ||= {} unless @limits_items_hash[item] @limits_items << item @limits_items_hash[item] = true end end end |
#write(name, value, value_type = :CONVERTED, buffer = @buffer) ⇒ Object
Write an item in the packet by name
665 666 667 |
# File 'lib/cosmos/packets/packet.rb', line 665 def write(name, value, value_type = :CONVERTED, buffer = @buffer) super(name, value, value_type, buffer) end |
#write_item(item, value, value_type = :CONVERTED, buffer = @buffer) ⇒ Object
Write an item in the packet
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 |
# File 'lib/cosmos/packets/packet.rb', line 613 def write_item(item, value, value_type = :CONVERTED, buffer = @buffer) case value_type when :RAW super(item, value, value_type, buffer) when :CONVERTED if item.states # Convert from state to value if possible state_value = item.states[value.to_s.upcase] value = state_value if state_value end if item.write_conversion value = item.write_conversion.call(value, self, buffer) else raise "Cannot write DERIVED item without a write conversion" if item.data_type == :DERIVED end begin super(item, value, :RAW, buffer) unless item.data_type == :DERIVED rescue ArgumentError => err if item.states and String === value and err. =~ /invalid value for/ raise "Unknown state #{value} for #{item.name}" else raise err end end when :FORMATTED, :WITH_UNITS raise ArgumentError, "Invalid value type on write: #{value_type}" else raise ArgumentError, "Unknown value type on write: #{value_type}" end if @read_conversion_cache synchronize() do @read_conversion_cache.clear end end end |