Skip to content

Commit

Permalink
Get Position from GPSd and convert into three styles.
Browse files Browse the repository at this point in the history
  • Loading branch information
n2ygk committed Mar 14, 2022
1 parent f926df8 commit 6ad523e
Showing 1 changed file with 58 additions and 26 deletions.
84 changes: 58 additions & 26 deletions internal/forms/forms.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"io"
"io/ioutil"
"log"
"math"
"net/http"
"os"
"path"
Expand Down Expand Up @@ -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
// <GPS> - Inserts the current GPS location (if available)
// Ex: 46-22.77N 121-35.01W
// <GPS_DECIMAL> - Inserts the current GPS location (if available)
// Ex: 46-22.77N 121-35.01W (this example is probably incorrect)
// Ex: 46.3795N 121.5835W
// <GPS_SIGNED_DECIMAL> - 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 != "" {
Expand All @@ -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)"
Expand Down Expand Up @@ -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() {
Expand All @@ -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)
}
Expand Down

0 comments on commit 6ad523e

Please sign in to comment.