From 434f593b9be33dc69f4666e0e27de951874436a2 Mon Sep 17 00:00:00 2001 From: Damon To Date: Mon, 17 Jun 2024 16:58:57 +0800 Subject: [PATCH] refactor: add error handling for modem restart command selection --- internal/app/handler/profile.go | 5 ++++- internal/pkg/modem/modem.go | 17 ++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/internal/app/handler/profile.go b/internal/app/handler/profile.go index ca3ebb7..ec1d529 100644 --- a/internal/app/handler/profile.go +++ b/internal/app/handler/profile.go @@ -3,6 +3,7 @@ package handler import ( "context" "fmt" + "log/slog" "time" "github.com/damonto/telegram-sms/internal/pkg/conversation" @@ -196,8 +197,10 @@ func (h *ProfileHandler) handleActionEnable(c telebot.Context) error { return err } h.modem.Unlock() + + // Sometimes the modem needs to be restarted to apply the changes. if err := h.modem.Restart(); err != nil { - return err + slog.Error("unable to restart modem, you may need to restart this modem manually", "error", err) } return c.Send("Your profile has been enabled. Please wait a moment for it to take effect. /profiles") } diff --git a/internal/pkg/modem/modem.go b/internal/pkg/modem/modem.go index b7399da..131049a 100644 --- a/internal/pkg/modem/modem.go +++ b/internal/pkg/modem/modem.go @@ -15,10 +15,10 @@ import ( ) var ( - ErrNoATPortFound = errors.New("no at port found") + ErrNoATPortFound = errors.New("no at port found") + ErrRestartCommandNotFound = errors.New("restart command not found") - defaultRestartCommand = "AT+CFUN=1,1" - restartCommands = map[string]string{ + restartCommands = map[string]string{ "quectel": "AT+QPOWD=1", "fibocom": "AT+CPWROFF", "simcom": "AT+CPOF", @@ -57,13 +57,20 @@ func (m *Modem) Restart() error { if err != nil { return err } - restartCommand := defaultRestartCommand + manufacturer, err := m.GetManufacturer() + if err != nil { + return err + } + var restartCommand string for brand, command := range restartCommands { - if strings.Contains(strings.ToLower(model), brand) { + if strings.Contains(strings.ToLower(model), brand) || strings.Contains(strings.ToLower(manufacturer), brand) { restartCommand = command break } } + if restartCommand == "" { + return ErrRestartCommandNotFound + } _, err = m.RunATCommand(restartCommand) return err }