updates on weather
This commit is contained in:
parent
afe739bcd0
commit
ceb9f7a971
67
functions.go
67
functions.go
@ -9,6 +9,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
client "github.com/influxdata/influxdb1-client/v2"
|
||||||
"gopkg.in/ini.v1"
|
"gopkg.in/ini.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -66,47 +67,43 @@ func FetchData(city string) (Data, error) {
|
|||||||
return d, err
|
return d, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// // SendToInflux sends time series data to influxdb
|
// SendToInflux sends time series data to influxdb
|
||||||
// func SendToInflux(wc *WeatherConfig, data *[]Data) error {
|
func SendToInflux(wc *WeatherConfig, city string, measurement string, value float64) 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
|
|
||||||
// }
|
|
||||||
|
|
||||||
// bp, err := client.NewBatchPoints(client.BatchPointsConfig{
|
httpClient, err := client.NewHTTPClient(client.HTTPConfig{
|
||||||
// Database: wc.InfluxDB,
|
Addr: fmt.Sprintf("http://%s:%d", wc.InfluxHost, wc.InfluxPort),
|
||||||
// })
|
Username: wc.InfluxUser,
|
||||||
// if err != nil {
|
Password: wc.InfluxPass,
|
||||||
// return err
|
})
|
||||||
// }
|
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}
|
tags := map[string]string{"city": city, "measurement": measurement}
|
||||||
// fields := map[string]interface{}{"value": p.Value}
|
fields := map[string]interface{}{"value": value}
|
||||||
|
|
||||||
// point, _ := client.NewPoint(
|
point, _ := client.NewPoint(
|
||||||
// wc.Table,
|
wc.WeatherTable,
|
||||||
// tags,
|
tags,
|
||||||
// fields,
|
fields,
|
||||||
// time.Now(),
|
time.Now(),
|
||||||
// )
|
)
|
||||||
|
|
||||||
// log.Println(point)
|
bp.AddPoint(point)
|
||||||
|
err = httpClient.Write(bp)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// bp.AddPoint(point)
|
return nil
|
||||||
// err = httpClient.Write(bp)
|
}
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return nil
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Usage displays possible arguments
|
// Usage displays possible arguments
|
||||||
func Usage() {
|
func Usage() {
|
||||||
|
27
weather.go
27
weather.go
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -20,19 +21,31 @@ func main() {
|
|||||||
flag.StringVar(&configpath, "configfile", "common.ini", "config file to use with fuelprices section")
|
flag.StringVar(&configpath, "configfile", "common.ini", "config file to use with fuelprices section")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
var data []Data
|
|
||||||
|
|
||||||
err := GetConfig(configpath, &wc)
|
err := GetConfig(configpath, &wc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, city := range wc.WeatherCities {
|
for _, city := range wc.WeatherCities {
|
||||||
d, _ := FetchData(city)
|
d, err := FetchData(city)
|
||||||
data = append(data, d)
|
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