From c9ad2a10f525f3516ece2c272eb7869887e5862f Mon Sep 17 00:00:00 2001 From: The_Spider Date: Fri, 10 Dec 2021 15:15:21 -0600 Subject: [PATCH] first functional version --- cmd/tpstate/config.go | 2 +- cmd/tpstate/init.go | 8 +++--- cmd/tpstate/main.go | 46 ++++++++++++++++++++++++++++------ cmd/tpstate/sunCalculations.go | 20 ++++++--------- internal/tplink/tplink.go | 4 +++ 5 files changed, 56 insertions(+), 24 deletions(-) diff --git a/cmd/tpstate/config.go b/cmd/tpstate/config.go index 47304c7..d96169e 100644 --- a/cmd/tpstate/config.go +++ b/cmd/tpstate/config.go @@ -37,7 +37,7 @@ type configStructure struct { // Set Defaults var config = configStructure{ LogLevel: 50, - TimeFormat: "2006-01-02 15:04:05", + TimeFormat: "2006-01-02 15:04:05 MST", Log: &logutils.LevelFilter{ Levels: []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARNING", "ERROR"}, Writer: os.Stderr, diff --git a/cmd/tpstate/init.go b/cmd/tpstate/init.go index e22c9f4..e699abd 100644 --- a/cmd/tpstate/init.go +++ b/cmd/tpstate/init.go @@ -152,28 +152,30 @@ func initialize() { setLogLevel(config.LogLevel) log.SetOutput(config.Log) + // check to make sure options were formatted correctly and can be parsed config.CalculateDate, err = time.Parse(config.TimeFormat, dt) if err != nil { runError(fmt.Sprintf("Unable to parse time: %v\n", err)) } - config.TimeZone, err = time.LoadLocation(tz) if err != nil { runError(fmt.Sprintf("Unable to parse time zone: %v\n", err)) } + // check to see if the program was called correctly if config.On && config.Off { runError("Can not set '-on' and '-off' at the same time.") } - if config.Host == "" && (config.On || config.Off || config.GetState) { runError("Must supply '-host' with '-on', '-off', '-get-state'.") } + if config.NextSunrise && config.NextSunset { + runError("Can not set '-next-sunrise' and '-next-sunset' at the same time.") + } // Get timezone offset _, tzOffset := config.CalculateDate.In(config.TimeZone).Zone() config.SunRiseSet.UtcOffset = float64(tzOffset / 60 / 60) - } func runError(errorMsg string) { diff --git a/cmd/tpstate/main.go b/cmd/tpstate/main.go index 6d6bd97..d8417a2 100644 --- a/cmd/tpstate/main.go +++ b/cmd/tpstate/main.go @@ -1,23 +1,21 @@ package main import ( + "flag" "fmt" - "log" "math" "os" "time" + + "tplink/internal/tplink" ) func main() { initialize() - _, _, err := nextSunriseSunsetTime(config.CalculateDate) - if err != nil { - log.Fatalf("[ERROR] Unable to calculate sunrise/sunset: %v\n", err) - } - - if config.NextSunrise || config.NextSunset { - sR, sS, err := nextSunriseSunsetTime(config.SunRiseSet.Date) + 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)) } @@ -30,5 +28,37 @@ func main() { } 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() } } diff --git a/cmd/tpstate/sunCalculations.go b/cmd/tpstate/sunCalculations.go index fe9f1d2..cb4a27b 100644 --- a/cmd/tpstate/sunCalculations.go +++ b/cmd/tpstate/sunCalculations.go @@ -2,6 +2,7 @@ package main import ( "log" + "math" "time" ) @@ -25,25 +26,20 @@ func nextSunriseSunsetTime(t time.Time) (sunrise, sunset time.Time, err error) { return time.Time{}, time.Time{}, err } - var ( - nSR time.Time - nSS time.Time - ) + nSR := currentSR + nSS := currentSS - if currentSR.After(t) { - nSR = currentSR - } else { + if t.After(currentSR) { nSR = nextSR } - if currentSS.After(t) { - nSS = currentSS - } else { + if t.After(currentSS) { nSS = nextSS } - log.Printf("[TRACE] Next calculated sunrise: %s\n", nSR.Format("2006-01-02 15:04:05")) - log.Printf("[TRACE] Next calculated sunset : %s\n", nSS.Format("2006-01-02 15:04:05")) + log.Printf("[TRACE] Calculated Time: %s\n", t.Format(config.TimeFormat)) + log.Printf("[TRACE] Next calculated sunrise: %s (%v)\n", nSR.Format(config.TimeFormat), math.Round(nSR.Sub(time.Now()).Seconds())) + log.Printf("[TRACE] Next calculated sunset : %s (%v)\n", nSS.Format(config.TimeFormat), math.Round(nSS.Sub(time.Now()).Seconds())) return nSR, nSS, nil } diff --git a/internal/tplink/tplink.go b/internal/tplink/tplink.go index 4518291..0b811d0 100644 --- a/internal/tplink/tplink.go +++ b/internal/tplink/tplink.go @@ -1,5 +1,9 @@ package tplink +// Credit to: +// sausheong - https://github.com/sausheong/hs1xxplug +// jaedle - https://github.com/jaedle/golang-tplink-hs100/blob/master/internal/connector/connector.go + import ( "bufio" "bytes"