Skip to content

Commit

Permalink
refactor(agent,device): merge code into agent to simplify logic
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Oct 1, 2023
1 parent 657d853 commit e616b63
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 97 deletions.
40 changes: 27 additions & 13 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ import (
"sync"
"syscall"

deviceConfig "github.com/joshuar/go-hass-agent/internal/config"
"github.com/joshuar/go-hass-agent/internal/device"
"github.com/joshuar/go-hass-agent/internal/hass/api"

"github.com/joshuar/go-hass-agent/internal/agent/config"
"github.com/joshuar/go-hass-agent/internal/agent/ui"
"github.com/joshuar/go-hass-agent/internal/device"
"github.com/joshuar/go-hass-agent/internal/tracker"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -48,14 +47,6 @@ type Agent struct {
headless bool
}

//go:generate moq -out mock_AgentUI_test.go . AgentUI
type AgentUI interface {
DisplayNotification(string, string)
DisplayTrayIcon(context.Context, ui.Agent)
DisplayRegistrationWindow(context.Context, ui.Agent, chan struct{})
Run()
}

// AgentOptions holds options taken from the command-line that was used to
// invoke go-hass-agent that are relevant for agent functionality.
type AgentOptions struct {
Expand Down Expand Up @@ -114,9 +105,7 @@ func Run(options AgentOptions) {
wg.Add(1)
go func() {
defer wg.Done()
sensorWorkers := deviceConfig.SensorWorkers()
sensorWorkers = append(sensorWorkers, device.ExternalIPUpdater)
device.StartWorkers(ctx, agent.sensors, sensorWorkers...)
agent.startWorkers(ctx)
}()
wg.Add(1)
go func() {
Expand Down Expand Up @@ -281,3 +270,28 @@ func (agent *Agent) SensorList() []string {
func (agent *Agent) SensorValue(id string) (tracker.Sensor, error) {
return agent.sensors.Get(id)
}

// StartWorkers will call all the sensor worker functions that have been defined
// for this device.
func (agent *Agent) startWorkers(ctx context.Context) {
wokerFuncs := sensorWorkers()
wokerFuncs = append(wokerFuncs, device.ExternalIPUpdater)

workerCh := make(chan func(context.Context, device.SensorTracker), len(wokerFuncs))

for i := 0; i < len(workerCh); i++ {
workerCh <- wokerFuncs[i]
}

var wg sync.WaitGroup
for _, workerFunc := range wokerFuncs {
wg.Add(1)
go func(workerFunc func(context.Context, device.SensorTracker)) {
defer wg.Done()
workerFunc(ctx, agent.sensors)
}(workerFunc)
}

close(workerCh)
wg.Wait()
}
8 changes: 0 additions & 8 deletions internal/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ const (
webHookPath = "/api/webhook/"
)

//go:generate moq -out mock_agentConfig_test.go . AgentConfig
type AgentConfig interface {
Get(string, interface{}) error
Set(string, interface{}) error
Delete(string) error
StoragePath(string) (string, error)
}

// ValidateConfig takes an agentConfig and ensures that it meets the minimum
// requirements for the agent to function correctly
func ValidateConfig(c AgentConfig) error {
Expand Down
24 changes: 23 additions & 1 deletion internal/agent/device_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,31 @@ package agent
import (
"context"

"github.com/joshuar/go-hass-agent/internal/device"
"github.com/joshuar/go-hass-agent/internal/linux"
)

func (agent *Agent) setupDevice(ctx context.Context) *linux.LinuxDevice {
func newDevice(ctx context.Context) *linux.LinuxDevice {
return linux.NewDevice(ctx, name, version)
}

// sensorWorkers returns a list of functions to start to enable sensor tracking.
func sensorWorkers() []func(context.Context, device.SensorTracker) {
var workers []func(context.Context, device.SensorTracker)
workers = append(workers,
linux.LocationUpdater,
linux.BatteryUpdater,
linux.AppUpdater,
linux.NetworkConnectionsUpdater,
linux.NetworkStatsUpdater,
linux.PowerUpater,
linux.ProblemsUpdater,
linux.MemoryUpdater,
linux.LoadAvgUpdater,
linux.DiskUsageUpdater,
linux.TimeUpdater,
linux.ScreenLockUpdater,
linux.UsersUpdater,
linux.Versions)
return workers
}
41 changes: 41 additions & 0 deletions internal/agent/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2023 Joshua Rich <[email protected]>
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

package agent

import (
"context"

"github.com/joshuar/go-hass-agent/internal/agent/ui"
)

// AgentConfig represents the methods that the agent uses to interact with
// its config. It is effectively a CRUD interface to wherever the configuration
// is stored.
//
//go:generate moq -out mockAgentConfig_test.go . AgentConfig
type AgentConfig interface {
Get(string, interface{}) error
Set(string, interface{}) error
Delete(string) error
StoragePath(string) (string, error)
}

// AgentUI are the methods required for the agent to display its windows, tray
// and notifications
//
//go:generate moq -out mockAgentUI_test.go . AgentUI
type AgentUI interface {
DisplayNotification(string, string)
DisplayTrayIcon(context.Context, ui.Agent)
DisplayRegistrationWindow(context.Context, ui.Agent, chan struct{})
Run()
}

//go:generate moq -out mockDevice.go . Device
type Device interface {
DeviceName() string
DeviceID() string
}
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion internal/agent/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (agent *Agent) registrationProcess(ctx context.Context, server, token strin
}
}

device := agent.setupDevice(ctx)
device := newDevice(ctx)
if !headless {
userInputDone := make(chan struct{})
agent.ui.DisplayRegistrationWindow(ctx, agent, userInputDone)
Expand Down
33 changes: 0 additions & 33 deletions internal/config/config_linux.go

This file was deleted.

41 changes: 0 additions & 41 deletions internal/device/device.go

This file was deleted.

16 changes: 16 additions & 0 deletions internal/device/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) 2023 Joshua Rich <[email protected]>
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

package device

import "context"

// SensorTracker is the interface through which a device can update its sensors.
type SensorTracker interface {
// UpdateSensors will take any number of sensor updates and pass them on to
// the sensor tracker, which will handle updating its internal database and
// Home Assistant.
UpdateSensors(context.Context, ...interface{}) error
}

0 comments on commit e616b63

Please sign in to comment.