Sustainability in .NET: Emission Tracking for Supply Chains

This post dives into building an emissions tracking system in .NET Core for supply chain optimization. Learn to calculate carbon emissions based on distance, vehicle type, and fuel efficiency. Discover how businesses can use this data to monitor and reduce their environmental impact effectively.

Sustainability in .NET: Emission Tracking for Supply Chains

As part of my series on sustainability in .NET, this post focuses on implementing emissions tracking within a supply chain optimization project. Tracking and reducing carbon emissions is critical for businesses striving to meet environmental goals. Let’s explore how you can build a system in .NET Core to calculate and manage emissions for logistics operations.


Why Emissions Matter in Supply Chains

Supply chains are a significant source of carbon emissions, from delivery trucks to last-mile vehicles. Tracking these emissions helps businesses identify high-impact areas and work toward more sustainable practices. With .NET Core, you can create a flexible, scalable solution to monitor and optimize emissions.

Setting Up Emission Factors

Emission factors represent how much CO₂e (carbon dioxide equivalent) a vehicle emits per kilometer. These values differ based on vehicle type and energy sources.

Here’s how to define emission factors:

public static class EmissionFactors
{
    public static readonly Dictionary<string, double> Factors = new Dictionary<string, double>
    {
        { "DieselTruck", 0.26 }, // kg CO₂e per km
        { "ElectricVan", 0.02 },
        { "PetrolCar", 0.21 },
        { "HybridCar", 0.15 }
    };
}

Modeling Vehicles and Routes

Define models to represent vehicles and routes in your system:

public class Vehicle
{
    public string Id { get; set; }
    public string Type { get; set; } // e.g., "DieselTruck", "ElectricVan"
    public double FuelEfficiency { get; set; } // Optional for advanced calculations
}

public class Route
{
    public string RouteId { get; set; }
    public double DistanceInKm { get; set; }
    public Vehicle Vehicle { get; set; } // Assigned vehicle for the route
}

Calculating Carbon Emissions

Here’s a service to calculate emissions for a given route:

public class EmissionCalculator
{
    public double CalculateEmissions(Route route)
    {
        if (EmissionFactors.Factors.TryGetValue(route.Vehicle.Type, out double emissionFactor))
        {
            return route.DistanceInKm * emissionFactor;
        }
        else
        {
            throw new Exception("Unknown vehicle type for emission calculation.");
        }
    }
}

Building an API for Emission Tracking

Expose your emissions calculation as an API endpoint:

[ApiController]
[Route("api/emissions")]
public class EmissionsController : ControllerBase
{
    private readonly EmissionCalculator _emissionCalculator;

    public EmissionsController()
    {
        _emissionCalculator = new EmissionCalculator();
    }

    [HttpPost("calculate")]
    public IActionResult CalculateEmissions([FromBody] Route route)
    {
        try
        {
            double emissions = _emissionCalculator.CalculateEmissions(route);
            return Ok(new { RouteId = route.RouteId, EmissionsInKg = emissions });
        }
        catch (Exception ex)
        {
            return BadRequest(new { Error = ex.Message });
        }
    }
}


Real-World Example: Tracking a Diesel Truck

Here’s how the API works for a 100 km route using a diesel truck:

Request:

POST /api/emissions/calculate
Content-Type: application/json

{
    "RouteId": "R001",
    "DistanceInKm": 100,
    "Vehicle": {
        "Id": "V001",
        "Type": "DieselTruck"
    }
}

Response:

{
    "RouteId": "R001",
    "EmissionsInKg": 26.0
}

Advanced Ideas to Expand Your Project

Add Fuel Efficiency for Precision

Incorporate fuel efficiency to calculate emissions more accurately. For example, diesel emits 2.68 kg CO₂e per liter of fuel consumed:

public double CalculateEmissionsWithFuelEfficiency(Route route)
{
    if (EmissionFactors.Factors.TryGetValue(route.Vehicle.Type, out double emissionFactor))
    {
        double fuelConsumed = route.DistanceInKm / (100 / route.Vehicle.FuelEfficiency);
        return fuelConsumed * 2.68;
    }
    else
    {
        throw new Exception("Unknown vehicle type for emission calculation.");
    }
}


Batch Calculations for a Fleet

Track emissions for an entire fleet by summing up emissions across multiple routes:

public double CalculateTotalEmissions(IEnumerable<Route> routes)
{
    return routes.Sum(route => CalculateEmissions(route));
}

Store Data for Reporting

Save routes and vehicles in a database to enable detailed reporting and long-term tracking:

public class EmissionContext : DbContext
{
    public DbSet Routes { get; set; }
    public DbSet Vehicles { get; set; }
}


With this data, you can create dashboards or integrate machine learning models to predict and optimize emissions.

Wrapping Up: Building Sustainable Solutions in .NET

By implementing carbon emissions tracking in your supply chain optimization project, you’re taking a crucial step toward sustainability. Whether you focus on single routes or entire fleets, this system provides actionable insights to reduce your carbon footprint.


Disclaimer: The views and opinions expressed on this website are solely those of the author and do not necessarily reflect the official policy or position of any employer or organization affiliated with the author.