commit before try switching to duration

This commit is contained in:
Sam Lewis
2022-10-11 01:44:53 -04:00
parent 7bcca889f9
commit 44678ae762
2 changed files with 28 additions and 21 deletions

View File

@@ -10,11 +10,10 @@ 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().Subtract(ga.TimeOfDay(0, 30))).Build()
s := ga.ScheduleBuilder().Call(lightsOut).Daily().At(ga.Sunset.Subtract(ga.TimeOfDay(0, 30))).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(time.Hour*4 + time.Minute*30).Offset(ga.TimeOfDay(1, 0)).Build()
app.RegisterSchedule(s2) app.RegisterSchedule(s2)
// err = app.Start() app.Start()
simpleListener := ga.EntityListenerBuilder(). simpleListener := ga.EntityListenerBuilder().
EntityId("light.lights"). EntityId("light.lights").

View File

@@ -14,16 +14,20 @@ type sunriseSunset struct {
subtraction timeOfDay subtraction timeOfDay
} }
var Sunrise *sunriseSunset = &sunriseSunset{ func Sunrise() *sunriseSunset {
base: TimeOfDay(0, 10000), return &sunriseSunset{
addition: TimeOfDay(0, 0), base: TimeOfDay(0, 10000),
subtraction: TimeOfDay(0, 0), addition: TimeOfDay(0, 0),
subtraction: TimeOfDay(0, 0),
}
} }
var Sunset *sunriseSunset = &sunriseSunset{ func Sunset() *sunriseSunset {
base: TimeOfDay(0, 20000), return &sunriseSunset{
addition: TimeOfDay(0, 0), base: TimeOfDay(0, 20000),
subtraction: TimeOfDay(0, 0), addition: TimeOfDay(0, 0),
subtraction: TimeOfDay(0, 0),
}
} }
func (ss *sunriseSunset) Add(hm timeOfDay) *sunriseSunset { func (ss *sunriseSunset) Add(hm timeOfDay) *sunriseSunset {
@@ -36,27 +40,28 @@ func (ss *sunriseSunset) Subtract(hm timeOfDay) *sunriseSunset {
return ss return ss
} }
func (ss *sunriseSunset) minutes() int { func (ss *sunriseSunset) Minutes() int {
return ss.base.minute + return ss.base.minute +
(ss.addition.hour*60 + ss.addition.minute) - (ss.addition.hour*60 + ss.addition.minute) -
(ss.subtraction.hour*60 + ss.subtraction.minute) (ss.subtraction.hour*60 + ss.subtraction.minute)
} }
// HourMinute is used to express a time of day // timeOfDay is used to express a time of day
// but it shouldn't be used directly. Use // but it shouldn't be used directly. Use
// HourMinute(), Sunset(), or Sunrise() to // TimeOfDay(), Sunset(), or Sunrise() to
// create one. Add() and Subtract() can be // create one. Add() and Subtract() can be
// called on Sunset and Sunrise to offset // called on Sunset and Sunrise to offset
// from that time. // the time, e.g. Sunset().Subtract(TimeOfDay(0, 30))
// would be 30 minutes before sunset.
type timeOfDay struct { type timeOfDay struct {
hour int hour int
minute int minute int
} }
type timeOfDayInterface interface { type timeOfDayInterface interface {
// Time represented as number of minutes // Time represented as number of Minutes
// after midnight. E.g. 02:00 would be 120. // after midnight. E.g. 02:00 would be 120.
minutes() int Minutes() int
} }
func (hm timeOfDay) minutes() int { func (hm timeOfDay) minutes() int {
@@ -188,20 +193,23 @@ func getFunctionName(i interface{}) string {
} }
func convertTimeOfDayToActualOffset(t timeOfDayInterface) timeOfDay { func convertTimeOfDayToActualOffset(t timeOfDayInterface) timeOfDay {
if t.minutes() > 15000 { mins := t.Minutes()
if mins > 15000 {
// TODO: same as below but w/ sunset // TODO: same as below but w/ sunset
// don't forget to subtract 20000 here
return TimeOfDay(0, 0) return TimeOfDay(0, 0)
} else if t.minutes() > 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
// don't forget to subtract 10000 here to get +- from sunrise that user requested
// retrieve next sunrise time // retrieve next sunrise time
// use carbon.Parse() to create time.Time of that time // use carbon.Parse() to create time.Time of that time
// return Time() of that many hours and minutes to set offset from midnight // return Time() of that many hours and minutes to set offset from midnight
} else if t.minutes() >= 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, t.minutes()) return TimeOfDay(0, mins)
} }