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" "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 // Provides more information about signal and possible problems using NMEA reports
// Collect reports // Collect reports
reports, err := g.collectNmeaReports(nmeaFlagsAll) // Now minimum reports, err := g.collectNmeaReports(nmeaFlagsAll) // Now minimum
if err != nil { if err != nil {
return fmt.Errorf("collect nmea reports: %w", err) return StatusNil, fmt.Errorf("collect nmea reports: %w", err)
} }
// Annalise // Annalise
// Now simpliest variant st := Status{}
// Checks if there is any satelites
sc := 0 // Satelites' counter
// asc := 0 // Active satelites' counter
checkLoop: checkLoop:
for _, s := range reports { for _, s := range reports {
// Check for NMEA format // Check for NMEA format
if len(s) < 1 || s[0] != '$' { if len(s) < 1 || s[0] != '$' {
continue checkLoop continue checkLoop
} }
st.GotResponses = true
g.logger.Println("NMEA check:", s) g.logger.Println("NMEA check:", s)
values := strings.Split(s, ",") values := strings.Split(s, ",")
if len(values[0]) != 6 { 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 switch values[0][3:] { // Switch by content
case "gsv": case "gsv":
@ -43,14 +61,9 @@ checkLoop:
g.logger.Println("GSV too small values") g.logger.Println("GSV too small values")
continue checkLoop continue checkLoop
} }
sc += c st.FoundSatelitesCount += c
} }
} }
g.logger.Println("FOUND:", sc, "SATELITES") return st, nil
if sc == 0 {
return fmt.Errorf("no satelites found")
}
return nil
} }

View File

@ -19,7 +19,7 @@ type Gps interface {
Init() error Init() error
Update() error Update() error
GetData() Data GetData() Data
CheckStatus() error CheckStatus() (Status, error)
io.Closer 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) return "", fmt.Errorf("serial port write 2: %w", err)
} }
time.Sleep(100 * time.Millisecond) // To enshure
// Read // Read
outBuf := make([]byte, 0) outBuf := make([]byte, 0)
buf := make([]byte, 128) buf := make([]byte, 128)

25
main.go
View File

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