Compare commits

..

3 Commits

Author SHA1 Message Date
b9e742dfe3 add make musl script 2026-07-03 13:38:35 +02:00
96f8cfa55c 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)
2026-07-03 11:41:25 +02:00
3b84fad254 initial commit 2026-06-13 09:11:52 +02:00
8 changed files with 321 additions and 9 deletions

62
cmd/tracker/main.go Normal file
View File

@@ -0,0 +1,62 @@
package main
import (
// "fmt"
"log"
// "maps"
trmaps "shellixa/puvetra/internal/maps"
"shellixa/puvetra/internal/types"
/*"slices"
"strings" */
"time"
)
func main() {
log.Printf("Starting up..\n")
trams := trmaps.GetTrips()
var allTrams []types.MapTrip
for _, t := range trams {
if t.Mode == "TRAM" && t.Trips[0].RouteShortName == "3" {
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
}
}
log.Printf("%+v\n", traminfo)
log.Printf("%d", len(traminfo))
/*
activeLights := make(map[int]bool)
for _, tram := range allTrams {
// Calculate current Station name depending on current time
realStation := tram.From
if now.Sub(depTime) > arrTime.Sub(now) {
// log.Printf("> %v, %v\n", now.Sub(tram.Departure), tram.Arrival.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
}
fmt.Println(slices.Collect(maps.Keys(activeLights)))
*/
}

98
cmd/web/main.go Normal file
View 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)
}
}

3
go.mod Normal file
View File

@@ -0,0 +1,3 @@
module shellixa/puvetra
go 1.26.4

49
internal/maps/trips.go Normal file
View File

@@ -0,0 +1,49 @@
package maps
import (
"encoding/json"
"io"
"log"
"net/http"
"shellixa/puvetra/internal/types"
"time"
)
func GetTrips() []types.MapTrip {
client := http.Client{}
req, err := http.NewRequest("GET", "https://api.transitous.org/api/v1/map/trips", nil)
if err != nil {
log.Fatal(err)
}
q := req.URL.Query()
now := time.Now()
delta := time.Minute * 5
lowerBound := now.Add(-delta).Format(time.RFC3339)
upperBound := now.Add(delta).Format(time.RFC3339)
q.Add("startTime", lowerBound)
q.Add("endTime", upperBound)
q.Add("min", "51.1626,13.5609")
q.Add("max", "50.9973,13.8596")
q.Add("zoom", "14")
q.Add("precision", "2")
req.URL.RawQuery = q.Encode()
response, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
var trips []types.MapTrip
buffer, _ := io.ReadAll(response.Body)
if err = json.Unmarshal(buffer, &trips); err != nil {
log.Fatal(err)
}
log.Printf("component='Transitous API' message='Response was %d bytes'", len(buffer))
return trips
}

View File

@@ -0,0 +1,34 @@
package timetable
import (
"encoding/json"
"fmt"
"log"
"net/http"
"shellixa/puvetra/internal/types"
)
func GetTrip(tripId string) types.Trip {
client := http.Client{}
url := fmt.Sprintf("https://api.transitous.org/api/v6/trip?tripId=%s", tripId)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal(err)
}
response, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
var trip types.Trip
if err := json.NewDecoder(response.Body).Decode(&trip); err != nil {
log.Fatal(err)
}
return trip
}

65
internal/types/types.go Normal file
View File

@@ -0,0 +1,65 @@
package types
import "time"
type TripMeta struct {
TripId string
RouteShortName string
DisplayName string `json:"omitempty"`
}
type MapTrip struct {
Trips []TripMeta
RouteColor string
Mode string
Distance float32
Departure time.Time
Arrival time.Time
From Place
To Place
ScheduledDeparture time.Time
ScheduledArrival time.Time
RealTime bool
Polyline string
}
type Trip struct {
Duration int64
StartTime time.Time
EndTine time.Time
Transfers int
Id string
Legs []TripLeg
}
type TripLeg struct {
Mode string
From Place
To Place
Duration int64
StartTime time.Time
EndTime time.Time
ScheduledStartTime time.Time
ScheduledEndTime time.Time
RealTime bool
Scheduled bool
Distance float32
Headsign string
TripFrom Place
TripTo Place
}
type Place struct {
Name string
StopId string
Lat float32
Lon float32
Arrival time.Time
Departure time.Time
Track string
StopCode string
Description string
Cancelled bool
Modes []string
}

1
scripts/make-musl.sh Executable file
View File

@@ -0,0 +1 @@
CC=/usr/bin/musl-gcc go build -o ./build/web-musl cmd/web/main.go

View File

@@ -6,10 +6,10 @@ if [[ $# -ne 1 ]]; then
fi
if [[ $1 -lt 1 || $1 -gt 13 || $1 -eq 5 ]]; then
>&2 echo "$1 is not a valid Tram in Dresden"
exit 1
fi
#if [[ $1 -lt 1 || $1 -gt 13 || $1 -eq 5 ]]; then
# >&2 echo "$1 is not a valid Tram in Dresden"
# exit 1
#fi
if [[ ! -x "$(command -v jq)" ]]; then
>&2 echo "You need to have 'jq' installed. You can download it here: https://jqlang.org/download/"
@@ -18,16 +18,16 @@ fi
line_num="$1"
current_date=$(date -Iseconds)
window=$(date -Iseconds -d "+10 minutes")
window=$(date -Iseconds -d "+5 minutes")
api="https://api.transitous.org/api"
response=$(curl -s --get \
response=$(curl --get \
--data-urlencode "startTime=$current_date" \
--data-urlencode "endTime=$current_date" \
--data-urlencode "min=51.1626,13.5609" \
--data-urlencode "max=50.9973,13.8596" \
--data-urlencode "zoom=15" \
--data-urlencode "zoom=13" \
--data-urlencode "precision=2" \
"$api/v1/map/trips")
@@ -38,13 +38,13 @@ all_trams=$(jq -c "$query" <<< "$response")
# echo "$all_trams"
tripId=$(jq -r '.[0].trips[0].tripId' <<< "$all_trams")
response=$(curl -s --get \
response=$(curl --get \
--data-urlencode "tripId=$tripId" \
"$api/v6/trip")
trip=$(jq -c '[.legs[0].from] + [.legs[0].intermediateStops[]] + [.legs[0].to] | map({stopId: .stopId, name: .name})' <<< $response)
# jq <<< $trip
# jq -c <<< $trip
currentStops=$(jq -c 'map(.from.stopId[:-2])' <<< "$all_trams")