Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Pieter Verberck committed Mar 27, 2024
2 parents 63945ec + f07766c commit 832193c
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 34 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/github-actions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Docker Compose CI

on:
push:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.PAT_SECRET }}

- name: Create .env file
run: echo "${{ secrets.ENV_FILE }}" > ./golang/.env

- name: Build and push with Docker Compose
run: |
docker-compose build
docker-compose push
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ services:
- 80:80
extra_hosts:
- "host.docker.internal:host-gateway"
image: ghcr.io/finnjanssens/elia-hackathon-bakend-golang:latest
steering-python:
network_mode: "host"
build:
context: ./steering-python/
dockerfile: ./Dockerfile
tty: true # colorized output
ports:
- 5001:5001
- 5001:5001
image: ghcr.io/finnjanssens/elia-hackathon-bakend-steering-python:latest
65 changes: 41 additions & 24 deletions golang/src/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"errors"
"log"
"net/url"
"os"
Expand Down Expand Up @@ -34,7 +35,7 @@ type AssetStatesResponse struct {
Values []AssetState `json:"$values"`
}

func getHistoricAssetStates(token, ean, startDate, endDate string) []AssetState {
func getHistoricAssetStates(token, ean, startDate, endDate string) ([]AssetState, error) {

headers := map[string]string{
"Authorization": "Bearer " + token,
Expand All @@ -47,63 +48,79 @@ func getHistoricAssetStates(token, ean, startDate, endDate string) []AssetState

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())
log.Println("Error on dispatching request. ", err.Error())
return nil, err
}

var assetStatesResponse AssetStatesResponse
if err := json.Unmarshal(body, &assetStatesResponse); err != nil {
log.Fatalln(err)
log.Println(err)
return nil, err
}

return assetStatesResponse.Values
if assetStatesResponse.Values == nil {
log.Println("No asset states found for the specified date")
return nil, errors.New("No asset states found for the specified date")
}

return assetStatesResponse.Values, nil
}

type Session struct {
StartState *AssetState `json:"startState"`
EndState *AssetState `json:"endState"`
StartState *AssetState `json:"startState"`
ChargedState *AssetState `json:"chargedState"`
EndState *AssetState `json:"endState"`
}

func getAssetSessionsForDay(token, ean, realTime string) []Session {
hackathonTime := getHackathonTime(token, realTime)
func getAssetSessionsForDay(token, ean, realTime string) ([]Session, error) {
hackathonTime, err := getHackathonTime(token, realTime)
if err != nil {
log.Println("Error getting hackathon time: ", err.Error())
return nil, err
}

var sessions []Session
var startState, endState *AssetState
var startState, chargedState, endState *AssetState

// Parse the date string into a time.Time
parsedDate, err := time.Parse(time.RFC3339, hackathonTime)
if err != nil {
log.Fatal("Error parsing date: ", err.Error())
log.Println("Error parsing date: ", err.Error())
return nil, err
}

// Start and end of the day
startOfDay := time.Date(parsedDate.Year(), parsedDate.Month(), parsedDate.Day(), 0, 0, 0, 0, parsedDate.Location()).Format(time.RFC3339)
endOfDay := time.Date(parsedDate.Year(), parsedDate.Month(), parsedDate.Day(), 23, 59, 59, 0, parsedDate.Location()).Format(time.RFC3339)
// Subtract 24 hours from the parsed date to get the start of the day
startDate := parsedDate.Add(-24 * time.Hour).Format(time.RFC3339)

// The end of the day is the parsed date
endDate := parsedDate.Format(time.RFC3339)

// Get the historic asset states for the specified date
assetStates := getHistoricAssetStates(token, ean, startOfDay, endOfDay)
assetStates, err := getHistoricAssetStates(token, ean, startDate, endDate)
if err != nil {
log.Println("Error getting asset states: ", err.Error())
return nil, err
}

for _, state := range assetStates {

if err != nil {
log.Fatal("Error parsing timestamp: ", err.Error())
}

if state.Connected && startState == nil {
// Start of a new session
startState = &state
} else if !state.Connected && startState != nil {
// End of the current session
endState = &state
sessions = append(sessions, Session{StartState: startState, EndState: endState})
startState, endState = nil, nil
sessions = append(sessions, Session{StartState: startState, ChargedState: chargedState, EndState: endState})
startState, chargedState, endState = nil, nil, nil
} else if state.Soc == state.SocMax && state.Connected && chargedState == nil {
// Fully charged state
chargedState = &state
}

}

// If there's an ongoing session at the end of the day, add it to the sessions
if startState != nil {
sessions = append(sessions, Session{StartState: startState, EndState: nil})
sessions = append(sessions, Session{StartState: startState, ChargedState: chargedState, EndState: nil})
}

return sessions
return sessions, nil
}
12 changes: 6 additions & 6 deletions golang/src/hackathon_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func getCurrentHackathonTime(token string) string {

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())
log.Println("Error on dispatching request. ", err.Error())
}

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

return hackathonTimeResponse.HackathonTime
Expand All @@ -42,7 +42,7 @@ func getNextDay(dateString string) string {
return parsed.Format("2006-01-02")
}

func getHackathonTime(token, realTime string) string {
func getHackathonTime(token, realTime string) (string, error) {
headers := map[string]string{
"Authorization": "Bearer " + token,
}
Expand All @@ -52,13 +52,13 @@ func getHackathonTime(token, realTime string) string {

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())
return "", err
}

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

return hackathonTimeResponse.HackathonTime
return hackathonTimeResponse.HackathonTime, nil
}
26 changes: 23 additions & 3 deletions golang/src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,39 @@ func main() {
mux.HandleFunc("/hello", handlerFunc)

mux.HandleFunc("/sessions", func(w http.ResponseWriter, r *http.Request) {
log.Println("received GET /sessions")
log.Println("received GET /sessions", r)

// Parse the query parameters
query := r.URL.Query()

ean := query.Get("ean")
realTime := query.Get("realTime")
if ean == "" {
http.Error(w, "Missing required 'ean' query parameter", http.StatusBadRequest)
return
}

if realTime == "" {
http.Error(w, "Missing required 'realTime' query parameter", http.StatusBadRequest)
return
}

realTime, err := url.QueryUnescape(realTime)
if err != nil {
log.Fatal(err)
log.Println(err)
}

// Get the asset sessions for the specified day
sessions := getAssetSessionsForDay(accessToken, ean, realTime)
sessions, err := getAssetSessionsForDay(accessToken, ean, realTime)
if err != nil {
http.Error(w, "Error getting asset sessions", http.StatusInternalServerError)
return
}

if sessions == nil {
http.Error(w, "No sessions found", http.StatusNotFound)
return
}

// Write the response
w.Header().Set("Content-Type", "application/json")
Expand Down

0 comments on commit 832193c

Please sign in to comment.