add total power factor

This commit is contained in:
Garionion 2022-08-01 11:58:23 +02:00
parent 3fdf996a93
commit cbfaa2f2da
2 changed files with 46 additions and 21 deletions

View File

@ -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))
}

View File

@ -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)