Skip to content

Commit

Permalink
refactor: add error handling for modem restart command selection
Browse files Browse the repository at this point in the history
  • Loading branch information
damonto committed Jun 17, 2024
1 parent 34ca5a9 commit 434f593
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
5 changes: 4 additions & 1 deletion internal/app/handler/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handler
import (
"context"
"fmt"
"log/slog"
"time"

"github.com/damonto/telegram-sms/internal/pkg/conversation"
Expand Down Expand Up @@ -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")
}
Expand Down
17 changes: 12 additions & 5 deletions internal/pkg/modem/modem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 434f593

Please sign in to comment.