-
Notifications
You must be signed in to change notification settings - Fork 0
/
gps.go
186 lines (165 loc) · 3.99 KB
/
gps.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"fmt"
"github.com/adrianmo/go-nmea"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"go.bug.st/serial"
"sort"
"strings"
)
type StringChannel chan string
type SentenceChannel chan nmea.Sentence
type BooleanChannel chan bool
type GpsController struct {
port serial.Port
portName string
stringChannel StringChannel
lineChannel StringChannel
cleanLineChannel StringChannel
sentenceChannel SentenceChannel
}
func (s *GpsController) GetPortString() error {
ports, err := serial.GetPortsList()
if err != nil {
return err
}
if len(ports) == 0 {
return fmt.Errorf("no serial ports found")
}
var portNames []string
for _, port := range ports {
if strings.Contains(port, "usbserial") && strings.Contains(port, "tty") {
portNames = append(portNames, port)
}
}
if len(portNames) == 0 {
return fmt.Errorf("no serial ports found")
}
sort.Strings(portNames)
s.portName = portNames[0]
log.Info().Str("port", s.portName).Msg("Port found")
return err
}
func (s *GpsController) OpenPort() error {
if s.portName == "" {
return fmt.Errorf("no port was identified. run GetPortString first")
}
mode := &serial.Mode{
BaudRate: 4800,
}
reader, err := serial.Open(s.portName, mode)
if err != nil {
return fmt.Errorf("failed to open serial port")
}
s.port = reader
log.Info().Str("port", s.portName).Msg("Port opened")
return nil
}
func (s *GpsController) Read() {
// Init channels and buffers
s.createChannels()
// read the serial bytes into a string channel
s.read()
// break up the strings into lines in the line channel
s.splitLines()
// clean lines of trailing spaces and invalid characters
s.cleanLines()
// parse the lines into NMEA sentences
s.parseLines()
for v := range s.sentenceChannel {
log.Printf("%#v", v)
}
log.Info().Msg("gps read done")
}
func (s *GpsController) createChannels() {
if s.stringChannel == nil {
s.stringChannel = make(StringChannel, 1024)
}
if s.lineChannel == nil {
s.lineChannel = make(StringChannel, 100)
}
if s.cleanLineChannel == nil {
s.cleanLineChannel = make(StringChannel, 100)
}
if s.sentenceChannel == nil {
s.sentenceChannel = make(SentenceChannel, 100)
}
}
func (s *GpsController) read() {
go func() {
readerBuff := make([]byte, 2048)
for {
n, err := s.port.Read(readerBuff)
if err != nil {
log.Error().Err(err).Msg("device removed or serial read failed")
break
}
if n == 0 {
log.Info().Msg("serial read ended")
break
}
s.stringChannel <- string(readerBuff[:n])
}
log.Debug().Msg("ending serial reader")
close(s.stringChannel) // start the closing domino chain
}()
}
func (s *GpsController) splitLines() {
go func() {
stringBuff := make([]string, 1024)
// skip the first line since it's likely partial
firstLine := true
for v := range s.stringChannel {
if strings.Contains(v, "\n") {
split := strings.Split(v, "\n")
stringBuff = append(stringBuff, split[0])
if !firstLine {
s.lineChannel <- strings.Join(stringBuff, "")
} else {
firstLine = false
}
for _, i := range split[1 : len(split)-1] {
s.lineChannel <- i
}
stringBuff = make([]string, 1024)
stringBuff = append(stringBuff, split[len(split)-1])
}
stringBuff = append(stringBuff, v)
}
close(s.lineChannel)
}()
}
func (s GpsController) cleanLines() {
go func() {
for line := range s.lineChannel {
s.cleanLineChannel <- strings.TrimSpace(line)
}
close(s.cleanLineChannel)
}()
}
func (s *GpsController) parseLines() {
go func() {
for v := range s.cleanLineChannel {
sentence, err := nmea.Parse(v)
if err != nil {
log.Error().Err(err).Str("line", v).Send()
}
s.sentenceChannel <- sentence
}
close(s.sentenceChannel)
}()
}
func main() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
controller := GpsController{}
err := controller.GetPortString()
if err != nil {
log.Fatal().Err(err).Send()
}
err = controller.OpenPort()
if err != nil {
log.Fatal().Err(err).Send()
}
controller.Read()
}