From 25a843fc7b40c4987f8fac7f2c3e1c7ccd4b340c Mon Sep 17 00:00:00 2001 From: Sam Lewis Date: Mon, 10 Oct 2022 00:11:41 -0400 Subject: [PATCH] few things: - easy comparison of hourMinute with .int() - created Sunset/Sunrise vars of type hourMinute for easy end-user use --- app.go | 24 +++++++++++++++++++----- entitylistener.go | 34 ++++++++++++++++++---------------- internal/setup/setup.go | 3 +-- schedule.go | 12 ++++++++++-- 4 files changed, 48 insertions(+), 25 deletions(-) diff --git a/app.go b/app.go index 7cc8caf..7f09416 100644 --- a/app.go +++ b/app.go @@ -2,7 +2,6 @@ package gomeassistant import ( "context" - "fmt" "time" "github.com/saml-dev/gome-assistant/internal/setup" @@ -17,6 +16,11 @@ type app struct { entityListeners []entityListener } +var ( + Sunrise hourMinute = hourMinute{1000, 0} + Sunset hourMinute = hourMinute{1001, 0} +) + /* App establishes the websocket connection and returns an object you can use to register schedules and listeners. @@ -43,7 +47,6 @@ func (a *app) Cleanup() { } func (a *app) RegisterSchedule(s schedule) { - fmt.Println(a.schedules) if s.err != nil { panic(s.err) // something wasn't configured properly when the schedule was built } @@ -56,9 +59,16 @@ func (a *app) RegisterSchedule(s schedule) { 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.Hour != 0 || s.offset.Minute != 0 { - startTime.Add(time.Hour * time.Duration(s.offset.Hour)) - startTime.Add(time.Minute * time.Duration(s.offset.Minute)) + if s.offset.int() != 0 { + if s.offset.int() == Sunrise.int() { + // TODO: same as sunset w/ sunrise + } else if s.offset.int() == Sunset.int() { + // TODO: add an http client (w/ token) to *app, use it to get state of sun.sun + // to get next sunset time + } else { + startTime.Add(time.Hour * time.Duration(s.offset.Hour)) + startTime.Add(time.Minute * time.Duration(s.offset.Minute)) + } } // advance first scheduled time by frequency until it is in the future @@ -70,6 +80,10 @@ func (a *app) RegisterSchedule(s schedule) { a.schedules = append(a.schedules, s) } +func (a *app) Start() { + // TODO: figure out looping listening to messages +} + const ( FrequencyMissing time.Duration = 0 diff --git a/entitylistener.go b/entitylistener.go index bddf492..4659537 100644 --- a/entitylistener.go +++ b/entitylistener.go @@ -13,6 +13,24 @@ type entityListenerCallback func(Service, Data) type Data struct{} +func (b elBuilder3) OnlyBetween(start hourMinute, end hourMinute) elBuilder3 { + b.entityListener.betweenStart = start + b.entityListener.betweenEnd = end + return b +} + +func (b elBuilder3) FromState(s string) elBuilder3 { + b.entityListener.fromState = s + return b +} + +func (b elBuilder3) ToState(s string) elBuilder3 { + b.entityListener.toState = s + return b +} + +/* Builders */ + func EntityListenerBuilder() elBuilder1 { return elBuilder1{entityListener{}} } @@ -38,19 +56,3 @@ func (b elBuilder2) Call(callback entityListenerCallback) elBuilder3 { type elBuilder3 struct { entityListener } - -func (b elBuilder3) OnlyBetween(start hourMinute, end hourMinute) elBuilder3 { - b.entityListener.betweenStart = start - b.entityListener.betweenEnd = end - return b -} - -func (b elBuilder3) FromState(s string) elBuilder3 { - b.entityListener.fromState = s - return b -} - -func (b elBuilder3) ToState(s string) elBuilder3 { - b.entityListener.toState = s - return b -} diff --git a/internal/setup/setup.go b/internal/setup/setup.go index 6e38a51..b68befe 100644 --- a/internal/setup/setup.go +++ b/internal/setup/setup.go @@ -18,7 +18,7 @@ type AuthMessage struct { func WriteMessage[T any](msg T, conn *websocket.Conn, ctx context.Context) error { msgJson, err := json.Marshal(msg) - fmt.Println(string(msgJson)) + // fmt.Println(string(msgJson)) if err != nil { return err } @@ -97,7 +97,6 @@ func VerifyAuthResponse(conn *websocket.Conn, ctx context.Context) error { var authResp authResponse json.Unmarshal(msg, &authResp) - fmt.Println(authResp) if authResp.MsgType != "auth_ok" { return errors.New("invalid auth token") } diff --git a/schedule.go b/schedule.go index 3ea7ccf..25dfc23 100644 --- a/schedule.go +++ b/schedule.go @@ -12,6 +12,10 @@ type hourMinute struct { Minute int } +func (hm hourMinute) int() int { + return hm.Hour + hm.Minute +} + func HourMinute(Hour, Minute int) hourMinute { return hourMinute{Hour, Minute} } @@ -76,7 +80,12 @@ type scheduleBuilderEnd struct { } func ScheduleBuilder() scheduleBuilder { - return scheduleBuilder{schedule{}} + return scheduleBuilder{ + schedule{ + frequency: 0, + offset: hourMinute{0, 0}, + }, + } } func (s schedule) String() string { @@ -88,7 +97,6 @@ func (s schedule) String() string { } func frequencyToString(d time.Duration) string { - fmt.Println(d.Hours(), d.Minutes(), d.Seconds()) if d.Hours() == 24 { return "daily at" }