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

63 lines
1.3 KiB
Go

package gps
import (
"fmt"
"strconv"
"strings"
)
type Status struct {
GotResponses bool `json:"gotResponses"`
FoundSatelitesCount int `json:"foundSatelitesCount"`
ActiveSatelitesCounte int `json:"activeSatelitesCount"`
}
var StatusNil = Status{
GotResponses: false,
FoundSatelitesCount: 0,
ActiveSatelitesCounte: 0,
}
func (g *gps) GetStatus() (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
}