From 6ad523e0eab227f7a40906aba2951d950d0181f7 Mon Sep 17 00:00:00 2001 From: Alan Crosswell Date: Mon, 14 Mar 2022 18:22:21 -0400 Subject: [PATCH] Get Position from GPSd and convert into three styles. --- internal/forms/forms.go | 84 ++++++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 26 deletions(-) diff --git a/internal/forms/forms.go b/internal/forms/forms.go index 074f1472..da1f55e2 100644 --- a/internal/forms/forms.go +++ b/internal/forms/forms.go @@ -17,6 +17,7 @@ import ( "io" "io/ioutil" "log" + "math" "net/http" "os" "path" @@ -705,21 +706,8 @@ func (m *Manager) findAbsPathForTemplatePath(tmplPath string) (string, error) { return retVal, nil } -func gpsPos(decimal bool, signed bool) string { - // return current GPS position in one of these alternative formats: - // decimal=false: degrees-minutes.seconds w/Northing and Easting - // decimal=true, signed=false: degrees.fraction w/Northing and Easting - // decimal=true, signed=true: degrees.fraction w/minus sign for South and West - - // documentation: https://www.winlink.org/sites/default/files/RMSE_FORMS/insertion_tags.zip - // - Inserts the current GPS location (if available) - // Ex: 46-22.77N 121-35.01W - // - Inserts the current GPS location (if available) - // Ex: 46-22.77N 121-35.01W (this example is probably incorrect) - // Ex: 46.3795N 121.5835W - // - Inserts the current GPS location as a signed decimal latitude/longitude (if available) - // Ex: 46.3795 -121.5835 - +func gpsPos() (gpsd.Position, error) { + // return current GPS Position addr := "localhost:2947" // TODO: get from config if addr != "" { @@ -736,12 +724,58 @@ func gpsPos(decimal bool, signed bool) string { if err != nil { log.Fatalf("GPSd: %s", err) } + return pos, err + } else { + return gpsd.Position{}, errors.New("GPSd: not configured.") + } +} - log.Printf("GPS pos: %f %f", pos.Lat, pos.Lon) - if decimal && signed { - return fmt.Sprintf("%f %f", pos.Lat, pos.Lon) - } else { - return "TBD" +type gpsStyle int + +const ( + // documentation: https://www.winlink.org/sites/default/files/RMSE_FORMS/insertion_tags.zip + signedDecimal gpsStyle = iota // 41.1234 -73.4567 + decimal // 46.3795N 121.5835W + degreeMinute // 46-22.77N 121-35.01W +) + +func gpsFmt(style gpsStyle, pos gpsd.Position) string { + var northing string + var easting string + var latDegrees int + var latMinutes float64 + var lonDegrees int + var lonMinutes float64 + + noPos := gpsd.Position{} + if pos != noPos { + switch style { + case degreeMinute: { + latDegrees = int(math.Trunc(math.Abs(pos.Lat))) + latMinutes = (math.Abs(pos.Lat) - float64(latDegrees)) * 60 + lonDegrees = int(math.Trunc(math.Abs(pos.Lon))) + lonMinutes = (math.Abs(pos.Lon) - float64(lonDegrees)) * 60 + } + fallthrough + case decimal: { + if pos.Lat >= 0 { + northing = "N" + } else { + northing = "S" + } + if pos.Lon >= 0 { + easting = "E" + } else { + easting = "W" + } + } + } + + switch style { + case signedDecimal: return fmt.Sprintf("%.4f %.4f",pos.Lat,pos.Lon) + case decimal: return fmt.Sprintf("%.4f%s %.4f%s", math.Abs(pos.Lat), northing, math.Abs(pos.Lon), easting) + case degreeMinute: return fmt.Sprintf("%d-%.4f%s %d-%.4f%s", latDegrees, latMinutes, northing, lonDegrees, lonMinutes, easting) + default: return "(Not available)" } } else { return "(Not available)" @@ -776,9 +810,7 @@ func (m *Manager) fillFormTemplate(absPathTemplate string, formDestURL string, p nowDateUTC := now.UTC().Format("2006-01-02Z") nowTimeUTC := now.UTC().Format("15:04:05Z") udtg := strings.ToUpper(now.UTC().Format("021504Z Jan 2006")) - nowGPS := gpsPos(false, false) - nowGPSDecimal := gpsPos(true, false) - nowGPSSignedDecimal := gpsPos(true, true) + nowPos,err := gpsPos() scanner := bufio.NewScanner(bytes.NewReader(sanitizedFileContent)) for scanner.Scan() { @@ -796,9 +828,9 @@ func (m *Manager) fillFormTemplate(absPathTemplate string, formDestURL string, p l = strings.ReplaceAll(l, "{UDTG}", udtg) l = strings.ReplaceAll(l, "{Time}", nowTime) l = strings.ReplaceAll(l, "{UTime}", nowTimeUTC) - l = strings.ReplaceAll(l, "{GPS}", nowGPS) - l = strings.ReplaceAll(l, "{GPS_DECIMAL}", nowGPSDecimal) - l = strings.ReplaceAll(l, "{GPS_SIGNED_DECIMAL}", nowGPSSignedDecimal) + l = strings.ReplaceAll(l, "{GPS}", gpsFmt(degreeMinute, nowPos)) + l = strings.ReplaceAll(l, "{GPS_DECIMAL}", gpsFmt(decimal, nowPos)) + l = strings.ReplaceAll(l, "{GPS_SIGNED_DECIMAL}", gpsFmt(signedDecimal, nowPos)) if placeholderRegEx != nil { l = fillPlaceholders(l, placeholderRegEx, formVars) }