Skip to content

Commit

Permalink
Handle empty values everywhere
Browse files Browse the repository at this point in the history
Follow-up from e336fc1
  • Loading branch information
daenney committed Jun 30, 2022
1 parent c6a0453 commit 5959ad5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 29 deletions.
14 changes: 9 additions & 5 deletions collectors/battery.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package collectors
import (
"log"

"lib.hemtjan.st/feature"
"github.com/prometheus/client_golang/prometheus"

"lib.hemtjan.st/feature"
"lib.hemtjan.st/server"
"github.com/prometheus/client_golang/prometheus"
)

// BatteryCollector gets battery status from sensors
Expand Down Expand Up @@ -36,14 +36,18 @@ func (c *BatteryCollector) Describe(ch chan<- *prometheus.Desc) {
func (c *BatteryCollector) Collect(ch chan<- prometheus.Metric) {
devices := c.m.Devices()
for _, s := range devices {
if s.Feature(feature.BatteryLevel.String()).Exists() {
v, err := toFloat(s.Feature(feature.BatteryLevel.String()).Value())
if ft := s.Feature(feature.BatteryLevel.String()); ft.Exists() {
v := ft.Value()
if v == "" {
continue
}
vf, err := toFloat(v)
if err != nil {
log.Print(err.Error())
continue
}
ch <- prometheus.MustNewConstMetric(c.batteryLevel,
prometheus.GaugeValue, v, s.Info().Topic)
prometheus.GaugeValue, vf, s.Info().Topic)
}
}
}
14 changes: 9 additions & 5 deletions collectors/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package collectors
import (
"log"

"lib.hemtjan.st/feature"
"github.com/prometheus/client_golang/prometheus"

"lib.hemtjan.st/feature"
"lib.hemtjan.st/server"
"github.com/prometheus/client_golang/prometheus"
)

// ContactCollector gets contact state from sensors
Expand Down Expand Up @@ -36,14 +36,18 @@ func (c *ContactCollector) Describe(ch chan<- *prometheus.Desc) {
func (c *ContactCollector) Collect(ch chan<- prometheus.Metric) {
devices := c.m.Devices()
for _, s := range devices {
if s.Feature(feature.ContactSensorState.String()).Exists() {
v, err := toFloat(s.Feature(feature.ContactSensorState.String()).Value())
if ft := s.Feature(feature.ContactSensorState.String()); ft.Exists() {
v := ft.Value()
if v == "" {
continue
}
vf, err := toFloat(v)
if err != nil {
log.Print(err.Error())
continue
}
ch <- prometheus.MustNewConstMetric(c.contactState,
prometheus.GaugeValue, v, s.Info().Topic)
prometheus.GaugeValue, vf, s.Info().Topic)
}
}
}
14 changes: 9 additions & 5 deletions collectors/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package collectors
import (
"log"

"lib.hemtjan.st/feature"

"github.com/prometheus/client_golang/prometheus"

"lib.hemtjan.st/feature"
"lib.hemtjan.st/server"
)

Expand Down Expand Up @@ -36,14 +36,18 @@ func (c *FilterCollector) Describe(ch chan<- *prometheus.Desc) {
func (c *FilterCollector) Collect(ch chan<- prometheus.Metric) {
devices := c.m.Devices()
for _, s := range devices {
if s.Feature(feature.FilterChangeIndication.String()).Exists() {
v, err := toFloat(s.Feature(feature.FilterChangeIndication.String()).Value())
if ft := s.Feature(feature.FilterChangeIndication.String()); ft.Exists() {
v := ft.Value()
if v == "" {
continue
}
vf, err := toFloat(v)
if err != nil {
log.Print(err.Error())
continue
}
ch <- prometheus.MustNewConstMetric(c.filterReplacement,
prometheus.GaugeValue, v, s.Info().Topic)
prometheus.GaugeValue, vf, s.Info().Topic)
}
}
}
44 changes: 30 additions & 14 deletions collectors/power.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package collectors
import (
"log"

"lib.hemtjan.st/feature"
"github.com/prometheus/client_golang/prometheus"

"lib.hemtjan.st/feature"
"lib.hemtjan.st/server"
"github.com/prometheus/client_golang/prometheus"
)

// PowerCollector gets power data from sensors
Expand Down Expand Up @@ -57,41 +57,57 @@ func (c *PowerCollector) Describe(ch chan<- *prometheus.Desc) {
func (c *PowerCollector) Collect(ch chan<- prometheus.Metric) {
devices := c.m.Devices()
for _, s := range devices {
if s.Feature(feature.CurrentPower.String()).Exists() {
v, err := toFloat(s.Feature(feature.CurrentPower.String()).Value())
if ft := s.Feature(feature.CurrentPower.String()); ft.Exists() {
v := ft.Value()
if v == "" {
continue
}
vf, err := toFloat(v)
if err != nil {
log.Print(err.Error())
continue
}
ch <- prometheus.MustNewConstMetric(c.powerCurrent,
prometheus.GaugeValue, v, s.Info().Topic)
prometheus.GaugeValue, vf, s.Info().Topic)
}
if s.Feature(feature.EnergyUsed.String()).Exists() {
v, err := toFloat(s.Feature(feature.EnergyUsed.String()).Value())
if ft := s.Feature(feature.EnergyUsed.String()); ft.Exists() {
v := ft.Value()
if v == "" {
continue
}
vf, err := toFloat(v)
if err != nil {
log.Print(err.Error())
continue
}
ch <- prometheus.MustNewConstMetric(c.powerTotal,
prometheus.CounterValue, v, s.Info().Topic)
prometheus.CounterValue, vf, s.Info().Topic)
}
if s.Feature(feature.CurrentVoltage.String()).Exists() {
v, err := toFloat(s.Feature(feature.CurrentVoltage.String()).Value())
if ft := s.Feature(feature.CurrentVoltage.String()); ft.Exists() {
v := ft.Value()
if v == "" {
continue
}
vf, err := toFloat(v)
if err != nil {
log.Print(err.Error())
continue
}
ch <- prometheus.MustNewConstMetric(c.voltageCurrent,
prometheus.GaugeValue, v, s.Info().Topic)
prometheus.GaugeValue, vf, s.Info().Topic)
}
if s.Feature(feature.CurrentAmpere.String()).Exists() {
v, err := toFloat(s.Feature(feature.CurrentAmpere.String()).Value())
if ft := s.Feature(feature.CurrentAmpere.String()); ft.Exists() {
v := ft.Value()
if v == "" {
continue
}
vf, err := toFloat(v)
if err != nil {
log.Print(err.Error())
continue
}
ch <- prometheus.MustNewConstMetric(c.ampereCurrent,
prometheus.GaugeValue, v, s.Info().Topic)
prometheus.GaugeValue, vf, s.Info().Topic)
}
}
}

0 comments on commit 5959ad5

Please sign in to comment.