package main import ( "flag" "fmt" "math" "os" "time" "github.com/TheSp1der/tplink" ) func main() { initialize() switch { case config.NextSunrise || config.NextSunset: sR, sS, err := nextSunriseSunsetTime(config.CalculateDate.In(config.TimeZone)) if err != nil { runError(fmt.Sprintf("Unable to calculate sunrise/sunset: %v\n", err)) } switch { case config.NextSunrise: fmt.Printf("%v\n", math.Round(sR.Sub(time.Now()).Seconds())) case config.NextSunset: fmt.Printf("%v\n", math.Round(sS.Sub(time.Now()).Seconds())) } os.Exit(0) case config.On: tp := tplink.Tplink{Host: config.Host, SwitchID: config.DeviceID} if err := tp.ChangeStateMultiSwitch(true); err != nil { runError(fmt.Sprintf("Error communcating with device: %v", err)) } case config.Off: tp := tplink.Tplink{Host: config.Host, SwitchID: config.DeviceID} if err := tp.ChangeStateMultiSwitch(false); err != nil { runError(fmt.Sprintf("Error communcating with device: %v", err)) } case config.GetState: tp := tplink.Tplink{Host: config.Host, SwitchID: config.DeviceID} info, err := tp.SystemInfo() if err != nil { runError(fmt.Sprintf("Error communcating with device: %v", err)) } var state int if len(info.System.GetSysinfo.Children) == 0 { fmt.Printf("Name:\t\t%s\n", info.System.GetSysinfo.Alias) state = info.System.GetSysinfo.RelayState } else { fmt.Printf("Name:\t\t%s\n", info.System.GetSysinfo.Children[config.DeviceID].Alias) state = info.System.GetSysinfo.Children[config.DeviceID].State } fmt.Printf("MAC Address:\t%s\n", info.System.GetSysinfo.Mac) if state == 0 { fmt.Printf("Power is:\t%s\n", "Off") } else { fmt.Printf("Power is:\t%s\n", "On") } default: flag.PrintDefaults() } }