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

130 lines
3.4 KiB
Go
Raw Normal View History

2023-06-02 03:24:12 +02:00
package providers
import (
"fmt"
"log"
"time"
"github.com/go-rod/rod"
)
type Nucom struct {
usernameName string
passwordName string
loginSubmitID string
wirelessTabID string
wirelessMenuText string
configureWarningID string
wifiRadioID string
saveID string
radioOff string
radioOn string
page *rod.Page
}
func NewNucom(page *rod.Page) Nucom {
return Nucom{
usernameName: "loginUser",
passwordName: "loginPass",
loginSubmitID: "btnApply",
wirelessTabID: "menu_wireless",
wirelessMenuText: "Básico",
configureWarningID: "Configurar ahora",
wifiRadioID: "radiohiddenButton",
radioOff: "Apagada",
radioOn: "Encendida",
saveID: "basicApply",
page: page,
}
}
func (p Nucom) Login(user, pass string) error {
username, err := p.page.Element(`input[name="` + p.usernameName + `"]`)
if err != nil {
return fmt.Errorf("page.Element %s: %s", p.passwordName, err)
}
username.MustInput(user)
log.Println(p.usernameName, "DONE")
userpassword, err := p.page.Element(`input[name="` + p.passwordName + `"]`)
if err != nil {
return fmt.Errorf("page.Element %s: %s", p.passwordName, err)
}
userpassword.MustInput(pass)
log.Println(p.passwordName, "DONE")
login, err := p.page.Element(`input[id="` + p.loginSubmitID + `"]`)
if err != nil {
return fmt.Errorf("page.Element %s: %s", p.loginSubmitID, err)
}
login.MustClick()
log.Println(p.loginSubmitID, "DONE")
return nil
}
func (p Nucom) SwitchWIFI() error {
wirelessMenu, err := p.page.Element(`a[id="` + p.wirelessTabID + `"]`)
if err != nil {
return fmt.Errorf("page.Element %s: %s", p.wirelessTabID, err)
}
wirelessMenu.MustClick()
log.Println(p.wirelessTabID, "DONE")
time.Sleep(time.Millisecond * 500)
log.Println(p.page.MustInfo().URL)
isOn, _, err := p.page.Has(`[id="basicRadioButton"]`)
if err != nil {
return fmt.Errorf("page.Element %s: %s", p.wifiRadioID, err)
}
log.Println("is on", isOn)
if !isOn {
warningButton, err := p.page.Element(`[value="` + p.configureWarningID + `"]`)
if err != nil {
return fmt.Errorf("page.Element %s: %s", p.configureWarningID, err)
}
warningButton.MustClick()
log.Println(warningButton, "DONE")
}
wifiRadios, err := p.page.Elements(`[name="radiohiddenButton"]`)
if err != nil {
return fmt.Errorf("page.Element %s: %s", p.wifiRadioID, err)
}
if len(wifiRadios) != 2 {
return fmt.Errorf("radios not found")
}
offChecked := wifiRadios[0].MustProperty("checked")
log.Println("wifiRadios[0]", wifiRadios[0].MustProperty("checked"))
log.Println("wifiRadios[1]", wifiRadios[1].MustProperty("checked"))
log.Println("offChecked.Bool()", offChecked.Bool())
if offChecked.Bool() {
wifiRadios[1].MustClick()
} else {
wifiRadios[0].MustClick()
}
wifiRadios, err = p.page.Elements(`[name="radiohiddenButton"]`)
if err != nil {
return fmt.Errorf("page.Element %s: %s", p.wifiRadioID, err)
}
offChecked, err = wifiRadios[0].Property("checked")
if err != nil {
return fmt.Errorf("page.Element %s: %s", p.wifiRadioID, err)
}
log.Println("offChecked.Bool() AFTER CHECK", offChecked.Bool())
log.Println(p.wifiRadioID, "DONE")
time.Sleep(time.Millisecond * 500)
saveButton, err := p.page.Element(`input[id="` + p.saveID + `"]`)
if err != nil {
return fmt.Errorf("page.Element %s: %s", p.saveID, err)
}
saveButton.MustClick()
log.Println(p.saveID, "DONE")
return nil
}