-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
261 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
# unreleased | ||
|
||
FEATURES: | ||
|
||
- gpsd support for webclient | ||
|
||
# 0.16.4 | ||
|
||
FEATURES: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package gpsd | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"log/slog" | ||
"net" | ||
"time" | ||
) | ||
|
||
const ( | ||
DefaultAddress = "localhost:2947" | ||
DialTimeout = time.Millisecond * 500 | ||
) | ||
|
||
type Msg struct { | ||
Class string `json:"class"` | ||
} | ||
|
||
type TPVMsg struct { | ||
Class string `json:"class"` | ||
Tag string `json:"tag"` | ||
Device string `json:"device"` | ||
Mode int `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"` | ||
Eph float64 `json:"eph"` | ||
} | ||
|
||
type VERSIONMsg 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"` | ||
} | ||
|
||
type GpsdClient struct { | ||
addr string | ||
conn net.Conn | ||
logger *slog.Logger | ||
reader *bufio.Reader | ||
} | ||
|
||
func New(addr string, logger *slog.Logger) *GpsdClient { | ||
c := &GpsdClient{ | ||
addr: DefaultAddress, | ||
conn: nil, | ||
logger: logger, | ||
reader: nil, | ||
} | ||
|
||
if addr != "" { | ||
c.addr = addr | ||
} | ||
|
||
return c | ||
} | ||
|
||
func (c *GpsdClient) connect(ctx context.Context) bool { | ||
timeout := time.Second | ||
|
||
for { | ||
conn, err := net.DialTimeout("tcp4", c.addr, DialTimeout) | ||
|
||
if err == nil { | ||
c.conn = conn | ||
c.reader = bufio.NewReader(c.conn) | ||
return true | ||
} | ||
|
||
c.logger.Error("dial error", "error", err) | ||
|
||
select { | ||
case <-time.After(timeout): | ||
case <-ctx.Done(): | ||
c.logger.Error("stop connection attempts") | ||
return false | ||
} | ||
|
||
if timeout < time.Minute*5 { | ||
timeout = timeout * 2 | ||
} | ||
} | ||
} | ||
|
||
func (c *GpsdClient) Listen(ctx context.Context, cb func(lat, lon, alt, speed, track float64)) { | ||
for ctx.Err() == nil { | ||
if c.conn == nil { | ||
if !c.connect(ctx) { | ||
return | ||
} | ||
} | ||
|
||
line, err := c.reader.ReadString('\n') | ||
|
||
if err != nil { | ||
c.logger.Error("error", "error", err) | ||
|
||
_ = c.conn.Close() | ||
c.conn = nil | ||
continue | ||
} | ||
|
||
data := []byte(line) | ||
|
||
var msg Msg | ||
|
||
if err = json.Unmarshal(data, &msg); err == nil { | ||
c.logger.Error("JSON decode error", "error", err) | ||
c.logger.Debug("bad json: " + line) | ||
_ = c.conn.Close() | ||
c.conn = nil | ||
continue | ||
} | ||
|
||
switch msg.Class { | ||
case "TPV": | ||
var r *TPVMsg | ||
if err1 := json.Unmarshal(data, &r); err1 != nil { | ||
c.logger.Error("JSON decode error", "error", err1) | ||
continue | ||
} | ||
|
||
if cb != nil { | ||
cb(r.Lat, r.Lon, r.Alt, r.Speed, r.Track) | ||
} | ||
case "VERSION": | ||
var r *VERSIONMsg | ||
if err1 := json.Unmarshal(data, &r); err1 != nil { | ||
c.logger.Error("JSON decode error", "error", err1) | ||
continue | ||
} | ||
c.logger.Info(fmt.Sprintf("got version %s, rev. %s", r.Release, r.Rev)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.