Skip to content

Commit

Permalink
refactor(agent): ♻️ clean up code for readabillity
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Jun 8, 2024
1 parent f344aeb commit baa89c0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
22 changes: 17 additions & 5 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/joshuar/go-hass-agent/internal/hass/sensor"
)

var ErrCtxFailed = errors.New("unable to create a context")

// Agent holds the data and structure representing an instance of the agent.
// This includes the data structure for the UI elements and tray and some
// strings such as app name and version.
Expand All @@ -39,15 +41,16 @@ type Options struct {
Headless, ForceRegister, IgnoreURLs bool
}

//nolint:exhaustruct
func New(o *Options) *Agent {
a := &Agent{
agent := &Agent{
done: make(chan struct{}),
Options: o,
}
if !a.Options.Headless {
a.ui = fyneui.NewFyneUI(a.Options.ID)
if !agent.Options.Headless {
agent.ui = fyneui.NewFyneUI(agent.Options.ID)
}
return a
return agent
}

// Run is the "main loop" of the agent. It sets up the agent, loads the config
Expand All @@ -58,7 +61,9 @@ func (agent *Agent) Run(trk SensorTracker, reg sensor.Registry) {

// Pre-flight: check if agent is registered. If not, run registration flow.
var regWait sync.WaitGroup

regWait.Add(1)

go func() {
defer regWait.Done()
if err := agent.checkRegistration(trk); err != nil {
Expand All @@ -67,6 +72,7 @@ func (agent *Agent) Run(trk SensorTracker, reg sensor.Registry) {
}()

wg.Add(1)

go func() {
defer wg.Done()
regWait.Wait()
Expand All @@ -86,26 +92,30 @@ func (agent *Agent) Run(trk SensorTracker, reg sensor.Registry) {

// Start worker funcs for sensors.
wg.Add(1)

go func() {
defer wg.Done()
runWorkers(runnerCtx, trk, reg)
}()
// Start any scripts.
wg.Add(1)

go func() {
defer wg.Done()
scriptPath := filepath.Join(xdg.ConfigHome, agent.AppID(), "scripts")
runScripts(runnerCtx, scriptPath, trk, reg)
}()
// Start the mqtt client
wg.Add(1)

go func() {
defer wg.Done()
runMQTTWorker(runnerCtx)
}()
// Listen for notifications from Home Assistant.
if !agent.IsHeadless() {
wg.Add(1)

go func() {
defer wg.Done()
agent.runNotificationsWorker(runnerCtx)
Expand All @@ -126,6 +136,7 @@ func (agent *Agent) Register(trk SensorTracker) {
var wg sync.WaitGroup

wg.Add(1)

go func() {
defer wg.Done()
if err := agent.checkRegistration(trk); err != nil {
Expand All @@ -144,6 +155,7 @@ func (agent *Agent) Register(trk SensorTracker) {
func (agent *Agent) handleSignals() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)

go func() {
defer close(agent.done)
<-c
Expand Down Expand Up @@ -173,7 +185,7 @@ func (agent *Agent) Stop() {
func (agent *Agent) Reset() error {
ctx, _ := hass.NewContext()
if ctx == nil {
return errors.New("unable to create a context")
return ErrCtxFailed
}
runnerCtx := setupDeviceContext(ctx)
resetMQTTWorker(runnerCtx)
Expand Down
5 changes: 4 additions & 1 deletion internal/agent/device_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,16 @@ func newDevice(_ context.Context) *linux.Device {
// sensorWorkers initialises the list of workers for sensors and returns those
// that are supported on this device.
func sensorWorkers() []Worker {
var activeWorkers []Worker
activeWorkers := make([]Worker, 0, len(workers))

for _, w := range workers {
worker, err := w()
if err != nil {
log.Warn().Err(err).Msg("Could not activate a worker.")

continue
}

activeWorkers = append(activeWorkers, worker)
}
return activeWorkers
Expand Down
5 changes: 5 additions & 0 deletions internal/agent/runners.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func runWorkers(ctx context.Context, trk SensorTracker, reg sensor.Registry) {
cancelFuncs := make([]context.CancelFunc, 0, len(workers))

log.Debug().Msg("Starting worker funcs.")

for worker := range len(workers) {
workerCtx, cancelFunc := context.WithCancel(ctx)
cancelFuncs = append(cancelFuncs, cancelFunc)
Expand All @@ -67,6 +68,7 @@ func runWorkers(ctx context.Context, trk SensorTracker, reg sensor.Registry) {
sensorUpdates := sensor.MergeSensorCh(ctx, outCh...)
go func() {
log.Debug().Msg("Listening for sensor updates.")

for update := range sensorUpdates {
go func(update sensor.Details) {
if err := trk.UpdateSensor(ctx, reg, update); err != nil {
Expand All @@ -85,6 +87,7 @@ func runWorkers(ctx context.Context, trk SensorTracker, reg sensor.Registry) {

go func() {
<-ctx.Done()

for _, c := range cancelFuncs {
c()
}
Expand Down Expand Up @@ -129,6 +132,7 @@ func runScripts(ctx context.Context, path string, trk SensorTracker, reg sensor.
}
log.Debug().Msg("Starting cron scheduler for script sensors.")
scheduler.Start()

go func() {
for scriptUpdates := range sensor.MergeSensorCh(ctx, outCh...) {
go func(update sensor.Details) {
Expand Down Expand Up @@ -162,6 +166,7 @@ func (agent *Agent) runNotificationsWorker(ctx context.Context) {
var wg sync.WaitGroup

wg.Add(1)

go func() {
defer wg.Done()

Expand Down
1 change: 1 addition & 0 deletions internal/agent/ui/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

//nolint:lll
package ui

const (
Expand Down

0 comments on commit baa89c0

Please sign in to comment.