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

105 lines
2.8 KiB
Go
Raw Permalink 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)
offWifiRadio, err := p.page.Element(`input[type="radio"][name="radiohiddenButton"][value="0"]`)
2023-06-02 03:24:12 +02:00
if err != nil {
return fmt.Errorf("page.Element %s: %s", p.wifiRadioID, err)
}
offChecked := offWifiRadio.MustProperty("checked")
2023-06-02 03:24:12 +02:00
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()
2023-06-02 03:24:12 +02:00
} else {
log.Println("it is on, time to switch off")
offWifiRadio.MustClick()
2023-06-02 03:24:12 +02:00
}
log.Println(p.wifiRadioID, "DONE")
2023-06-03 17:09:38 +02:00
time.Sleep(time.Millisecond * 150)
2023-06-02 03:24:12 +02:00
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)
2023-06-02 03:24:12 +02:00
return nil
}