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.



23
24
25
26
27
28
# 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
end

Instance Method Details

#configureObject



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

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 = template
  if @left_char != '('
    escaped_read_template = escaped_read_template.gsub('(', '\(')
  end
  if @right_char != ')'
    escaped_read_template = escaped_read_template.gsub(')', '\)')
  end

  @item_keys = []
  template_items.each do |item|
    @item_keys << item[1..-2]
    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 enfore that COSMOS DERIVED items must have a write_conversion to be written



146
147
148
# File 'lib/openc3/accessors/template_accessor.rb', line 146

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



126
127
128
# File 'lib/openc3/accessors/template_accessor.rb', line 126

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



133
134
135
# File 'lib/openc3/accessors/template_accessor.rb', line 133

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. Note that the buffer is still resized to the defined length



140
141
142
# File 'lib/openc3/accessors/template_accessor.rb', line 140

def enforce_short_buffer_allowed
  return true
end

#read_item(item, buffer) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/openc3/accessors/template_accessor.rb', line 59

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 Accessor.convert_to_type(value, item)
      end
    end
  end

  raise "Response does not include key #{item.key}: #{buffer}"
end

#read_items(items, buffer) ⇒ Object



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

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] = Accessor.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



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

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



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

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