Extract nmea sentence parsing from service

This commit is contained in:
Adriano Caloiaro 2020-08-11 18:57:09 -06:00
parent 7a7acd4e29
commit 1fa9095cdf
No known key found for this signature in database
GPG key ID: 9FFD0E7601F166AB

View file

@ -6,8 +6,8 @@ import (
"log"
"net/http"
"os"
"strconv"
"strings"
"github.com/acaloiaro/roam-location/nmea"
)
var port = 22495
@ -20,11 +20,6 @@ func Listen(dbPath string) {
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
}
type Location struct {
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
}
func listenHandler(dbPath string) (handler func(http.ResponseWriter, *http.Request)) {
return func(w http.ResponseWriter, r *http.Request) {
location := currentLocation(dbPath)
@ -38,7 +33,7 @@ func listenHandler(dbPath string) (handler func(http.ResponseWriter, *http.Reque
}
}
func currentLocation(dbPath string) (location Location) {
func currentLocation(dbPath string) (location nmea.Location) {
var err error
fileHandle, err := os.Open(dbPath)
if err != nil {
@ -57,7 +52,7 @@ func currentLocation(dbPath string) (location Location) {
fileHandle.ReadAt(char, cursor)
if cursor != -1 && (char[0] == '\n') { // stop if we find a line
parseNmeaSentence(line, &location)
location = nmea.ParseLocation(line)
// break when a 'good' location has been found. This will usually be myst last known location.
// when the router is reporting 0 coordinates, it doesn't have a signal
@ -75,15 +70,3 @@ func currentLocation(dbPath string) (location Location) {
return
}
func parseNmeaSentence(sentence string, location *Location) {
fields := strings.Split(sentence, ",")
if f, err := strconv.ParseFloat(fields[0], 64); err == nil {
location.Lat = f
}
if f, err := strconv.ParseFloat(fields[1], 64); err == nil {
location.Lon = f
}
}