Files

79 lines
2.5 KiB
C#

using Imagoverum.FleetManagement.BizLogic;
using Matrix42.Blob.Contracts;
using Matrix42.WebApi.Contracts;
using System.Runtime.InteropServices;
namespace Imagoverum.FleetManagement.Services
{
[RoutePrefix("api/car")]
public class CarController(IBlobManager blobManager) : ApiController
{
[HttpGet, Route("geoposition")]
public GeoLocation GetLocation(string region = "world")
{
return GeoLocation.GetRandom(region);
}
[HttpPost, Route("test")]
public string TestCar()
{
var blobState = blobManager.IsBlobConfigured()
? "With Configured Cloud"
: "Without Configured Cloud";
return CarTester.IsCarWorkingProperly()
? $"Car test successful completed ({blobState}) on {RuntimeInformation.OSDescription} environment at " + DateTime.UtcNow.ToString("o")
: $"Car test failed ({blobState}) on {RuntimeInformation.OSDescription} environment at " + DateTime.UtcNow.ToString("o");
}
[Route("error")]
[HttpGet]
public IHttpActionResult Throw()
{
throw new Exception("Boom! Unhandled!");
}
public class GeoLocation(double latitude, double longitude)
{
/// <summary>
/// Latitude in decimal degrees. Valid range: -90 to 90.
/// </summary>
public double Latitude { get; set; } = latitude;
/// <summary>
/// Longitude in decimal degrees. Valid range: -180 to 180.
/// </summary>
public double Longitude { get; set; } = longitude;
/// <summary>
/// Generates a random geographic location.
/// If a region is specified, limits the random coordinates within that region.
/// </summary>
/// <param name="region">Optional region: "europe", "asia", "north-america", "south-america", "africa", "australia", or "world" (default).</param>
public static GeoLocation GetRandom(string region = "world")
{
var rnd = new Random();
// Define approximate bounding boxes for major regions
(double latMin, double latMax, double lonMin, double lonMax) bounds = region.ToLower() switch
{
"europe" => (35, 71, -25, 45),
"asia" => (-10, 81, 25, 180),
"north-america" => (5, 83, -170, -30),
"south-america" => (-56, 13, -82, -34),
"africa" => (-35, 37, -18, 52),
"australia" => (-47, -10, 110, 155),
_ => (-90, 90, -180, 180) // whole world
};
double latitude = rnd.NextDouble() * (bounds.latMax - bounds.latMin) + bounds.latMin;
double longitude = rnd.NextDouble() * (bounds.lonMax - bounds.lonMin) + bounds.lonMin;
return new GeoLocation(latitude, longitude);
}
public override string ToString() => $"Lat: {Latitude:F5}, Lon: {Longitude:F5}";
}
}
}