go-wifi-switcher/internal/providers/archer_ax50.go

96 lines
2.1 KiB
Go
Raw Permalink Normal View History

2023-07-02 19:50:59 +02:00
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
}