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) { /// /// Latitude in decimal degrees. Valid range: -90 to 90. /// public double Latitude { get; set; } = latitude; /// /// Longitude in decimal degrees. Valid range: -180 to 180. /// public double Longitude { get; set; } = longitude; /// /// Generates a random geographic location. /// If a region is specified, limits the random coordinates within that region. /// /// Optional region: "europe", "asia", "north-america", "south-america", "africa", "australia", or "world" (default). 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}"; } } }