Skip to content

Commit

Permalink
refactor(agent): 🚚 naming simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Oct 17, 2024
1 parent 5496aed commit 2897ecb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
20 changes: 10 additions & 10 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ func Run(ctx context.Context) error {
}

var (
sensorControllers []WorkerController[sensor.Entity]
sensorControllers []Controller[sensor.Entity]
mqttControllers []MQTTController
eventControllers []WorkerController[event.Event]
eventControllers []Controller[event.Event]
)
// Setup and sort all controllers by type.
for _, c := range agent.setupControllers(runCtx, prefs) {
Expand All @@ -162,7 +162,14 @@ func Run(ctx context.Context) error {
// Run workers for any sensor controllers.
go func() {
defer wg.Done()
runControllerWorkers(runCtx, prefs, sensorControllers...)
runControllers(runCtx, prefs, sensorControllers...)
}()

wg.Add(1)
// Run workers for any event controllers.
go func() {
defer wg.Done()
runControllers(runCtx, prefs, eventControllers...)
}()

if len(mqttControllers) > 0 {
Expand All @@ -174,13 +181,6 @@ func Run(ctx context.Context) error {
}()
}

wg.Add(1)
// Run workers for any event controllers.
go func() {
defer wg.Done()
runControllerWorkers(runCtx, prefs, eventControllers...)
}()

wg.Add(1)
// Listen for notifications from Home Assistant.
go func() {
Expand Down
12 changes: 6 additions & 6 deletions internal/agent/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/joshuar/go-hass-agent/internal/preferences"
)

type WorkerController[T any] interface {
type Controller[T any] interface {
ID() string
ActiveWorkers() []string
// InactiveWorkers is a list of the names of all currently inactive Workers.
Expand All @@ -33,14 +33,14 @@ type WorkerController[T any] interface {

// SensorController represents an object that manages one or more Workers.
type SensorController interface {
WorkerController[sensor.Entity]
Controller[sensor.Entity]
// States returns the list of all sensor states tracked by all workers of
// this controller.
States(ctx context.Context) []sensor.Entity
}

type EventController interface {
WorkerController[event.Event]
Controller[event.Event]
}

// MQTTController represents an object that is responsible for controlling the
Expand Down Expand Up @@ -95,7 +95,7 @@ func (agent *Agent) setupControllers(ctx context.Context, prefs *preferences.Pre
return controllers
}

func runControllerWorkers[T any](ctx context.Context, prefs *preferences.Preferences, controllers ...WorkerController[T]) {
func runControllers[T any](ctx context.Context, prefs *preferences.Preferences, controllers ...Controller[T]) {
// Start all inactive workers of all controllers.
eventCh := startAllWorkers(ctx, controllers)
if len(eventCh) == 0 {
Expand Down Expand Up @@ -134,7 +134,7 @@ func runControllerWorkers[T any](ctx context.Context, prefs *preferences.Prefere
}
}

func startAllWorkers[T any](ctx context.Context, controllers []WorkerController[T]) []<-chan T {
func startAllWorkers[T any](ctx context.Context, controllers []Controller[T]) []<-chan T {
var eventCh []<-chan T

for _, controller := range controllers {
Expand All @@ -161,7 +161,7 @@ func startAllWorkers[T any](ctx context.Context, controllers []WorkerController[
return eventCh
}

func stopAllWorkers[T any](ctx context.Context, controllers []WorkerController[T]) {
func stopAllWorkers[T any](ctx context.Context, controllers []Controller[T]) {
for _, controller := range controllers {
logging.FromContext(ctx).Debug("Stopping controller", slog.String("controller", controller.ID()))

Expand Down

0 comments on commit 2897ecb

Please sign in to comment.