70 lines
1.5 KiB
Go
70 lines
1.5 KiB
Go
package gps
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type Status struct {
|
|
GotResponses bool `json:"gotResponses"`
|
|
FoundSatelitesCount int `json:"foundSatelitesCount"`
|
|
ActiveSatelitesCounte int `json:"activeSatelitesCount"`
|
|
}
|
|
|
|
func (st Status) String() string {
|
|
got := "false"
|
|
if st.GotResponses {
|
|
got = "true"
|
|
}
|
|
return "" +
|
|
"GotResponses: " + got + "\n" +
|
|
"FoundSatelitesCount: " + string(st.FoundSatelitesCount) + "\n" +
|
|
"ActiveSatelitesCounte: " + string(st.ActiveSatelitesCounte) + "\n"
|
|
}
|
|
|
|
var StatusNil = Status{}
|
|
|
|
func (g *gps) CheckStatus() (Status, error) {
|
|
// Provides more information about signal and possible problems using NMEA reports
|
|
|
|
// Collect reports
|
|
reports, err := g.collectNmeaReports(nmeaFlagsAll) // Now minimum
|
|
if err != nil {
|
|
return StatusNil, fmt.Errorf("collect nmea reports: %w", err)
|
|
}
|
|
|
|
// Annalise
|
|
st := Status{}
|
|
|
|
checkLoop:
|
|
for _, s := range reports {
|
|
// Check for NMEA format
|
|
if len(s) < 1 || s[0] != '$' {
|
|
continue checkLoop
|
|
}
|
|
st.GotResponses = true
|
|
|
|
g.logger.Println("NMEA check:", s)
|
|
values := strings.Split(s, ",")
|
|
if len(values[0]) != 6 {
|
|
return StatusNil, fmt.Errorf("nmea invalid sentence: %s", s)
|
|
}
|
|
switch values[0][3:] { // Switch by content
|
|
case "gsv":
|
|
if len(values) < 17 {
|
|
g.logger.Println("GSV too small values")
|
|
continue checkLoop
|
|
}
|
|
c, err := strconv.Atoi(values[4])
|
|
if err != nil {
|
|
g.logger.Println("GSV too small values")
|
|
continue checkLoop
|
|
}
|
|
st.FoundSatelitesCount += c
|
|
}
|
|
}
|
|
|
|
return st, nil
|
|
}
|