Skip to content

Commit

Permalink
refactor(linux): ♻️ improve readability and reduce lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Jun 8, 2024
1 parent baa89c0 commit 79763b9
Show file tree
Hide file tree
Showing 29 changed files with 822 additions and 404 deletions.
15 changes: 10 additions & 5 deletions internal/linux/apps/activeApps.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func (a *activeAppSensor) app() string {
if app, ok := a.State().(string); ok {
return app
}

return ""
}

Expand All @@ -28,17 +29,21 @@ func (a *activeAppSensor) update(l map[string]dbus.Variant) sensor.Details {
if appState, ok := v.Value().(uint32); ok {
if appState == 2 && a.app() != app {
a.Value = app

return a
}
}
}

return nil
}

//nolint:exhaustruct
func newActiveAppSensor() *activeAppSensor {
s := &activeAppSensor{}
s.SensorSrc = linux.DataSrcDbus
s.SensorTypeValue = linux.SensorAppActive
s.IconString = "mdi:application"
return s
newSensor := &activeAppSensor{}
newSensor.SensorSrc = linux.DataSrcDbus
newSensor.SensorTypeValue = linux.SensorAppActive
newSensor.IconString = "mdi:application"

return newSensor
}
16 changes: 12 additions & 4 deletions internal/linux/apps/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package apps

import (
"context"
"errors"
"fmt"

"github.com/godbus/dbus/v5"
Expand All @@ -32,6 +31,7 @@ type worker struct {
portalDest string
}

//nolint:exhaustruct
func (w *worker) Setup(_ context.Context) *dbusx.Watch {
return &dbusx.Watch{
Bus: dbusx.SessionBus,
Expand All @@ -48,8 +48,10 @@ func (w *worker) Watch(ctx context.Context, triggerCh chan dbusx.Trigger) chan s
appSensors, err := w.Sensors(ctx)
if err != nil {
log.Warn().Err(err).Msg("Failed to update app sensors.")

return
}

for _, s := range appSensors {
sensorCh <- s
}
Expand All @@ -58,10 +60,12 @@ func (w *worker) Watch(ctx context.Context, triggerCh chan dbusx.Trigger) chan s
// Watch for active app changes.
go func() {
defer close(sensorCh)

for {
select {
case <-ctx.Done():
log.Debug().Msg("Stopped app sensor.")

return
case <-triggerCh:
sendSensors(ctx, sensorCh)
Expand All @@ -79,26 +83,30 @@ func (w *worker) Watch(ctx context.Context, triggerCh chan dbusx.Trigger) chan s

func (w *worker) Sensors(ctx context.Context) ([]sensor.Details, error) {
var sensors []sensor.Details

appList, err := dbusx.GetData[map[string]dbus.Variant](ctx, dbusx.SessionBus, appStateDBusPath, w.portalDest, appStateDBusMethod)
if err != nil {
return nil, fmt.Errorf("could not retrieve app list from D-Bus: %w", err)
}

if appList != nil {
if s := w.activeApp.update(appList); s != nil {
sensors = append(sensors, s)
}

if s := w.runningApps.update(appList); s != nil {
sensors = append(sensors, s)
}
}

return sensors, nil
}

func NewAppWorker() (*linux.SensorWorker, error) {
// If we cannot find a portal interface, we cannot monitor the active app.
portalDest := linux.FindPortal()
if portalDest == "" {
return nil, errors.New("unable to monitor for active applications: no portal present")
portalDest, err := linux.FindPortal()
if err != nil {
return nil, fmt.Errorf("unable to monitor for active applications: %w", err)
}

return &linux.SensorWorker{
Expand Down
36 changes: 24 additions & 12 deletions internal/linux/apps/runningApps.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,54 +23,66 @@ type runningAppsSensor struct {
}

type runningAppsSensorAttributes struct {
DataSource string `json:"Data Source"`
RunningApps []string `json:"Running Apps"`
DataSource string `json:"data_source"`
RunningApps []string `json:"running_apps"`
}

//nolint:exhaustruct
func (r *runningAppsSensor) Attributes() any {
attrs := &runningAppsSensorAttributes{}

r.mu.Lock()
for appName, state := range r.appList {
if dbusx.VariantToValue[uint32](state) > 0 {
attrs.RunningApps = append(attrs.RunningApps, appName)
}
}
r.mu.Unlock()

attrs.DataSource = linux.DataSrcDbus

return attrs
}

func (r *runningAppsSensor) count() int {
if count, ok := r.State().(int); ok {
return count
}

return -1
}

func (r *runningAppsSensor) update(l map[string]dbus.Variant) sensor.Details {
func (r *runningAppsSensor) update(apps map[string]dbus.Variant) sensor.Details {
var count int

r.mu.Lock()
defer r.mu.Unlock()
r.appList = l
for _, raw := range l {
if appState, ok := raw.Value().(uint32); ok {
r.appList = apps

for _, appState := range apps {
if appState, ok := appState.Value().(uint32); ok {
if appState > 0 {
count++
}
}
}

if r.count() != count {
r.Value = count

return r
}

return nil
}

//nolint:exhaustruct
func newRunningAppsSensor() *runningAppsSensor {
s := &runningAppsSensor{}
s.SensorTypeValue = linux.SensorAppRunning
s.IconString = "mdi:apps"
s.UnitsString = "apps"
s.StateClassValue = types.StateClassMeasurement
return s
newSensor := &runningAppsSensor{}
newSensor.SensorTypeValue = linux.SensorAppRunning
newSensor.IconString = "mdi:apps"
newSensor.UnitsString = "apps"
newSensor.StateClassValue = types.StateClassMeasurement

return newSensor
}
Loading

0 comments on commit 79763b9

Please sign in to comment.