Class: OpenC3::TemplateAccessor
- Defined in:
- lib/openc3/accessors/template_accessor.rb
Instance Attribute Summary
Attributes inherited from Accessor
Instance Method Summary collapse
- #configure ⇒ Object
-
#enforce_derived_write_conversion(_item) ⇒ Object
If this is true it will enforce that COSMOS DERIVED items must have a write_conversion to be written.
-
#enforce_encoding ⇒ Object
If this is set it will enforce that buffer data is encoded in a specific encoding.
-
#enforce_length ⇒ Object
This affects whether the Packet class enforces the buffer length at all.
-
#enforce_short_buffer_allowed ⇒ Object
This sets the short_buffer_allowed flag in the Packet class which allows packets that have a buffer shorter than the defined size.
-
#initialize(packet, left_char = '<', right_char = '>') ⇒ TemplateAccessor
constructor
A new instance of TemplateAccessor.
- #read_item(item, buffer) ⇒ Object
- #read_items(items, buffer) ⇒ Object
- #write_item(item, value, buffer) ⇒ Object
- #write_items(items, values, buffer) ⇒ Object
Methods inherited from Accessor
#args, convert_to_type, read_item, read_items, write_item, write_items
Constructor Details
#initialize(packet, left_char = '<', right_char = '>') ⇒ TemplateAccessor
Returns a new instance of TemplateAccessor.
23 24 25 26 27 28 29 |
# File 'lib/openc3/accessors/template_accessor.rb', line 23 def initialize(packet, left_char = '<', right_char = '>') super(packet) @left_char = left_char @right_char = right_char @configured = false @args = [left_char, right_char] end |
Instance Method Details
#configure ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/openc3/accessors/template_accessor.rb', line 31 def configure return if @configured escaped_left_char = @left_char escaped_left_char = "\\#{@left_char}" if @left_char == '(' escaped_right_char = @right_char escaped_right_char = "\\#{@right_char}" if @right_char == ')' # Convert the template into a Regexp for reading each item template = @packet.template.dup template_items = template.scan(Regexp.new("#{escaped_left_char}.*?#{escaped_right_char}")) escaped_read_template = Regexp.escape(template) @item_keys = [] template_items.each do |item| @item_keys << item[1..-2] # If they're using parens we have to escape them # since we're working with the already escaped template item = "\\#{item}" if @left_char == '(' item = "#{item[0..-2]}\\)" if @right_char == ')' escaped_read_template.gsub!(item, "(.*)") end @read_regexp = Regexp.new(escaped_read_template) @configured = true end |
#enforce_derived_write_conversion(_item) ⇒ Object
If this is true it will enforce that COSMOS DERIVED items must have a write_conversion to be written
144 145 146 |
# File 'lib/openc3/accessors/template_accessor.rb', line 144 def enforce_derived_write_conversion(_item) return true end |
#enforce_encoding ⇒ Object
If this is set it will enforce that buffer data is encoded in a specific encoding
124 125 126 |
# File 'lib/openc3/accessors/template_accessor.rb', line 124 def enforce_encoding return nil end |
#enforce_length ⇒ Object
This affects whether the Packet class enforces the buffer length at all. Set to false to remove any correlation between buffer length and defined sizes of items in COSMOS
131 132 133 |
# File 'lib/openc3/accessors/template_accessor.rb', line 131 def enforce_length return false end |
#enforce_short_buffer_allowed ⇒ Object
This sets the short_buffer_allowed flag in the Packet class which allows packets that have a buffer shorter than the defined size. Note that the buffer is still resized to the defined length
138 139 140 |
# File 'lib/openc3/accessors/template_accessor.rb', line 138 def enforce_short_buffer_allowed return true end |
#read_item(item, buffer) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/openc3/accessors/template_accessor.rb', line 57 def read_item(item, buffer) return nil if item.data_type == :DERIVED configure() # Scan the response for all the variables in brackets <VARIABLE> values = buffer.scan(@read_regexp)[0] if !values || (values.length != @item_keys.length) raise "Unexpected number of items found in buffer: #{values ? values.length : 0}, Expected: #{@item_keys.length}" else values.each_with_index do |value, i| item_key = @item_keys[i] if item_key == item.key return self.class.convert_to_type(value, item) end end end raise "Response does not include key #{item.key}: #{buffer}" end |
#read_items(items, buffer) ⇒ Object
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 |
# File 'lib/openc3/accessors/template_accessor.rb', line 77 def read_items(items, buffer) result = {} configure() # Scan the response for all the variables in brackets <VARIABLE> values = buffer.scan(@read_regexp)[0] if !values || (values.length != @item_keys.length) raise "Unexpected number of items found in buffer: #{values ? values.length : 0}, Expected: #{@item_keys.length}" else items.each do |item| if item.data_type == :DERIVED result[item.name] = nil next end index = @item_keys.index(item.key) if index result[item.name] = self.class.convert_to_type(values[index], item) else raise "Unknown item with key #{item.key} requested" end end end return result end |
#write_item(item, value, buffer) ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/openc3/accessors/template_accessor.rb', line 103 def write_item(item, value, buffer) return nil if item.data_type == :DERIVED configure() success = buffer.gsub!("#{@left_char}#{item.key}#{@right_char}", value.to_s) raise "Key #{item.key} not found in template" unless success return value end |
#write_items(items, values, buffer) ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'lib/openc3/accessors/template_accessor.rb', line 112 def write_items(items, values, buffer) configure() items.each_with_index do |item, index| next if item.data_type == :DERIVED success = buffer.gsub!("#{@left_char}#{item.key}#{@right_char}", values[index].to_s) raise "Key #{item.key} not found in template" unless success end return values end |