tableau ordonné par le plein le moins chere avec le trajet

This commit is contained in:
David Manuguerra
2026-04-05 22:28:58 +02:00
parent e51b52eb75
commit b29d7a6a2d
5 changed files with 168 additions and 71 deletions

View File

@@ -3,6 +3,7 @@ using ApiCarburant.Services;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.VisualBasic;
namespace ApiCarburant.Controllers; namespace ApiCarburant.Controllers;
@@ -10,6 +11,19 @@ namespace ApiCarburant.Controllers;
[Route("[controller]")] [Route("[controller]")]
public class CarburantController : ControllerBase public class CarburantController : ControllerBase
{ {
public record ResultatStation(
string nom,
double distance,
decimal? diesel,
decimal? e85,
decimal? e5,
decimal? sp98)
{
public override string ToString()
{
return $"{{ nom = {nom}, distance = {distance}, diesel = {diesel}, e85 = {e85} }}";
}
}
/// <summary> /// <summary>
/// Retrieves a collection of fuel stations located near the specified geographic coordinates. /// Retrieves a collection of fuel stations located near the specified geographic coordinates.
@@ -24,29 +38,34 @@ public class CarburantController : ControllerBase
/// ?lon=4.816992463720462&lat=43.9291477523281 /// ?lon=4.816992463720462&lat=43.9291477523281
/// </example> /// </example>
[HttpGet(Name = "GetCarburant")] [HttpGet(Name = "GetCarburant")]
public async Task<IEnumerable<Station>> Get([FromServices] PompeService pompeService, public async Task<IEnumerable<ResultatStation>> Get([FromServices] PompeService pompeService,
[FromQuery] double? lon, [FromQuery] double? lat, [FromQuery] long dist) [FromQuery] double? lon, [FromQuery] double? lat, [FromQuery] long? dist, [FromQuery] string? carburant = "E85")
{ {
dist = dist == 0 ? 10000 : dist; dist = dist ?? 25;
double lon1 = lon ?? 4.816992463720462; double lon1 = lon ?? 4.816992463720462;
double lat1 = lat ?? 43.9291477523281; double lat1 = lat ?? 43.9291477523281;
if (pompeService.Station == null) if (!pompeService.Station.Any())
{ await pompeService.RefreshAsync(); } {
return pompeService.Station.Select(s => new { distance = 110574 * Math.Sqrt((s.Latitude - lat1) * (s.Latitude - lat1) + (s.Longitude - lon1) * (s.Longitude - lon1)), station = s }) await pompeService.RefreshAsync();
.Where(x => x.distance < dist) }
.OrderBy(x => x.distance) return pompeService.Station.Select(s => new { distance = 110.574 * Math.Sqrt((s.Latitude - lat1) * (s.Latitude - lat1) + (s.Longitude - lon1) * (s.Longitude - lon1)), station = s })
.Select(x => x.station) .Where(x => x.distance < dist && x.station.Prix.Any(p => p.nom == carburant))
.OrderBy(x => x.station.E85 * 50m + (decimal)x.distance * (2m * 0.2m * x.station.E85.Value + 2m))
.Select(x =>
new ResultatStation(x.station.Ville + x.station.Adresse, x.distance, x.station.Gazole, x.station.E85, x.station.E5, x.station.SP98)
)
.ToList(); .ToList();
// .OrderBy(x => x.Prix.OrderBy(p => p.Valeur).Select(p => p.Valeur).FirstOrDefault()??15).ToList(); }
//throw new Exception("Test exception"); [HttpGet("nom", Name = "GetNomCarburant")]
//return Enumerable.Range(1, 5).Select(index => new WeatherForecast public async Task<IEnumerable<String>> GetNomCarburant([FromServices] PompeService pompeService)
//{ {
// Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), if (!pompeService.Station.Any())
// TemperatureC = Random.Shared.Next(-20, 55), {
// Summary = Summaries[Random.Shared.Next(Summaries.Length)] await pompeService.RefreshAsync();
//}) }
//.ToArray(); return pompeService.Station.SelectMany(x => x.Prix).Select(p => p.nom).Distinct();
} }
[HttpGet("refresh", Name = "GetRefreshCarburant")] [HttpGet("refresh", Name = "GetRefreshCarburant")]
public async Task<IActionResult> Refresh([FromServices] PompeService pompeService) public async Task<IActionResult> Refresh([FromServices] PompeService pompeService)

View File

@@ -2,16 +2,10 @@ namespace ApiCarburant
{ {
public class Prix public class Prix
{ {
public string Id { get; set; }
public string? nom { get; set; } public string? nom { get; set; }
public decimal Valeur { get; set; } public decimal Valeur { get; set; }
public DateTime Maj { get; set; } public DateTime Maj { get; set; }
} }
public enum TypeCarburant
{
E85,
SP95,
}
} }

View File

@@ -1,17 +1,27 @@
namespace ApiCarburant; namespace ApiCarburant;
public class Station public class StationSansPrix
{ {
public string Id { get; set; } public string Id { get; set; }
public double Latitude { get; set; } public double Latitude { get; set; }
public double Longitude { get; set; } public double Longitude { get; set; }
public string cp { get; set; }
public string Ville { get; set; } public string Ville { get; set; }
public string Adresse { get; set; } public string Adresse { get; set; }
public decimal? E85 { get; set; }
public decimal? E5 { get; set; }
public decimal? E10 { get; set; }
public decimal? SP98 { get; set; }
public decimal? Gazole { get; set; }
}
public class Station : StationSansPrix
{
public List<Prix> Prix { get; set; } = new(); public List<Prix> Prix { get; set; } = new();
public StationSansPrix WithoutListPrix()
{
return this;
}
} }

View File

@@ -51,7 +51,6 @@ namespace ApiCarburant.Services
while (await reader.ReadAsync()) while (await reader.ReadAsync())
{ {
if (reader.Name == "pdv") if (reader.Name == "pdv")
{ {
if (reader.NodeType == XmlNodeType.Element) if (reader.NodeType == XmlNodeType.Element)
@@ -62,7 +61,6 @@ namespace ApiCarburant.Services
Latitude = double.Parse(reader.GetAttribute("latitude")) / 100000, Latitude = double.Parse(reader.GetAttribute("latitude")) / 100000,
Longitude = double.Parse(reader.GetAttribute("longitude")) / 100000, Longitude = double.Parse(reader.GetAttribute("longitude")) / 100000,
}; };
} }
else if (reader.NodeType == XmlNodeType.EndElement) else if (reader.NodeType == XmlNodeType.EndElement)
@@ -95,14 +93,30 @@ namespace ApiCarburant.Services
Maj = DateTime.Parse(reader.GetAttribute("maj")) Maj = DateTime.Parse(reader.GetAttribute("maj"))
}; };
currentStation.Prix.Add(prix); currentStation.Prix.Add(prix);
} if (prix.nom == "Gazole")
else
{ {
currentStation.Gazole = prix.Valeur;
}
else if (prix.nom == "E5")
{
currentStation.E5 = prix.Valeur;
}
else if (prix.nom == "E10")
{
currentStation.E10 = prix.Valeur;
}
else if (prix.nom == "SP98")
{
currentStation.SP98 = prix.Valeur;
}
else if (prix.nom == "E85")
{
currentStation.E85 = prix.Valeur;
}
} }
} }
} }
this.Station = stations; this.Station = stations;
@@ -129,6 +143,10 @@ namespace ApiCarburant.Services
//}; //};
} }
public List<Station> Station { get; set; } public List<Station> Station
{
get => field;
set => field = value;
} = [];
} }
} }

View File

@@ -6,12 +6,35 @@
</head> </head>
<body> <body>
<h1>Stations autour de moi</h1> <h1>Stations autour de moi</h1>
<p>Coucou</p> <input type="range" min="5" max="150" value="10" id="distanceSlider">
<button onclick="getLocation()">Localiser</button> <span id="distanceValue">10 km</span>
<pre id="result"></pre> <button onclick="getLocation()">Localiser</button>
<table id="stationsTable" border="1">
<thead>
<tr>
<th onclick="sortTable(0)">Nom</th>
<th onclick="sortTable(1)">Distance</th>
<th onclick="sortTable(2)">Diesel</th>
<th onclick="sortTable(3)">E85</th>
<th onclick="sortTable(4)">E5</th>
<th onclick="sortTable(3)">SP98</th>
</tr>
</thead>
<tbody></tbody>
</table>
<script>async function getLocation() { <pre id="result"></pre>
<script>
const slider = document.getElementById("distanceSlider");
const value = document.getElementById("distanceValue");
slider.oninput = () => {
value.innerText = slider.value + " km";
};
async function getLocation() {
if (!navigator.geolocation) { if (!navigator.geolocation) {
alert("Géolocalisation non supportée"); alert("Géolocalisation non supportée");
return; return;
@@ -23,23 +46,56 @@
return; return;
} }
navigator.geolocation.getCurrentPosition(async (pos) => { navigator.geolocation.getCurrentPosition(async (pos) => {
const lat = pos.coords.latitude; const lat = pos.coords.latitude;
const lon = pos.coords.longitude; const lon = pos.coords.longitude;
const distance = document.getElementById("distanceSlider").value;
try { try {
const res = await fetch(`/carburant?lat=${lat}&lon=${lon}`); const res = await fetch(`/carburant?lat=${lat}&lon=${lon}&dist=${distance}`);
const data = await res.json(); const data = await res.json();
document.getElementById("result").textContent = const tbody = document.querySelector("#stationsTable tbody");
JSON.stringify(data, null, 2); tbody.innerHTML = "";
data.forEach(s => {
const row = `
<tr>
<td>${s.nom}</td>
<td>${s.distance.toFixed(2)} km</td>
<td>${s.diesel ?? "-"}</td>
<td>${s.e85 ?? "-"}</td>
<td>${s.E5 ?? "-"}</td>
<td>${s.sp98 ?? "-"}</td>
</tr>`;
tbody.innerHTML += row;
});
//document.getElementById("result").textContent =
// JSON.stringify(data, null, 2);
} catch (e) { } catch (e) {
console.error(e); console.error(e);
} }
}, (err) => { }, (err) => {
alert("Erreur géolocalisation: " + err.message); alert("Erreur géolocalisation: " + err.message);
}); });
function sortTable(colIndex) {
const table = document.getElementById("stationsTable");
const rows = Array.from(table.rows).slice(1);
const asc = table.getAttribute("data-sort") !== "asc";
table.setAttribute("data-sort", asc ? "asc" : "desc");
rows.sort((a, b) => {
const A = a.cells[colIndex].innerText;
const B = b.cells[colIndex].innerText;
return asc
? A.localeCompare(B, undefined, { numeric: true })
: B.localeCompare(A, undefined, { numeric: true });
});
rows.forEach(r => table.appendChild(r));
}
}</script> }</script>
</body> </body>
</html> </html>