mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-08 04:07:18 -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.")
|
log.Fatalln("A schedule must call either Daily() or Every() when built.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: consider moving all time stuff to carbon?
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
startTime := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) // start at midnight today
|
startTime := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) // start at midnight today
|
||||||
|
|
||||||
// apply offset if set
|
// apply offset if set
|
||||||
if s.offset.minutes() != 0 {
|
if s.offset.Minutes() > 0 {
|
||||||
startTime.Add(time.Hour * time.Duration(s.offset.hour))
|
startTime.Add(s.offset)
|
||||||
startTime.Add(time.Minute * time.Duration(s.offset.minute))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// advance first scheduled time by frequency until it is in the future
|
// advance first scheduled time by frequency until it is in the future
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ 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()).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)
|
||||||
app.Start()
|
app.Start()
|
||||||
|
|||||||
@@ -1,19 +1,21 @@
|
|||||||
package gomeassistant
|
package gomeassistant
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
type entityListener struct {
|
type entityListener struct {
|
||||||
entityId string
|
entityId string
|
||||||
callback entityListenerCallback
|
callback entityListenerCallback
|
||||||
fromState string
|
fromState string
|
||||||
toState string
|
toState string
|
||||||
betweenStart timeOfDay
|
betweenStart time.Duration
|
||||||
betweenEnd timeOfDay
|
betweenEnd time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
type entityListenerCallback func(Service, Data)
|
type entityListenerCallback func(Service, Data)
|
||||||
|
|
||||||
type Data struct{}
|
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.betweenStart = start
|
||||||
b.entityListener.betweenEnd = end
|
b.entityListener.betweenEnd = end
|
||||||
return b
|
return b
|
||||||
|
|||||||
65
schedule.go
65
schedule.go
@@ -9,9 +9,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type sunriseSunset struct {
|
type sunriseSunset struct {
|
||||||
base timeOfDay
|
base time.Duration
|
||||||
addition timeOfDay
|
addition time.Duration
|
||||||
subtraction timeOfDay
|
subtraction time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func Sunrise() *sunriseSunset {
|
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
|
ss.addition = hm
|
||||||
return ss
|
return ss
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ss *sunriseSunset) Subtract(hm timeOfDay) *sunriseSunset {
|
func (ss *sunriseSunset) Subtract(hm time.Duration) *sunriseSunset {
|
||||||
ss.subtraction = hm
|
ss.subtraction = hm
|
||||||
return ss
|
return ss
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ss *sunriseSunset) Minutes() int {
|
func (ss *sunriseSunset) Minutes() float64 {
|
||||||
return ss.base.minute +
|
return ss.base.Minutes() + ss.addition.Minutes() - ss.subtraction.Minutes()
|
||||||
(ss.addition.hour*60 + ss.addition.minute) -
|
|
||||||
(ss.subtraction.hour*60 + ss.subtraction.minute)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// timeOfDay is used to express a time of day
|
type timeOfDay interface {
|
||||||
// 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 {
|
|
||||||
// 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() float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hm timeOfDay) minutes() int {
|
func TimeOfDay(Hour, Minute int) time.Duration {
|
||||||
return hm.hour*60 + hm.minute
|
return time.Hour*time.Duration(Hour) + time.Minute*time.Duration(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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type scheduleCallback func(Service, State)
|
type scheduleCallback func(Service, State)
|
||||||
@@ -102,7 +80,7 @@ type schedule struct {
|
|||||||
offset: "0003"
|
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 :)
|
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.
|
RegisterSchedule will exit if the error is set.
|
||||||
@@ -135,7 +113,7 @@ func ScheduleBuilder() scheduleBuilder {
|
|||||||
return scheduleBuilder{
|
return scheduleBuilder{
|
||||||
schedule{
|
schedule{
|
||||||
frequency: 0,
|
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",
|
return fmt.Sprintf("Run %q %s %s",
|
||||||
getFunctionName(s.callback),
|
getFunctionName(s.callback),
|
||||||
frequencyToString(s.frequency),
|
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 {
|
func frequencyToString(d time.Duration) string {
|
||||||
if d.Hours() == 24 {
|
if d.Hours() == 24 {
|
||||||
return "daily at"
|
return "daily at"
|
||||||
@@ -165,7 +150,7 @@ func (sb scheduleBuilderCall) Daily() scheduleBuilderDaily {
|
|||||||
return scheduleBuilderDaily(sb)
|
return scheduleBuilderDaily(sb)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sb scheduleBuilderDaily) At(t timeOfDayInterface) scheduleBuilderEnd {
|
func (sb scheduleBuilderDaily) At(t timeOfDay) scheduleBuilderEnd {
|
||||||
sb.schedule.offset = convertTimeOfDayToActualOffset(t)
|
sb.schedule.offset = convertTimeOfDayToActualOffset(t)
|
||||||
return scheduleBuilderEnd(sb)
|
return scheduleBuilderEnd(sb)
|
||||||
}
|
}
|
||||||
@@ -175,7 +160,7 @@ func (sb scheduleBuilderCall) Every(duration time.Duration) scheduleBuilderCusto
|
|||||||
return scheduleBuilderCustom(sb)
|
return scheduleBuilderCustom(sb)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sb scheduleBuilderCustom) Offset(t timeOfDay) scheduleBuilderEnd {
|
func (sb scheduleBuilderCustom) Offset(t time.Duration) scheduleBuilderEnd {
|
||||||
sb.schedule.offset = t
|
sb.schedule.offset = t
|
||||||
return scheduleBuilderEnd(sb)
|
return scheduleBuilderEnd(sb)
|
||||||
}
|
}
|
||||||
@@ -192,7 +177,7 @@ func getFunctionName(i interface{}) string {
|
|||||||
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
|
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
|
||||||
}
|
}
|
||||||
|
|
||||||
func convertTimeOfDayToActualOffset(t timeOfDayInterface) timeOfDay {
|
func convertTimeOfDayToActualOffset(t timeOfDay) time.Duration {
|
||||||
mins := t.Minutes()
|
mins := t.Minutes()
|
||||||
if mins > 15000 {
|
if mins > 15000 {
|
||||||
// TODO: same as below but w/ sunset
|
// TODO: same as below but w/ sunset
|
||||||
@@ -211,5 +196,5 @@ func convertTimeOfDayToActualOffset(t timeOfDayInterface) timeOfDay {
|
|||||||
} 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, mins)
|
return TimeOfDay(0, int(mins))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user