Class: OpenC3::Accessor

Inherits:
Object show all
Defined in:
lib/openc3/accessors/accessor.rb,
ext/openc3/ext/structure/structure.c

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(packet = nil) ⇒ Accessor

Returns a new instance of Accessor.



21
22
23
24
# File 'lib/openc3/accessors/accessor.rb', line 21

def initialize(packet = nil)
  @packet = packet
  @args = []
end

Instance Attribute Details

#packetObject

Returns the value of attribute packet.



19
20
21
# File 'lib/openc3/accessors/accessor.rb', line 19

def packet
  @packet
end

Class Method Details

.convert_to_type(value, item) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/openc3/accessors/accessor.rb', line 119

def self.convert_to_type(value, item)
  return value if value.nil?
  case item.data_type
  when :ANY
    begin
      value = JSON.parse(value) if value.is_a? String
    rescue Exception
      # Just leave value as is
    end
  when :BOOL
    value = ConfigParser.handle_true_false(value) if value.is_a? String
  when :OBJECT, :ARRAY
    value = JSON.parse(value) if value.is_a? String
  when :STRING, :BLOCK
    if item.array_size
      value = JSON.parse(value) if value.is_a? String
      value = value.map { |v| v.to_s }
    else
      value = value.to_s
    end
  when :UINT, :INT
    if item.array_size
      value = JSON.parse(value) if value.is_a? String
      value = value.map { |v| Integer(v) }
    else
      value = Integer(value)
    end
  when :FLOAT
    if item.array_size
      value = JSON.parse(value) if value.is_a? String
      value = value.map { |v| Float(v) }
    else
      value = Float(value)
    end
  else
    raise(ArgumentError, "data_type #{item.data_type} is not recognized")
  end
  return value
end

.read_item(_item, _buffer) ⇒ Object



96
97
98
# File 'lib/openc3/accessors/accessor.rb', line 96

def self.read_item(_item, _buffer)
  raise "Must be defined by subclass if needed"
end

.read_items(items, buffer) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/openc3/accessors/accessor.rb', line 104

def self.read_items(items, buffer)
  result = {}
  items.each do |item|
    result[item.name] = read_item(item, buffer)
  end
  return result
end

.write_item(_item, _value, _buffer) ⇒ Object



100
101
102
# File 'lib/openc3/accessors/accessor.rb', line 100

def self.write_item(_item, _value, _buffer)
  raise "Must be defined by subclass if needed"
end

.write_items(items, values, buffer) ⇒ Object



112
113
114
115
116
117
# File 'lib/openc3/accessors/accessor.rb', line 112

def self.write_items(items, values, buffer)
  items.each_with_index do |item, index|
    write_item(item, values[index], buffer)
  end
  return values
end

Instance Method Details

#argsObject



66
67
68
# File 'lib/openc3/accessors/accessor.rb', line 66

def args
  return @args
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



92
93
94
# File 'lib/openc3/accessors/accessor.rb', line 92

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



72
73
74
# File 'lib/openc3/accessors/accessor.rb', line 72

def enforce_encoding
  return 'ASCII-8BIT'.freeze
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



79
80
81
# File 'lib/openc3/accessors/accessor.rb', line 79

def enforce_length
  return true
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.



86
87
88
# File 'lib/openc3/accessors/accessor.rb', line 86

def enforce_short_buffer_allowed
  return false
end

#read_item(item, buffer) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/openc3/accessors/accessor.rb', line 26

def read_item(item, buffer)
  if item.parent_item
    # Structure is used to read items with parent, not accessor
    parent_item = @packet.get_item(item.parent_item)
    structure_buffer = read_item(parent_item, buffer)
    structure = parent_item.structure
    structure.read(item.key, :RAW, structure_buffer)
  else
    self.class.read_item(item, buffer)
  end
end

#read_items(items, buffer) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/openc3/accessors/accessor.rb', line 51

def read_items(items, buffer)
  result = {}
  items.each do |item|
    result[item.name] = read_item(item, buffer)
  end
  return result
end

#write_item(item, value, buffer) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/openc3/accessors/accessor.rb', line 38

def write_item(item, value, buffer)
  if item.parent_item
    # Structure is used to write items with parent, not accessor
    parent_item = @packet.get_item(item.parent_item)
    structure_buffer = read_item(parent_item, buffer)
    structure = parent_item.structure
    structure.write(item.key, value, :RAW, structure_buffer)
    self.class.write_item(parent_item, structure_buffer, buffer)
  else
    self.class.write_item(item, value, buffer)
  end
end

#write_items(items, values, buffer) ⇒ Object



59
60
61
62
63
64
# File 'lib/openc3/accessors/accessor.rb', line 59

def write_items(items, values, buffer)
  items.each_with_index do |item, index|
    write_item(item, values[index], buffer)
  end
  return values
end