feat: add archer ax50

This commit is contained in:
Urko 2023-07-02 19:50:59 +02:00
parent 55ed1c1b36
commit cc9c9354bc
4 changed files with 101 additions and 7 deletions

View File

@ -0,0 +1,95 @@
package providers
import (
"fmt"
"log"
"time"
"github.com/go-rod/rod"
)
type ArcherAx50 struct {
passwordID string
loginButtonID string
wirelessTab string
chkWifi2gID string
saveButtonID string
page *rod.Page
}
func NewArcherAx50(page *rod.Page) ArcherAx50 {
return ArcherAx50{
passwordID: "#login-password",
loginButtonID: "#login-btn",
wirelessTab: "wireless",
chkWifi2gID: "chk_enable_wireless_24g",
saveButtonID: "#total_save",
page: page,
}
}
func (p ArcherAx50) Login(user, pass string) error {
time.Sleep(time.Millisecond * 1250)
p.page.MustEvaluate(&rod.EvalOptions{
JS: `() => {
$("input.password-text").each((i,v) => {
v.click();
v.focus();
v.value="` + pass + `";
})
return '';
}`,
})
time.Sleep(time.Millisecond * 250)
log.Println(p.passwordID, "DONE")
login, err := p.page.Element(p.loginButtonID)
if err != nil {
return fmt.Errorf("page.Element %s: %s", p.loginButtonID, err)
}
login.MustClick()
log.Println(p.loginButtonID, "DONE")
time.Sleep(time.Millisecond * 400)
return nil
}
func (p ArcherAx50) SwitchWIFI() error {
time.Sleep(time.Second * 1)
pageURL := p.page.MustInfo().URL
log.Println("p.page.MustInfo().URL", pageURL)
wirelessTab, err := p.page.Element(`[name="` + p.wirelessTab + `"]`)
if err != nil {
return fmt.Errorf("page.Element %s: %s", p.wirelessTab, err)
}
wirelessTab.MustClick()
log.Println(p.wirelessTab, "DONE")
time.Sleep(time.Millisecond * 750)
checked := p.page.MustEvaluate(&rod.EvalOptions{
JS: `() => {
return document.getElementById('` + p.chkWifi2gID + `').checked;
}`,
})
isChecked := checked.Value.Bool()
p.page.MustEvaluate(&rod.EvalOptions{
JS: `() => {
document.getElementById('` + p.chkWifi2gID + `').checked=` + fmt.Sprint(!isChecked) + `;
return '';
}`,
})
log.Println(p.chkWifi2gID, "DONE")
saveButton, err := p.page.Element(p.saveButtonID)
if err != nil {
return fmt.Errorf("page.Element %s: %s", p.saveButtonID, err)
}
saveButton.MustClick()
log.Println(p.saveButtonID, "DONE")
time.Sleep(time.Millisecond * 1500)
return nil
}

View File

@ -34,7 +34,7 @@ func NewHuawei(page *rod.Page) Huawei {
}
}
func (p *Huawei) Login(user, pass string) error {
func (p Huawei) Login(user, pass string) error {
username, err := p.page.Element(p.usernameID)
if err != nil {
return fmt.Errorf("page.Element %s: %s", p.userpasswordID, err)

View File

@ -8,13 +8,13 @@ import (
"github.com/go-rod/rod"
)
type switcher struct {
type Switcher struct {
Browser *rod.Browser
Page *rod.Page
config *cfg.Config
}
func NewSwitcher(remoteControlBrowserUrl string, config *cfg.Config) switcher {
func NewSwitcher(remoteControlBrowserUrl string, config *cfg.Config) Switcher {
browser := rod.New().
ControlURL(remoteControlBrowserUrl).
MustConnect().
@ -22,14 +22,14 @@ func NewSwitcher(remoteControlBrowserUrl string, config *cfg.Config) switcher {
page := browser.MustPage(config.Page)
return switcher{
return Switcher{
Browser: browser,
Page: page,
config: config,
}
}
func (s switcher) SwitchWIFI(prv provider.ProviderIface) error {
func (s Switcher) SwitchWIFI(prv provider.ProviderIface) error {
defer s.Browser.MustClose()
defer s.Page.Close()

View File

@ -23,7 +23,6 @@ func main() {
config := cfg.NewConfig(envFile)
log.SetFlags(log.Lmicroseconds)
config.LogFile = false
if config.LogFile {
logFileName := fmt.Sprintf("%s.txt", time.Now().Format(strings.ReplaceAll(time.RFC1123Z, ":", "_")))
f, err := os.OpenFile(logFileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
@ -57,7 +56,7 @@ func main() {
}
sw := switcher.NewSwitcher(remoteControlBrowserURL, config)
prv := providers.NewNucom(sw.Page)
prv := providers.NewArcherAx50(sw.Page)
err := sw.SwitchWIFI(prv)
if err != nil {
panic(err)