From cbfaa2f2da007d6bc7d6bb7c993d069cd4acab85 Mon Sep 17 00:00:00 2001 From: Garionion Date: Mon, 1 Aug 2022 11:58:23 +0200 Subject: [PATCH] add total power factor --- exporter/exporter.go | 20 ++++++++++++++----- scraper/scraper.go | 47 +++++++++++++++++++++++++++++--------------- 2 files changed, 46 insertions(+), 21 deletions(-) diff --git a/exporter/exporter.go b/exporter/exporter.go index 326abc0..006bd54 100644 --- a/exporter/exporter.go +++ b/exporter/exporter.go @@ -6,11 +6,12 @@ import ( ) type Exporter struct { - Voltage *prometheus.GaugeVec - Current *prometheus.GaugeVec - ApparantPower *prometheus.GaugeVec - ActivePower *prometheus.GaugeVec - Frequency *prometheus.GaugeVec + Voltage *prometheus.GaugeVec + Current *prometheus.GaugeVec + ApparantPower *prometheus.GaugeVec + ActivePower *prometheus.GaugeVec + Frequency *prometheus.GaugeVec + TotalPowerFactor *prometheus.GaugeVec } func New() Exporter { @@ -51,11 +52,19 @@ func New() Exporter { Help: "Frequency in Hz", }, []string{"device"}) + exporter.TotalPowerFactor = prometheus.NewGaugeVec(prometheus.GaugeOpts{ + Namespace: "siemens", + Subsystem: "pac3100", + Name: "total_power_factor", + Help: "Total Power Factor", + }, []string{"device"}) + prometheus.MustRegister(exporter.Voltage) prometheus.MustRegister(exporter.Current) prometheus.MustRegister(exporter.ApparantPower) prometheus.MustRegister(exporter.ActivePower) prometheus.MustRegister(exporter.Frequency) + prometheus.MustRegister(exporter.TotalPowerFactor) return exporter } @@ -82,4 +91,5 @@ func (e Exporter) SetMeasurements(measurement scraper.Measurements, deviceName s e.ActivePower.WithLabelValues(deviceName, "C").Set(float64(measurement.ActivePowerC)) e.Frequency.WithLabelValues(deviceName).Set(float64(measurement.Frequency)) + e.TotalPowerFactor.WithLabelValues(deviceName).Set(float64(measurement.TotalPowerFactor)) } diff --git a/scraper/scraper.go b/scraper/scraper.go index 2cfc11a..924f77a 100644 --- a/scraper/scraper.go +++ b/scraper/scraper.go @@ -15,22 +15,23 @@ type Scraper struct { } type Measurements struct { - VoltageAN float32 - VoltageBN float32 - VoltageCN float32 - VoltageAB float32 - VoltageBC float32 - VoltageCA float32 - CurrentA float32 - CurrentB float32 - CurrentC float32 - ApparantPowerA float32 - ApparantPowerB float32 - ApparantPowerC float32 - ActivePowerA float32 - ActivePowerB float32 - ActivePowerC float32 - Frequency float32 + VoltageAN float32 + VoltageBN float32 + VoltageCN float32 + VoltageAB float32 + VoltageBC float32 + VoltageCA float32 + CurrentA float32 + CurrentB float32 + CurrentC float32 + ApparantPowerA float32 + ApparantPowerB float32 + ApparantPowerC float32 + ActivePowerA float32 + ActivePowerB float32 + ActivePowerC float32 + Frequency float32 + TotalPowerFactor float32 } func New(address string, logger zerolog.Logger) (*Scraper, error) { @@ -139,6 +140,11 @@ func (s *Scraper) GetMeasurments() (Measurements, error) { s.logger.Err(err) } + measurement.TotalPowerFactor, err = s.GetTotalPowerFactor() + if err != nil { + s.logger.Err(err) + } + return measurement, nil } @@ -286,6 +292,15 @@ func (s *Scraper) GetFrequency() (float32, error) { return Float32fromBytes(results), nil } +// no Unit +func (s *Scraper) GetTotalPowerFactor() (float32, error) { + results, err := s.modbus.ReadInputRegistersBytes(126, 53, 2) + if err != nil { + return 0, fmt.Errorf("reading Total Power Factor: %w", err) + } + return Float32fromBytes(results), nil +} + func Float32fromBytes(bytes []byte) float32 { bits := binary.BigEndian.Uint32(bytes) float := math.Float32frombits(bits)