Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 047-TrafficControlWithDapr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@ When the car passes an exit-camera, another photo and timestamp are registered.
- Rob Vettor
- Edwin van Wijk
- Chandrasekar B
- Vaclav Jirovsky
2 changes: 1 addition & 1 deletion 047-TrafficControlWithDapr/Student/Challenge-00.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Your coach will provide you with a `Resources.zip` package file that contains th
Install all the prerequisites listed below and make sure they're working correctly:

- Git ([download](https://git-scm.com/))
- .NET 5 SDK ([download](https://dotnet.microsoft.com/download/dotnet/5.0))
- .NET 5 SDK ([download](https://dotnet.microsoft.com/download/dotnet/5.0)), .NET 6 SDK ([download](https://dotnet.microsoft.com/download/dotnet/6.0))
- Visual Studio Code ([download](https://code.visualstudio.com/download)) with the following extensions installed:
- [C#](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp)
- [REST Client](https://marketplace.visualstudio.com/items?itemName=humao.rest-client)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
Expand All @@ -11,17 +12,19 @@ namespace Simulation
public class CameraSimulation
{
private readonly ITrafficControlService _trafficControlService;
private readonly UiRenderingService _uiRenderingService;
private Random _rnd;
private int _camNumber;
private int _minEntryDelayInMS = 50;
private int _maxEntryDelayInMS = 5000;
private int _minExitDelayInS = 4;
private int _maxExitDelayInS = 10;

public CameraSimulation(int camNumber, ITrafficControlService trafficControlService)
public CameraSimulation(int camNumber, ITrafficControlService trafficControlService, UiRenderingService uiRenderingService)
{
_camNumber = camNumber;
_trafficControlService = trafficControlService;
_uiRenderingService = uiRenderingService;
}

public void Start()
Expand All @@ -47,18 +50,24 @@ public void Start()
{
Lane = _camNumber,
LicenseNumber = GenerateRandomLicenseNumber(),
Timestamp = entryTimestamp
Timestamp = entryTimestamp,
BackgroundColor = GenerateRandomColor()

};
_trafficControlService.SendVehicleEntry(vehicleRegistered);
Console.WriteLine($"Simulated ENTRY of vehicle with license-number {vehicleRegistered.LicenseNumber} in lane {vehicleRegistered.Lane}");
Debug.WriteLine($"Simulated ENTRY of vehicle with license-number {vehicleRegistered.LicenseNumber} in lane {vehicleRegistered.Lane}");
_uiRenderingService.AddCarToBeAnimatedInWindow(0, _camNumber, vehicleRegistered.LicenseNumber, "#FFFFFF", vehicleRegistered.BackgroundColor);



// simulate exit
TimeSpan exitDelay = TimeSpan.FromSeconds(_rnd.Next(_minExitDelayInS, _maxExitDelayInS) + _rnd.NextDouble());
Task.Delay(exitDelay).Wait();
vehicleRegistered.Timestamp = DateTime.Now;
vehicleRegistered.Lane = _rnd.Next(1, 4);
_trafficControlService.SendVehicleExit(vehicleRegistered);
Console.WriteLine($"Simulated EXIT of vehicle with license-number {vehicleRegistered.LicenseNumber} in lane {vehicleRegistered.Lane}");
Debug.WriteLine($"Simulated EXIT of vehicle with license-number {vehicleRegistered.LicenseNumber} in lane {vehicleRegistered.Lane}");
_uiRenderingService.AddCarToBeAnimatedInWindow(1, _camNumber, vehicleRegistered.LicenseNumber, "#FFFFFF", vehicleRegistered.BackgroundColor);
});
}
catch (Exception ex)
Expand Down Expand Up @@ -107,6 +116,38 @@ private string GenerateRandomLicenseNumber()
return kenteken;
}


private string GenerateRandomColor()
{
int type = _rnd.Next(1, 9);
string mycolor = null;
switch (type)
{
default:
case 1: // silver
mycolor = "#A5BCB6";
break;
case 2: // blue
mycolor = "#1524EC";
break;
case 3: // yellow
mycolor = "#ffe599";
break;
case 4: // white
mycolor = "#FFFFFF";
break;
case 5: // red
mycolor = "#ea9999";
break;
case 6: // green #2
mycolor = "#d9ead3";
break;
}

return mycolor;
}


private string GenerateRandomCharacters(int aantal)
{
char[] chars = new char[aantal];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Text.Json.Serialization;

namespace Simulation.Events
{
Expand All @@ -7,5 +8,8 @@ public class VehicleRegistered
public int Lane { get; set; }
public string LicenseNumber { get; set; }
public DateTime Timestamp { get; set; }

[JsonIgnore]
public string BackgroundColor { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Pastel;
using Simulation.Proxies;

namespace Simulation
Expand All @@ -12,12 +15,43 @@ static void Main(string[] args)
{
var httpClient = new HttpClient();
int lanes = 3;

var rand = new Random();
int lanesOffsetWindow1 = rand.Next(0, 5);
int lanesOffsetWindow2 = rand.Next(0, 5);

var uiRenderingService = new UiRenderingService(Console.BufferWidth, Console.BufferHeight, lanes);

Task.Run(() =>
{
while (true)
{
uiRenderingService.RedrawScene(Console.BufferWidth - 1, Console.BufferHeight - 2, lanes, lanesOffsetWindow1, lanesOffsetWindow2);
Thread.Sleep(1000);
}
}
);

Task.Run(() =>
{

while (true)
{
uiRenderingService.MoveAllCarsForWindow(0);
uiRenderingService.MoveAllCarsForWindow(1);
Thread.Sleep(100);
}
}
);



CameraSimulation[] cameras = new CameraSimulation[lanes];
for (var i = 0; i < lanes; i++)
{
int camNumber = i + 1;
var trafficControlService = new HttpTrafficControlService(httpClient);
cameras[i] = new CameraSimulation(camNumber, trafficControlService);
cameras[i] = new CameraSimulation(camNumber, trafficControlService, uiRenderingService);
}
Parallel.ForEach(cameras, cam => cam.Start());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Pastel" Version="3.0.1" />
</ItemGroup>
</Project>
Loading