Compare commits
12 Commits
feature/ar
...
main
Author | SHA1 | Date |
---|---|---|
Urko. | 1dc6ce6e5e | |
Urko. | 69389d030b | |
Urko | cc7a59c109 | |
Urko | 62bda54d98 | |
Urko | 0933174ea1 | |
Urko | bf700eeeb3 | |
Urko | bbc94e0375 | |
Urko | ad8ed58366 | |
Urko | db9f7495aa | |
Urko | 0ae922e58d | |
Urko | fc35559ab8 | |
Urko | c09933767b |
11
cfg/cfg.go
11
cfg/cfg.go
|
@ -8,11 +8,12 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Page string `required:"true" split_words:"true"`
|
Page string `required:"true" split_words:"true"`
|
||||||
AdminUser string `required:"true" split_words:"true"`
|
AdminUser string `required:"true" split_words:"true"`
|
||||||
Password string `required:"true" split_words:"true"`
|
Password string `required:"true" split_words:"true"`
|
||||||
LogFile bool `required:"true" split_words:"true"`
|
LogFile bool `required:"true" split_words:"true"`
|
||||||
Bin string `required:"true" split_words:"true"`
|
Bin string `required:"true" split_words:"true"`
|
||||||
|
SubmitDelay int `default:"7500" required:"false" split_words:"true"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConfig(envFilePath string) *Config {
|
func NewConfig(envFilePath string) *Config {
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gitea.urkob.com/urko/go-wifi-switcher/pkg/provider"
|
||||||
"github.com/go-rod/rod"
|
"github.com/go-rod/rod"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -29,11 +30,11 @@ func NewArcherAx50(page *rod.Page) ArcherAx50 {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p ArcherAx50) Login(user, pass string) error {
|
func (p ArcherAx50) Login(user, pass string) error {
|
||||||
time.Sleep(time.Millisecond * 1250)
|
time.Sleep(time.Millisecond * 5550)
|
||||||
|
|
||||||
p.page.MustEvaluate(&rod.EvalOptions{
|
p.page.MustEvaluate(&rod.EvalOptions{
|
||||||
JS: `() => {
|
JS: `() => {
|
||||||
$("input.password-text").each((i,v) => {
|
document.querySelectorAll("input.password-text").forEach((v, i) => {
|
||||||
v.click();
|
v.click();
|
||||||
v.focus();
|
v.focus();
|
||||||
v.value="` + pass + `";
|
v.value="` + pass + `";
|
||||||
|
@ -41,7 +42,8 @@ func (p ArcherAx50) Login(user, pass string) error {
|
||||||
return '';
|
return '';
|
||||||
}`,
|
}`,
|
||||||
})
|
})
|
||||||
time.Sleep(time.Millisecond * 250)
|
|
||||||
|
time.Sleep(time.Millisecond * 550)
|
||||||
|
|
||||||
log.Println(p.passwordID, "DONE")
|
log.Println(p.passwordID, "DONE")
|
||||||
|
|
||||||
|
@ -51,12 +53,11 @@ func (p ArcherAx50) Login(user, pass string) error {
|
||||||
}
|
}
|
||||||
login.MustClick()
|
login.MustClick()
|
||||||
log.Println(p.loginButtonID, "DONE")
|
log.Println(p.loginButtonID, "DONE")
|
||||||
time.Sleep(time.Millisecond * 400)
|
time.Sleep(time.Millisecond * 4550)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p ArcherAx50) SwitchWIFI() error {
|
func (p ArcherAx50) SwitchWIFI(cfg provider.SwitchConfig) error {
|
||||||
time.Sleep(time.Second * 1)
|
|
||||||
pageURL := p.page.MustInfo().URL
|
pageURL := p.page.MustInfo().URL
|
||||||
log.Println("p.page.MustInfo().URL", pageURL)
|
log.Println("p.page.MustInfo().URL", pageURL)
|
||||||
|
|
||||||
|
@ -67,7 +68,7 @@ func (p ArcherAx50) SwitchWIFI() error {
|
||||||
wirelessTab.MustClick()
|
wirelessTab.MustClick()
|
||||||
log.Println(p.wirelessTab, "DONE")
|
log.Println(p.wirelessTab, "DONE")
|
||||||
|
|
||||||
time.Sleep(time.Millisecond * 750)
|
time.Sleep(time.Millisecond * 1850)
|
||||||
checked := p.page.MustEvaluate(&rod.EvalOptions{
|
checked := p.page.MustEvaluate(&rod.EvalOptions{
|
||||||
JS: `() => {
|
JS: `() => {
|
||||||
return document.getElementById('` + p.chkWifi2gID + `').checked;
|
return document.getElementById('` + p.chkWifi2gID + `').checked;
|
||||||
|
@ -75,6 +76,7 @@ func (p ArcherAx50) SwitchWIFI() error {
|
||||||
})
|
})
|
||||||
|
|
||||||
isChecked := checked.Value.Bool()
|
isChecked := checked.Value.Bool()
|
||||||
|
log.Println(getCheckedMessage(isChecked))
|
||||||
p.page.MustEvaluate(&rod.EvalOptions{
|
p.page.MustEvaluate(&rod.EvalOptions{
|
||||||
JS: `() => {
|
JS: `() => {
|
||||||
document.getElementById('` + p.chkWifi2gID + `').checked=` + fmt.Sprint(!isChecked) + `;
|
document.getElementById('` + p.chkWifi2gID + `').checked=` + fmt.Sprint(!isChecked) + `;
|
||||||
|
@ -82,14 +84,25 @@ func (p ArcherAx50) SwitchWIFI() error {
|
||||||
}`,
|
}`,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
time.Sleep(time.Millisecond * 550)
|
||||||
log.Println(p.chkWifi2gID, "DONE")
|
log.Println(p.chkWifi2gID, "DONE")
|
||||||
saveButton, err := p.page.Element(p.saveButtonID)
|
saveButton, err := p.page.Element(p.saveButtonID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("page.Element %s: %s", p.saveButtonID, err)
|
return fmt.Errorf("page.Element %s: %s", p.saveButtonID, err)
|
||||||
}
|
}
|
||||||
saveButton.MustClick()
|
saveButton.MustClick()
|
||||||
|
saveButton.MustClick()
|
||||||
|
saveButton.MustClick()
|
||||||
log.Println(p.saveButtonID, "DONE")
|
log.Println(p.saveButtonID, "DONE")
|
||||||
|
|
||||||
time.Sleep(time.Millisecond * 1500)
|
time.Sleep(time.Millisecond * time.Duration(cfg.SubmitDelay))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getCheckedMessage(isChecked bool) string {
|
||||||
|
if isChecked {
|
||||||
|
return "Time to sleep, let's switch off"
|
||||||
|
}
|
||||||
|
|
||||||
|
return "Time to laborate, let's switch WIFI on"
|
||||||
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gitea.urkob.com/urko/go-wifi-switcher/pkg/provider"
|
||||||
"github.com/go-rod/rod"
|
"github.com/go-rod/rod"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -66,7 +67,7 @@ func (p Huawei) Login(user, pass string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p Huawei) SwitchWIFI() error {
|
func (p Huawei) SwitchWIFI(cfg provider.SwitchConfig) error {
|
||||||
net, err := p.page.Element(p.netID)
|
net, err := p.page.Element(p.netID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("page.Element %s: %s", p.netID, err)
|
return fmt.Errorf("page.Element %s: %s", p.netID, err)
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gitea.urkob.com/urko/go-wifi-switcher/pkg/provider"
|
||||||
"github.com/go-rod/rod"
|
"github.com/go-rod/rod"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -62,7 +63,7 @@ func (p Nucom) Login(user, pass string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p Nucom) SwitchWIFI() error {
|
func (p Nucom) SwitchWIFI(cfg provider.SwitchConfig) error {
|
||||||
wirelessMenu, err := p.page.Element(`a[id="` + p.wirelessTabID + `"]`)
|
wirelessMenu, err := p.page.Element(`a[id="` + p.wirelessTabID + `"]`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("page.Element %s: %s", p.wirelessTabID, err)
|
return fmt.Errorf("page.Element %s: %s", p.wirelessTabID, err)
|
||||||
|
|
|
@ -37,7 +37,11 @@ func (s Switcher) SwitchWIFI(prv provider.ProviderIface) error {
|
||||||
return fmt.Errorf("prv.Login %w", err)
|
return fmt.Errorf("prv.Login %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := prv.SwitchWIFI(); err != nil {
|
switchConfig := provider.SwitchConfig{
|
||||||
|
SubmitDelay: s.config.SubmitDelay,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := prv.SwitchWIFI(switchConfig); err != nil {
|
||||||
return fmt.Errorf("prv.SwitchWIFI %w", err)
|
return fmt.Errorf("prv.SwitchWIFI %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
1
main.go
1
main.go
|
@ -22,6 +22,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
config := cfg.NewConfig(envFile)
|
config := cfg.NewConfig(envFile)
|
||||||
|
log.SetFlags(log.Ldate)
|
||||||
log.SetFlags(log.Lmicroseconds)
|
log.SetFlags(log.Lmicroseconds)
|
||||||
if config.LogFile {
|
if config.LogFile {
|
||||||
logFileName := fmt.Sprintf("%s.txt", time.Now().Format(strings.ReplaceAll(time.RFC1123Z, ":", "_")))
|
logFileName := fmt.Sprintf("%s.txt", time.Now().Format(strings.ReplaceAll(time.RFC1123Z, ":", "_")))
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
package provider
|
package provider
|
||||||
|
|
||||||
type ProviderIface interface {
|
type ProviderIface interface {
|
||||||
Login(user, pass string) error
|
Login(string, string) error
|
||||||
SwitchWIFI() error
|
SwitchWIFI(SwitchConfig) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type SwitchConfig struct {
|
||||||
|
SubmitDelay int
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue