updates on weather

This commit is contained in:
Paul 2019-07-14 21:44:57 +02:00
parent afe739bcd0
commit ceb9f7a971
2 changed files with 52 additions and 42 deletions

View File

@ -9,6 +9,7 @@ import (
"os"
"time"
client "github.com/influxdata/influxdb1-client/v2"
"gopkg.in/ini.v1"
)
@ -66,47 +67,43 @@ func FetchData(city string) (Data, error) {
return d, err
}
// // SendToInflux sends time series data to influxdb
// func SendToInflux(wc *WeatherConfig, data *[]Data) error {
// httpClient, err := client.NewHTTPClient(client.HTTPConfig{
// Addr: fmt.Sprintf("http://%s:%d", wc.InfluxHost, wc.InfluxPort),
// Username: wc.InfluxUser,
// Password: wc.InfluxPass,
// })
// if err != nil {
// return err
// }
// SendToInflux sends time series data to influxdb
func SendToInflux(wc *WeatherConfig, city string, measurement string, value float64) error {
// bp, err := client.NewBatchPoints(client.BatchPointsConfig{
// Database: wc.InfluxDB,
// })
// if err != nil {
// return err
// }
httpClient, err := client.NewHTTPClient(client.HTTPConfig{
Addr: fmt.Sprintf("http://%s:%d", wc.InfluxHost, wc.InfluxPort),
Username: wc.InfluxUser,
Password: wc.InfluxPass,
})
if err != nil {
return err
}
// for _, p := range *data {
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: wc.InfluxDB,
})
if err != nil {
return err
}
// tags := map[string]string{"city": p.city, "fuel": p.Fuel}
// fields := map[string]interface{}{"value": p.Value}
tags := map[string]string{"city": city, "measurement": measurement}
fields := map[string]interface{}{"value": value}
// point, _ := client.NewPoint(
// wc.Table,
// tags,
// fields,
// time.Now(),
// )
point, _ := client.NewPoint(
wc.WeatherTable,
tags,
fields,
time.Now(),
)
// log.Println(point)
bp.AddPoint(point)
err = httpClient.Write(bp)
if err != nil {
return err
}
// bp.AddPoint(point)
// err = httpClient.Write(bp)
// if err != nil {
// return err
// }
// }
// return nil
// }
return nil
}
// Usage displays possible arguments
func Usage() {

View File

@ -2,6 +2,7 @@ package main
import (
"flag"
"fmt"
"log"
"time"
@ -20,19 +21,31 @@ func main() {
flag.StringVar(&configpath, "configfile", "common.ini", "config file to use with fuelprices section")
flag.Parse()
var data []Data
err := GetConfig(configpath, &wc)
if err != nil {
log.Fatal(err)
}
for _, city := range wc.WeatherCities {
d, _ := FetchData(city)
data = append(data, d)
d, err := FetchData(city)
if err != nil {
log.Fatal(err)
}
err = SendToInflux(&wc, d.City, "temperature", d.Temperature)
if err != nil {
log.Fatal(err)
} else {
log.Println(fmt.Sprintf("Successfully sent temperature 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))
}
}
// bp, err := client.NewBatchPoints(client.BatchPointsConfig{
// Database: wc.InfluxDB,
// })
}