From 46090498016938da469365490865435bdd1ecf50 Mon Sep 17 00:00:00 2001 From: Grimmgockel Date: Tue, 23 May 2023 16:52:05 +0200 Subject: [PATCH] rename MockData into CarData --- .../ConsoleApp1/clients/CarClient.cs | 42 ++++++++--------- .../behaviour/CruiserClientBehaviour.cs | 36 +++++++-------- .../clients/behaviour/ICarClientBehaviour.cs | 10 ++-- .../behaviour/PgsParkerClientBehaviour.cs | 38 +++++++-------- .../behaviour/RandomParkerClientBehaviour.cs | 46 +++++++++---------- .../sim/{MockCar.cs => CarData.cs} | 4 +- 6 files changed, 88 insertions(+), 88 deletions(-) rename src/Solution1/ConsoleApp1/sim/{MockCar.cs => CarData.cs} (95%) diff --git a/src/Solution1/ConsoleApp1/clients/CarClient.cs b/src/Solution1/ConsoleApp1/clients/CarClient.cs index aa733b3..5720743 100644 --- a/src/Solution1/ConsoleApp1/clients/CarClient.cs +++ b/src/Solution1/ConsoleApp1/clients/CarClient.cs @@ -15,14 +15,14 @@ protected CarClient(IMqttClient mqttClient, ICarClientBehaviour behaviour, Behaviour = behaviour; CallCount = 0; - Car = new MockCar(id, world, pgs, logging); + CarData = new CarData(id, world, pgs, logging); Pgs = pgs; // get initial dest - Car.Status = CarStatus.Driving; - Car.ResetKpiMetrics(); - Behaviour.UpdateDestination(Car); + CarData.Status = CarStatus.Driving; + CarData.ResetKpiMetrics(); + Behaviour.UpdateDestination(CarData); } public int CallCount { get; set; } @@ -33,7 +33,7 @@ protected CarClient(IMqttClient mqttClient, ICarClientBehaviour behaviour, private ICarClientBehaviour Behaviour { get; set; } - public MockCar Car { get; set; } + public CarData CarData { get; set; } /* * Creation through factory @@ -50,45 +50,45 @@ public static async Task Create(MqttClientFactory clientFactory, ICar */ protected override async Task MqttClientOnApplicationMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs arg) { - switch (Car.Status) + switch (CarData.Status) { case CarStatus.PathingFailed: - Car.Status = CarStatus.Driving; - Car.RespawnAtRandom(); - Behaviour.UpdateDestination(Car); + CarData.Status = CarStatus.Driving; + CarData.RespawnAtRandom(); + Behaviour.UpdateDestination(CarData); break; case CarStatus.Driving: - if (Car.DestinationReached()) + if (CarData.DestinationReached()) { - Car.Status = CarStatus.Parking; + CarData.Status = CarStatus.Parking; } else { - Behaviour.DriveAlongPath(Car); + Behaviour.DriveAlongPath(CarData); } break; case CarStatus.Parking: - Behaviour.SeekParkingSpot(Car); - new CruiserClientBehaviour().DriveAlongPath(Car); - if (await Behaviour.AttemptLocalParking(Car)) + Behaviour.SeekParkingSpot(CarData); + new CruiserClientBehaviour().DriveAlongPath(CarData); + if (await Behaviour.AttemptLocalParking(CarData)) { - Car.Status = CarStatus.Parked; - await Car.PublishAll(MqttClient); + CarData.Status = CarStatus.Parked; + await CarData.PublishAll(MqttClient); } break; case CarStatus.Parked: - if (!Behaviour.StayParked(Car)) + if (!Behaviour.StayParked(CarData)) { - Car.Status = CarStatus.Driving; - Behaviour.UpdateDestination(Car); + CarData.Status = CarStatus.Driving; + Behaviour.UpdateDestination(CarData); } break; default: - throw new InvalidOperationException($"{this}\tInvalid status: {Car.Status}"); + throw new InvalidOperationException($"{this}\tInvalid status: {CarData.Status}"); } } diff --git a/src/Solution1/ConsoleApp1/clients/behaviour/CruiserClientBehaviour.cs b/src/Solution1/ConsoleApp1/clients/behaviour/CruiserClientBehaviour.cs index 08ec196..aa9e034 100644 --- a/src/Solution1/ConsoleApp1/clients/behaviour/CruiserClientBehaviour.cs +++ b/src/Solution1/ConsoleApp1/clients/behaviour/CruiserClientBehaviour.cs @@ -7,49 +7,49 @@ namespace ConsoleApp1.clients; public class CruiserClientBehaviour: ICarClientBehaviour { - public void DriveAlongPath(MockCar car) + public void DriveAlongPath(CarData carData) { - if (car.Position.DistanceFromSource < car.Position.StreetEdge.Length) // driving on street + if (carData.Position.DistanceFromSource < carData.Position.StreetEdge.Length) // driving on street { // update position - double speed = car.Position.StreetEdge.CurrentMaxSpeed(); - car.Position = new StreetPosition(car.Position.StreetEdge, car.Position.DistanceFromSource + MathUtil.KmhToMs(speed)); + double speed = carData.Position.StreetEdge.CurrentMaxSpeed(); + carData.Position = new StreetPosition(carData.Position.StreetEdge, carData.Position.DistanceFromSource + MathUtil.KmhToMs(speed)); - if (car.Logging) + if (carData.Logging) { - Console.WriteLine($"{car}\ttick | {car.Position.ToString()} | dest: {car.Destination.Id} | car count: {car.Position.StreetEdge.CarCount} | driving at {speed:F2}kmh/{car.Position.StreetEdge.SpeedLimit:F2}kmh"); + Console.WriteLine($"{carData}\ttick | {carData.Position.ToString()} | dest: {carData.Destination.Id} | car count: {carData.Position.StreetEdge.CarCount} | driving at {speed:F2}kmh/{carData.Position.StreetEdge.SpeedLimit:F2}kmh"); } } else { // turn on next street - car.DistanceTravelled += car.Position.StreetEdge.Length; - car.Turn(car.Path.First()); - if (car.Logging) + carData.DistanceTravelled += carData.Position.StreetEdge.Length; + carData.Turn(carData.Path.First()); + if (carData.Logging) { - Console.WriteLine($"{car}\ttick | {car.Position.ToString()} | dest: {car.Destination.Id} | car count: {car.Position.StreetEdge.CarCount} | driving at {car.Position.StreetEdge.CurrentMaxSpeed():F2}kmh/{car.Position.StreetEdge.SpeedLimit:F2}kmh"); + Console.WriteLine($"{carData}\ttick | {carData.Position.ToString()} | dest: {carData.Destination.Id} | car count: {carData.Position.StreetEdge.CarCount} | driving at {carData.Position.StreetEdge.CurrentMaxSpeed():F2}kmh/{carData.Position.StreetEdge.SpeedLimit:F2}kmh"); } } } - public void UpdateDestination(MockCar car) + public void UpdateDestination(CarData carData) { - car.Destination = car.World.StreetNodes.RandomElement(); - if(!car.TryUpdatePath()) car.Status = CarStatus.PathingFailed; + carData.Destination = carData.World.StreetNodes.RandomElement(); + if(!carData.TryUpdatePath()) carData.Status = CarStatus.PathingFailed; } - public void SeekParkingSpot(MockCar car) + public void SeekParkingSpot(CarData carData) { - car.Status = CarStatus.Driving; - UpdateDestination(car); + carData.Status = CarStatus.Driving; + UpdateDestination(carData); } - public Task AttemptLocalParking(MockCar car) + public Task AttemptLocalParking(CarData carData) { throw new NotImplementedException("Cruisers don't park"); } - public bool StayParked(MockCar car) + public bool StayParked(CarData carData) { throw new NotImplementedException("Cruisers don't park."); } diff --git a/src/Solution1/ConsoleApp1/clients/behaviour/ICarClientBehaviour.cs b/src/Solution1/ConsoleApp1/clients/behaviour/ICarClientBehaviour.cs index e7d32b0..0e20f25 100644 --- a/src/Solution1/ConsoleApp1/clients/behaviour/ICarClientBehaviour.cs +++ b/src/Solution1/ConsoleApp1/clients/behaviour/ICarClientBehaviour.cs @@ -4,10 +4,10 @@ namespace ConsoleApp1.clients; public interface ICarClientBehaviour { - public void DriveAlongPath(MockCar car); - public void UpdateDestination(MockCar car); - public void SeekParkingSpot(MockCar car); - public Task AttemptLocalParking(MockCar car); - public bool StayParked(MockCar car); + public void DriveAlongPath(CarData carData); + public void UpdateDestination(CarData carData); + public void SeekParkingSpot(CarData carData); + public Task AttemptLocalParking(CarData carData); + public bool StayParked(CarData carData); } \ No newline at end of file diff --git a/src/Solution1/ConsoleApp1/clients/behaviour/PgsParkerClientBehaviour.cs b/src/Solution1/ConsoleApp1/clients/behaviour/PgsParkerClientBehaviour.cs index dd5284b..2243573 100644 --- a/src/Solution1/ConsoleApp1/clients/behaviour/PgsParkerClientBehaviour.cs +++ b/src/Solution1/ConsoleApp1/clients/behaviour/PgsParkerClientBehaviour.cs @@ -7,51 +7,51 @@ namespace ConsoleApp1.clients; public class PgsParkerClientBehaviour: ICarClientBehaviour { - public void DriveAlongPath(MockCar car) + public void DriveAlongPath(CarData carData) { - new CruiserClientBehaviour().DriveAlongPath(car); + new CruiserClientBehaviour().DriveAlongPath(carData); } - public void UpdateDestination(MockCar car) + public void UpdateDestination(CarData carData) { // call to calculate path to destination - car.Destination = car.World.StreetNodes.RandomElement(); - if (!car.TryUpdatePath()) + carData.Destination = carData.World.StreetNodes.RandomElement(); + if (!carData.TryUpdatePath()) { - car.Status = CarStatus.PathingFailed; + carData.Status = CarStatus.PathingFailed; return; } - PathResponse? pathRespone = car.Pgs.RequestGuidanceFromServer(car.Position, car.Destination); + PathResponse? pathRespone = carData.Pgs.RequestGuidanceFromServer(carData.Position, carData.Destination); if (pathRespone is null) { // TODO: this generates new dest, implement behaviour in case of not finding parking spot - car.Status = CarStatus.PathingFailed; + carData.Status = CarStatus.PathingFailed; return; } - car.Path = pathRespone.PathToReservedParkingSpot; - car.Destination = car.World.ParkingSpotMap[pathRespone.ReservedParkingSpot].Target; + carData.Path = pathRespone.PathToReservedParkingSpot; + carData.Destination = carData.World.ParkingSpotMap[pathRespone.ReservedParkingSpot].Target; // reserve spot - car.OccupiedSpot = pathRespone.ReservedParkingSpot; - car.OccupiedSpot.Occupied = true; + carData.OccupiedSpot = pathRespone.ReservedParkingSpot; + carData.OccupiedSpot.Occupied = true; } - public void SeekParkingSpot(MockCar car) + public void SeekParkingSpot(CarData carData) { - car.Path = new List { car.World.ParkingSpotMap[car.OccupiedSpot] } ; + carData.Path = new List { carData.World.ParkingSpotMap[carData.OccupiedSpot] } ; } - public async Task AttemptLocalParking(MockCar car) + public async Task AttemptLocalParking(CarData carData) { - if (car.Position.DistanceFromSource < car.OccupiedSpot.DistanceFromSource) return false; - car.Park(); + if (carData.Position.DistanceFromSource < carData.OccupiedSpot.DistanceFromSource) return false; + carData.Park(); return true; } - public bool StayParked(MockCar car) + public bool StayParked(CarData carData) { - return new RandomParkerClientBehaviour().StayParked(car); + return new RandomParkerClientBehaviour().StayParked(carData); } } \ No newline at end of file diff --git a/src/Solution1/ConsoleApp1/clients/behaviour/RandomParkerClientBehaviour.cs b/src/Solution1/ConsoleApp1/clients/behaviour/RandomParkerClientBehaviour.cs index 7f4a0e8..adab9fd 100644 --- a/src/Solution1/ConsoleApp1/clients/behaviour/RandomParkerClientBehaviour.cs +++ b/src/Solution1/ConsoleApp1/clients/behaviour/RandomParkerClientBehaviour.cs @@ -6,40 +6,40 @@ namespace ConsoleApp1.clients; public class RandomParkerClientBehaviour: ICarClientBehaviour { - public void DriveAlongPath(MockCar car) + public void DriveAlongPath(CarData carData) { - new CruiserClientBehaviour().DriveAlongPath(car); + new CruiserClientBehaviour().DriveAlongPath(carData); } - public void UpdateDestination(MockCar car) + public void UpdateDestination(CarData carData) { - new CruiserClientBehaviour().UpdateDestination(car); + new CruiserClientBehaviour().UpdateDestination(carData); } - public void SeekParkingSpot(MockCar car) + public void SeekParkingSpot(CarData carData) { - if (car.TargetNodeReached()) + if (carData.TargetNodeReached()) { // update kpis upon reached node - car.UpdateTrafficKpis(); - car.DistanceTravelled += car.Position.StreetEdge.Length; + carData.UpdateTrafficKpis(); + carData.DistanceTravelled += carData.Position.StreetEdge.Length; // turn on next random street to look for parking StreetEdge nextStreet; - if (car.World.Graph.TryGetOutEdges(car.Position.StreetEdge.Target, out var outGoingStreets)) // TODO possible parking spot search heuristic + if (carData.World.Graph.TryGetOutEdges(carData.Position.StreetEdge.Target, out var outGoingStreets)) // TODO possible parking spot search heuristic { nextStreet = outGoingStreets.ToList().RandomElement(); - car.Path = new List { nextStreet } ; + carData.Path = new List { nextStreet } ; } } } - public async Task AttemptLocalParking(MockCar car) + public async Task AttemptLocalParking(CarData carData) { - var previousPosition = car.Position.DistanceFromSource - MathUtil.KmhToMs(car.Position.StreetEdge.CurrentMaxSpeed()); + var previousPosition = carData.Position.DistanceFromSource - MathUtil.KmhToMs(carData.Position.StreetEdge.CurrentMaxSpeed()); Stack passedParkingSpots = new Stack( - car.Position.StreetEdge.ParkingSpots - .Where(spot => spot.DistanceFromSource >= previousPosition && spot.DistanceFromSource <= car.Position.DistanceFromSource) + carData.Position.StreetEdge.ParkingSpots + .Where(spot => spot.DistanceFromSource >= previousPosition && spot.DistanceFromSource <= carData.Position.DistanceFromSource) .Reverse()); while (passedParkingSpots.Count != 0 && passedParkingSpots.Peek().Occupied) @@ -50,24 +50,24 @@ public async Task AttemptLocalParking(MockCar car) var nearestUnoccupiedSpot = passedParkingSpots.Peek(); // occupy - car.OccupiedSpot = nearestUnoccupiedSpot; - car.OccupiedSpot.Occupied = true; + carData.OccupiedSpot = nearestUnoccupiedSpot; + carData.OccupiedSpot.Occupied = true; // physically park - car.Park(); + carData.Park(); return true; } - public bool StayParked(MockCar car) + public bool StayParked(CarData carData) { - if(car.Logging) - Console.WriteLine($"{car}\ttick | Parked at {car.Position} | {car.ParkTime} ticks remaining"); - if (car.ParkTime <= 0) + if(carData.Logging) + Console.WriteLine($"{carData}\ttick | Parked at {carData.Position} | {carData.ParkTime} ticks remaining"); + if (carData.ParkTime <= 0) { - car.ResetAfterParking(); + carData.ResetAfterParking(); return false; } - car.ParkTime--; + carData.ParkTime--; return true; } diff --git a/src/Solution1/ConsoleApp1/sim/MockCar.cs b/src/Solution1/ConsoleApp1/sim/CarData.cs similarity index 95% rename from src/Solution1/ConsoleApp1/sim/MockCar.cs rename to src/Solution1/ConsoleApp1/sim/CarData.cs index a3d7c85..d8e1129 100644 --- a/src/Solution1/ConsoleApp1/sim/MockCar.cs +++ b/src/Solution1/ConsoleApp1/sim/CarData.cs @@ -11,7 +11,7 @@ namespace ConsoleApp1.clients; public record Kpi(string Topic, byte[] Payload); -public class MockCar +public class CarData { private int Id { get; } public PhysicalWorld World { get; } @@ -45,7 +45,7 @@ public void ResetKpiMetrics() } public override string ToString() => $"[CAR\t{Id},\t{Status}\t]"; - public MockCar(int id, PhysicalWorld world, ParkingGuidanceSystem parkingGuidanceSystem, bool logging) + public CarData(int id, PhysicalWorld world, ParkingGuidanceSystem parkingGuidanceSystem, bool logging) { Id = id; World = world;