addejans / ev-routing

2 stars 0 forks source link

Refactor Code and Add Battery Charging Station Logic for EV Route Planning #11

Closed addejans closed 1 week ago

addejans commented 1 week ago

Summary:

This PR includes two major updates:

  1. 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.
  2. 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:

  1. 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.

  2. 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:

How to Test:

  1. Start the Flask app (app.py).
  2. 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:

Example API Response:

{
  "start": [34.05, -118.25],
  "end": [37.77, -122.41],
  "route": [
    {
      "location": [34.05, -118.25],
      "action": "Drive",
      "distance_to_next": 100.0,
      "speed": 60,
      "travel_time_hours": 1.6667
    },
    {
      "location": [36.16, -115.15],
      "action": "Charge",
      "type": "Regular Charger",
      "distance_to_next": 50.0,
      "charging_time_hours": 1.0
    },
    {
      "location": [37.77, -122.41],
      "action": "Drive",
      "distance_to_next": 150.0,
      "speed": 60,
      "travel_time_hours": 2.5
    }
  ]
}

Notes: