Browse Source

Major Update to version 2

Signed-off-by: Alexander Karl <akarl@darknebu.la>
master
Alexander Karl 3 years ago
parent
commit
348e115770
  1. 3
      api/influx.go
  2. 85
      api/tankapi.go
  3. 4
      main.go
  4. 1
      struct/config.go
  5. 63
      struct/list.go

3
api/influx.go

@ -7,7 +7,7 @@ import (
"time"
)
func InsertInflux(pName string, pE5Price float64, pE10Price float64, pDieselPrice float64, pLNGPrice float64) {
func InsertInflux(pName string, pE5Price float64, pE10Price float64, pDieselPrice float64) {
// Create a new HTTPClient
c, err := client.NewHTTPClient(client.HTTPConfig{
@ -36,7 +36,6 @@ func InsertInflux(pName string, pE5Price float64, pE10Price float64, pDieselPric
"E5": pE5Price,
"E10": pE10Price,
"Diesel": pDieselPrice,
"LNG": pLNGPrice,
}
pt, err := client.NewPoint("price", tags, fields, time.Now())

85
api/tankapi.go

@ -8,13 +8,59 @@ import (
"io/ioutil"
"log"
"net/http"
"strings"
"time"
)
func GetDetailInfoByUuid(Uuid string) (Details structure.DetailInfos, err error) {
fmt.Printf("Fetching DetailInfo for: %s\n", Uuid)
url := fmt.Sprintf("https://creativecommons.tankerkoenig.de/json/detail.php?id=%s&apikey=%s", Uuid, viper.GetString("Endpoint.Api"))
tankApiClient := http.Client{
Timeout: time.Second * 2,
}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
log.Fatal(err)
}
res, getErr := tankApiClient.Do(req)
if getErr != nil {
log.Fatal(getErr)
}
data, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
log.Fatal(readErr)
}
defer res.Body.Close()
log.Printf("%v", string(data))
var record structure.DetailInfos
if err := json.Unmarshal(data, &record); err != nil {
return structure.DetailInfos{}, err
} else {
return record, nil
}
}
func GetPrices() {
fmt.Println("Cron runs now!")
url := fmt.Sprintf("https://creativecommons.tankerkoenig.de/json/list.php?lat=51.6648467&lng=6.6090983&rad=10&sort=dist&type=all&apikey=%s", viper.GetString("Endpoint.Api"))
now := time.Now()
fmt.Printf("Run Cron: %v \n", now.Format("02.01.2006 15:04"))
t := viper.GetStringSlice("Stations")
stationsPart := strings.Join(t, ",")
log.Printf("Fetching Price for: %s", stationsPart)
url := fmt.Sprintf("https://creativecommons.tankerkoenig.de/json/prices.php?ids=%s&apikey=%s", stationsPart, viper.GetString("Endpoint.Api"))
tankApiClient := http.Client{
Timeout: time.Second * 2,
@ -38,17 +84,40 @@ func GetPrices() {
defer res.Body.Close()
var record structure.TankerkoenigJson
var record structure.PricesRoot
if err := json.Unmarshal(data, &record); err != nil {
log.Println(err)
log.Print(record.Ok)
log.Printf("Error Unmarshal JSON in GetPrices %v", err)
} else {
for _, element := range record.Stations {
if element.IsOpen {
InsertInflux(element.Name, element.E5, element.E10, element.Diesel, element.Lng)
for key, element := range record.Prices {
var stationName string
// Get Detailinfo for station - wanted cause you dont get the name of the station from the prices entpoint
data, err := GetDetailInfoByUuid(key)
// If the station is open you will add points to influx?
if element.Status == "open" {
// Check if you get a name from Details Endpoint
// if an error occurred
if err != nil {
log.Println(err.Error())
stationName = ""
} else {
// if no error occurred
stationName = data.Station.Name
}
// insert data in influxdb
InsertInflux(stationName, element.E5, element.E10, element.Diesel)
} else {
// Log that the station is closed
log.Printf("Station %s is closed.", key)
}
}
}
}

4
main.go

@ -1,7 +1,6 @@
package main
import (
"fmt"
"git.darknebu.la/akarl/tankerkoenig2influx/api"
"git.darknebu.la/akarl/tankerkoenig2influx/struct"
"github.com/jasonlvhit/gocron"
@ -24,8 +23,5 @@ func main() {
}
gocron.Every(1).Minute().Do(api.GetPrices)
_, time := gocron.NextRun()
fmt.Println(time)
<-gocron.Start()
}

1
struct/config.go

@ -3,4 +3,5 @@ package structure
type Configuration struct {
Endpoint EndpointConfiguration
Database DatabaseConfiguration
Stations []string
}

63
struct/list.go

@ -1,24 +1,45 @@
package structure
type TankerkoenigJson struct {
Ok bool `json:"ok"`
License string `json:"license"`
Data string `json:"data"`
Status string `json:"status"`
Stations []struct {
ID string `json:"id"`
Name string `json:"name"`
Brand string `json:"brand"`
Street string `json:"street"`
Place string `json:"place"`
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
Dist float64 `json:"dist"`
Diesel float64 `json:"diesel"`
E5 float64 `json:"e5"`
E10 float64 `json:"e10"`
IsOpen bool `json:"isOpen"`
HouseNumber string `json:"houseNumber"`
PostCode int `json:"postCode"`
} `json:"stations"`
type Price struct {
Status string `json:"status"`
E5 float64 `json:"e5"`
E10 float64 `json:"e10"`
Diesel float64 `json:"diesel"`
}
type PricesRoot struct {
Ok bool `json:"ok"`
License string `json:"license"`
Data string `json:"data"`
Prices map[string]Price `json:"prices"`
}
type DetailInfos struct {
Ok bool `json:"ok"`
License string `json:"license"`
Data string `json:"data"`
Status string `json:"status"`
Station struct {
ID string `json:"id"`
Name string `json:"name"`
Brand string `json:"brand"`
Street string `json:"street"`
HouseNumber string `json:"houseNumber"`
PostCode int `json:"postCode"`
Place string `json:"place"`
OpeningTimes []struct {
Text string `json:"text"`
Start string `json:"start"`
End string `json:"end"`
} `json:"openingTimes"`
Overrides []interface{} `json:"overrides"`
WholeDay bool `json:"wholeDay"`
IsOpen bool `json:"isOpen"`
E5 float64 `json:"e5"`
E10 float64 `json:"e10"`
Diesel float64 `json:"diesel"`
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
State interface{} `json:"state"`
} `json:"station"`
}

Loading…
Cancel
Save