go-wifi-switcher/internal/switcher/switcher.go

46 lines
901 B
Go
Raw Normal View History

2023-03-07 22:57:30 +01:00
package switcher
2023-03-07 22:50:07 +01:00
import (
"fmt"
"gitea.urkob.com/urko/go-wifi-switcher/cfg"
2023-06-02 03:24:12 +02:00
"gitea.urkob.com/urko/go-wifi-switcher/pkg/provider"
2023-03-07 22:50:07 +01:00
"github.com/go-rod/rod"
)
type switcher struct {
2023-06-02 03:24:12 +02:00
Browser *rod.Browser
Page *rod.Page
config *cfg.Config
2023-03-07 22:50:07 +01:00
}
2023-03-07 23:07:20 +01:00
func NewSwitcher(remoteControlBrowserUrl string, config *cfg.Config) switcher {
2023-03-07 22:50:07 +01:00
browser := rod.New().
2023-06-02 03:24:12 +02:00
ControlURL(remoteControlBrowserUrl).
2023-03-07 22:50:07 +01:00
MustConnect().
MustIgnoreCertErrors(true)
2023-06-02 03:24:12 +02:00
page := browser.MustPage(config.Page)
2023-03-07 22:50:07 +01:00
2023-06-02 03:24:12 +02:00
return switcher{
Browser: browser,
Page: page,
config: config,
2023-03-07 22:50:07 +01:00
}
2023-06-02 03:24:12 +02:00
}
2023-03-07 22:50:07 +01:00
2023-06-02 03:24:12 +02:00
func (s switcher) SwitchWIFI(prv provider.ProviderIface) error {
defer s.Browser.MustClose()
defer s.Page.Close()
2023-03-07 22:50:07 +01:00
2023-06-02 03:24:12 +02:00
if err := prv.Login(s.config.AdminUser, s.config.Password); err != nil {
return fmt.Errorf("prv.Login %w", err)
2023-03-07 22:50:07 +01:00
}
2023-06-02 03:24:12 +02:00
if err := prv.SwitchWIFI(); err != nil {
return fmt.Errorf("prv.SwitchWIFI %w", err)
2023-03-07 22:50:07 +01:00
}
return nil
}