Added GPS status.

This commit is contained in:
yotia 2024-08-02 19:43:15 +03:00
parent e2e02ebbfe
commit 90a06e6afa
4 changed files with 46 additions and 26 deletions

View File

@ -6,31 +6,49 @@ import (
"strings"
)
func (g *gps) CheckStatus() error {
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 fmt.Errorf("collect nmea reports: %w", err)
return StatusNil, fmt.Errorf("collect nmea reports: %w", err)
}
// Annalise
// Now simpliest variant
// Checks if there is any satelites
sc := 0 // Satelites' counter
// asc := 0 // Active satelites' counter
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 fmt.Errorf("nmea invalid sentence: %s", s)
return StatusNil, fmt.Errorf("nmea invalid sentence: %s", s)
}
switch values[0][3:] { // Switch by content
case "gsv":
@ -43,14 +61,9 @@ checkLoop:
g.logger.Println("GSV too small values")
continue checkLoop
}
sc += c
st.FoundSatelitesCount += c
}
}
g.logger.Println("FOUND:", sc, "SATELITES")
if sc == 0 {
return fmt.Errorf("no satelites found")
}
return nil
return st, nil
}

View File

@ -19,7 +19,7 @@ type Gps interface {
Init() error
Update() error
GetData() Data
CheckStatus() error
CheckStatus() (Status, error)
io.Closer
}

View File

@ -102,6 +102,8 @@ func (g *gps) rawCollect(flags nmeaFlags) (string, error) {
return "", fmt.Errorf("serial port write 2: %w", err)
}
time.Sleep(100 * time.Millisecond) // To enshure
// Read
outBuf := make([]byte, 0)
buf := make([]byte, 128)

27
main.go
View File

@ -16,30 +16,35 @@ func main() {
}
func mainE() error {
m := modem.New(log.New(os.Stdout, "modem:", log.LstdFlags))
log.Println("||||||||||||||||| INIT |||||||||||||||")
logger := log.New(os.Stdout, "main:", log.LstdFlags)
m := modem.New(log.New(logger.Writer(), "modem:", log.LstdFlags))
logger.Println("||||||||||||||||| INIT |||||||||||||||")
if err := m.Init(); err != nil {
return err
}
if !m.Validate() {
log.Println("AAAAAAAAAAAAAAA Validation failed")
logger.Println("AAAAAAAAAAAAAAA Validation failed")
return nil
}
log.Println("||||||||||||||||| GET INFO |||||||||||||||||")
log.Println(m.GetData())
logger.Println("||||||||||||||||| GET INFO |||||||||||||||||")
logger.Println(m.GetData())
// log.Println("||||||||||||||||| SEND SMS |||||||||||||||||")
// log.Println(m.At().Send("AT+CNUM"))
// logger.Println("||||||||||||||||| SEND SMS |||||||||||||||||")
// logger.Println(m.At().Send("AT+CNUM"))
// if err := m.Sms().Send("+79218937173", "CGSG forever"); err != nil {
// return err
// }
// if ms, err := m.Sms().ReadNew(); err != nil {
// return err
// return err
// } else {
// log.Println("NEW:", ms)
// logger.Println("NEW:", ms)
// }
log.Println("||||||||||||||||| Checking gps status |||||||||||||||||")
log.Println(m.Gps().CheckStatus())
logger.Println("||||||||||||||||| Checking gps status |||||||||||||||||")
st, err := m.Gps().CheckStatus()
if err != nil {
return err
}
logger.Println("GPS Status:\n", st)
return nil
}