-
Notifications
You must be signed in to change notification settings - Fork 5
/
griddy.go
79 lines (72 loc) · 2.12 KB
/
griddy.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
package griddy
import (
"bytes"
"encoding/json"
"fmt"
"github.com/BurntSushi/toml"
"io/ioutil"
"net/http"
)
type Griddy struct {
Url string `json:"-" toml"url"`
Meter string `json:"meterID" toml:"meterid"`
Member string `json:"memberID" toml:"memberid"`
Settlement string `json:"settlement_point" toml:"settlement"`
}
func (g *Griddy) LoadConfig(path string) error {
var config struct {
Griddy *Griddy
}
config.Griddy = g
_, err := toml.DecodeFile(path, &config)
return err
}
type GriddyResponse struct {
Now GriddyStatus `json:"now"`
TTL uint32 `json:"seconds_until_refresh,string"`
}
type GriddyStatus struct {
Date string `json:"date"`
Hour uint32 `json:"hour_num,string"`
Minute uint32 `json:"min_num,string"`
Settlement string `json:"settlement_point"`
PriceType string `json:"price_type"`
Price float64 `json:"price_ckwh,string"`
ValueScore uint32 `json:"value_score,string"`
MeanPrice float64 `json:"mean_price_ckwh,string"`
DiffMean float64 `json:"diff_mean_ckwh,string"`
High float64 `json:"high_ckwh,string"`
Low float64 `json:"low_ckwh,string"`
PriceDisplay float64 `json:"price_display,string"`
PriceDisplaySign string `json:"price_display_sign"`
DateLocalTZ string `json:"date_local_tz"`
}
func (g *Griddy) Get() (GriddyResponse, error) {
response := GriddyResponse{}
if g.Url == "" || g.Meter == "" || g.Member == "" || g.Settlement == "" {
return response, fmt.Errorf("griddy: Url, Meter, Member and Settlement fields are required")
}
jsonMember, err := json.Marshal(g)
if err != nil {
return response, err
}
resp, err := http.Post(g.Url, "application/json", bytes.NewBuffer(jsonMember))
if err != nil {
return response, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return response, err
}
err = json.Unmarshal(body, &response)
if err != nil {
return response, err
}
return response, nil
}
func New() Griddy {
griddy := Griddy{}
griddy.Url = "https://app.gogriddy.com/api/v1/insights/getnow"
return griddy
}