support sensor name mapping
All checks were successful
Build and Publish / release (push) Successful in 4m6s
All checks were successful
Build and Publish / release (push) Successful in 4m6s
This commit is contained in:
@ -33,4 +33,14 @@ type WeatherStation struct {
|
||||
// Check weather.WeatherUpdateField for options
|
||||
KeepMetrics []string `yaml:"keepMetrics"`
|
||||
DropMetrics []string `yaml:"dropMetrics"`
|
||||
|
||||
// Relabels battery and sensor names
|
||||
// Temp+Humidity Sensors:
|
||||
// - TempHumiditySensor[1-8]
|
||||
// Batteries:
|
||||
// - IndoorSensor
|
||||
// - OutdoorSensor
|
||||
// - RainSensor
|
||||
// - CO2Sensor
|
||||
SensorMappings map[string]string `yaml:"sensorMappings"`
|
||||
}
|
||||
|
12
pkg/ambient/config/ws_map.go
Normal file
12
pkg/ambient/config/ws_map.go
Normal file
@ -0,0 +1,12 @@
|
||||
package config
|
||||
|
||||
// If the weather-station has a mapping, returns the new
|
||||
// name for the sensor
|
||||
func (ws *WeatherStation) MapSensor(sensor string) string {
|
||||
for name, replacement := range ws.SensorMappings {
|
||||
if name == sensor && replacement != "" {
|
||||
return replacement
|
||||
}
|
||||
}
|
||||
return sensor
|
||||
}
|
67
pkg/ambient/config/ws_map_test.go
Normal file
67
pkg/ambient/config/ws_map_test.go
Normal file
@ -0,0 +1,67 @@
|
||||
package config
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestWeatherStation_MapSensor(t *testing.T) {
|
||||
type fields struct {
|
||||
Name string
|
||||
Equipment string
|
||||
WundergroundID string
|
||||
WundergroundPassword string
|
||||
AWNPassKey string
|
||||
ProxyToAWN bool
|
||||
ProxyToWunderground bool
|
||||
KeepMetrics []string
|
||||
DropMetrics []string
|
||||
SensorMappings map[string]string
|
||||
}
|
||||
type args struct {
|
||||
sensor string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "Check sensor mapping",
|
||||
fields: fields{
|
||||
SensorMappings: map[string]string{
|
||||
"TempHumiditySensor1": "TestSensor",
|
||||
},
|
||||
},
|
||||
args: args{sensor: "TempHumiditySensor1"},
|
||||
want: "TestSensor",
|
||||
},
|
||||
{
|
||||
name: "Check sensor no mapping",
|
||||
fields: fields{
|
||||
SensorMappings: map[string]string{
|
||||
"TempHumiditySensor1": "TestSensor",
|
||||
},
|
||||
},
|
||||
args: args{sensor: "TempHumiditySensor2"},
|
||||
want: "TempHumiditySensor2",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ws := &WeatherStation{
|
||||
Name: tt.fields.Name,
|
||||
Equipment: tt.fields.Equipment,
|
||||
WundergroundID: tt.fields.WundergroundID,
|
||||
WundergroundPassword: tt.fields.WundergroundPassword,
|
||||
AWNPassKey: tt.fields.AWNPassKey,
|
||||
ProxyToAWN: tt.fields.ProxyToAWN,
|
||||
ProxyToWunderground: tt.fields.ProxyToWunderground,
|
||||
KeepMetrics: tt.fields.KeepMetrics,
|
||||
DropMetrics: tt.fields.DropMetrics,
|
||||
SensorMappings: tt.fields.SensorMappings,
|
||||
}
|
||||
if got := ws.MapSensor(tt.args.sensor); got != tt.want {
|
||||
t.Errorf("WeatherStation.MapSensor() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user