From 145c12dd25fe20f809fa6547a9b7278d3c59c1c2 Mon Sep 17 00:00:00 2001 From: Sam Lewis Date: Sun, 16 Oct 2022 22:45:13 -0400 Subject: [PATCH] all builders now take duration strings --- app.go | 22 +++++----------------- cmd/main/testing.go | 4 ++++ entitylistener.go | 6 +++--- internal/internal.go | 4 ++-- schedule.go | 35 +++++++++++++++++------------------ 5 files changed, 31 insertions(+), 40 deletions(-) diff --git a/app.go b/app.go index 3858719..3ee7262 100644 --- a/app.go +++ b/app.go @@ -29,12 +29,6 @@ type app struct { entityListenersId int64 } -/* -Time is a 24-hr format string with hour and minute, -e.g. "07:00" for 7AM or "23:00" for 11PM. -*/ -type Time string - /* NewApp establishes the websocket connection and returns an object you can use to register schedules and listeners. @@ -67,10 +61,6 @@ func (a *app) Cleanup() { } func (a *app) RegisterSchedule(s schedule) { - if s.err != nil { - log.Fatalln(s.err) // something wasn't configured properly when the schedule was built - } - if s.frequency == 0 { log.Fatalln("A schedule must call either Daily() or Every() when built.") } @@ -105,18 +95,18 @@ func (a *app) RegisterEntityListener(el entityListener) { // Sunrise take an optional string that is passed to time.ParseDuration. // Examples include "-1.5h", "30m", etc. See https://pkg.go.dev/time#ParseDuration // for full list. -func (a *app) Sunrise(offset ...string) Time { +func (a *app) Sunrise(offset ...string) string { return getSunriseSunset(a, true, offset) } // Sunset take an optional string that is passed to time.ParseDuration. // Examples include "-1.5h", "30m", etc. See https://pkg.go.dev/time#ParseDuration // for full list. -func (a *app) Sunset(offset ...string) Time { +func (a *app) Sunset(offset ...string) string { return getSunriseSunset(a, false, offset) } -func getSunriseSunset(a *app, sunrise bool, offset []string) Time { +func getSunriseSunset(a *app, sunrise bool, offset []string) string { printString := "Sunset" attrKey := "next_setting" if sunrise { @@ -138,19 +128,17 @@ func getSunriseSunset(a *app, sunrise bool, offset []string) Time { } nextSetOrRise := carbon.Parse(state.Attributes[attrKey].(string)) - log.Default().Println(nextSetOrRise) // add offset if set, this code works for negative values too if t.Microseconds() != 0 { nextSetOrRise = nextSetOrRise.AddMinutes(int(t.Minutes())) - log.Default().Println(nextSetOrRise) } return carbon2TimeString(nextSetOrRise) } -func carbon2TimeString(c carbon.Carbon) Time { - return Time(fmt.Sprintf("%02d:%02d", c.Hour(), c.Minute())) +func carbon2TimeString(c carbon.Carbon) string { + return fmt.Sprintf("%02d:%02d", c.Hour(), c.Minute()) } type subEvent struct { diff --git a/cmd/main/testing.go b/cmd/main/testing.go index b07d93b..4afcc4d 100644 --- a/cmd/main/testing.go +++ b/cmd/main/testing.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "log" ga "github.com/saml-dev/gome-assistant" @@ -10,7 +11,10 @@ func main() { app := ga.NewApp("192.168.86.67:8123") defer app.Cleanup() s := ga.ScheduleBuilder().Call(lightsOut).Daily().At(app.Sunset("1h")).Build() + s2 := ga.ScheduleBuilder().Call(lightsOut).Every("2h").Offset("10m").Build() + fmt.Println(s2) app.RegisterSchedule(s) + app.RegisterSchedule(s2) simpleListener := ga.EntityListenerBuilder(). EntityIds("group.office_ceiling_lights"). Call(listenerCB). diff --git a/entitylistener.go b/entitylistener.go index f284c05..02fdcc2 100644 --- a/entitylistener.go +++ b/entitylistener.go @@ -14,8 +14,8 @@ type entityListener struct { callback entityListenerCallback fromState string toState string - betweenStart Time - betweenEnd Time + betweenStart string + betweenEnd string err error } @@ -119,7 +119,7 @@ type elBuilder3 struct { entityListener } -func (b elBuilder3) OnlyBetween(start Time, end Time) elBuilder3 { +func (b elBuilder3) OnlyBetween(start string, end string) elBuilder3 { b.entityListener.betweenStart = start b.entityListener.betweenEnd = end return b diff --git a/internal/internal.go b/internal/internal.go index 2f1f9a2..0ad2913 100644 --- a/internal/internal.go +++ b/internal/internal.go @@ -14,8 +14,8 @@ func GetId() int64 { return id } -func ParseTime[T ~string](s T) carbon.Carbon { - t, err := time.Parse("15:04", string(s)) +func ParseTime(s string) carbon.Carbon { + t, err := time.Parse("15:04", s) if err != nil { log.Fatalf("Failed to parse time string \"%s\"; format must be HH:MM.", s) } diff --git a/schedule.go b/schedule.go index 85baf18..38d0e86 100644 --- a/schedule.go +++ b/schedule.go @@ -2,6 +2,7 @@ package gomeassistant import ( "fmt" + "log" "reflect" "runtime" "time" @@ -24,23 +25,13 @@ type schedule struct { frequency time.Duration callback scheduleCallback /* - offset is 4 character string representing hours and minutes - in a 24-hr format. - It is the base that your frequency will be added to. - Defaults to "0000" (which is probably fine for most cases). + offset is the base that your frequency will be added to. + Defaults to 0 (which is probably fine for most cases). Example: Run in the 3rd minute of every hour. - Schedule{ - frequency: gomeassistant.Hourly // helper const for time.Hour - offset: "0003" - } + ScheduleBuilder().Call(myFunc).Every("1h").Offset("3m") */ - offset time.Duration - /* - err will be set rather than returning an error to avoid checking err for nil on every schedule :) - RegisterSchedule will exit if the error is set. - */ - err error + offset time.Duration realStartTime time.Time } @@ -110,18 +101,26 @@ func (sb scheduleBuilderCall) Daily() scheduleBuilderDaily { } // At takes a string 24hr format time like "15:30". -func (sb scheduleBuilderDaily) At(s Time) scheduleBuilderEnd { +func (sb scheduleBuilderDaily) At(s string) scheduleBuilderEnd { t := internal.ParseTime(s) sb.schedule.offset = time.Duration(t.Hour())*time.Hour + time.Duration(t.Minute())*time.Minute return scheduleBuilderEnd(sb) } -func (sb scheduleBuilderCall) Every(duration time.Duration) scheduleBuilderCustom { - sb.schedule.frequency = duration +func (sb scheduleBuilderCall) Every(s string) scheduleBuilderCustom { + d, err := time.ParseDuration(s) + if err != nil { + log.Fatalf("couldn't parse string duration passed to Every(): \"%s\" see https://pkg.go.dev/time#ParseDuration for valid time units", s) + } + sb.schedule.frequency = d return scheduleBuilderCustom(sb) } -func (sb scheduleBuilderCustom) Offset(t time.Duration) scheduleBuilderEnd { +func (sb scheduleBuilderCustom) Offset(s string) scheduleBuilderEnd { + t, err := time.ParseDuration(s) + if err != nil { + log.Fatalf("Couldn't parse string duration passed to Offset(): \"%s\" see https://pkg.go.dev/time#ParseDuration for valid time units", s) + } sb.schedule.offset = t return scheduleBuilderEnd(sb) }