make some things private

This commit is contained in:
Sam Lewis
2022-10-23 18:53:38 -04:00
parent f4d44cb6fe
commit e2fb6872ae
6 changed files with 18 additions and 14 deletions

4
app.go
View File

@@ -43,7 +43,7 @@ func NewApp(connString string) app {
httpClient := http.NewHttpClient(connString, token) httpClient := http.NewHttpClient(connString, token)
service := NewService(conn, ctx, httpClient) service := NewService(conn, ctx, httpClient)
state := NewState(httpClient) state := newState(httpClient)
return app{ return app{
conn: conn, conn: conn,
@@ -159,7 +159,7 @@ func carbon2TimeString(c carbon.Carbon) string {
func (a *app) Start() { func (a *app) Start() {
// schedules // schedules
go RunSchedules(a) go runSchedules(a)
// subscribe to state_changed events // subscribe to state_changed events
id := internal.GetId() id := internal.GetId()

View File

@@ -138,16 +138,16 @@ func callEntityListeners(app *app, msgBytes []byte) {
for _, l := range listeners { for _, l := range listeners {
// Check conditions // Check conditions
if c := CheckWithinTimeRange(l.betweenStart, l.betweenEnd); c.fail { if c := checkWithinTimeRange(l.betweenStart, l.betweenEnd); c.fail {
return return
} }
if c := CheckStatesMatch(l.fromState, data.OldState.State); c.fail { if c := checkStatesMatch(l.fromState, data.OldState.State); c.fail {
return return
} }
if c := CheckStatesMatch(l.toState, data.NewState.State); c.fail { if c := checkStatesMatch(l.toState, data.NewState.State); c.fail {
return return
} }
if c := CheckThrottle(l.throttle, l.lastRan); c.fail { if c := checkThrottle(l.throttle, l.lastRan); c.fail {
return return
} }

View File

@@ -105,10 +105,10 @@ func callEventListeners(app *app, msg ws.ChanMsg) {
for _, l := range listeners { for _, l := range listeners {
// Check conditions // Check conditions
if c := CheckWithinTimeRange(l.betweenStart, l.betweenEnd); c.fail { if c := checkWithinTimeRange(l.betweenStart, l.betweenEnd); c.fail {
return return
} }
if c := CheckThrottle(l.throttle, l.lastRan); c.fail { if c := checkThrottle(l.throttle, l.lastRan); c.fail {
return return
} }

View File

@@ -11,7 +11,7 @@ type conditionCheck struct {
fail bool fail bool
} }
func CheckWithinTimeRange(startTime, endTime string) conditionCheck { func checkWithinTimeRange(startTime, endTime string) conditionCheck {
cc := conditionCheck{fail: false} cc := conditionCheck{fail: false}
// if betweenStart and betweenEnd both set, first account for midnight // if betweenStart and betweenEnd both set, first account for midnight
// overlap, then check if between those times. // overlap, then check if between those times.
@@ -42,7 +42,7 @@ func CheckWithinTimeRange(startTime, endTime string) conditionCheck {
return cc return cc
} }
func CheckStatesMatch(listenerState, s string) conditionCheck { func checkStatesMatch(listenerState, s string) conditionCheck {
cc := conditionCheck{fail: false} cc := conditionCheck{fail: false}
// check if fromState or toState are set and don't match // check if fromState or toState are set and don't match
if listenerState != "" && listenerState != s { if listenerState != "" && listenerState != s {
@@ -51,7 +51,7 @@ func CheckStatesMatch(listenerState, s string) conditionCheck {
return cc return cc
} }
func CheckThrottle(throttle time.Duration, lastRan carbon.Carbon) conditionCheck { func checkThrottle(throttle time.Duration, lastRan carbon.Carbon) conditionCheck {
cc := conditionCheck{fail: false} cc := conditionCheck{fail: false}
// check if Throttle is set and that duration hasn't passed since lastRan // check if Throttle is set and that duration hasn't passed since lastRan
if throttle.Seconds() > 0 && if throttle.Seconds() > 0 &&

View File

@@ -138,7 +138,7 @@ func getFunctionName(i interface{}) string {
} }
// app.Start() functions // app.Start() functions
func RunSchedules(a *app) { func runSchedules(a *app) {
if a.schedules.Len() == 0 { if a.schedules.Len() == 0 {
return return
} }
@@ -167,6 +167,11 @@ func popSchedule(a *app) schedule {
} }
func requeueSchedule(a *app, s schedule) { func requeueSchedule(a *app, s schedule) {
// TODO: figure out how to handle sunset/sunrise in here. Maybe just
// add sunrise bool and sunset bool to Schedule, might have to change
// API to be .Call().Sunset("1h") instead of .Call().At(ga.Sunset("1h"))
// then that function could easily set the flag. Kinda ruins the english
// language sentence structure but maybe simplest way to get it working
s.realStartTime = s.realStartTime.Add(s.frequency) s.realStartTime = s.realStartTime.Add(s.frequency)
a.schedules.Insert(s, float64(s.realStartTime.Unix())) a.schedules.Insert(s, float64(s.realStartTime.Unix()))
} }

View File

@@ -17,10 +17,9 @@ type EntityState struct {
State string `json:"state"` State string `json:"state"`
Attributes map[string]any `json:"attributes"` Attributes map[string]any `json:"attributes"`
LastChanged time.Time `json:"last_changed"` LastChanged time.Time `json:"last_changed"`
LastUpdated time.Time `json:"last_updated"`
} }
func NewState(c *http.HttpClient) *State { func newState(c *http.HttpClient) *State {
return &State{httpClient: c} return &State{httpClient: c}
} }