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

109 lines
2.5 KiB
Go
Raw Normal View History

2023-07-02 19:50:59 +02:00
package providers
import (
"fmt"
"log"
"time"
2023-12-27 07:38:48 +01:00
"gitea.urkob.com/urko/go-wifi-switcher/pkg/provider"
2023-07-02 19:50:59 +02:00
"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 {
2023-07-02 23:56:23 +02:00
time.Sleep(time.Millisecond * 5550)
2023-07-02 19:50:59 +02:00
p.page.MustEvaluate(&rod.EvalOptions{
JS: `() => {
2023-07-02 21:28:53 +02:00
document.querySelectorAll("input.password-text").forEach((v, i) => {
2023-07-02 19:50:59 +02:00
v.click();
v.focus();
v.value="` + pass + `";
})
2023-07-02 23:28:38 +02:00
return '';
2023-07-02 19:50:59 +02:00
}`,
})
2023-07-02 23:08:07 +02:00
time.Sleep(time.Millisecond * 550)
2023-07-02 19:50:59 +02:00
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 * 4550)
2023-07-02 19:50:59 +02:00
return nil
}
2023-12-27 07:38:48 +01:00
func (p ArcherAx50) SwitchWIFI(cfg provider.SwitchConfig) error {
2023-07-02 19:50:59 +02:00
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")
2023-07-02 23:56:23 +02:00
time.Sleep(time.Millisecond * 1850)
2023-07-02 19:50:59 +02:00
checked := p.page.MustEvaluate(&rod.EvalOptions{
JS: `() => {
return document.getElementById('` + p.chkWifi2gID + `').checked;
}`,
})
isChecked := checked.Value.Bool()
2023-07-02 20:15:54 +02:00
log.Println(getCheckedMessage(isChecked))
2023-07-02 19:50:59 +02:00
p.page.MustEvaluate(&rod.EvalOptions{
JS: `() => {
document.getElementById('` + p.chkWifi2gID + `').checked=` + fmt.Sprint(!isChecked) + `;
return '';
}`,
})
time.Sleep(time.Millisecond * 550)
2023-07-02 19:50:59 +02:00
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()
saveButton.MustClick()
saveButton.MustClick()
2023-07-02 19:50:59 +02:00
log.Println(p.saveButtonID, "DONE")
2023-12-27 07:42:38 +01:00
time.Sleep(time.Millisecond * time.Duration(cfg.SubmitDelay))
2023-07-02 19:50:59 +02:00
return nil
}
2023-07-02 20:15:54 +02:00
func getCheckedMessage(isChecked bool) string {
if isChecked {
return "Time to sleep, let's switch off"
}
return "Time to laborate, let's switch WIFI on"
}