Browse Source

Init commit

Signed-off-by: Alexander Karl <akarl@darknebu.la>
master
Alexander Karl 3 years ago
commit
d41554fd07
  1. 57
      api/influx.go
  2. 53
      api/tankapi.go
  3. 16
      main.go
  4. 24
      struct/list.go

57
api/influx.go

@ -0,0 +1,57 @@
package api
import (
"github.com/influxdata/influxdb/client/v2"
"log"
"time"
)
func InsertInflux(pName string, pE5Price float64, pE10Price float64, pDieselPrice float64, pLNGPrice float64) {
// Create a new HTTPClient
c, err := client.NewHTTPClient(client.HTTPConfig{
Addr: "http://localhost:8086",
Username: "tankapi",
Password: "TankAPI123!._",
})
if err != nil {
log.Fatal(err)
}
defer c.Close()
// Create a new point batch
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: "tankapi",
Precision: "s",
})
if err != nil {
log.Fatal(err)
}
// Create a point and add to batch
tags := map[string]string{"petrol_station": pName}
fields := map[string]interface{}{
"E5": pE5Price,
"E10": pE10Price,
"Diesel": pDieselPrice,
"LNG": pLNGPrice,
}
pt, err := client.NewPoint("price", tags, fields, time.Now())
if err != nil {
log.Fatal(err)
}
bp.AddPoint(pt)
// Write the batch
if err := c.Write(bp); err != nil {
log.Fatal(err)
}
// Close client resources
if err := c.Close(); err != nil {
log.Fatal(err)
}
}

53
api/tankapi.go

@ -0,0 +1,53 @@
package api
import (
"encoding/json"
"fmt"
"git.darknebu.la/akarl/tank-api/struct"
"io/ioutil"
"log"
"net/http"
"time"
)
func GetPrices() {
fmt.Println("Cron runs now!")
url := "https://creativecommons.tankerkoenig.de/json/list.php?lat=51.6648467&lng=6.6090983&rad=10&sort=dist&type=all&apikey=090270da-7e5c-444a-e6b5-83a122f7e20c"
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()
var record structure.TankerkoenigJson
if err := json.Unmarshal(data, &record); err != nil {
log.Println(err)
} else {
for _, element := range record.Stations {
if element.IsOpen {
InsertInflux(element.Name, element.E5, element.E10, element.Diesel, element.Lng)
}
}
}
}

16
main.go

@ -0,0 +1,16 @@
package main
import (
"fmt"
"git.darknebu.la/akarl/tank-api/api"
"github.com/jasonlvhit/gocron"
)
func main() {
gocron.Every(1).Minute().Do(api.GetPrices)
_, time := gocron.NextRun()
fmt.Println(time)
<-gocron.Start()
}

24
struct/list.go

@ -0,0 +1,24 @@
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"`
}
Loading…
Cancel
Save