make it work for my tram board
adds webserer with endpoint /line?number=3 you need to specify a post body text using b64-encoded, semicolon-separated names of the stops index of stop name corresponds to returned value (led index)
This commit is contained in:
98
cmd/web/main.go
Normal file
98
cmd/web/main.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"maps"
|
||||
"net/http"
|
||||
trmaps "shellixa/puvetra/internal/maps"
|
||||
"shellixa/puvetra/internal/types"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
mux := http.NewServeMux()
|
||||
|
||||
server := &http.Server{
|
||||
Handler: mux,
|
||||
Addr: ":8080",
|
||||
}
|
||||
|
||||
mux.HandleFunc("POST /line", GetLineTrams)
|
||||
|
||||
err := server.ListenAndServe()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func GetLineTrams(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// log.Printf("%+v\n", r)
|
||||
b64Body := base64.NewDecoder(base64.StdEncoding, r.Body)
|
||||
defer r.Body.Close()
|
||||
|
||||
body, _ := io.ReadAll(b64Body)
|
||||
|
||||
stationSeq := strings.Split(string(body), ";")
|
||||
|
||||
tramLine := r.URL.Query().Get("number")
|
||||
|
||||
trams := trmaps.GetTrips()
|
||||
|
||||
var allTrams []types.MapTrip
|
||||
|
||||
for _, t := range trams {
|
||||
if t.Mode == "TRAM" && t.Trips[0].RouteShortName == tramLine {
|
||||
allTrams = append(allTrams, t)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var traminfo = make(map[string]types.MapTrip)
|
||||
|
||||
now := time.Now()
|
||||
for _, tr := range allTrams {
|
||||
// Überschreibe [tripId] nur, wenn (OR)
|
||||
// - [tripId] == nil
|
||||
// - departure < now && arrival > [tripId].departure
|
||||
storedTram := traminfo[tr.Trips[0].TripId]
|
||||
|
||||
if tr.Departure.Sub(now).Abs() < storedTram.Departure.Sub(now).Abs() {
|
||||
// Current is past now, Stored is future now
|
||||
traminfo[tr.Trips[0].TripId] = tr
|
||||
}
|
||||
}
|
||||
|
||||
stationLookupTable := make(map[string]int)
|
||||
for i, v := range stationSeq {
|
||||
stationLookupTable[strings.ToLower(v)] = i
|
||||
}
|
||||
|
||||
activeLights := make(map[int]bool)
|
||||
for _, tram := range slices.Collect(maps.Values(traminfo)) {
|
||||
// Calculate current Station name depending on current time
|
||||
realStation := tram.From
|
||||
if tram.Departure.Sub(now).Abs() > tram.Arrival.Sub(now).Abs() {
|
||||
// log.Printf("> %v, %v\n", now.Sub(depTime), arrTime.Sub(now))
|
||||
realStation = tram.To
|
||||
}
|
||||
// log.Printf("From='%s' To='%s' Real='%s'\n", tram.From.Name, tram.To.Name, realStation.Name)
|
||||
activeLights[stationLookupTable[strings.ToLower(realStation.Name)]] = true
|
||||
|
||||
}
|
||||
|
||||
lightIds := slices.Collect(maps.Keys(activeLights))
|
||||
// log.Println(lightIds)
|
||||
|
||||
enc := json.NewEncoder(w)
|
||||
err := enc.Encode(lightIds)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user