first functional version

This commit is contained in:
Hyatt 2021-12-10 15:15:21 -06:00
parent 4278e622ce
commit c9ad2a10f5
Signed by: nhyatt
GPG Key ID: C50D0BBB5BC40BEA
5 changed files with 56 additions and 24 deletions

@ -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,

@ -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) {

@ -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()
}
}

@ -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
}

@ -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"