Skip to content

Commit

Permalink
rename MockData into CarData
Browse files Browse the repository at this point in the history
  • Loading branch information
Grimmgockel committed May 23, 2023
1 parent 4720148 commit 4609049
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 88 deletions.
42 changes: 21 additions & 21 deletions src/Solution1/ConsoleApp1/clients/CarClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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
Expand All @@ -50,45 +50,45 @@ public static async Task<CarClient> 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}");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> AttemptLocalParking(MockCar car)
public Task<bool> 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.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> 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<bool> AttemptLocalParking(CarData carData);
public bool StayParked(CarData carData);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<StreetEdge> { car.World.ParkingSpotMap[car.OccupiedSpot] } ;
carData.Path = new List<StreetEdge> { carData.World.ParkingSpotMap[carData.OccupiedSpot] } ;
}

public async Task<bool> AttemptLocalParking(MockCar car)
public async Task<bool> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<StreetEdge> { nextStreet } ;
carData.Path = new List<StreetEdge> { nextStreet } ;
}
}
}

public async Task<bool> AttemptLocalParking(MockCar car)
public async Task<bool> 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<ParkingSpot> passedParkingSpots = new Stack<ParkingSpot>(
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)
Expand All @@ -50,24 +50,24 @@ public async Task<bool> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 4609049

Please sign in to comment.