bug on types, updated send function

This commit is contained in:
Paul 2019-07-15 01:16:36 +02:00
parent 7baad7e839
commit 0ed528c6ef
3 changed files with 24 additions and 17 deletions

View File

@ -61,14 +61,14 @@ func FetchData(city string) (Data, error) {
b, err := ioutil.ReadAll(resp.Body) b, err := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(b, &d) err = json.Unmarshal(b, &d)
d.Humidity = d.Main["humidity"]
d.Temperature = kelvin + d.Main["temp"] d.Temperature = kelvin + d.Main["temp"]
d.Humidity = int64(d.Main["humidity"])
return d, err return d, err
} }
// SendToInflux sends time series data to influxdb // SendToInflux sends time series data to influxdb
func SendToInflux(wc *WeatherConfig, city string, measurement string, value float64) error { func SendToInflux(wc *WeatherConfig, d Data) error {
httpClient, err := client.NewHTTPClient(client.HTTPConfig{ httpClient, err := client.NewHTTPClient(client.HTTPConfig{
Addr: fmt.Sprintf("http://%s:%d", wc.InfluxHost, wc.InfluxPort), Addr: fmt.Sprintf("http://%s:%d", wc.InfluxHost, wc.InfluxPort),
@ -86,11 +86,27 @@ func SendToInflux(wc *WeatherConfig, city string, measurement string, value floa
return err return err
} }
tags := map[string]string{"city": city} tags := map[string]string{"city": d.City}
fields := map[string]interface{}{"value": value} fields := map[string]interface{}{"value": d.Temperature}
point, _ := client.NewPoint( point, _ := client.NewPoint(
measurement, "temperature",
tags,
fields,
time.Now(),
)
bp.AddPoint(point)
err = httpClient.Write(bp)
if err != nil {
return err
}
tags = map[string]string{"city": d.City}
fields = map[string]interface{}{"value": d.Humidity}
point, _ = client.NewPoint(
"humidity",
tags, tags,
fields, fields,
time.Now(), time.Now(),

View File

@ -18,6 +18,6 @@ type WeatherConfig struct {
type Data struct { type Data struct {
City string `json:"name"` City string `json:"name"`
Temperature float64 Temperature float64
Humidity float64 Humidity int64
Main map[string]float64 Main map[string]float64
} }

View File

@ -32,20 +32,11 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
err = SendToInflux(&wc, d.City, "temperature", d.Temperature) err = SendToInflux(&wc, d)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} else { } else {
log.Println(fmt.Sprintf("Successfully sent temperature data for %s", city)) log.Println(fmt.Sprintf("Successfully sent data for %s", city))
} }
err = SendToInflux(&wc, d.City, "humidity", d.Humidity)
if err != nil {
log.Fatal(err)
} else {
log.Println(fmt.Sprintf("Successfully sent humidity data for %s", city))
} }
}
} }