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.



25
26
27
28
# File 'lib/openc3/accessors/accessor.rb', line 25

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

Instance Attribute Details

#packetObject

Returns the value of attribute packet.



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

def packet
  @packet
end

Class Method Details

.convert_to_type(value, item) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/openc3/accessors/accessor.rb', line 106

def self.convert_to_type(value, item)
  return value if value.nil?
  case item.data_type
  when :OBJECT, :ARRAY
    # Do nothing for complex object types
  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



83
84
85
# File 'lib/openc3/accessors/accessor.rb', line 83

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

.read_items(items, buffer) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/openc3/accessors/accessor.rb', line 91

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



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

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

.write_items(items, values, buffer) ⇒ Object



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

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



53
54
55
# File 'lib/openc3/accessors/accessor.rb', line 53

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



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

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



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

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



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

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



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

def enforce_short_buffer_allowed
  return false
end

#read_item(item, buffer) ⇒ Object



30
31
32
# File 'lib/openc3/accessors/accessor.rb', line 30

def read_item(item, buffer)
  self.class.read_item(item, buffer)
end

#read_items(items, buffer) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/openc3/accessors/accessor.rb', line 38

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



34
35
36
# File 'lib/openc3/accessors/accessor.rb', line 34

def write_item(item, value, buffer)
  self.class.write_item(item, value, buffer)
end

#write_items(items, values, buffer) ⇒ Object



46
47
48
49
50
51
# File 'lib/openc3/accessors/accessor.rb', line 46

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