mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-06 17:15:14 -06:00
switched to time.Duration
This commit is contained in:
6
app.go
6
app.go
@@ -64,13 +64,13 @@ func (a *app) RegisterSchedule(s schedule) {
|
||||
log.Fatalln("A schedule must call either Daily() or Every() when built.")
|
||||
}
|
||||
|
||||
// TODO: consider moving all time stuff to carbon?
|
||||
now := time.Now()
|
||||
startTime := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) // start at midnight today
|
||||
|
||||
// apply offset if set
|
||||
if s.offset.minutes() != 0 {
|
||||
startTime.Add(time.Hour * time.Duration(s.offset.hour))
|
||||
startTime.Add(time.Minute * time.Duration(s.offset.minute))
|
||||
if s.offset.Minutes() > 0 {
|
||||
startTime.Add(s.offset)
|
||||
}
|
||||
|
||||
// advance first scheduled time by frequency until it is in the future
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
func main() {
|
||||
app := ga.App("192.168.86.67:8123")
|
||||
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()).Build()
|
||||
s2 := ga.ScheduleBuilder().Call(lightsOut).Every(time.Hour*4 + time.Minute*30).Offset(ga.TimeOfDay(1, 0)).Build()
|
||||
app.RegisterSchedule(s2)
|
||||
app.Start()
|
||||
|
||||
@@ -1,19 +1,21 @@
|
||||
package gomeassistant
|
||||
|
||||
import "time"
|
||||
|
||||
type entityListener struct {
|
||||
entityId string
|
||||
callback entityListenerCallback
|
||||
fromState string
|
||||
toState string
|
||||
betweenStart timeOfDay
|
||||
betweenEnd timeOfDay
|
||||
betweenStart time.Duration
|
||||
betweenEnd time.Duration
|
||||
}
|
||||
|
||||
type entityListenerCallback func(Service, Data)
|
||||
|
||||
type Data struct{}
|
||||
|
||||
func (b elBuilder3) OnlyBetween(start timeOfDay, end timeOfDay) elBuilder3 {
|
||||
func (b elBuilder3) OnlyBetween(start time.Duration, end time.Duration) elBuilder3 {
|
||||
b.entityListener.betweenStart = start
|
||||
b.entityListener.betweenEnd = end
|
||||
return b
|
||||
|
||||
65
schedule.go
65
schedule.go
@@ -9,9 +9,9 @@ import (
|
||||
)
|
||||
|
||||
type sunriseSunset struct {
|
||||
base timeOfDay
|
||||
addition timeOfDay
|
||||
subtraction timeOfDay
|
||||
base time.Duration
|
||||
addition time.Duration
|
||||
subtraction time.Duration
|
||||
}
|
||||
|
||||
func Sunrise() *sunriseSunset {
|
||||
@@ -30,50 +30,28 @@ func Sunset() *sunriseSunset {
|
||||
}
|
||||
}
|
||||
|
||||
func (ss *sunriseSunset) Add(hm timeOfDay) *sunriseSunset {
|
||||
func (ss *sunriseSunset) Add(hm time.Duration) *sunriseSunset {
|
||||
ss.addition = hm
|
||||
return ss
|
||||
}
|
||||
|
||||
func (ss *sunriseSunset) Subtract(hm timeOfDay) *sunriseSunset {
|
||||
func (ss *sunriseSunset) Subtract(hm time.Duration) *sunriseSunset {
|
||||
ss.subtraction = hm
|
||||
return ss
|
||||
}
|
||||
|
||||
func (ss *sunriseSunset) Minutes() int {
|
||||
return ss.base.minute +
|
||||
(ss.addition.hour*60 + ss.addition.minute) -
|
||||
(ss.subtraction.hour*60 + ss.subtraction.minute)
|
||||
func (ss *sunriseSunset) Minutes() float64 {
|
||||
return ss.base.Minutes() + ss.addition.Minutes() - ss.subtraction.Minutes()
|
||||
}
|
||||
|
||||
// timeOfDay is used to express a time of day
|
||||
// but it shouldn't be used directly. Use
|
||||
// TimeOfDay(), Sunset(), or Sunrise() to
|
||||
// create one. Add() and Subtract() can be
|
||||
// called on Sunset and Sunrise to offset
|
||||
// the time, e.g. Sunset().Subtract(TimeOfDay(0, 30))
|
||||
// would be 30 minutes before sunset.
|
||||
type timeOfDay struct {
|
||||
hour int
|
||||
minute int
|
||||
}
|
||||
|
||||
type timeOfDayInterface interface {
|
||||
type timeOfDay interface {
|
||||
// Time represented as number of Minutes
|
||||
// after midnight. E.g. 02:00 would be 120.
|
||||
Minutes() int
|
||||
Minutes() float64
|
||||
}
|
||||
|
||||
func (hm timeOfDay) minutes() int {
|
||||
return hm.hour*60 + hm.minute
|
||||
}
|
||||
|
||||
func TimeOfDay(Hour, Minute int) timeOfDay {
|
||||
return timeOfDay{Hour, Minute}
|
||||
}
|
||||
|
||||
func (hm timeOfDay) String() string {
|
||||
return fmt.Sprintf("%02d:%02d", hm.hour, hm.minute)
|
||||
func TimeOfDay(Hour, Minute int) time.Duration {
|
||||
return time.Hour*time.Duration(Hour) + time.Minute*time.Duration(Minute)
|
||||
}
|
||||
|
||||
type scheduleCallback func(Service, State)
|
||||
@@ -102,7 +80,7 @@ type schedule struct {
|
||||
offset: "0003"
|
||||
}
|
||||
*/
|
||||
offset timeOfDay
|
||||
offset time.Duration
|
||||
/*
|
||||
This 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.
|
||||
@@ -135,7 +113,7 @@ func ScheduleBuilder() scheduleBuilder {
|
||||
return scheduleBuilder{
|
||||
schedule{
|
||||
frequency: 0,
|
||||
offset: timeOfDay{0, 0},
|
||||
offset: TimeOfDay(0, 0),
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -144,10 +122,17 @@ func (s schedule) String() string {
|
||||
return fmt.Sprintf("Run %q %s %s",
|
||||
getFunctionName(s.callback),
|
||||
frequencyToString(s.frequency),
|
||||
s.offset,
|
||||
offsetToString(s),
|
||||
)
|
||||
}
|
||||
|
||||
func offsetToString(s schedule) string {
|
||||
if s.frequency.Hours() == 24 {
|
||||
return fmt.Sprintf("%02d:%02d", int(s.offset.Hours()), int(s.offset.Minutes())%60)
|
||||
}
|
||||
return s.offset.String()
|
||||
}
|
||||
|
||||
func frequencyToString(d time.Duration) string {
|
||||
if d.Hours() == 24 {
|
||||
return "daily at"
|
||||
@@ -165,7 +150,7 @@ func (sb scheduleBuilderCall) Daily() scheduleBuilderDaily {
|
||||
return scheduleBuilderDaily(sb)
|
||||
}
|
||||
|
||||
func (sb scheduleBuilderDaily) At(t timeOfDayInterface) scheduleBuilderEnd {
|
||||
func (sb scheduleBuilderDaily) At(t timeOfDay) scheduleBuilderEnd {
|
||||
sb.schedule.offset = convertTimeOfDayToActualOffset(t)
|
||||
return scheduleBuilderEnd(sb)
|
||||
}
|
||||
@@ -175,7 +160,7 @@ func (sb scheduleBuilderCall) Every(duration time.Duration) scheduleBuilderCusto
|
||||
return scheduleBuilderCustom(sb)
|
||||
}
|
||||
|
||||
func (sb scheduleBuilderCustom) Offset(t timeOfDay) scheduleBuilderEnd {
|
||||
func (sb scheduleBuilderCustom) Offset(t time.Duration) scheduleBuilderEnd {
|
||||
sb.schedule.offset = t
|
||||
return scheduleBuilderEnd(sb)
|
||||
}
|
||||
@@ -192,7 +177,7 @@ func getFunctionName(i interface{}) string {
|
||||
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
|
||||
}
|
||||
|
||||
func convertTimeOfDayToActualOffset(t timeOfDayInterface) timeOfDay {
|
||||
func convertTimeOfDayToActualOffset(t timeOfDay) time.Duration {
|
||||
mins := t.Minutes()
|
||||
if mins > 15000 {
|
||||
// TODO: same as below but w/ sunset
|
||||
@@ -211,5 +196,5 @@ func convertTimeOfDayToActualOffset(t timeOfDayInterface) timeOfDay {
|
||||
} else if mins >= 1440 {
|
||||
log.Fatalln("Offset (set via At() or Offset()) cannot be more than 1 day (23h59m)")
|
||||
}
|
||||
return TimeOfDay(0, mins)
|
||||
return TimeOfDay(0, int(mins))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user