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

106 lines
2.8 KiB
Go

package providers
import (
"fmt"
"log"
"time"
"gitea.urkob.com/urko/go-wifi-switcher/pkg/provider"
"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(cfg provider.SwitchConfig) 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)
offWifiRadio, err := p.page.Element(`input[type="radio"][name="radiohiddenButton"][value="0"]`)
if err != nil {
return fmt.Errorf("page.Element %s: %s", p.wifiRadioID, err)
}
offChecked := offWifiRadio.MustProperty("checked")
if offChecked.Bool() {
log.Println("it is off, let's switch on")
onRadio, err := p.page.Element(`input[type="radio"][name="radiohiddenButton"][value="1"]`)
if err != nil {
return fmt.Errorf("page.Element %s: %s", p.wifiRadioID, err)
}
onRadio.MustClick()
} else {
log.Println("it is on, time to switch off")
offWifiRadio.MustClick()
}
log.Println(p.wifiRadioID, "DONE")
time.Sleep(time.Millisecond * 150)
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")
time.Sleep(time.Millisecond * 150)
return nil
}