forked from atotto/go-gpsd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgpsd.go
336 lines (300 loc) · 8.11 KB
/
gpsd.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package gpsd
import (
"bufio"
"encoding/json"
"fmt"
"io"
"net"
"strings"
"time"
)
// DefaultAddress of gpsd (localhost:2947)
const DefaultAddress = "localhost:2947"
const dialTimeout = 2 * time.Second
// Filter is a gpsd entry filter function
type Filter func(interface{})
// Session represents a connection to gpsd
type Session struct {
address string
socket net.Conn
reader *bufio.Reader
filters map[string][]Filter
done chan struct{}
}
// Mode describes status of a TPV report
type Mode byte
const (
// NoValueSeen indicates no data has been received yet
NoValueSeen Mode = 0
// NoFix indicates fix has not been required yet
NoFix Mode = 1
// Mode2D represents quality of the fix
Mode2D Mode = 2
// Mode3D represents quality of the fix
Mode3D Mode = 3
)
type gpsdReport struct {
Class string `json:"class"`
}
// TPVReport is a Time-Position-Velocity report
type TPVReport struct {
Class string `json:"class"`
Tag string `json:"tag"`
Device string `json:"device"`
Mode Mode `json:"mode"`
Time time.Time `json:"time"`
Ept float64 `json:"ept"`
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
Alt float64 `json:"alt"`
Epx float64 `json:"epx"`
Epy float64 `json:"epy"`
Epv float64 `json:"epv"`
Track float64 `json:"track"`
Speed float64 `json:"speed"`
Climb float64 `json:"climb"`
Epd float64 `json:"epd"`
Eps float64 `json:"eps"`
Epc float64 `json:"epc"`
}
// SKYReport reports sky view of GPS satellites
type SKYReport struct {
Class string `json:"class"`
Tag string `json:"tag"`
Device string `json:"device"`
Time time.Time `json:"time"`
Xdop float64 `json:"xdop"`
Ydop float64 `json:"ydop"`
Vdop float64 `json:"vdop"`
Tdop float64 `json:"tdop"`
Hdop float64 `json:"hdop"`
Pdop float64 `json:"pdop"`
Gdop float64 `json:"gdop"`
Satellites []Satellite `json:"satellites"`
}
// GSTReport is pseudorange noise report
type GSTReport struct {
Class string `json:"class"`
Tag string `json:"tag"`
Device string `json:"device"`
Time time.Time `json:"time"`
Rms float64 `json:"rms"`
Major float64 `json:"major"`
Minor float64 `json:"minor"`
Orient float64 `json:"orient"`
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
Alt float64 `json:"alt"`
}
// ATTReport reports vehicle-attitude from the digital compass or the gyroscope
type ATTReport struct {
Class string `json:"class"`
Tag string `json:"tag"`
Device string `json:"device"`
Time time.Time `json:"time"`
Heading float64 `json:"heading"`
MagSt string `json:"mag_st"`
Pitch float64 `json:"pitch"`
PitchSt string `json:"pitch_st"`
Yaw float64 `json:"yaw"`
YawSt string `json:"yaw_st"`
Roll float64 `json:"roll"`
RollSt string `json:"roll_st"`
Dip float64 `json:"dip"`
MagLen float64 `json:"mag_len"`
MagX float64 `json:"mag_x"`
MagY float64 `json:"mag_y"`
MagZ float64 `json:"mag_z"`
AccLen float64 `json:"acc_len"`
AccX float64 `json:"acc_x"`
AccY float64 `json:"acc_y"`
AccZ float64 `json:"acc_z"`
GyroX float64 `json:"gyro_x"`
GyroY float64 `json:"gyro_y"`
Depth float64 `json:"depth"`
Temperature float64 `json:"temperature"`
}
// VERSIONReport returns version details of gpsd client
type VERSIONReport struct {
Class string `json:"class"`
Release string `json:"release"`
Rev string `json:"rev"`
ProtoMajor int `json:"proto_major"`
ProtoMinor int `json:"proto_minor"`
Remote string `json:"remote"`
}
// DEVICESReport lists all devices connected to the system
type DEVICESReport struct {
Class string `json:"class"`
Devices []DEVICEReport `json:"devices"`
Remote string `json:"remote"`
}
// DEVICEReport reports a state of a particular device
type DEVICEReport struct {
Class string `json:"class"`
Path string `json:"path"`
Activated string `json:"activated"`
Flags int `json:"flags"`
Driver string `json:"driver"`
Subtype string `json:"subtype"`
Bps int `json:"bps"`
Parity string `json:"parity"`
Stopbits string `json:"stopbits"`
Native int `json:"native"`
Cycle float64 `json:"cycle"`
Mincycle float64 `json:"mincycle"`
}
// PPSReport is triggered on each pulse-per-second strobe from a device
type PPSReport struct {
Class string `json:"class"`
Device string `json:"device"`
RealSec float64 `json:"real_sec"`
RealMusec float64 `json:"real_musec"`
ClockSec float64 `json:"clock_sec"`
ClockMusec float64 `json:"clock_musec"`
}
// ERRORReport is an error response
type ERRORReport struct {
Class string `json:"class"`
Message string `json:"message"`
}
// Satellite describes a location of a GPS satellite
type Satellite struct {
PRN float64 `json:"PRN"`
Az float64 `json:"az"`
El float64 `json:"el"`
Ss float64 `json:"ss"`
Used bool `json:"used"`
}
// Dial opens a new connection to GPSD.
func Dial(address string) (*Session, error) {
s := &Session{
address: address,
}
if err := s.dial(); err != nil {
return nil, err
}
s.filters = make(map[string][]Filter)
return s, nil
}
func (s *Session) dial() error {
conn, err := net.DialTimeout("tcp4", s.address, dialTimeout)
if err != nil {
return err
}
s.socket = conn
s.reader = bufio.NewReader(conn)
_, err = s.reader.ReadString('\n')
return err
}
// Close closes the connection to GPSD
func (s *Session) Close() error {
fmt.Fprintf(s.socket, "?WATCH={\"enable\":false}")
close(s.done)
return s.socket.Close()
}
// Run starts monitoring the connection to GPSD
func (s *Session) Run() {
go s.run()
}
func (s *Session) run() {
s.done = make(chan struct{})
for {
select {
case <-s.done:
return
default:
}
fmt.Fprintf(s.socket, "?WATCH={\"enable\":true,\"json\":true}")
s.watch()
time.Sleep(time.Second)
s.dial()
}
}
// SendCommand sends a command to GPSD
func (s *Session) SendCommand(command string) {
fmt.Fprintf(s.socket, "?"+command+";")
}
func (s *Session) Subscribe(class string, f Filter) {
s.filters[class] = append(s.filters[class], f)
}
func (s *Session) deliverReport(class string, report interface{}) {
for _, f := range s.filters[class] {
f(report)
}
}
func (s *Session) watch() {
// We're not using a JSON decoder because we first need to inspect
// the JSON string to determine it's "class"
for {
select {
case <-s.done:
return
default:
}
line, err := s.reader.ReadString('\n')
if err != nil {
if err == io.EOF {
return
}
if op, ok := err.(*net.OpError); ok && strings.Contains(op.Err.Error(), "use of closed network connection") {
return
}
fmt.Printf("Stream reader error (is gpsd running?): %#v\n", err)
return
}
var reportPeek gpsdReport
lineBytes := []byte(line)
if err := json.Unmarshal(lineBytes, &reportPeek); err != nil {
fmt.Printf("failed to json unmarshal: %s\n", err)
continue
}
if len(s.filters[reportPeek.Class]) == 0 {
continue
}
report, err := unmarshalReport(reportPeek.Class, lineBytes)
if err != nil {
fmt.Printf("failed to unmarshal report: %s\n", err)
continue
}
s.deliverReport(reportPeek.Class, report)
}
}
func unmarshalReport(class string, bytes []byte) (interface{}, error) {
var err error
switch class {
case "TPV":
var r *TPVReport
err = json.Unmarshal(bytes, &r)
return r, err
case "SKY":
var r *SKYReport
err = json.Unmarshal(bytes, &r)
return r, err
case "GST":
var r *GSTReport
err = json.Unmarshal(bytes, &r)
return r, err
case "ATT":
var r *ATTReport
err = json.Unmarshal(bytes, &r)
return r, err
case "VERSION":
var r *VERSIONReport
err = json.Unmarshal(bytes, &r)
return r, err
case "DEVICES":
var r *DEVICESReport
err = json.Unmarshal(bytes, &r)
return r, err
case "PPS":
var r *PPSReport
err = json.Unmarshal(bytes, &r)
return r, err
case "ERROR":
var r *ERRORReport
err = json.Unmarshal(bytes, &r)
return r, err
}
return nil, err
}