From dde1411b188d383c1ca610b954de09aa79ad8cbe Mon Sep 17 00:00:00 2001 From: Andrey Egorov Date: Fri, 9 Aug 2024 11:21:02 +0300 Subject: [PATCH] Fixed RmFront panic. --- api/modem/at/response.go | 7 ++++--- api/modem/gps/gps.go | 5 +++-- main.go | 5 +++++ 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/api/modem/at/response.go b/api/modem/at/response.go index d3d10f9..b858617 100644 --- a/api/modem/at/response.go +++ b/api/modem/at/response.go @@ -9,12 +9,13 @@ func (resp Resp) Check() bool { } func (resp Resp) RmFront(str string) Resp { - if !resp.Check() { - return RespNil - } return Resp(string(resp)[len(str):]) } +func (resp Resp) CheckFront(str string) bool { + return len(resp) >= len(str) && resp[:len(str)].String() == str +} + func (resp Resp) String() string { return string(resp) } diff --git a/api/modem/gps/gps.go b/api/modem/gps/gps.go index 9888b9f..9476e3a 100644 --- a/api/modem/gps/gps.go +++ b/api/modem/gps/gps.go @@ -49,9 +49,10 @@ func (g *gps) Update() error { if err != nil { return fmt.Errorf("receive GPS data: %w", err) } - if !resp.Check() { + if !resp.Check() || !resp.CheckFront("+CGPSINFO:") { return fmt.Errorf("get GPS info: error response: %s", resp) } + if err := g.data.decode(strings.Split(strings.Replace(resp.RmFront("+CGPSINFO:").String(), "\r", "", -1), "\n")[0]); err != nil { g.logger.Printf("error decode: %s\n", err.Error()) } @@ -86,7 +87,7 @@ func (g *gps) switchGpsMode(on bool) error { if err != nil { return fmt.Errorf("make at ask: %w", err) } - if !resp.Check() { + if !resp.Check() || !resp.CheckFront("+CGPS:") { return fmt.Errorf("get GPS mode: error response: %s", resp) } ans := strings.Replace(strings.Split(strings.Split(resp.RmFront("+CGPS:").String(), "\n")[0], ",")[0], " ", "", -1) diff --git a/main.go b/main.go index 6a3c4f3..b2dc805 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ func main() { func mainE() error { 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 { logger.Println("Init ended with error:", err.Error()) @@ -34,6 +35,10 @@ func mainE() error { logger.Println("AAAAAAAAAAAAAAA Modem is not connected") return nil } + // m.PowerOff() + // time.Sleep(10 * time.Second) + // m.PowerOn() + logger.Println("||||||||||||||||| GET INFO |||||||||||||||||") logger.Println(m.Update()) logger.Printf("DATA: %+v\n", m.GetData())