diff --git a/api/modem/gps/check.go b/api/modem/gps/check.go index 1200b62..5096f57 100644 --- a/api/modem/gps/check.go +++ b/api/modem/gps/check.go @@ -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 } diff --git a/api/modem/gps/gps.go b/api/modem/gps/gps.go index cd8eafc..c987fb6 100644 --- a/api/modem/gps/gps.go +++ b/api/modem/gps/gps.go @@ -19,7 +19,7 @@ type Gps interface { Init() error Update() error GetData() Data - CheckStatus() error + CheckStatus() (Status, error) io.Closer } diff --git a/api/modem/gps/nmea.go b/api/modem/gps/nmea.go index f701323..cda5c95 100644 --- a/api/modem/gps/nmea.go +++ b/api/modem/gps/nmea.go @@ -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) diff --git a/main.go b/main.go index d2d66d1..6ed584f 100644 --- a/main.go +++ b/main.go @@ -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 }