Class: OpenC3::TemplateAccessor

Inherits:
Accessor show all
Defined in:
lib/openc3/accessors/template_accessor.rb

Instance Attribute Summary

Attributes inherited from Accessor

#packet

Instance Method Summary collapse

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.



18
19
20
21
22
23
24
# File 'lib/openc3/accessors/template_accessor.rb', line 18

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

#configureObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/openc3/accessors/template_accessor.rb', line 26

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



148
149
150
# File 'lib/openc3/accessors/template_accessor.rb', line 148

def enforce_derived_write_conversion(_item)
  return true
end

#enforce_encodingObject

If this is set it will enforce that buffer data is encoded in a specific encoding



128
129
130
# File 'lib/openc3/accessors/template_accessor.rb', line 128

def enforce_encoding
  return nil
end

#enforce_lengthObject

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



135
136
137
# File 'lib/openc3/accessors/template_accessor.rb', line 135

def enforce_length
  return false
end

#enforce_short_buffer_allowedObject

This sets the short_buffer_allowed flag in the Packet class which allows packets that have a buffer shorter than the defined size. Items outside the buffer bounds will return nil when read.



142
143
144
# File 'lib/openc3/accessors/template_accessor.rb', line 142

def enforce_short_buffer_allowed
  return true
end

#read_item(item, buffer) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/openc3/accessors/template_accessor.rb', line 52

def read_item(item, buffer)
  return nil if item.data_type == :DERIVED
  configure()

  # No template items to read (e.g. command with fixed template string)
  return nil if @item_keys.empty?

  # 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



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
# File 'lib/openc3/accessors/template_accessor.rb', line 75

def read_items(items, buffer)
  result = {}
  configure()

  # No template items to read (e.g. command with fixed template string)
  if @item_keys.empty?
    items.each { |item| result[item.name] = nil }
    return result
  end

  # 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



107
108
109
110
111
112
113
114
# File 'lib/openc3/accessors/template_accessor.rb', line 107

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



116
117
118
119
120
121
122
123
124
# File 'lib/openc3/accessors/template_accessor.rb', line 116

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