Class: XBee::Frames::Frame

Inherits:
Object
  • Object
show all
Includes:
SemanticLogger::Loggable
Defined in:
lib/xbee/frames/frame.rb

Constant Summary collapse

@@frame_types =
<Integer, Frame>

Map of frame type IDs to implementation classes

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(packet: nil) ⇒ Frame

Subclasses should shift @parse_bytes as necessary to get their data fields.



45
46
47
48
49
50
51
52
# File 'lib/xbee/frames/frame.rb', line 45

def initialize(packet: nil)
	logger.trace 'Initializing...', packet: packet
	@packet = packet
	if packet
		@parse_bytes = packet.data.dup
		@frame_type = @parse_bytes.shift
	end
end

Instance Attribute Details

#packetObject (readonly)

XBee::Packet

if this frame was received, it’ll belong to a Packet. (Frames prepped for transmit have no Packet association.)



42
43
44
# File 'lib/xbee/frames/frame.rb', line 42

def packet
  @packet
end

Class Method Details

.api_id(byte) ⇒ Object

Registers the frame type



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/xbee/frames/frame.rb', line 19

def api_id(byte)
	@@frame_types ||= {}
	raise "Attempted to redefine API ID #{byte.inspect}" if @@frame_types.has_key? byte
	@@frame_types[byte] = self

	define_singleton_method(:frame_type) do
		byte
	end

	define_method(:frame_type) do
		byte
	end
end

.from_packet(packet) ⇒ Object



34
35
36
37
# File 'lib/xbee/frames/frame.rb', line 34

def from_packet(packet)
	raise Exceptions::UnknownFrameType, packet unless @@frame_types.has_key? packet.data[0]
	@@frame_types[packet.data[0]].new packet: packet
end

Instance Method Details

#bytesObject



55
56
57
# File 'lib/xbee/frames/frame.rb', line 55

def bytes
	[frame_type]
end

#to_packetObject



60
61
62
# File 'lib/xbee/frames/frame.rb', line 60

def to_packet
	@packet = Packet.new bytes
end