diff --git a/cmd/main.go b/cmd/main.go index 1f7bc49..6ecb88c 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -7,6 +7,7 @@ import ( "net/http" "os" "slices" + "strconv" pb "github.com/cjdenio/uta-trax-api/proto" "google.golang.org/protobuf/proto" @@ -41,7 +42,7 @@ func getVehicles() ([]*pb.VehiclePosition, error) { type TripInfo struct { Line pb.VehicleFeed_Line - Direction int + Direction int32 } var trips = make(map[string]*TripInfo) @@ -61,12 +62,17 @@ func loadTrips() error { route_id := slices.Index(header, "route_id") trip_id := slices.Index(header, "trip_id") - // direction_id := slices.Index(header, "direction_id") + direction_id := slices.Index(header, "direction_id") for { if record, err := r.Read(); err == nil { trip_info := new(TripInfo) + direction, err := strconv.ParseInt(record[direction_id], 10, 32) + if err == nil { + trip_info.Direction = int32(direction) + } + switch record[route_id] { case "8246": trip_info.Line = pb.VehicleFeed_RED @@ -80,6 +86,9 @@ func loadTrips() error { case "45389": trip_info.Line = pb.VehicleFeed_STREETCAR trips[record[trip_id]] = trip_info + case "41065": + trip_info.Line = pb.VehicleFeed_FRONTRUNNER + trips[record[trip_id]] = trip_info } } else { break @@ -90,7 +99,6 @@ func loadTrips() error { } func feedifyVehicles(vehicles []*pb.VehiclePosition) pb.VehicleFeed { - vehicle_feed := make([]*pb.VehicleFeed_Vehicle, 0, len(vehicles)) for _, vehicle := range vehicles { @@ -100,9 +108,11 @@ func feedifyVehicles(vehicles []*pb.VehiclePosition) pb.VehicleFeed { } vehicle_feed = append(vehicle_feed, &pb.VehicleFeed_Vehicle{ - Lat: *vehicle.Position.Latitude, - Lon: *vehicle.Position.Longitude, - Line: trip.Line, + Lat: *vehicle.Position.Latitude, + Lon: *vehicle.Position.Longitude, + Line: trip.Line, + Id: *vehicle.Vehicle.Id, + Direction: trip.Direction, }) } @@ -116,7 +126,9 @@ func main() { log.Fatalln(err) } - http.DefaultServeMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + http.Handle("/", http.FileServer(http.Dir("./static"))) + + http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) { vehicles, err := getVehicles() if err != nil { log.Fatalln(err) diff --git a/proto/schema.pb.go b/proto/schema.pb.go index eb900b6..697514d 100644 --- a/proto/schema.pb.go +++ b/proto/schema.pb.go @@ -29,6 +29,7 @@ const ( VehicleFeed_RED VehicleFeed_Line = 2 VehicleFeed_BLUE VehicleFeed_Line = 3 VehicleFeed_STREETCAR VehicleFeed_Line = 4 + VehicleFeed_FRONTRUNNER VehicleFeed_Line = 5 ) // Enum value maps for VehicleFeed_Line. @@ -39,6 +40,7 @@ var ( 2: "RED", 3: "BLUE", 4: "STREETCAR", + 5: "FRONTRUNNER", } VehicleFeed_Line_value = map[string]int32{ "LINE_UNSPECIFIED": 0, @@ -46,6 +48,7 @@ var ( "RED": 2, "BLUE": 3, "STREETCAR": 4, + "FRONTRUNNER": 5, } ) @@ -126,6 +129,7 @@ type VehicleFeed_Vehicle struct { Lon float32 `protobuf:"fixed32,2,opt,name=lon,proto3" json:"lon,omitempty"` Line VehicleFeed_Line `protobuf:"varint,3,opt,name=line,proto3,enum=VehicleFeed_Line" json:"line,omitempty"` Direction int32 `protobuf:"varint,4,opt,name=direction,proto3" json:"direction,omitempty"` + Id string `protobuf:"bytes,5,opt,name=id,proto3" json:"id,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -188,24 +192,33 @@ func (x *VehicleFeed_Vehicle) GetDirection() int32 { return 0 } +func (x *VehicleFeed_Vehicle) GetId() string { + if x != nil { + return x.Id + } + return "" +} + var File_proto_schema_proto protoreflect.FileDescriptor const file_proto_schema_proto_rawDesc = "" + "\n" + - "\x12proto/schema.proto\"\xfe\x01\n" + + "\x12proto/schema.proto\"\xa0\x02\n" + "\vVehicleFeed\x120\n" + - "\bvehicles\x18\x01 \x03(\v2\x14.VehicleFeed.VehicleR\bvehicles\x1ar\n" + + "\bvehicles\x18\x01 \x03(\v2\x14.VehicleFeed.VehicleR\bvehicles\x1a\x82\x01\n" + "\aVehicle\x12\x10\n" + "\x03lat\x18\x01 \x01(\x02R\x03lat\x12\x10\n" + "\x03lon\x18\x02 \x01(\x02R\x03lon\x12%\n" + "\x04line\x18\x03 \x01(\x0e2\x11.VehicleFeed.LineR\x04line\x12\x1c\n" + - "\tdirection\x18\x04 \x01(\x05R\tdirection\"I\n" + + "\tdirection\x18\x04 \x01(\x05R\tdirection\x12\x0e\n" + + "\x02id\x18\x05 \x01(\tR\x02id\"Z\n" + "\x04Line\x12\x14\n" + "\x10LINE_UNSPECIFIED\x10\x00\x12\t\n" + "\x05GREEN\x10\x01\x12\a\n" + "\x03RED\x10\x02\x12\b\n" + "\x04BLUE\x10\x03\x12\r\n" + - "\tSTREETCAR\x10\x04B'Z%github.com/cjdenio/uta-trax-api/protob\x06proto3" + "\tSTREETCAR\x10\x04\x12\x0f\n" + + "\vFRONTRUNNER\x10\x05B'Z%github.com/cjdenio/uta-trax-api/protob\x06proto3" var ( file_proto_schema_proto_rawDescOnce sync.Once diff --git a/proto/schema.proto b/proto/schema.proto index 811315e..341fe21 100644 --- a/proto/schema.proto +++ b/proto/schema.proto @@ -8,6 +8,7 @@ message VehicleFeed { RED = 2; BLUE = 3; STREETCAR = 4; + FRONTRUNNER = 5; } message Vehicle { @@ -15,6 +16,7 @@ message VehicleFeed { float lon = 2; Line line = 3; int32 direction = 4; + string id = 5; } repeated Vehicle vehicles = 1; diff --git a/static/index.html b/static/index.html new file mode 100644 index 0000000..88c3dac --- /dev/null +++ b/static/index.html @@ -0,0 +1,67 @@ + + + + + + + + + + +
+ + + + + + +