sim-modem/api/modem/gps/check.go

74 lines
1.6 KiB
Go
Raw Normal View History

package gps
import (
"fmt"
"strconv"
"strings"
)
2024-08-02 16:43:15 +00:00
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{
GotResponses: false,
FoundSatelitesCount: 0,
ActiveSatelitesCounte: 0,
}
2024-08-02 16:43:15 +00:00
func (g *gps) CheckStatus() (Status, error) {
// Provides more information about signal and possible problems using NMEA reports
// Collect reports
2024-08-01 16:34:58 +00:00
reports, err := g.collectNmeaReports(nmeaFlagsAll) // Now minimum
if err != nil {
2024-08-02 16:43:15 +00:00
return StatusNil, fmt.Errorf("collect nmea reports: %w", err)
}
// Annalise
2024-08-02 16:43:15 +00:00
st := Status{}
checkLoop:
for _, s := range reports {
2024-08-02 16:16:39 +00:00
// Check for NMEA format
if len(s) < 1 || s[0] != '$' {
continue checkLoop
}
2024-08-02 16:43:15 +00:00
st.GotResponses = true
2024-08-02 16:16:39 +00:00
g.logger.Println("NMEA check:", s)
values := strings.Split(s, ",")
2024-08-02 16:16:39 +00:00
if len(values[0]) != 6 {
2024-08-02 16:43:15 +00:00
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
}
2024-08-02 16:43:15 +00:00
st.FoundSatelitesCount += c
}
}
2024-08-02 16:43:15 +00:00
return st, nil
}