Fixed RmFront panic.

This commit is contained in:
Andrey Egorov 2024-08-09 11:21:02 +03:00
parent 6c110b9a8b
commit dde1411b18
3 changed files with 12 additions and 5 deletions

View File

@ -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)
}

View File

@ -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)

View File

@ -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())