tableau ordonné par le plein le moins chere avec le trajet
This commit is contained in:
@@ -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)
|
||||||
|
|||||||
@@ -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,
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -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)
|
||||||
@@ -61,7 +60,6 @@ namespace ApiCarburant.Services
|
|||||||
Id = reader.GetAttribute("id"),
|
Id = reader.GetAttribute("id"),
|
||||||
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,
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -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;
|
||||||
|
} = [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,43 +3,99 @@
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
<title>Stations carburant</title>
|
<title>Stations carburant</title>
|
||||||
|
|
||||||
</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>
|
||||||
if (!navigator.geolocation) {
|
|
||||||
alert("Géolocalisation non supportée");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const perm = await navigator.permissions.query({ name: "geolocation" });
|
|
||||||
|
|
||||||
if (perm.state === "denied") {
|
<script>
|
||||||
alert("Veuillez autoriser la localisation dans les paramètres du navigateur");
|
const slider = document.getElementById("distanceSlider");
|
||||||
return;
|
const value = document.getElementById("distanceValue");
|
||||||
}
|
|
||||||
|
|
||||||
|
slider.oninput = () => {
|
||||||
|
value.innerText = slider.value + " km";
|
||||||
|
};
|
||||||
|
|
||||||
navigator.geolocation.getCurrentPosition(async (pos) => {
|
async function getLocation() {
|
||||||
const lat = pos.coords.latitude;
|
if (!navigator.geolocation) {
|
||||||
const lon = pos.coords.longitude;
|
alert("Géolocalisation non supportée");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const perm = await navigator.permissions.query({ name: "geolocation" });
|
||||||
|
|
||||||
try {
|
if (perm.state === "denied") {
|
||||||
const res = await fetch(`/carburant?lat=${lat}&lon=${lon}`);
|
alert("Veuillez autoriser la localisation dans les paramètres du navigateur");
|
||||||
const data = await res.json();
|
return;
|
||||||
document.getElementById("result").textContent =
|
}
|
||||||
JSON.stringify(data, null, 2);
|
|
||||||
} catch (e) {
|
navigator.geolocation.getCurrentPosition(async (pos) => {
|
||||||
console.error(e);
|
const lat = pos.coords.latitude;
|
||||||
}
|
const lon = pos.coords.longitude;
|
||||||
}, (err) => {
|
const distance = document.getElementById("distanceSlider").value;
|
||||||
alert("Erreur géolocalisation: " + err.message);
|
|
||||||
});
|
try {
|
||||||
}</script>
|
const res = await fetch(`/carburant?lat=${lat}&lon=${lon}&dist=${distance}`);
|
||||||
|
const data = await res.json();
|
||||||
|
const tbody = document.querySelector("#stationsTable tbody");
|
||||||
|
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) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
}, (err) => {
|
||||||
|
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>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
Reference in New Issue
Block a user