Class: OpenC3::CborAccessor

Inherits:
JsonAccessor show all
Defined in:
lib/openc3/accessors/cbor_accessor.rb

Instance Attribute Summary

Attributes inherited from Accessor

#packet

Class Method Summary collapse

Methods inherited from JsonAccessor

#enforce_derived_write_conversion, #enforce_encoding, #enforce_length, #enforce_short_buffer_allowed, write_item_internal

Methods inherited from Accessor

#args, convert_to_type, #enforce_derived_write_conversion, #enforce_encoding, #enforce_length, #enforce_short_buffer_allowed, #initialize, #read_item, #read_items, #write_item, #write_items

Constructor Details

This class inherits a constructor from OpenC3::Accessor

Class Method Details

.read_item(item, buffer) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/openc3/accessors/cbor_accessor.rb', line 24

def self.read_item(item, buffer)
  return nil if item.data_type == :DERIVED
  if String === buffer
    parsed = CBOR.decode(buffer)
  else
    parsed = buffer
  end
  return super(item, parsed)
end

.read_items(items, buffer) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/openc3/accessors/cbor_accessor.rb', line 55

def self.read_items(items, buffer)
  # Prevent JsonPath from decoding every call
  if String === buffer
    decoded = CBOR.decode(buffer)
  else
    decoded = buffer
  end
  super(items, decoded)
end

.write_item(item, value, buffer) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/openc3/accessors/cbor_accessor.rb', line 34

def self.write_item(item, value, buffer)
  return nil if item.data_type == :DERIVED

  # Convert to ruby objects
  if String === buffer
    decoded = CBOR.decode(buffer)
  else
    decoded = buffer
  end

  # Write the value
  write_item_internal(item, value, decoded)

  # Update buffer
  if String === buffer
    buffer.replace(decoded.to_cbor)
  end

  return value
end

.write_items(items, values, buffer) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/openc3/accessors/cbor_accessor.rb', line 65

def self.write_items(items, values, buffer)
  # Convert to ruby objects
  if String === buffer
    decoded = CBOR.decode(buffer)
  else
    decoded = buffer
  end

  items.each_with_index do |item, index|
    write_item_internal(item, values[index], decoded)
  end

  # Update buffer
  if String === buffer
    buffer.replace(decoded.to_cbor)
  end

  return values
end