46 lines
873 B
Go
46 lines
873 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
"tplink/internal/tplink"
|
|
)
|
|
|
|
func switchDevice(host string, id int, state bool) (string, error) {
|
|
t := tplink.Tplink{
|
|
Host: host,
|
|
SwitchID: id,
|
|
}
|
|
|
|
// send request to device
|
|
if err := t.ChangeStateMultiSwitch(state); err != nil {
|
|
return "", fmt.Errorf("Unable to change device state: %v\n", err)
|
|
}
|
|
|
|
// give device time to execute command
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
// get device state
|
|
time.Sleep(100 * time.Millisecond)
|
|
sysInfo, err := t.SystemInfo()
|
|
if err != nil {
|
|
return "", fmt.Errorf("Unable to get info from target device: %v\n", err)
|
|
}
|
|
|
|
var ds int
|
|
if len(sysInfo.System.GetSysinfo.Children) == 0 {
|
|
ds = sysInfo.System.GetSysinfo.RelayState
|
|
} else {
|
|
ds = sysInfo.System.GetSysinfo.Children[id].State
|
|
}
|
|
|
|
var fs string
|
|
if ds == 0 {
|
|
fs = "OFF"
|
|
} else {
|
|
fs = "ON"
|
|
}
|
|
|
|
return fs, nil
|
|
}
|