Skip to content

Commit

Permalink
feat(agent,tracker): move device worker init from tracker to agent
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Sep 25, 2023
1 parent ba309fa commit 05b3b1b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 31 deletions.
31 changes: 16 additions & 15 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ import (
"sync"
"syscall"

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

"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 @@ -83,33 +86,31 @@ func Run(options AgentOptions) {
go agent.registrationProcess(agentCtx, "", "", options.Register, options.Headless, registrationDone)

var workerWg sync.WaitGroup
trackerCh := make(chan *tracker.SensorTracker)
go func() {
<-registrationDone
if err := UpgradeConfig(agent.config); err != nil {
log.Warn().Err(err).Msg("Could not upgrade config.")
var err error
if err = UpgradeConfig(agent.config); err != nil {
log.Warn().Err(err).Msg("Could not start.")
}
if err = ValidateConfig(agent.config); err != nil {
log.Fatal().Err(err).Msg("Could not start.")
}
if err := ValidateConfig(agent.config); err != nil {
log.Fatal().Err(err).Msg("Invalid config. Cannot start.")
if agent.sensors, err = tracker.NewSensorTracker(agent); err != nil {
log.Fatal().Err(err).Msg("Could not start.")
}
// Start all the sensor workers as appropriate

sensorWorkers := deviceConfig.SensorWorkers()
sensorWorkers = append(sensorWorkers, device.ExternalIPUpdater)

workerWg.Add(1)
go func() {
agent.sensors = <-trackerCh
if agent.sensors == nil {
log.Fatal().Msg("Could not start sensor tracker.")
}
device.StartWorkers(agentCtx, sensorWorkers, agent.sensors)
}()
workerWg.Add(1)
go func() {
defer workerWg.Done()
agent.runNotificationsWorker(agentCtx, options)
}()
workerWg.Add(1)
go func() {
defer workerWg.Done()
tracker.RunSensorTracker(agentCtx, agent, trackerCh)
}()
}()
agent.handleSignals(cancelFunc)
agent.handleShutdown(agentCtx)
Expand Down
19 changes: 3 additions & 16 deletions internal/tracker/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"sort"
"sync"

"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"
registry "github.com/joshuar/go-hass-agent/internal/tracker/registry/jsonFiles"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -45,33 +43,22 @@ type SensorTracker struct {
mu sync.RWMutex
}

func RunSensorTracker(ctx context.Context, agentConfig agent, trackerCh chan *SensorTracker) {
func NewSensorTracker(agentConfig agent) (*SensorTracker, error) {
registryPath, err := agentConfig.StoragePath(registryStorageID)
if err != nil {
log.Warn().Err(err).
Msg("Path for sensor registry is not valid, using in-memory registry.")
}
db, err := registry.NewJsonFilesRegistry(registryPath)
if err != nil {
log.Error().Err(err).Msg("Unable to create a sensor tracker.")
close(trackerCh)
return nil, errors.New("unable to create a sensor tracker")
}
sensorTracker := &SensorTracker{
registry: db,
sensor: make(map[string]Sensor),
agentConfig: agentConfig,
}
trackerCh <- sensorTracker
var wg sync.WaitGroup
sensorWorkers := config.SensorWorkers()
sensorWorkers = append(sensorWorkers, device.ExternalIPUpdater)

wg.Add(1)
go func() {
device.StartWorkers(ctx, sensorWorkers, sensorTracker)
}()
wg.Wait()
close(trackerCh)
return sensorTracker, nil
}

// Add creates a new sensor in the tracker based on a recieved state update.
Expand Down

0 comments on commit 05b3b1b

Please sign in to comment.