change helper from TimeOfDay to Duration

This commit is contained in:
Sam Lewis
2022-10-11 02:24:48 -04:00
parent 911c3521ba
commit 7c1e8faa43
2 changed files with 18 additions and 18 deletions

View File

@@ -1,8 +1,7 @@
package main package main
import ( import (
"fmt" "log"
"time"
ga "github.com/saml-dev/gome-assistant" ga "github.com/saml-dev/gome-assistant"
) )
@@ -10,18 +9,19 @@ import (
func main() { func main() {
app := ga.App("192.168.86.67:8123") app := ga.App("192.168.86.67:8123")
defer app.Cleanup() defer app.Cleanup()
s := ga.ScheduleBuilder().Call(lightsOut).Daily().At(ga.Sunset()).Build() s := ga.ScheduleBuilder().Call(lightsOut).Daily().At(ga.Duration(23, 00)).Build()
s2 := ga.ScheduleBuilder().Call(lightsOut).Every(time.Hour*4 + time.Minute*30).Offset(ga.TimeOfDay(1, 0)).Build() s2 := ga.ScheduleBuilder().Call(lightsOut).Every(ga.Duration(04, 30)).Offset(ga.Duration(1, 0)).Build()
app.RegisterSchedule(s2) app.RegisterSchedule(s2)
app.Start() app.Start()
simpleListener := ga.EntityListenerBuilder(). simpleListener := ga.EntityListenerBuilder().
EntityId("light.lights"). EntityId("light.lights").
Call(cool). Call(cool).
OnlyBetween(ga.TimeOfDay(22, 00), ga.TimeOfDay(07, 00)) OnlyBetween(ga.Duration(22, 00), ga.Duration(07, 00))
fmt.Println(simpleListener) log.Println(simpleListener)
fmt.Println(s, "\n", s2) log.Println(s)
log.Println(s2)
} }
func lightsOut(service ga.Service, state ga.State) { func lightsOut(service ga.Service, state ga.State) {

View File

@@ -16,17 +16,17 @@ type sunriseSunset struct {
func Sunrise() *sunriseSunset { func Sunrise() *sunriseSunset {
return &sunriseSunset{ return &sunriseSunset{
base: TimeOfDay(0, 10000), base: Duration(0, 10000),
addition: TimeOfDay(0, 0), addition: Duration(0, 0),
subtraction: TimeOfDay(0, 0), subtraction: Duration(0, 0),
} }
} }
func Sunset() *sunriseSunset { func Sunset() *sunriseSunset {
return &sunriseSunset{ return &sunriseSunset{
base: TimeOfDay(0, 20000), base: Duration(0, 20000),
addition: TimeOfDay(0, 0), addition: Duration(0, 0),
subtraction: TimeOfDay(0, 0), subtraction: Duration(0, 0),
} }
} }
@@ -50,7 +50,7 @@ type timeOfDay interface {
Minutes() float64 Minutes() float64
} }
func TimeOfDay(Hour, Minute int) time.Duration { func Duration(Hour, Minute int) time.Duration {
return time.Hour*time.Duration(Hour) + time.Minute*time.Duration(Minute) return time.Hour*time.Duration(Hour) + time.Minute*time.Duration(Minute)
} }
@@ -113,13 +113,13 @@ func ScheduleBuilder() scheduleBuilder {
return scheduleBuilder{ return scheduleBuilder{
schedule{ schedule{
frequency: 0, frequency: 0,
offset: TimeOfDay(0, 0), offset: Duration(0, 0),
}, },
} }
} }
func (s schedule) String() string { func (s schedule) String() string {
return fmt.Sprintf("Run %q %s %s", return fmt.Sprintf("Schedule{ call %q %s %s }",
getFunctionName(s.callback), getFunctionName(s.callback),
frequencyToString(s.frequency), frequencyToString(s.frequency),
offsetToString(s), offsetToString(s),
@@ -182,7 +182,7 @@ func convertTimeOfDayToActualOffset(t timeOfDay) time.Duration {
if mins > 15000 { if mins > 15000 {
// TODO: same as below but w/ sunset // TODO: same as below but w/ sunset
// don't forget to subtract 20000 here // don't forget to subtract 20000 here
return TimeOfDay(0, 0) return Duration(0, 0)
} else if mins > 5000 { } else if mins > 5000 {
// TODO: use httpClient to get state of sun.sun // TODO: use httpClient to get state of sun.sun
// to get next sunrise time // to get next sunrise time
@@ -196,5 +196,5 @@ func convertTimeOfDayToActualOffset(t timeOfDay) time.Duration {
} else if mins >= 1440 { } else if mins >= 1440 {
log.Fatalln("Offset (set via At() or Offset()) cannot be more than 1 day (23h59m)") log.Fatalln("Offset (set via At() or Offset()) cannot be more than 1 day (23h59m)")
} }
return TimeOfDay(0, int(mins)) return Duration(0, int(mins))
} }