diff --git a/hemtjanst.go b/hemtjanst.go index 81414d2..82440c6 100644 --- a/hemtjanst.go +++ b/hemtjanst.go @@ -19,29 +19,19 @@ func topicName(name string) string { "ö", "oe") } -func newTempSensor(name string, tr device.Transport) client.Device { +func newWeatherStation(name, id string, tr device.Transport) client.Device { dev, _ := client.NewDevice(&device.Info{ - Topic: fmt.Sprintf("sensor/temperature/%s", topicName(name)), + Topic: fmt.Sprintf("sensor/environment/%s", topicName(name)), Manufacturer: "trafikväder", - Name: fmt.Sprintf("Temperature (%s)", name), - Type: "temperatureSensor", + Name: fmt.Sprintf("%s (%s)", name, id), + Type: "weatherStation", Features: map[string]*feature.Info{ "currentTemperature": { Min: -50, - }}, - }, tr) - - return dev -} - -func newRHSensor(name string, tr device.Transport) client.Device { - dev, _ := client.NewDevice(&device.Info{ - Topic: fmt.Sprintf("sensor/humidity/%s", topicName(name)), - Manufacturer: "trafikväder", - Name: fmt.Sprintf("Relative Humidity (%s)", name), - Type: "humiditySensor", - Features: map[string]*feature.Info{ - "currentRelativeHumidity": {}}, + }, + "currentRelativeHumidity": {}, + "precipitation": {}, + }, }, tr) return dev diff --git a/main.go b/main.go index 76fcc57..8e5e8a7 100644 --- a/main.go +++ b/main.go @@ -90,23 +90,29 @@ func main() { } }() - tempSensor := newTempSensor(data.name, m) - rhSensor := newRHSensor(data.name, m) + station := newWeatherStation(data.name, *stationID, m) - err = tempSensor.Feature("currentTemperature").Update( + err = station.Feature("currentTemperature").Update( strconv.FormatFloat(data.tempC, 'f', 1, 32), ) if err != nil { log.Printf("MQTT: %s\n", err) } - err = rhSensor.Feature("currentRelativeHumidity").Update( + err = station.Feature("currentRelativeHumidity").Update( strconv.FormatFloat(data.rhPct, 'f', 1, 32), ) if err != nil { log.Printf("MQTT: %s\n", err) } + err = station.Feature("precipitation").Update( + strconv.FormatFloat(data.precip, 'f', 1, 32), + ) + if err != nil { + log.Printf("MQTT: %s\n", err) + } + log.Println("MQTT: published initial sensor data") loop: @@ -122,43 +128,34 @@ loop: log.Printf("failed to fetch data from API: %s\n", err) continue } - err = tempSensor.Feature("currentTemperature").Update( + err = station.Feature("currentTemperature").Update( strconv.FormatFloat(data.tempC, 'f', 1, 32), ) if err != nil { log.Printf("MQTT: failed to publish temperature update: %s\n", err) } - err = rhSensor.Feature("currentRelativeHumidity").Update( + err = station.Feature("currentRelativeHumidity").Update( strconv.FormatFloat(data.rhPct, 'f', 1, 32), ) if err != nil { log.Printf("MQTT: failed to publish relative humidity update: %s\n", err) } + err = station.Feature("precipitation").Update( + strconv.FormatFloat(data.precip, 'f', 1, 32), + ) + if err != nil { + log.Printf("MQTT: failed to publish precipitation update: %s\n", err) + } } } os.Exit(0) } type data struct { - name string - tempC float64 - rhPct float64 -} - -type weatherstationResp struct { - Response struct { - Result []struct { - WeatherStation []*struct { - Name string `json:"Name"` - Measurement struct { - Air struct { - Temp float64 `json:"Temp"` - RelativeHumidity float64 `json:"RelativeHumidity"` - } `json:"Air"` - } `json:"Measurement"` - } `json:"WeatherStation"` - } `json:"RESULT"` - } `json:"RESPONSE"` + name string + tempC float64 + rhPct float64 + precip float64 } func retrieve(ctx context.Context, client *http.Client, body []byte) (data, error) { @@ -203,6 +200,25 @@ func retrieve(ctx context.Context, client *http.Client, body []byte) (data, erro return data{}, fmt.Errorf("got status code: %d %s", resp.StatusCode, http.StatusText(resp.StatusCode)) } + type weatherstationResp struct { + Response struct { + Result []struct { + WeatherStation []*struct { + Name string `json:"Name"` + Measurement struct { + Air struct { + Temp float64 `json:"Temp"` + RelativeHumidity float64 `json:"RelativeHumidity"` + } `json:"Air"` + Precipitation struct { + Amount float64 `json:"Amount"` + } `json:"Precipitation"` + } `json:"Measurement"` + } `json:"WeatherStation"` + } `json:"RESULT"` + } `json:"RESPONSE"` + } + d := json.NewDecoder(resp.Body) var wr weatherstationResp err = d.Decode(&wr) @@ -215,8 +231,9 @@ func retrieve(ctx context.Context, client *http.Client, body []byte) (data, erro } return data{ - name: wr.Response.Result[0].WeatherStation[0].Name, - tempC: wr.Response.Result[0].WeatherStation[0].Measurement.Air.Temp, - rhPct: wr.Response.Result[0].WeatherStation[0].Measurement.Air.RelativeHumidity, + name: wr.Response.Result[0].WeatherStation[0].Name, + tempC: wr.Response.Result[0].WeatherStation[0].Measurement.Air.Temp, + rhPct: wr.Response.Result[0].WeatherStation[0].Measurement.Air.RelativeHumidity, + precip: wr.Response.Result[0].WeatherStation[0].Measurement.Precipitation.Amount, }, nil }