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,17 +2,19 @@ package cfg
import ( import (
"log" "log"
"time"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig" "github.com/kelseyhightower/envconfig"
) )
type Config struct { type Config struct {
Page string `required:"true" split_words:"true"` Page string `required:"true" split_words:"true"`
AdminUser string `required:"true" split_words:"true"` AdminUser string `required:"true" split_words:"true"`
Password string `required:"true" split_words:"true"` Password string `required:"true" split_words:"true"`
LogFile bool `required:"true" split_words:"true"` LogFile bool `required:"true" split_words:"true"`
Bin string `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 { func NewConfig(envFilePath string) *Config {

View File

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

View File

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

View File

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

View File

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