mirror of
https://github.com/tess1o/go-ecoflow-exporter.git
synced 2025-10-24 08:33:42 +00:00
* Added MQTT support that doesn't require Developer's access and secret keys * Upgraded go-ecoflow library
55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"github.com/tess1o/go-ecoflow"
|
|
"log/slog"
|
|
"time"
|
|
)
|
|
|
|
type MetricHandler interface {
|
|
Handle(ctx context.Context, device ecoflow.DeviceInfo, rawParameters map[string]interface{})
|
|
}
|
|
|
|
type MetricsExporter struct {
|
|
c *ecoflow.Client
|
|
interval time.Duration
|
|
handlers []MetricHandler
|
|
}
|
|
|
|
func NewMetricsExporter(c *ecoflow.Client, interval time.Duration, handlers ...MetricHandler) *MetricsExporter {
|
|
return &MetricsExporter{
|
|
c: c,
|
|
interval: interval,
|
|
handlers: handlers,
|
|
}
|
|
}
|
|
|
|
func (e *MetricsExporter) ExportMetrics() {
|
|
ticker := time.NewTicker(e.interval)
|
|
now := time.Now()
|
|
for _ = time.Now(); ; now = <-ticker.C {
|
|
ctx := context.Background()
|
|
slog.Info("Getting ecoflow devices", "time", now)
|
|
devices, err := e.c.GetDeviceList(ctx)
|
|
if err != nil {
|
|
slog.Error("Cannot get devices list", "error", err)
|
|
continue
|
|
}
|
|
|
|
for _, dev := range devices.Devices {
|
|
slog.Info("Getting ecoflow parameters", "sn", dev.SN)
|
|
rawParameters, pErr := e.c.GetDeviceAllParameters(ctx, dev.SN)
|
|
if pErr != nil {
|
|
slog.Error("Cannot get device quota", "SN", dev.SN, "error", pErr)
|
|
continue
|
|
}
|
|
|
|
for _, handler := range e.handlers {
|
|
hh := handler
|
|
go hh.Handle(ctx, dev, rawParameters)
|
|
}
|
|
}
|
|
}
|
|
}
|