38 lines
857 B
Go
38 lines
857 B
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"strings"
|
|
|
|
"gitea.unprism.ru/KRBL/sim-modem/api/modem/at"
|
|
)
|
|
|
|
func CheckService(port at.Port, logger *log.Logger) (bool, error) {
|
|
srv, err := getService(port)
|
|
if err != nil {
|
|
return false, fmt.Errorf("get service: %w", err)
|
|
}
|
|
if srv == "NO SERVICE" {
|
|
return false, nil
|
|
}
|
|
logger.Println("Current service:", srv)
|
|
return true, nil
|
|
}
|
|
|
|
// Returns service
|
|
func getService(port at.Port) (string, error) {
|
|
resp, err := port.Send("AT+CPSI?")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if !resp.Check() || !resp.CheckFront("+CPSI: ") {
|
|
return "", fmt.Errorf("error response: %s", resp)
|
|
}
|
|
values := strings.Split(strings.ReplaceAll(strings.Split(resp.RmFront("+CPSI: ").String(), "\n")[0], "\r", ""), ",")
|
|
if len(values) < 2 {
|
|
return "", fmt.Errorf("invalid values(len): [% s]", values)
|
|
}
|
|
return values[0], nil
|
|
}
|