energyd/pkg/isoclient/isoclient.go
2022-06-25 22:13:35 -04:00

124 lines
2.6 KiB
Go

package isoclient
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
"github.com/amigan/energyd/internal/config"
)
type Client interface {
GetCurrentLoad() (*FiveMinSystemLoad, error)
GetHourlyLoadForecast() (*HourlyLoadForecasts, error)
}
type client struct {
user string
passwd string
Debug bool
}
func New(cfg *config.Conf) Client {
c := &client{
user: cfg.ISOUser,
passwd: cfg.ISOPassword,
}
return c
}
const BASEURL = "https://webservices.iso-ne.com/api/v1.1"
const GETCURRENTLOADEP = "/fiveminutesystemload/current"
func (c *client) makeRequest(endpoint string, into interface{}) error {
req, err := http.NewRequest(http.MethodGet, BASEURL+endpoint, nil)
if err != nil {
return err
}
req.SetBasicAuth(c.user, c.passwd)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
client := http.Client{}
resp, err := client.Do(req)
if resp.StatusCode != http.StatusOK {
return errors.New("non 2xx response")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if c.Debug {
log.Println(string(body))
}
return json.Unmarshal(body, into)
}
type FiveMinSystemLoad struct {
BeginDate time.Time `json:"BeginDate"`
LoadMW float32 `json:"LoadMw"`
NativeLoad float32 `json:"NativeLoad"`
ArdDemand float32 `json:"ArdDemand"`
SystemLoadBtmPv float32 `json:"SystemLoadBtmPv"`
NativeLoadBtmPv float32 `json:"NativeLoadBtmPv"`
}
func (c *client) GetCurrentLoad() (*FiveMinSystemLoad, error) {
fms := struct {
FiveMinSystemLoad []*FiveMinSystemLoad
}{}
err := c.makeRequest(GETCURRENTLOADEP, &fms)
if err != nil {
return nil, err
}
if len(fms.FiveMinSystemLoad) < 1 {
return nil, errors.New("less than one element")
}
return fms.FiveMinSystemLoad[0], nil
}
const GETHOURLYFORECASTEP = "/hourlyloadforecast/day/"
type HourlyLoadForecast struct {
BeginDate time.Time `json:"BeginDate"`
CreationDate time.Time `json:"CreationDate"`
LoadMW float32 `json:"LoadMw"`
LoadAdjustmentMW *float32 `json:"LoadAdjustmentMw"`
NetLoadMW float32 `json:"NetLoadMw"`
}
type HourlyLoadForecasts struct {
HourlyLoadForecast [23]HourlyLoadForecast `json:"HourlyLoadForecast"`
}
func (c *client) GetHourlyLoadForecast() (*HourlyLoadForecasts, error) {
hlfc := struct {
HourlyLoadForecasts HourlyLoadForecasts
}{}
year, month, day := time.Now().Date()
endpoint := fmt.Sprintf("%s%04d%02d%02d", GETHOURLYFORECASTEP, year, month, day)
err := c.makeRequest(endpoint, &hlfc)
if err != nil {
return nil, err
}
return &hlfc.HourlyLoadForecasts, nil
}