Skip to content

Commit

Permalink
refactor(linux): stringify battery sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Dec 8, 2023
1 parent 6fd0b7d commit 5660d67
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 71 deletions.
18 changes: 18 additions & 0 deletions internal/linux/batteryLevels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2023 Joshua Rich <[email protected]>
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

package linux

//go:generate stringer -type=batteryLevel -output batteryLevelsStrings.go -linecomment
const (
batteryLevelNone batteryLevel = iota + 1 // None
batteryLevelLow // Low
batteryLevelCrit // Critical
batteryLevelNorm // Normal
batteryLevelHigh // High
batteryLevelFull // Full
)

type batteryLevel uint32
29 changes: 29 additions & 0 deletions internal/linux/batteryLevelsStrings.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 11 additions & 71 deletions internal/linux/batterySensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (b *upowerBattery) marshalBatteryStateUpdate(ctx context.Context, t sensorT
Type string `json:"Battery Type"`
DataSource string `json:"Data Source"`
}{
Type: battTypeAsString(dbushelpers.VariantToValue[uint32](b.getValue(battType))),
Type: batteryType(dbushelpers.VariantToValue[uint32](b.getValue(battType))).String(),
DataSource: srcDbus,
}
}
Expand Down Expand Up @@ -209,15 +209,20 @@ func (state *upowerBatteryState) StateClass() sensor.SensorStateClass {
}

func (state *upowerBatteryState) State() interface{} {
switch state.prop.name {
propType := state.prop.name
rawValue := state.prop.value
if rawValue == nil {
return sensor.StateUnknown
}
switch propType {
case battVoltage, battTemp, battEnergy, battEnergyRate, battPercentage:
return state.prop.value.(float64)
return rawValue.(float64)
case battState:
return battStateAsString(state.prop.value.(uint32))
return rawValue.(battChargeState).String()
case battLevel:
return battLevelAsString(state.prop.value.(uint32))
return rawValue.(batteryLevel).String()
default:
return state.prop.value.(string)
return rawValue.(string)
}
}

Expand All @@ -242,71 +247,6 @@ func (state *upowerBatteryState) Attributes() interface{} {
return state.attributes
}

func battStateAsString(state uint32) string {
switch state {
case 1:
return "Charging"
case 2:
return "Discharging"
case 3:
return "Empty"
case 4:
return "Fully Charged"
case 5:
return "Pending Charge"
case 6:
return "Pending Discharge"
default:
return sensor.StateUnknown
}
}

func battTypeAsString(t uint32) string {
switch t {
case 0:
return sensor.StateUnknown
case 1:
return "Line Power"
case 2:
return "Battery"
case 3:
return "Ups"
case 4:
return "Monitor"
case 5:
return "Mouse"
case 6:
return "Keyboard"
case 7:
return "Pda"
case 8:
return "Phone"
default:
return sensor.StateUnknown
}
}

func battLevelAsString(l uint32) string {
switch l {
case 0:
return sensor.StateUnknown
case 1:
return "None"
case 3:
return "Low"
case 4:
return "Critical"
case 6:
return "Normal"
case 7:
return "High"
case 8:
return "Full"
default:
return sensor.StateUnknown
}
}

func BatteryUpdater(ctx context.Context) chan tracker.Sensor {
sensorCh := make(chan tracker.Sensor)
batteryList := dbushelpers.NewBusRequest(ctx, dbushelpers.SystemBus).
Expand Down
18 changes: 18 additions & 0 deletions internal/linux/batteryStates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2023 Joshua Rich <[email protected]>
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

package linux

//go:generate stringer -type=battChargeState -output batteryStatesStrings.go -linecomment
const (
stateCharging battChargeState = iota + 1 // Charging
stateDischarging // Discharging
stateEmpty // Empty
stateFullyCharged // Fully Charged
statePendingCharge // Pending Charge
statePendingDischarge // Pending Discharge
)

type battChargeState uint32
29 changes: 29 additions & 0 deletions internal/linux/batteryStatesStrings.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

package linux

//go:generate stringer -type=batteryType -output batteryTypeStrings.go -linecomment
const (
batteryTypeLinePower batteryType = iota + 1 // Line Power
batteryTypeBattery // Battery
batteryTypeUps // UPS
batteryTypeMonitor // Monitor
batteryTypeMouse // Mouse
batteryTypeKeyboard // Keyboard
batteryTypePda // Pda
batteryTypePhone // Phone
)

type batteryType uint32
31 changes: 31 additions & 0 deletions internal/linux/batteryTypeStrings.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5660d67

Please sign in to comment.