mirror of
https://github.com/tess1o/go-ecoflow-exporter.git
synced 2025-10-23 03:51:55 +00:00
Fixed issue when ECOFLOW_DEVICES is populated (for MQTT) and ECOFLOW_DEVICES_PRETTY_NAMES is empty (#16)
This commit is contained in:
@@ -6,10 +6,13 @@ ECOFLOW_ACCESS_KEY=YOUR_ECOFLOW_ACCESS_KEY
|
||||
ECOFLOW_SECRET_KEY=YOUR_ECOFLOW_SECRET_KEY
|
||||
|
||||
### If EXPORTER_TYPE=mqtt, then the following parameters are mandatory:
|
||||
ECOFLOW_DEVICES=Serial_Number_1,Serial_Number_2,Serial_Number_3
|
||||
ECOFLOW_EMAIL=
|
||||
ECOFLOW_PASSWORD=
|
||||
|
||||
ECOFLOW_DEVICES=Serial_Number_1,Serial_Number_2,Serial_Number_3
|
||||
#OR
|
||||
ECOFLOW_DEVICES_PRETTY_NAMES='{"SN1":"My Cool Ecoflow device"}'
|
||||
|
||||
# if device is not sending parameters up to XXX seconds (and we're connected to the MQTT broker),
|
||||
# then consider that device is offline
|
||||
MQTT_DEVICE_OFFLINE_THRESHOLD_SECONDS=30
|
||||
|
@@ -14,6 +14,7 @@ services:
|
||||
ECOFLOW_EMAIL: ${ECOFLOW_EMAIL}
|
||||
ECOFLOW_PASSWORD: ${ECOFLOW_PASSWORD}
|
||||
ECOFLOW_DEVICES: ${ECOFLOW_DEVICES}
|
||||
ECOFLOW_DEVICES_PRETTY_NAMES: ${ECOFLOW_DEVICES_PRETTY_NAMES}
|
||||
MQTT_DEVICE_OFFLINE_THRESHOLD_SECONDS: ${MQTT_DEVICE_OFFLINE_THRESHOLD_SECONDS}
|
||||
PROMETHEUS_ENABLED: ${PROMETHEUS_ENABLED}
|
||||
METRIC_PREFIX: ${METRIC_PREFIX}
|
||||
|
@@ -12,6 +12,7 @@ services:
|
||||
ECOFLOW_EMAIL: ${ECOFLOW_EMAIL}
|
||||
ECOFLOW_PASSWORD: ${ECOFLOW_PASSWORD}
|
||||
ECOFLOW_DEVICES: ${ECOFLOW_DEVICES}
|
||||
ECOFLOW_DEVICES_PRETTY_NAMES: ${ECOFLOW_DEVICES_PRETTY_NAMES}
|
||||
MQTT_DEVICE_OFFLINE_THRESHOLD_SECONDS: ${MQTT_DEVICE_OFFLINE_THRESHOLD_SECONDS}
|
||||
PROMETHEUS_ENABLED: ${PROMETHEUS_ENABLED}
|
||||
METRIC_PREFIX: ${METRIC_PREFIX}
|
||||
|
25
main.go
25
main.go
@@ -45,7 +45,8 @@ const (
|
||||
|
||||
var (
|
||||
accessTokenMandatoryErr = errors.New("ECOFLOW_ACCESS_KEY and ECOFLOW_SECRET_KEY are mandatory")
|
||||
emailPasswordMandatoryErr = errors.New("ECOFLOW_EMAIL and ECOFLOW_PASSWORD and ECOFLOW_DEVICES are mandatory")
|
||||
emailPasswordMandatoryErr = errors.New("ECOFLOW_EMAIL and ECOFLOW_PASSWORD are mandatory")
|
||||
devicesMandatoryErr = errors.New("either ECOFLOW_DEVICES_PRETTY_NAMES or ECOFLOW_DEVICES must be provided")
|
||||
)
|
||||
|
||||
type Shutdownable interface {
|
||||
@@ -111,7 +112,7 @@ func createAndStartMqttExporter(handlers []MetricHandler) error {
|
||||
return err
|
||||
}
|
||||
if len(devicesList) == 0 {
|
||||
return errors.New("either ECOFLOW_DEVICES_PRETTY_NAMES or ECOFLOW_DEVICES must be provided")
|
||||
return devicesMandatoryErr
|
||||
}
|
||||
|
||||
exporter, err := NewMqttMetricsExporter(email, password, devicesList, time.Second*time.Duration(offlineThreshold), handlers...)
|
||||
@@ -145,23 +146,27 @@ func createAndStartRestExporter(handlers []MetricHandler) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// get the devices mapping, where the key is a device serial number and the value is
|
||||
// either the pretty name (specified in ECOFLOW_DEVICES_PRETTY_NAMES) or SN itself taken from ECOFLOW_DEVICES
|
||||
func getDeviceMapping() (map[string]string, error) {
|
||||
var mapping map[string]string
|
||||
names := os.Getenv("ECOFLOW_DEVICES_PRETTY_NAMES")
|
||||
if len(names) == 0 {
|
||||
return mapping, nil
|
||||
}
|
||||
err := json.Unmarshal([]byte(names), &mapping)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to parse device mapping, make sure it has format {\"R33XXXXXXXXX\":\"My Delta 2\", \"R33YYYYY\":\"Delta Pro backup\"}. Original error: %w\n", err)
|
||||
var mapping = make(map[string]string)
|
||||
prettyNames := os.Getenv("ECOFLOW_DEVICES_PRETTY_NAMES")
|
||||
// we have pretty names specified, so will add them to the mapping
|
||||
if len(prettyNames) != 0 {
|
||||
err := json.Unmarshal([]byte(prettyNames), &mapping)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to parse device mapping, make sure it has format {\"R33XXXXXXXXX\":\"My Delta 2\", \"R33YYYYY\":\"Delta Pro backup\"}. Original error: %w\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
devices := os.Getenv("ECOFLOW_DEVICES")
|
||||
|
||||
//no devices are specified, will return either empty map or whatever was specified in ECOFLOW_DEVICES_PRETTY_NAMES
|
||||
if len(devices) == 0 {
|
||||
return mapping, nil
|
||||
}
|
||||
|
||||
// the ECOFLOW_DEVICES is not empty, so will add them to the mapping
|
||||
devicesList := strings.Split(devices, ",")
|
||||
|
||||
for _, device := range devicesList {
|
||||
|
Reference in New Issue
Block a user