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

113 lines
2.6 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 Huawei struct {
usernameID string
userpasswordID string
loginText string
skipText string
netID string
netWlanID string
mainFrameID string
wlanEnableID string
page *rod.Page
}
func NewHuawei(page *rod.Page) Huawei {
return Huawei{
usernameID: "#username",
userpasswordID: "#userpassword",
loginText: "Login",
skipText: "Skip",
netID: "#Net",
netWlanID: "#Net-WLAN",
mainFrameID: "#mainFrame",
wlanEnableID: "#wlanEnable",
page: page,
}
}
2023-07-02 19:50:59 +02:00
func (p Huawei) Login(user, pass string) error {
2023-06-02 03:24:12 +02:00
username, err := p.page.Element(p.usernameID)
if err != nil {
return fmt.Errorf("page.Element %s: %s", p.userpasswordID, err)
}
username.MustInput(user)
log.Println(p.usernameID, "DONE")
userpassword, err := p.page.Element(p.userpasswordID)
if err != nil {
return fmt.Errorf("page.Element %s: %s", p.userpasswordID, err)
}
userpassword.MustInput(pass)
log.Println(p.userpasswordID, "DONE")
login, err := p.page.Element(`input[value="` + p.loginText + `"]`)
if err != nil {
return fmt.Errorf("page.Element %s: %s", p.loginText, err)
}
login.MustClick()
log.Println(p.loginText, "DONE")
skip, err := p.page.Element(`input[value="` + p.skipText + `"]`)
if err != nil {
return fmt.Errorf("page.Element %s: %s", p.skipText, err)
}
skip.MustClick()
log.Println(p.skipText, "DONE")
return nil
}
func (p Huawei) SwitchWIFI() error {
net, err := p.page.Element(p.netID)
if err != nil {
return fmt.Errorf("page.Element %s: %s", p.netID, err)
}
net.MustClick()
log.Println(p.netID, "DONE")
netWlan, err := p.page.Element(p.netWlanID)
if err != nil {
return fmt.Errorf("page.Element %s: %s", p.netWlanID, err)
}
netWlan.MustClick()
log.Println(p.netWlanID, "DONE")
frameElement, err := p.page.Element(p.mainFrameID)
if err != nil {
return fmt.Errorf("page.Element %s: %s", p.mainFrameID, err)
}
mainFrame, err := frameElement.Frame()
if err != nil {
return fmt.Errorf("frameElement.Frame: %s", err)
}
wlanEnable, err := mainFrame.Element(p.wlanEnableID)
if err != nil {
return fmt.Errorf("mainFrame.Element %s: %s", p.wlanEnableID, err)
}
wlanEnable.MustClick()
log.Println(p.wlanEnableID, "DONE")
sysSubmitID := "#sysSubmit"
sysSubmit, err := mainFrame.Element(sysSubmitID)
if err != nil {
return fmt.Errorf("mainFrame.Element %s: %s", sysSubmitID, err)
}
sysSubmit.MustClick()
log.Println(sysSubmitID, "DONE")
// TODO: improve this and wait until something should happens (refresh)
time.Sleep(time.Second * 3)
return nil
}