more development

This commit is contained in:
2021-12-09 16:28:45 -06:00
parent 8d8f68957f
commit 4278e622ce
4 changed files with 91 additions and 10 deletions

View File

@@ -2,6 +2,7 @@ package main
import (
"flag"
"fmt"
"log"
"os"
"strconv"
@@ -111,10 +112,40 @@ func initialize() {
getEnvString("DATE", time.Now().Format(config.TimeFormat)),
"(DATE) Date to use for sunrise/sunset calculation.",
)
flag.BoolVar(&config.NextSunriseSunset,
"next",
getEnvBool("NEXT", true),
"(NEXT) Determine and calculate the next occuring sunrise/sunset date & time.",
flag.BoolVar(&config.NextSunrise,
"next-sunrise",
getEnvBool("SUNRISE", false),
"(NEXT) Return the number of seconds until the next sunrise.",
)
flag.BoolVar(&config.NextSunset,
"next-sunset",
getEnvBool("SUNSET", false),
"(NEXT) Return the number of seconds until the next sunrise.",
)
flag.StringVar(&config.Host,
"host",
getEnvString("HOST", ""),
"(HOST) Hostname or IP address of device.",
)
flag.IntVar(&config.DeviceID,
"device-id",
getEnvInt("DEVICE_ID", 0),
"(DEVICE_ID) For devices with multiple switchable elements, the\ndevice-id is used to identify the switch port.\nIDs start at 0.",
)
flag.BoolVar(&config.GetState,
"get-state",
getEnvBool("GET_STATE", false),
"(ON) Get the power state of the device.",
)
flag.BoolVar(&config.On,
"on",
getEnvBool("ON", false),
"(ON) Turn the device on.",
)
flag.BoolVar(&config.Off,
"off",
getEnvBool("OFF", false),
"(OFF) Turn the device off.",
)
flag.Parse()
@@ -123,14 +154,31 @@ func initialize() {
config.CalculateDate, err = time.Parse(config.TimeFormat, dt)
if err != nil {
log.Fatalf("[ERROR] Unable to parse time: %v\n", err)
runError(fmt.Sprintf("Unable to parse time: %v\n", err))
}
config.TimeZone, err = time.LoadLocation(tz)
if err != nil {
log.Fatalf("[ERROR] Unable to parse time zone: %v\n", err)
runError(fmt.Sprintf("Unable to parse time zone: %v\n", err))
}
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'.")
}
// Get timezone offset
_, tzOffset := config.CalculateDate.In(config.TimeZone).Zone()
config.SunRiseSet.UtcOffset = float64(tzOffset / 60 / 60)
}
func runError(errorMsg string) {
flag.PrintDefaults()
fmt.Printf("\n\n")
fmt.Printf("[ERROR] %s\n", errorMsg)
os.Exit(1)
}