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"
|
|
|
|
)
|
|
|
|
|
2023-07-02 19:50:59 +02:00
|
|
|
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-07-02 19:50:59 +02: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-07-02 19:50:59 +02:00
|
|
|
return Switcher{
|
2023-06-02 03:24:12 +02:00
|
|
|
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-07-02 19:50:59 +02:00
|
|
|
func (s Switcher) SwitchWIFI(prv provider.ProviderIface) error {
|
2023-06-02 03:24:12 +02:00
|
|
|
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-12-27 07:38:48 +01:00
|
|
|
switchConfig := provider.SwitchConfig{
|
|
|
|
SubmitDelay: s.config.SubmitDelay,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := prv.SwitchWIFI(switchConfig); err != nil {
|
2023-06-02 03:24:12 +02:00
|
|
|
return fmt.Errorf("prv.SwitchWIFI %w", err)
|
2023-03-07 22:50:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|