Skip to content

Commit

Permalink
group range and percent in fuel type
Browse files Browse the repository at this point in the history
  • Loading branch information
JustCris654 committed Dec 9, 2024
1 parent 4e591cb commit 28944ba
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.opentripplanner.apis.gtfs.generated.GraphQLDataFetchers;
import org.opentripplanner.service.vehiclerental.model.RentalVehicleFuel;
import org.opentripplanner.service.vehiclerental.model.RentalVehicleType;
import org.opentripplanner.service.vehiclerental.model.VehicleRentalStationUris;
import org.opentripplanner.service.vehiclerental.model.VehicleRentalSystem;
Expand All @@ -17,16 +18,8 @@ public DataFetcher<Boolean> allowPickupNow() {
}

@Override
public DataFetcher<Double> currentFuelPercent() {
return environment -> getSource(environment).getCurrentFuelPercent();
}

@Override
public DataFetcher<Integer> currentRangeMeters() {
return environment ->
getSource(environment).getCurrentRange() != null
? getSource(environment).getCurrentRange().toMeters()
: null;
public DataFetcher<RentalVehicleFuel> fuel() {
return environment -> getSource(environment).getFuel();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.opentripplanner.service.realtimevehicles.model.RealtimeVehicle;
import org.opentripplanner.service.realtimevehicles.model.RealtimeVehicle.StopRelationship;
import org.opentripplanner.service.vehiclerental.model.RentalVehicleEntityCounts;
import org.opentripplanner.service.vehiclerental.model.RentalVehicleFuel;
import org.opentripplanner.service.vehiclerental.model.RentalVehicleType;
import org.opentripplanner.service.vehiclerental.model.RentalVehicleTypeCount;
import org.opentripplanner.service.vehiclerental.model.VehicleRentalPlace;
Expand Down Expand Up @@ -853,9 +854,7 @@ public interface GraphQLRentalPlace extends TypeResolver {}
public interface GraphQLRentalVehicle {
public DataFetcher<Boolean> allowPickupNow();

public DataFetcher<Double> currentFuelPercent();

public DataFetcher<Integer> currentRangeMeters();
public DataFetcher<RentalVehicleFuel> fuel();

public DataFetcher<graphql.relay.Relay.ResolvedGlobalId> id();

Expand Down Expand Up @@ -884,6 +883,13 @@ public interface GraphQLRentalVehicleEntityCounts {
public DataFetcher<Integer> total();
}

/** Rental vehicle fuel represent the current status of the battery or fuel of a rental vehicle */
public interface GraphQLRentalVehicleFuel {
public DataFetcher<Double> percent();

public DataFetcher<Integer> range();
}

public interface GraphQLRentalVehicleType {
public DataFetcher<org.opentripplanner.apis.gtfs.generated.GraphQLTypes.GraphQLFormFactor> formFactor();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,5 @@ config:
RiderCategory: org.opentripplanner.model.fare.RiderCategory#RiderCategory
StopPosition: org.opentripplanner.apis.gtfs.model.StopPosition#StopPosition
RentalPlace: org.opentripplanner.service.vehiclerental.model.VehicleRentalPlace#VehicleRentalPlace
RentalVehicleFuel: org.opentripplanner.service.vehiclerental.model.RentalVehicleFuel#RentalVehicleFuel

Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ public static GraphQLObjectType create(
.newFieldDefinition()
.name("currentRangeMeters")
.type(Scalars.GraphQLFloat)
.dataFetcher(environment -> ((VehicleRentalVehicle) environment.getSource()).currentRange)
.dataFetcher(environment ->
((VehicleRentalVehicle) environment.getSource()).getFuel().getRange()
)
.build()
)
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.opentripplanner.service.vehiclerental.model;

import javax.annotation.Nullable;
import org.opentripplanner.transit.model.basic.Distance;

/**
* Contains information about the current battery or fuel status.
* See the <a href="https://github.com/MobilityData/gbfs/blob/v3.0/gbfs.md#vehicle_statusjson">GBFS
* vehicle_status specification</a> for more details.
*/
public class RentalVehicleFuel {

/**
* Current fuel percentage, expressed from 0 to 1.
* <p>
* May be {@code null}.
*/
@Nullable
public final Double percent;

/**
* Distance that the vehicle can travel with the current charge or fuel.
* <p>
* May be {@code null}.
*/
@Nullable
public final Distance range;

public RentalVehicleFuel(@Nullable Double fuelPercent, @Nullable Distance range) {
this.percent = fuelPercent;
this.range = range;
}

@Nullable
public Double getPercent() {
return percent;
}

@Nullable
public Integer getRange() {
return this.range != null ? this.range.toMeters() : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import java.time.Instant;
import java.util.Set;
import javax.annotation.Nullable;
import org.opentripplanner.framework.i18n.I18NString;
import org.opentripplanner.street.model.RentalFormFactor;
import org.opentripplanner.transit.model.basic.Distance;
import org.opentripplanner.transit.model.framework.FeedScopedId;

/**
Expand All @@ -24,10 +22,9 @@ public class VehicleRentalVehicle implements VehicleRentalPlace {
public boolean isReserved = false;
public boolean isDisabled = false;
public Instant lastReported;
public Distance currentRange;
public VehicleRentalStation station;
public String pricingPlanId;
public Double currentFuelPercent;
public RentalVehicleFuel fuel;

@Override
public FeedScopedId getId() {
Expand Down Expand Up @@ -137,11 +134,7 @@ public VehicleRentalSystem getVehicleRentalSystem() {
return system;
}

public Double getCurrentFuelPercent() {
return currentFuelPercent;
}

public Distance getCurrentRange() {
return this.currentRange;
public RentalVehicleFuel getFuel() {
return fuel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.mobilitydata.gbfs.v2_3.free_bike_status.GBFSBike;
import org.mobilitydata.gbfs.v2_3.free_bike_status.GBFSRentalUris;
import org.opentripplanner.framework.i18n.NonLocalizedString;
import org.opentripplanner.service.vehiclerental.model.RentalVehicleFuel;
import org.opentripplanner.service.vehiclerental.model.RentalVehicleType;
import org.opentripplanner.service.vehiclerental.model.VehicleRentalStationUris;
import org.opentripplanner.service.vehiclerental.model.VehicleRentalSystem;
Expand Down Expand Up @@ -53,11 +54,12 @@ public VehicleRentalVehicle mapFreeVehicleStatus(GBFSBike vehicle) {
vehicle.getLastReported() != null
? Instant.ofEpochSecond((long) (double) vehicle.getLastReported())
: null;
rentalVehicle.currentFuelPercent = vehicle.getCurrentFuelPercent();
rentalVehicle.currentRange =
RentalVehicleFuel fuel = new RentalVehicleFuel(
vehicle.getCurrentFuelPercent(),
vehicle.getCurrentRangeMeters() != null
? Distance.ofMeters(vehicle.getCurrentRangeMeters())
: null;
: null
);
rentalVehicle.pricingPlanId = vehicle.getPricingPlanId();
GBFSRentalUris rentalUris = vehicle.getRentalUris();
if (rentalUris != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1801,10 +1801,8 @@ type RealTimeEstimate {
type RentalVehicle implements Node & PlaceInterface {
"If true, vehicle is currently available for renting."
allowPickupNow: Boolean
"Fuel or battery power remaining in the vehicle. Expressed from 0 to 1."
currentFuelPercent: Ratio
"Range in meters that the vehicle can travel with the current charge or fuel."
currentRangeMeters: Int
"Fuel or battery status of the rental vehicle"
fuel: RentalVehicleFuel
"Global object ID provided by Relay. This value can be used to refetch this object using **node** query."
id: ID!
"Latitude of the vehicle (WGS 84)"
Expand Down Expand Up @@ -1834,6 +1832,14 @@ type RentalVehicleEntityCounts {
total: Int!
}

"Rental vehicle fuel represent the current status of the battery or fuel of a rental vehicle"
type RentalVehicleFuel {
"Fuel or battery power remaining in the vehicle. Expressed from 0 to 1."
percent: Ratio
"Range in meters that the vehicle can travel with the current charge or fuel."
range: Int
}

type RentalVehicleType {
"The vehicle's general form factor"
formFactor: FormFactor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,6 @@ public TestFreeFloatingRentalVehicleBuilder withVehicleCar() {
return buildVehicleType(RentalFormFactor.CAR);
}

private TestFreeFloatingRentalVehicleBuilder buildVehicleType(RentalFormFactor rentalFormFactor) {
this.vehicleType =
new RentalVehicleType(
new FeedScopedId(TestFreeFloatingRentalVehicleBuilder.NETWORK_1, rentalFormFactor.name()),
rentalFormFactor.name(),
rentalFormFactor,
RentalVehicleType.PropulsionType.ELECTRIC,
100000d
);
return this;
}

public VehicleRentalVehicle build() {
var vehicle = new VehicleRentalVehicle();
var stationName = "free-floating-" + vehicleType.formFactor.name().toLowerCase();
Expand All @@ -106,9 +94,23 @@ public VehicleRentalVehicle build() {
vehicle.longitude = longitude;
vehicle.vehicleType = vehicleType;
vehicle.system = system;
vehicle.currentFuelPercent = currentFuelPercent;
vehicle.currentRange =
currentRangeMeters != null ? Distance.ofMeters(currentRangeMeters) : null;
vehicle.fuel =
new RentalVehicleFuel(
currentFuelPercent,
currentRangeMeters != null ? Distance.ofMeters(currentRangeMeters) : null
);
return vehicle;
}

private TestFreeFloatingRentalVehicleBuilder buildVehicleType(RentalFormFactor rentalFormFactor) {
this.vehicleType =
new RentalVehicleType(
new FeedScopedId(TestFreeFloatingRentalVehicleBuilder.NETWORK_1, rentalFormFactor.name()),
rentalFormFactor.name(),
rentalFormFactor,
RentalVehicleType.PropulsionType.ELECTRIC,
100000d
);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
"networkId": "Network-1",
"url": "https://foo.bar"
},
"currentFuelPercent": 0.5,
"currentRangeMeters": 5501
"fuel": {
"percent": 0.5,
"range": 5501
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@
"networkId": "Network-1",
"url": "https://foo.bar"
},
"currentFuelPercent": 0.5,
"currentRangeMeters": 5501
"fuel": {
"percent": 0.5,
"range": 5501
}
},
{
"__typename": "RentalVehicle",
Expand All @@ -97,8 +99,10 @@
"networkId": "Network-2",
"url": "https://foo.bar.baz"
},
"currentFuelPercent": null,
"currentRangeMeters": null
"fuel": {
"percent": null,
"range": null
}
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
networkId
url
}
currentFuelPercent
currentRangeMeters
fuel {
percent
range
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
networkId
url
}
currentFuelPercent
currentRangeMeters
fuel {
percent
range
}
}
... on VehicleRentalStation {
stationId
Expand Down

0 comments on commit 28944ba

Please sign in to comment.