-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
160 lines (134 loc) · 3.72 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math"
"net/http"
"os"
"strconv"
"time"
_ "time/tzdata" // Embed the IANA Time Zone database
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
)
var (
version = "0.1.2"
debug bool
)
type PriceData struct {
SEKPerKWh float64 `json:"SEK_per_kWh"`
TimeStart string `json:"time_start"`
}
type Response []PriceData
func doEvery(d time.Duration, f func(time.Time)) {
f(time.Now()) // Execute immediately
for x := range time.Tick(d) {
f(x)
}
}
func pushToInflux(t time.Time) {
priceArea := os.Getenv("PRICE_AREA")
if priceArea == "" {
fmt.Println("Error: PRICE_AREA environment variable is not set")
os.Exit(1)
}
now := time.Now()
today := now.Format("2006/01-02")
tomorrow := now.AddDate(0, 0, 1).Format("2006/01-02")
var urls []string
baseURL := "https://www.elprisetjustnu.se/api/v1/prices/"
urls = append(urls, baseURL+today+"_"+priceArea+".json")
if now.Hour() >= 14 && now.Minute() >= 30 {
urls = append(urls, baseURL+tomorrow+"_"+priceArea+".json")
}
client := &http.Client{}
for _, url := range urls {
if debug {
fmt.Println("Fetching URL:", url)
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println("Error creating request:", err)
continue
}
req.Header.Set("User-Agent", fmt.Sprintf("vattenfall-to-influxdb/%s (+https://github.com/rvoitenko/vattenfall-to-influxdb)", version))
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error on request:", err)
continue
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Printf("Request to %s failed with status code: %d\n", url, resp.StatusCode)
bodyBytes, _ := ioutil.ReadAll(resp.Body)
fmt.Println("Response body:", string(bodyBytes))
continue
}
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
continue
}
var result Response
if err := json.Unmarshal(body, &result); err != nil {
fmt.Println("Can not unmarshal JSON:", err)
continue
}
if debug {
fmt.Println("Received data:", result)
}
influxClient := influxdb2.NewClient(os.Getenv("INFLUXDB_URL"), os.Getenv("INFLUXDB_TOKEN"))
writeAPI := influxClient.WriteAPIBlocking(os.Getenv("INFLUXDB_ORG"), os.Getenv("INFLUXDB_BUCKET"))
for _, data := range result {
startDate, err := time.Parse(time.RFC3339, data.TimeStart)
if err != nil {
fmt.Println("Error parsing start time:", err)
continue
}
// Round the SEK_per_kWh value to two decimal places
roundedSEK := math.Round(data.SEKPerKWh*100) / 100
point := influxdb2.NewPoint("current",
map[string]string{"unit": "price"},
map[string]interface{}{"last": roundedSEK},
startDate)
if debug {
fmt.Printf("Data to be written to InfluxDB: Time: %s, SEK_per_kWh: %.2f\n", data.TimeStart, data.SEKPerKWh)
}
writeAPI.WritePoint(context.Background(), point)
}
influxClient.Close()
}
if debug {
fmt.Printf("%v: Tick\n", t)
}
}
func main() {
location, err := time.LoadLocation("Europe/Stockholm")
if err != nil {
fmt.Println("Error loading location 'Europe/Stockholm':", err)
os.Exit(1)
}
time.Local = location
intervalStr := os.Getenv("INTERVAL")
interval := 1800
if intervalStr != "" {
intervalInt, err := strconv.Atoi(intervalStr)
if err == nil {
interval = intervalInt
} else {
fmt.Println("Invalid INTERVAL value, using default 30 seconds")
}
}
debugStr := os.Getenv("DEBUG")
if debugStr != "" {
debugBool, err := strconv.ParseBool(debugStr)
if err == nil {
debug = debugBool
} else {
fmt.Println("Invalid DEBUG value, should be either true or false, using default false")
}
}
doEvery(time.Duration(interval)*time.Second, pushToInflux)
}