updates on weather
This commit is contained in:
parent
afe739bcd0
commit
ceb9f7a971
67
functions.go
67
functions.go
@ -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() {
|
||||
|
27
weather.go
27
weather.go
@ -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,
|
||||
// })
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user