forked from massivebox/ecodash
240 lines
5 KiB
Go
240 lines
5 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
|
"io"
|
||
|
"log"
|
||
|
"math"
|
||
|
"net/http"
|
||
|
"net/url"
|
||
|
"strconv"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type HistoryResult [][]struct {
|
||
|
State string `json:"state"`
|
||
|
LastUpdated time.Time `json:"last_updated"`
|
||
|
}
|
||
|
|
||
|
func (config Config) queryHistory(entityID string, startTime, endTime time.Time) (HistoryResult, error) {
|
||
|
|
||
|
req, err := http.NewRequest("GET", config.HomeAssistant.BaseURL+
|
||
|
"/api/history/period/"+url.QueryEscape(startTime.Format(time.RFC3339))+
|
||
|
"?filter_entity_id="+entityID+
|
||
|
"&end_time="+url.QueryEscape(endTime.Format(time.RFC3339)), /*+
|
||
|
"&minimal_response",*/nil)
|
||
|
if err != nil {
|
||
|
return HistoryResult{}, err
|
||
|
}
|
||
|
|
||
|
req.Header.Add("Authorization", "Bearer "+config.HomeAssistant.ApiKey)
|
||
|
client := &http.Client{}
|
||
|
|
||
|
resp, err := client.Do(req)
|
||
|
if err != nil {
|
||
|
return HistoryResult{}, err
|
||
|
}
|
||
|
defer resp.Body.Close()
|
||
|
|
||
|
if resp.StatusCode != 200 {
|
||
|
return HistoryResult{}, errors.New("got a non-200 status code. Check the correctness of sensors IDs -" + resp.Status)
|
||
|
}
|
||
|
|
||
|
body, err := io.ReadAll(resp.Body)
|
||
|
if err != nil {
|
||
|
return HistoryResult{}, err
|
||
|
}
|
||
|
|
||
|
var result HistoryResult
|
||
|
err = json.Unmarshal(body, &result)
|
||
|
if err != nil {
|
||
|
return result, err
|
||
|
}
|
||
|
|
||
|
return result, nil
|
||
|
|
||
|
}
|
||
|
|
||
|
type DayData struct {
|
||
|
DayNumber int
|
||
|
DayTime time.Time
|
||
|
Measurements int
|
||
|
Value float32
|
||
|
High float32
|
||
|
Low float32
|
||
|
}
|
||
|
|
||
|
func (config Config) historyAverageAndConvertToGreen(entityID string, startTime, endTime time.Time) ([]DayData, error) {
|
||
|
|
||
|
history, err := config.queryHistory(entityID, startTime, endTime)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
var days []DayData
|
||
|
|
||
|
for _, historyChange := range history[0] {
|
||
|
val, err := strconv.ParseFloat(historyChange.State, 32)
|
||
|
if err != nil {
|
||
|
continue
|
||
|
}
|
||
|
value := float32(val)
|
||
|
var found bool
|
||
|
dayNo := historyChange.LastUpdated.Local().Day()
|
||
|
for key, day := range days {
|
||
|
if dayNo == day.DayNumber {
|
||
|
found = true
|
||
|
day.Value += value
|
||
|
day.Measurements++
|
||
|
days[key] = day
|
||
|
}
|
||
|
}
|
||
|
if !found {
|
||
|
days = append(days, DayData{
|
||
|
DayNumber: dayNo,
|
||
|
DayTime: historyChange.LastUpdated.Local(),
|
||
|
Measurements: 1,
|
||
|
Value: value,
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for key, day := range days {
|
||
|
// by using 100 - value we get the percentage of green energy instead of the percentage of fossil-generated energy
|
||
|
day.Value = 100 - (day.Value / float32(day.Measurements))
|
||
|
days[key] = day
|
||
|
}
|
||
|
|
||
|
days = fillMissing(days)
|
||
|
|
||
|
return days, nil
|
||
|
|
||
|
}
|
||
|
|
||
|
func (config Config) historyDelta(entityID string, startTime, endTime time.Time) ([]DayData, error) {
|
||
|
|
||
|
history, err := config.queryHistory(entityID, startTime, endTime)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
var days []DayData
|
||
|
|
||
|
for _, historyChange := range history[0] {
|
||
|
if historyChange.State != "off" {
|
||
|
val, err := strconv.ParseFloat(historyChange.State, 32)
|
||
|
if err != nil {
|
||
|
continue
|
||
|
}
|
||
|
value := float32(val)
|
||
|
var found bool
|
||
|
dayNo := historyChange.LastUpdated.Local().Day()
|
||
|
for key, day := range days {
|
||
|
if dayNo == day.DayNumber {
|
||
|
found = true
|
||
|
if value > day.High {
|
||
|
day.High = value
|
||
|
}
|
||
|
if value < day.Low || day.Low == 0 {
|
||
|
day.Low = value
|
||
|
}
|
||
|
days[key] = day
|
||
|
}
|
||
|
}
|
||
|
if !found {
|
||
|
days = append(days, DayData{
|
||
|
DayNumber: dayNo,
|
||
|
DayTime: historyChange.LastUpdated.Local(),
|
||
|
Value: value,
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for key, day := range days {
|
||
|
day.Value = day.High - day.Low
|
||
|
days[key] = day
|
||
|
}
|
||
|
|
||
|
days = fillMissing(days)
|
||
|
|
||
|
return days, nil
|
||
|
|
||
|
}
|
||
|
|
||
|
func fillMissing(days []DayData) []DayData {
|
||
|
|
||
|
var (
|
||
|
previousDay time.Time
|
||
|
defaultDay time.Time
|
||
|
previousValue float32
|
||
|
ret []DayData
|
||
|
currentTime = time.Now()
|
||
|
)
|
||
|
|
||
|
for key, day := range days {
|
||
|
|
||
|
if key != 0 {
|
||
|
|
||
|
if day.DayTime.Day() != previousDay.Add(24*time.Hour).Day() {
|
||
|
daysDiff := math.Trunc(day.DayTime.Sub(previousDay).Hours() / 24)
|
||
|
for i := 1; float64(i) < daysDiff; i++ {
|
||
|
fakeTime := previousDay.Add(time.Duration(24*i) * time.Hour)
|
||
|
ret = append(ret, DayData{
|
||
|
DayNumber: fakeTime.Day(),
|
||
|
DayTime: fakeTime,
|
||
|
Value: previousValue,
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
ret = append(ret, day)
|
||
|
previousValue = day.Value
|
||
|
previousDay = day.DayTime
|
||
|
|
||
|
}
|
||
|
|
||
|
// note that here previousDay is the last logged day
|
||
|
if previousDay == defaultDay {
|
||
|
return []DayData{}
|
||
|
}
|
||
|
|
||
|
if previousDay.Day() != currentTime.Day() {
|
||
|
daysDiff := math.Trunc(currentTime.Sub(previousDay).Hours() / 24)
|
||
|
for i := 1; float64(i) < daysDiff; i++ {
|
||
|
fakeTime := previousDay.Add(time.Duration(24*i) * time.Hour)
|
||
|
ret = append(ret, DayData{
|
||
|
DayNumber: fakeTime.Day(),
|
||
|
DayTime: fakeTime,
|
||
|
Value: previousValue,
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if len(ret) < 8 {
|
||
|
shouldAdd := 8 - len(ret)
|
||
|
startDay := currentTime.Add(-time.Duration(24*len(ret)) * time.Hour)
|
||
|
for i := 0; i < shouldAdd; i++ {
|
||
|
fakeTime := startDay.Add(-time.Duration(24*i) * time.Hour)
|
||
|
ret = append([]DayData{
|
||
|
{
|
||
|
DayNumber: fakeTime.Day(),
|
||
|
DayTime: fakeTime,
|
||
|
Value: 0,
|
||
|
},
|
||
|
}, ret...)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if len(ret) != 8 {
|
||
|
// oh shit
|
||
|
log.Panicln("You've found a logic bug! Open a bug report ASAP.")
|
||
|
}
|
||
|
|
||
|
return ret
|
||
|
|
||
|
}
|