Skip to content

Commit

Permalink
Initial proof of concept of substituting GPS var in form.
Browse files Browse the repository at this point in the history
  • Loading branch information
n2ygk committed Mar 13, 2022
1 parent ccb4934 commit f926df8
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions internal/forms/forms.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"unicode/utf8"

"github.com/dimchansky/utfbom"
"github.com/la5nta/pat/internal/gpsd"
)

const (
Expand Down Expand Up @@ -704,6 +705,49 @@ 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


addr := "localhost:2947" // TODO: get from config
if addr != "" {
conn, err := gpsd.Dial(addr)
if err != nil {
log.Fatalf("GPSd daemon: %s", err)
}
defer conn.Close()

conn.Watch(true)

log.Println("Waiting for position from GPSd...") // TODO: Spinning bar?
pos, err := conn.NextPos()
if err != nil {
log.Fatalf("GPSd: %s", err)
}

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"
}
} else {
return "(Not available)"
}
}

func (m *Manager) fillFormTemplate(absPathTemplate string, formDestURL string, placeholderRegEx *regexp.Regexp, formVars map[string]string) (string, error) {
fUnsanitized, err := os.Open(absPathTemplate)
if err != nil {
Expand Down Expand Up @@ -732,6 +776,9 @@ 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)

scanner := bufio.NewScanner(bytes.NewReader(sanitizedFileContent))
for scanner.Scan() {
Expand All @@ -749,6 +796,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)
if placeholderRegEx != nil {
l = fillPlaceholders(l, placeholderRegEx, formVars)
}
Expand Down

0 comments on commit f926df8

Please sign in to comment.