temp
This commit is contained in:
@@ -26,8 +26,17 @@ type TripDetail struct {
|
||||
Stops []string `json:"stops"`
|
||||
}
|
||||
|
||||
type JSONTrip struct {
|
||||
TripId string `json:"trip_id"`
|
||||
RouteId string `json:"route_id"`
|
||||
AgencyName string `json:"agency_name"`
|
||||
TripHeadsign string `json:"trip_headsign"`
|
||||
TripShortName string `json:"trip_short_name"`
|
||||
}
|
||||
|
||||
type Leg struct {
|
||||
From *types.StopTime `json:"start"`
|
||||
Trip *JSONTrip `json:"trip"`
|
||||
To *types.StopTime `json:"end"`
|
||||
}
|
||||
|
||||
@@ -101,6 +110,64 @@ func main() {
|
||||
json.NewEncoder(w).Encode(stopList)
|
||||
})
|
||||
|
||||
http.HandleFunc("/api/trips", func(w http.ResponseWriter, r *http.Request) {
|
||||
from := r.URL.Query().Get("from")
|
||||
whenStr := r.URL.Query().Get("when")
|
||||
if from == "" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": "from parameter required"})
|
||||
return
|
||||
}
|
||||
stop, ok := tp.Stops[from]
|
||||
if !ok {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": "stop not found"})
|
||||
return
|
||||
}
|
||||
when := time.Now()
|
||||
if whenStr != "" {
|
||||
var err error
|
||||
when, err = time.Parse(time.RFC3339, whenStr)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": "invalid when parameter"})
|
||||
return
|
||||
}
|
||||
}
|
||||
trips := []map[string]interface{}{}
|
||||
for trip := range stop.GetTripsAfter(when) {
|
||||
// Find the index of the stop in the trip
|
||||
startIdx := 0
|
||||
for i, st := range trip.Stops {
|
||||
if st.StopId == from {
|
||||
startIdx = i
|
||||
break
|
||||
}
|
||||
}
|
||||
tripData := map[string]interface{}{
|
||||
"trip_id": trip.TripId,
|
||||
"headsign": trip.TripHeadsign,
|
||||
"short_name": trip.TripShortName,
|
||||
"route_id": trip.RouteID,
|
||||
"agency_id": trip.AgencyID,
|
||||
"agency_name": trip.Route.Agency.AgencyName,
|
||||
"stops": []map[string]interface{}{},
|
||||
}
|
||||
for _, st := range trip.Stops[startIdx:] {
|
||||
tripData["stops"] = append(tripData["stops"].([]map[string]interface{}), map[string]interface{}{
|
||||
"stop_id": st.StopId,
|
||||
"stop_name": st.Stop.StopName,
|
||||
"location": []float64{st.Stop.StopLat, st.Stop.StopLon},
|
||||
"arrival_time": st.ArrivalTime,
|
||||
"departure_time": st.DepartureTime,
|
||||
})
|
||||
}
|
||||
trips = append(trips, tripData)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(trips)
|
||||
})
|
||||
|
||||
http.HandleFunc("/api/route", func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
from := r.URL.Query().Get("from")
|
||||
@@ -125,7 +192,7 @@ func main() {
|
||||
return
|
||||
}
|
||||
log.Printf("using num %v", num)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
log.Printf("start time %v", when)
|
||||
route, err := tp.FindRoute(from, to, when)
|
||||
if err != nil {
|
||||
@@ -133,6 +200,7 @@ func main() {
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": "no route found"})
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(route)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user