Code Refactor for Separation of Concerns: The code has been restructured to separate the HTTP API logic from the business logic, utility functions, and models. This enhances code maintainability and scalability.
Battery Charging Station Logic: Introduced the logic to dynamically plan EV routes based on battery levels and available charging stations. The backend now calculates whether charging stops are required along the route, and how long the charging process will take at each stop.
Changes:
Code Restructure:
app.py: Now only responsible for setting up the Flask app and routing the HTTP requests.
api.py: Contains the route definitions and API logic (moved from app.py).
services/route_service.py: Handles the route planning logic, including calculating battery usage and finding charging stations.
utils/math_utils.py: Provides utility functions for distance calculation and charging time estimation.
models/route.py: Stores the charging station data.
The goal of this restructure is to ensure a clean separation of concerns, making the codebase more modular and easier to maintain as the project grows.
Battery Charging Station Logic:
Introduced logic to determine if the EV requires a charging stop along the route, based on battery range and distance to the destination.
Charging time at each station is now calculated based on the remaining battery percentage and the charging speed of the station.
The route planning algorithm factors in the nearest charging stations when the battery is insufficient to reach the destination.
New Directory Structure:
/app
├── /services
│ ├── route_service.py # Business logic for route planning and battery management
├── /models
│ ├── route.py # Charging station data
├── /utils
│ ├── math_utils.py # Utility functions for calculations
├── api.py # HTTP API for the route endpoint
app.py # Main app file for Flask setup
Key Features:
EV Route Planning: The backend now calculates the best route for an electric vehicle, taking into account battery capacity and charging stations.
Dynamic Charging Stops: If the car’s battery cannot last for the entire trip, the system intelligently selects the nearest charging station and includes it in the route.
Charging Time Calculation: Charging stops now return the estimated charging time based on the battery level and the type of charging station (fast/regular).
Separation of Concerns: Business logic, utilities, and models are now split into separate files, improving maintainability and readability.
How to Test:
Start the Flask app (app.py).
Use a GET request to /route with the following parameters:
start: Latitude and longitude of the starting point (e.g., 34.05,-118.25).
end: Latitude and longitude of the destination point (e.g., 37.77,-122.41).
battery_range: Optional, specifies the battery range of the EV in miles (default is 100 miles).
Example request:
GET /route?start=34.05,-118.25&end=37.77,-122.41&battery_range=100
Expected result:
The response should include the route, along with any necessary charging stops, distances between waypoints, travel times, and charging times.
This refactor sets up a foundation for future features, such as more complex EV routing logic, traffic considerations, and real-world charging station data integration.
Additional utility functions can be added in the future to further enhance the modularity of the system.
Summary:
This PR includes two major updates:
Changes:
Code Restructure:
app.py
: Now only responsible for setting up the Flask app and routing the HTTP requests.api.py
: Contains the route definitions and API logic (moved fromapp.py
).services/route_service.py
: Handles the route planning logic, including calculating battery usage and finding charging stations.utils/math_utils.py
: Provides utility functions for distance calculation and charging time estimation.models/route.py
: Stores the charging station data.The goal of this restructure is to ensure a clean separation of concerns, making the codebase more modular and easier to maintain as the project grows.
Battery Charging Station Logic:
New Directory Structure:
Key Features:
How to Test:
app.py
)./route
with the following parameters:start
: Latitude and longitude of the starting point (e.g.,34.05,-118.25
).end
: Latitude and longitude of the destination point (e.g.,37.77,-122.41
).battery_range
: Optional, specifies the battery range of the EV in miles (default is 100 miles).Example request:
Expected result:
Example API Response:
Notes: