more development
This commit is contained in:
parent
8d8f68957f
commit
4278e622ce
@ -22,8 +22,16 @@ type configStructure struct {
|
||||
|
||||
// mode of operation
|
||||
CalculateDate time.Time `json:"sunrise_sunset_calculation_date"`
|
||||
SecondsUntil bool `json:"seconds_until"`
|
||||
NextSunriseSunset bool `json:"next_sunrise_sunset"`
|
||||
SecondsUntil bool `json:"seconds_until"`
|
||||
NextSunrise bool `json:"next_sunrise"`
|
||||
NextSunset bool `json:"next_sunset"`
|
||||
GetState bool `json:"get_state"`
|
||||
On bool `json:"on"`
|
||||
Off bool `json:"off"`
|
||||
|
||||
// Host
|
||||
Host string `json:"hostname"`
|
||||
DeviceID int `json:"device_id"`
|
||||
}
|
||||
|
||||
// Set Defaults
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -1,7 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@ -11,4 +15,20 @@ func main() {
|
||||
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)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,12 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func nextSunriseSunsetTime(t time.Time) (time.Time, time.Time, error) {
|
||||
func nextSunriseSunsetTime(t time.Time) (sunrise, sunset time.Time, err error) {
|
||||
var (
|
||||
nextSR time.Time
|
||||
nextSS time.Time
|
||||
)
|
||||
|
||||
s := config.SunRiseSet
|
||||
|
||||
s.Date = t
|
||||
@ -15,7 +20,7 @@ func nextSunriseSunsetTime(t time.Time) (time.Time, time.Time, error) {
|
||||
}
|
||||
|
||||
s.Date = t.Add(24 * time.Hour)
|
||||
nextSR, nextSS, err := s.GetSunriseSunset()
|
||||
nextSR, nextSS, err = s.GetSunriseSunset()
|
||||
if err != nil {
|
||||
return time.Time{}, time.Time{}, err
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user