refactor: parameterize submit delay

This commit is contained in:
Urko. 2023-12-27 07:38:48 +01:00
parent cc7a59c109
commit 69389d030b
6 changed files with 27 additions and 12 deletions

View File

@ -2,6 +2,7 @@ package cfg
import (
"log"
"time"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
@ -13,6 +14,7 @@ type Config struct {
Password string `required:"true" split_words:"true"`
LogFile bool `required:"true" split_words:"true"`
Bin string `required:"true" split_words:"true"`
SubmitDelay time.Duration `default:"7500" required:"false" split_words:"true"`
}
func NewConfig(envFilePath string) *Config {

View File

@ -5,6 +5,7 @@ import (
"log"
"time"
"gitea.urkob.com/urko/go-wifi-switcher/pkg/provider"
"github.com/go-rod/rod"
)
@ -56,7 +57,7 @@ func (p ArcherAx50) Login(user, pass string) error {
return nil
}
func (p ArcherAx50) SwitchWIFI() error {
func (p ArcherAx50) SwitchWIFI(cfg provider.SwitchConfig) error {
pageURL := p.page.MustInfo().URL
log.Println("p.page.MustInfo().URL", pageURL)
@ -94,7 +95,7 @@ func (p ArcherAx50) SwitchWIFI() error {
saveButton.MustClick()
log.Println(p.saveButtonID, "DONE")
time.Sleep(time.Millisecond * 7500)
time.Sleep(time.Millisecond * cfg.SubmitDelay)
return nil
}

View File

@ -5,6 +5,7 @@ import (
"log"
"time"
"gitea.urkob.com/urko/go-wifi-switcher/pkg/provider"
"github.com/go-rod/rod"
)
@ -66,7 +67,7 @@ func (p Huawei) Login(user, pass string) error {
return nil
}
func (p Huawei) SwitchWIFI() error {
func (p Huawei) SwitchWIFI(cfg provider.SwitchConfig) error {
net, err := p.page.Element(p.netID)
if err != nil {
return fmt.Errorf("page.Element %s: %s", p.netID, err)

View File

@ -5,6 +5,7 @@ import (
"log"
"time"
"gitea.urkob.com/urko/go-wifi-switcher/pkg/provider"
"github.com/go-rod/rod"
)
@ -62,7 +63,7 @@ func (p Nucom) Login(user, pass string) error {
return nil
}
func (p Nucom) SwitchWIFI() error {
func (p Nucom) SwitchWIFI(cfg provider.SwitchConfig) error {
wirelessMenu, err := p.page.Element(`a[id="` + p.wirelessTabID + `"]`)
if err != nil {
return fmt.Errorf("page.Element %s: %s", p.wirelessTabID, err)

View File

@ -37,7 +37,11 @@ func (s Switcher) SwitchWIFI(prv provider.ProviderIface) error {
return fmt.Errorf("prv.Login %w", err)
}
if err := prv.SwitchWIFI(); err != nil {
switchConfig := provider.SwitchConfig{
SubmitDelay: s.config.SubmitDelay,
}
if err := prv.SwitchWIFI(switchConfig); err != nil {
return fmt.Errorf("prv.SwitchWIFI %w", err)
}

View File

@ -1,6 +1,12 @@
package provider
import "time"
type ProviderIface interface {
Login(user, pass string) error
SwitchWIFI() error
Login(string, string) error
SwitchWIFI(SwitchConfig) error
}
type SwitchConfig struct {
SubmitDelay time.Duration
}