2022-11-19 23:47:19 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-12-03 23:46:36 +00:00
|
|
|
"errors"
|
|
|
|
|
2022-11-19 23:47:19 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/Luzifer/mercedes-byocar-exporter/internal/mercedes"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getCronFunc(mc mercedes.Client) func() {
|
|
|
|
return func() {
|
|
|
|
for i := range cfg.VehicleID {
|
2022-11-20 12:26:31 +00:00
|
|
|
runFetcher(mc, cfg.VehicleID[i])
|
2022-11-19 23:47:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runFetcher(mc mercedes.Client, vehicleID string) {
|
|
|
|
logger := logrus.WithField("vehicle_id", vehicleID)
|
|
|
|
logger.Info("fetching data")
|
|
|
|
|
|
|
|
s1, err := mc.GetPayAsYouDriveInsurance(cfg.VehicleID[0])
|
2022-12-03 23:46:36 +00:00
|
|
|
handleMetricsEntries(logger, "pay-as-you-go", err, func() { enabledExporters.SetPayAsYouGo(vehicleID, s1) })
|
2022-11-19 23:47:19 +00:00
|
|
|
|
|
|
|
s2, err := mc.GetFuelStatus(cfg.VehicleID[0])
|
2022-12-03 23:46:36 +00:00
|
|
|
handleMetricsEntries(logger, "fuel-status", err, func() { enabledExporters.SetFuelStatus(vehicleID, s2) })
|
2022-11-19 23:47:19 +00:00
|
|
|
|
|
|
|
s3, err := mc.GetVehicleStatus(cfg.VehicleID[0])
|
2022-12-03 23:46:36 +00:00
|
|
|
handleMetricsEntries(logger, "vehicle-status", err, func() { enabledExporters.SetVehicleStatus(vehicleID, s3) })
|
2022-11-19 23:47:19 +00:00
|
|
|
|
|
|
|
s4, err := mc.GetLockStatus(cfg.VehicleID[0])
|
2022-12-03 23:46:36 +00:00
|
|
|
handleMetricsEntries(logger, "lock-status", err, func() { enabledExporters.SetLockStatus(vehicleID, s4) })
|
2022-11-19 23:47:19 +00:00
|
|
|
|
2022-12-03 23:22:53 +00:00
|
|
|
s5, err := mc.GetElectricStatus(cfg.VehicleID[0])
|
2022-12-03 23:46:36 +00:00
|
|
|
handleMetricsEntries(logger, "electric-status", err, func() { enabledExporters.SetElectricStatus(vehicleID, s5) })
|
|
|
|
|
|
|
|
logger.Info("data updated")
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleMetricsEntries(logger *logrus.Entry, dataType string, err error, submit func()) {
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
submit()
|
|
|
|
|
|
|
|
case errors.Is(err, mercedes.ErrNoDataAvailable):
|
|
|
|
logger.Warnf("%s data is not available", dataType)
|
|
|
|
return
|
|
|
|
|
|
|
|
default:
|
|
|
|
logger.WithError(err).Errorf("fetching %s data", dataType)
|
2022-12-03 23:22:53 +00:00
|
|
|
return
|
|
|
|
|
2022-12-03 23:46:36 +00:00
|
|
|
}
|
2022-11-19 23:47:19 +00:00
|
|
|
}
|