mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-07 16:07:19 -06:00
listeners working, sunrise/sunset working with string offset
This commit is contained in:
87
schedule.go
87
schedule.go
@@ -2,66 +2,13 @@ package gomeassistant
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/saml-dev/gome-assistant/internal"
|
||||
)
|
||||
|
||||
type sunriseSunset struct {
|
||||
base time.Duration
|
||||
addition time.Duration
|
||||
subtraction time.Duration
|
||||
}
|
||||
|
||||
func Sunrise() *sunriseSunset {
|
||||
return &sunriseSunset{
|
||||
base: TimeOfDay(0, 10000),
|
||||
addition: TimeOfDay(0, 0),
|
||||
subtraction: TimeOfDay(0, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func Sunset() *sunriseSunset {
|
||||
return &sunriseSunset{
|
||||
base: TimeOfDay(0, 20000),
|
||||
addition: TimeOfDay(0, 0),
|
||||
subtraction: TimeOfDay(0, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (ss *sunriseSunset) Add(hm time.Duration) *sunriseSunset {
|
||||
ss.addition = hm
|
||||
return ss
|
||||
}
|
||||
|
||||
func (ss *sunriseSunset) Subtract(hm time.Duration) *sunriseSunset {
|
||||
ss.subtraction = hm
|
||||
return ss
|
||||
}
|
||||
|
||||
func (ss *sunriseSunset) Minutes() float64 {
|
||||
return ss.base.Minutes() + ss.addition.Minutes() - ss.subtraction.Minutes()
|
||||
}
|
||||
|
||||
type timeOfDay interface {
|
||||
// Time represented as number of Minutes
|
||||
// after midnight. E.g. 02:00 would be 120.
|
||||
Minutes() float64
|
||||
}
|
||||
|
||||
// TimeOfDay is a helper function to easily represent
|
||||
// a time of day as a time.Duration since midnight.
|
||||
func TimeOfDay(hour, minute int) time.Duration {
|
||||
return time.Hour*time.Duration(hour) + time.Minute*time.Duration(minute)
|
||||
}
|
||||
|
||||
// Duration is a wrapper for TimeOfDay that makes
|
||||
// semantic sense when used with Every()
|
||||
func Duration(hour, minute int) time.Duration {
|
||||
return TimeOfDay(hour, minute)
|
||||
}
|
||||
|
||||
type scheduleCallback func(*Service, *State)
|
||||
|
||||
type schedule struct {
|
||||
@@ -125,7 +72,7 @@ func ScheduleBuilder() scheduleBuilder {
|
||||
return scheduleBuilder{
|
||||
schedule{
|
||||
frequency: 0,
|
||||
offset: TimeOfDay(0, 0),
|
||||
offset: 0,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -162,8 +109,10 @@ func (sb scheduleBuilderCall) Daily() scheduleBuilderDaily {
|
||||
return scheduleBuilderDaily(sb)
|
||||
}
|
||||
|
||||
func (sb scheduleBuilderDaily) At(t timeOfDay) scheduleBuilderEnd {
|
||||
sb.schedule.offset = convertTimeOfDayToActualOffset(t)
|
||||
// At takes a string 24hr format time like "15:30".
|
||||
func (sb scheduleBuilderDaily) At(s Time) scheduleBuilderEnd {
|
||||
t := internal.ParseTime(s)
|
||||
sb.schedule.offset = time.Duration(t.Hour())*time.Hour + time.Duration(t.Minute())*time.Minute
|
||||
return scheduleBuilderEnd(sb)
|
||||
}
|
||||
|
||||
@@ -189,28 +138,6 @@ func getFunctionName(i interface{}) string {
|
||||
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
|
||||
}
|
||||
|
||||
func convertTimeOfDayToActualOffset(t timeOfDay) time.Duration {
|
||||
mins := t.Minutes()
|
||||
if mins > 15000 {
|
||||
// TODO: same as below but w/ sunset
|
||||
// don't forget to subtract 20000 here
|
||||
return TimeOfDay(0, 0)
|
||||
} else if mins > 5000 {
|
||||
// TODO: use httpClient to get state of sun.sun
|
||||
// to get next sunrise time
|
||||
// don't forget to subtract 10000 here to get +- from sunrise that user requested
|
||||
|
||||
// retrieve next sunrise time
|
||||
|
||||
// use carbon.Parse() to create time.Time of that time
|
||||
|
||||
// return Time() of that many hours and minutes to set offset from midnight
|
||||
} else if mins >= 1440 {
|
||||
log.Fatalln("Offset (set via At() or Offset()) cannot be more than 1 day (23h59m)")
|
||||
}
|
||||
return TimeOfDay(0, int(mins))
|
||||
}
|
||||
|
||||
// app.Start() functions
|
||||
func RunSchedules(a *app) {
|
||||
if a.schedules.Len() == 0 {
|
||||
|
||||
Reference in New Issue
Block a user