Skip to content

Commit

Permalink
✨ Export data through WeatherStation type (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
daenney authored Jun 24, 2022
1 parent db2ed91 commit c8c4028
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 46 deletions.
26 changes: 8 additions & 18 deletions hemtjanst.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
73 changes: 45 additions & 28 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

0 comments on commit c8c4028

Please sign in to comment.