diff --git a/service/current_location.go b/service/current_location.go index 1bd0980..8d35e39 100644 --- a/service/current_location.go +++ b/service/current_location.go @@ -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 - } -}