-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from GJSBRT/feature/power-prices
Added power price sources and All In Power
- Loading branch information
Showing
13 changed files
with
416 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
huawei-modbus* | ||
vonkje* | ||
config.yaml | ||
temp/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Modbus | ||
|
||
## Links | ||
- **Modbus definitions** https://www.photovoltaikforum.com/core/attachment/251184-solar-inverter-modbus-interface-definitions-pdf/ | ||
- **Helpful modbus definitions guide** https://community.openhab.org/t/reading-data-from-huawei-inverter-sun-2000-3ktl-10ktl-via-modbus-tcp-and-rtu/87670 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Power prices | ||
Power prices can be obtained from multiple sources. We want to create an enviroment which can accept multiple power price sources. | ||
|
||
## Support sources | ||
- [All In Power](https://allinpower.nl) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package victoria_metrics | ||
|
||
type Config struct { | ||
URL string | ||
Username string | ||
Password string | ||
} | ||
|
||
type VictoriaMetricsRequest struct { | ||
Metric map[string]string `json:"metric"` | ||
Values []float64 `json:"values"` | ||
Timestamps []int64 `json:"timestamps"` | ||
} | ||
|
||
type VictoriaMetricsQueryResponse struct { | ||
Status string `json:"status"` | ||
Data struct { | ||
ResultType string `json:"resultType"` | ||
Result []struct { | ||
Metric map[string]string `json:"metric"` | ||
Values [][]interface{} `json:"values"` | ||
} `json:"result"` | ||
} `json:"data"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package victoria_metrics | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
"bytes" | ||
"errors" | ||
"net/url" | ||
"strconv" | ||
"net/http" | ||
"io/ioutil" | ||
"encoding/json" | ||
"encoding/base64" | ||
) | ||
|
||
type VictoriaMetrics struct { | ||
Config Config | ||
Client *http.Client | ||
} | ||
|
||
// New creates a new GoVictoria instance | ||
func New(config Config) *VictoriaMetrics { | ||
return &VictoriaMetrics{ | ||
Config: config, | ||
Client: &http.Client{}, | ||
} | ||
} | ||
|
||
// SendMetrics sends the metrics to VictoriaMetrics | ||
func (g *VictoriaMetrics) SendMetrics(requests []VictoriaMetricsRequest) error { | ||
if len(requests) == 0 { | ||
return errors.New("No requests to send") | ||
} | ||
|
||
// Loop through the request and build the body | ||
body := "" | ||
for _, requestBody := range requests { | ||
jsonRequest, err := json.Marshal(requestBody) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
body += string(jsonRequest) | ||
} | ||
|
||
// Create the request to Victoria Metrics | ||
request, err := http.NewRequest("POST", g.Config.URL+"/api/v1/import", bytes.NewBuffer([]byte(body))) | ||
request.Header.Add("Authorization", "Basic "+BasicAuth(g.Config.Username, g.Config.Password)) | ||
request.Header.Add("User-Agent", "Vonkje (github.com/GJSBRT/vonkje)") | ||
|
||
// Send the request to Victoria Metrics | ||
response, err := g.Client.Do(request) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Close the response body | ||
defer response.Body.Close() | ||
|
||
// Check if the status code is not 204 | ||
if response.StatusCode != http.StatusNoContent { | ||
return errors.New(fmt.Sprintf("Victoria Metrics returned a non-200 status code: %d", response.StatusCode)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// QueryTimeRange queries Victoria Metrics for metrics in a time range | ||
func (g *VictoriaMetrics) QueryTimeRange(promql string, startTime time.Time, endTime time.Time, step string) (VictoriaMetricsQueryResponse, error) { | ||
// Check if the start time is before the end time | ||
if startTime.After(endTime) { | ||
return VictoriaMetricsQueryResponse{}, errors.New("Start time must be before end time") | ||
} | ||
|
||
// Add the query parameters to the request | ||
params := url.Values{} | ||
params.Add("query", promql) | ||
params.Add("start", strconv.FormatInt(startTime.Unix(), 10)) | ||
params.Add("end", strconv.FormatInt(endTime.Unix(), 10)) | ||
params.Add("step", step) | ||
|
||
url := g.Config.URL + "/api/v1/query_range?" + params.Encode() | ||
|
||
// Create the request to Victoria Metrics | ||
request, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
return VictoriaMetricsQueryResponse{}, err | ||
} | ||
|
||
// Add the query parameters to the request | ||
request.Header.Add("Authorization", "Basic "+BasicAuth(g.Config.Username, g.Config.Password)) | ||
request.Header.Add("User-Agent", "Vonkje (github.com/GJSBRT/vonkje)") | ||
|
||
// Send the request to Victoria Metrics | ||
response, err := g.Client.Do(request) | ||
if err != nil { | ||
return VictoriaMetricsQueryResponse{}, err | ||
} | ||
|
||
// Close the response body | ||
defer response.Body.Close() | ||
|
||
// Check if the status code is not 200 | ||
if response.StatusCode != http.StatusOK { | ||
return VictoriaMetricsQueryResponse{}, errors.New(fmt.Sprintf("Victoria Metrics returned a non-200 status code: %d", response.StatusCode)) | ||
} | ||
|
||
// Read the response body | ||
body, err := ioutil.ReadAll(response.Body) | ||
if err != nil { | ||
return VictoriaMetricsQueryResponse{}, err | ||
} | ||
|
||
// Unmarshal the response | ||
var metrics VictoriaMetricsQueryResponse | ||
err = json.Unmarshal([]byte(body), &metrics) | ||
if err != nil { | ||
return VictoriaMetricsQueryResponse{}, err | ||
} | ||
|
||
return metrics, nil | ||
} | ||
|
||
// BasicAuth returns the base64 encoded string for basic auth | ||
func BasicAuth(username, password string) string { | ||
auth := username + ":" + password | ||
return base64.StdEncoding.EncodeToString([]byte(auth)) | ||
} |
Oops, something went wrong.