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

50 lines
994 B
Go

package switcher
import (
"fmt"
"gitea.urkob.com/urko/go-wifi-switcher/cfg"
"gitea.urkob.com/urko/go-wifi-switcher/pkg/provider"
"github.com/go-rod/rod"
)
type Switcher struct {
Browser *rod.Browser
Page *rod.Page
config *cfg.Config
}
func NewSwitcher(remoteControlBrowserUrl string, config *cfg.Config) Switcher {
browser := rod.New().
ControlURL(remoteControlBrowserUrl).
MustConnect().
MustIgnoreCertErrors(true)
page := browser.MustPage(config.Page)
return Switcher{
Browser: browser,
Page: page,
config: config,
}
}
func (s Switcher) SwitchWIFI(prv provider.ProviderIface) error {
defer s.Browser.MustClose()
defer s.Page.Close()
if err := prv.Login(s.config.AdminUser, s.config.Password); err != nil {
return fmt.Errorf("prv.Login %w", err)
}
switchConfig := provider.SwitchConfig{
SubmitDelay: s.config.SubmitDelay,
}
if err := prv.SwitchWIFI(switchConfig); err != nil {
return fmt.Errorf("prv.SwitchWIFI %w", err)
}
return nil
}