Skip to content

Commit

Permalink
WIP steering assets
Browse files Browse the repository at this point in the history
  • Loading branch information
Pieter Verberck committed Mar 27, 2024
1 parent b6ffe24 commit 63945ec
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 10 deletions.
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ services:
tty: true # colorized output
ports:
- 80:80
extra_hosts:
- "host.docker.internal:host-gateway"
steering-python:
network_mode: "host"
build:
Expand Down
7 changes: 3 additions & 4 deletions golang/src/api_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ import (
"io"
"net/http"
"net/url"
"os"
)

func makeRequest(method, path string, headers map[string]string, params url.Values) ([]byte, error) {
u, err := url.Parse(os.Getenv("TRAXES_API_BASE_URI") + path)
func makeRequest(baseUrl string, method, path string, headers map[string]string, params url.Values, data io.Reader) ([]byte, error) {
u, err := url.Parse(baseUrl + path)
if err != nil {
return nil, err
}

u.RawQuery = params.Encode()

req, err := http.NewRequest(method, u.String(), nil)
req, err := http.NewRequest(method, u.String(), data)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion golang/src/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"log"
"net/url"
"os"
"time"
)

Expand Down Expand Up @@ -44,7 +45,7 @@ func getHistoricAssetStates(token, ean, startDate, endDate string) []AssetState
params.Add("startDate", startDate)
params.Add("endDate", endDate)

body, err := makeRequest("GET", "/assets/states", headers, params)
body, err := makeRequest(os.Getenv("TRAXES_API_BASE_URI"), "GET", "/assets/states", headers, params, nil)
if err != nil {
log.Fatal("Error on dispatching request. ", err.Error())
}
Expand Down
16 changes: 14 additions & 2 deletions golang/src/hackathon_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"log"
"net/url"
"os"
"time"
)

type HackathonTimeResponse struct {
Expand All @@ -17,7 +19,7 @@ func getCurrentHackathonTime(token string) string {
"Authorization": "Bearer " + token,
}

body, err := makeRequest("GET", "/times/HackathonTimeForNow", headers, nil)
body, err := makeRequest(os.Getenv("TRAXES_API_BASE_URI"), "GET", "/times/HackathonTimeForNow", headers, nil, nil)
if err != nil {
log.Fatal("Error on dispatching request. ", err.Error())
}
Expand All @@ -30,6 +32,16 @@ func getCurrentHackathonTime(token string) string {
return hackathonTimeResponse.HackathonTime
}

func getDateString(hackathonTime string) string {
return hackathonTime[0:10]
}

func getNextDay(dateString string) string {
parsed, _ := time.Parse("2006-01-02", dateString)
parsed = parsed.AddDate(0, 0, 1)
return parsed.Format("2006-01-02")
}

func getHackathonTime(token, realTime string) string {
headers := map[string]string{
"Authorization": "Bearer " + token,
Expand All @@ -38,7 +50,7 @@ func getHackathonTime(token, realTime string) string {
params := url.Values{}
params.Add("realTime", realTime)

body, err := makeRequest("GET", "/times/HackathonTimeForDateTime", headers, params)
body, err := makeRequest(os.Getenv("TRAXES_API_BASE_URI"), "GET", "/times/HackathonTimeForDateTime", headers, params, nil)
if err != nil {
log.Fatal("Error on dispatching request. ", err.Error())
}
Expand Down
4 changes: 4 additions & 0 deletions golang/src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/url"
"os"
"path/filepath"
"time"

"github.com/joho/godotenv"
)
Expand Down Expand Up @@ -65,6 +66,9 @@ func main() {
}
})

time.Sleep(time.Second * 5)
go steerAssets(accessToken)

server := http.Server{
Addr: ":80",
Handler: mux,
Expand Down
36 changes: 36 additions & 0 deletions golang/src/steer_assets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import "log"

func steerAssets(token string) {

currentDateString := ""

for {
// Get date of current day
currentHackathonTime := getCurrentHackathonTime(token)
newDateString := getDateString(currentHackathonTime)
log.Println("### Current time:", currentHackathonTime)

// If start of new day (or need not set)
if newDateString != currentDateString {
log.Println("### Starting new day", newDateString)
currentDateString = newDateString

// Calculate or assume new need
//need := 300 // kWh

// Get day ahead time of new day
dayAheadPricesJson := getDayAheadPrices(token, currentDateString)

// Find roof of cheapest surface
calculateRoofPricePerQuarter(dayAheadPricesJson)
}

// Get real-time price

// If real-time price < roof => charge

}

}
30 changes: 30 additions & 0 deletions golang/src/steering_python.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"bytes"
"fmt"
"log"
"net/url"
)

func calculateRoofPricePerQuarter(dayAheadPricesJson string) {
headers := map[string]string{}
headers["Content-Type"] = "application/json"
params := url.Values{}

dataJson := fmt.Sprintf("{\"time_series_data\": %s, \"ev_comfort_charge_capacity_kwh\": %d, \"ev_max_charge_capacity_kwh\": %d, \"buffer\": %f}",
dayAheadPricesJson,
50,
100,
1.0)

log.Println("calculateRoofPricePerQuarter")
log.Println(dataJson)

body, err := makeRequest("http://host.docker.internal:5001", "POST", "/calculate_roof_price_per_quarter", headers, params, bytes.NewBuffer([]byte(dataJson)))
if err != nil {
log.Fatal("Error on dispatching request. ", err.Error())
}

log.Println(string(body))
}
32 changes: 32 additions & 0 deletions golang/src/traxes_prices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"log"
"net/url"
"os"
)

func getDayAheadPrices(token string, dateString string) string {
headers := map[string]string{
"Authorization": "Bearer " + token,
}

params := url.Values{}

params.Add("startDate", dateString+"T00:15:00+01:00")
params.Add("endDate", getNextDay(dateString)+"T00:15:00+01:00")
log.Println("getDayAheadPrices ", params)

body, err := makeRequest(os.Getenv("TRAXES_API_BASE_URI"), "GET", "/prices/day-ahead-prices", headers, params, nil)
if err != nil {
log.Fatal("Error on dispatching request. ", err.Error())
}

return string(body)

/*var hackathonTimeResponse HackathonTimeResponse
if err := json.Unmarshal(body, &hackathonTimeResponse); err != nil {
log.Fatalln(err)
}*/

}
Empty file.
6 changes: 3 additions & 3 deletions steering-python/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
load_dotenv('/app/.env')


app = FastAPI()
app = FastAPI(debug=True)
app.include_router(http_router)
origins = [
"http://localhost",
Expand All @@ -29,5 +29,5 @@
async def root():
return 200

if __name__ == "__main__":
uvicorn.run("main:app", port=8000, reload=True, debug=True) # TODO change debug eventually to False
#if __name__ == "__main__":
# uvicorn.run("main:app", port=8000, reload=True, debug=True) # TODO change debug eventually to False

0 comments on commit 63945ec

Please sign in to comment.