Class: GPSTool::Message
- Inherits:
-
Object
show all
- Defined in:
- lib/gpstool/message.rb
Constant Summary
collapse
- @@messages =
{}
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(body) ⇒ Message
Returns a new instance of Message.
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/gpstool/message.rb', line 86
def initialize(body)
if Array === body
@name = body.shift
@parts = body
else
@name, @parts = body.split(",", 2)
if @parts
if structure == nil
@parts = @parts.split(",")
elsif structure.size > 1
@parts = @parts.split(",", structure.size)
else
@parts = [@parts]
end
else
@parts = []
end
end
end
|
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
108
109
110
|
# File 'lib/gpstool/message.rb', line 108
def name
@name
end
|
#parts ⇒ Object
Returns the value of attribute parts.
107
108
109
|
# File 'lib/gpstool/message.rb', line 107
def parts
@parts
end
|
Class Method Details
.checksum(data) ⇒ Object
25
26
27
28
29
30
31
32
33
|
# File 'lib/gpstool/message.rb', line 25
def self.checksum(data)
sum = 0
data.size.times do |i|
sum = sum ^ data[i]
end
return sum
end
|
.checksum_hex(body) ⇒ Object
35
36
37
|
# File 'lib/gpstool/message.rb', line 35
def self.checksum_hex(body)
sprintf("%X", checksum(body).to_i).rjust(2, "0")
end
|
.parse(data) ⇒ Object
58
59
60
61
62
|
# File 'lib/gpstool/message.rb', line 58
def self.parse(data)
data = self.validate(data)
return self.new(data)
end
|
.read(io, options = {}) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/gpstool/message.rb', line 68
def self.read(io, options = {})
while io.readbytes(1) != '$'
end
name = ''
while true
c = io.readbytes(1)
if c == ',' || c == '*'
io.ungetc(c[0])
return self.read_body(io, name, options)
end
name += c
end
end
|
.read_body(io, name, options) ⇒ Object
64
65
66
|
# File 'lib/gpstool/message.rb', line 64
def self.read_body(io, name, options)
return self.parse('$' + name + io.gets)
end
|
.validate(data) ⇒ Object
This function performs validation and normalisation on the input data.
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/gpstool/message.rb', line 40
def self.validate(data)
data.strip!
data = data[1..-1] if data[0,1] == "$"
sum = nil
if data[-3,1] == "*"
sum = data[-2,2]
data = data[0..-4]
if checksum_hex(data) != sum
raise InvalidChecksumError.new(data, sum)
end
end
return data
end
|
Instance Method Details
#[](key) ⇒ Object
122
123
124
125
126
127
128
129
|
# File 'lib/gpstool/message.rb', line 122
def [](key)
if structure
offset = structure.index(key)
return @parts[offset]
end
return nil
end
|
#structure ⇒ Object
110
111
112
113
|
# File 'lib/gpstool/message.rb', line 110
def structure
messages = self.class.class_eval('@@messages')
messages[@name]
end
|
#to_hash ⇒ Object
115
116
117
118
119
120
|
# File 'lib/gpstool/message.rb', line 115
def to_hash
result = {}
structure.each_with_index do |n,i|
result[n] = @parts[i] if n
end
end
|
#to_s ⇒ Object
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/gpstool/message.rb', line 131
def to_s
body = nil
if @parts.length > 0
body = "#{@name},#{@parts.join(',')}"
else
body = @name
end
sum = self.class.checksum_hex(body)
return "$#{body}*#{sum}"
end
|