Jobbet litt med trafikklys

This commit is contained in:
Geir Okkenhaug Jerstad 2024-12-10 14:42:11 +01:00
parent d3db60a6e3
commit 2549653f2c
14 changed files with 296 additions and 1 deletions

View file

@ -0,0 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "APITerje", "APITerje\APITerje.csproj", "{E4E2138D-5CBC-489A-B0AA-2116EB2BF7CD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E4E2138D-5CBC-489A-B0AA-2116EB2BF7CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E4E2138D-5CBC-489A-B0AA-2116EB2BF7CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E4E2138D-5CBC-489A-B0AA-2116EB2BF7CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E4E2138D-5CBC-489A-B0AA-2116EB2BF7CD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View file

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
</ItemGroup>
</Project>

View file

@ -0,0 +1,6 @@
@APITerje_HostAddress = http://localhost:5253
GET {{APITerje_HostAddress}}/weatherforecast/
Accept: application/json
###

View file

@ -0,0 +1,44 @@
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
app.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast")
.WithOpenApi();
app.Run();
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}

View file

@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:14325",
"sslPort": 44307
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5253",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7131;http://localhost:5253",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View file

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View file

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

View file

@ -0,0 +1,17 @@
namespace Trafikklys.Demo2;
internal class Demo2
{
public static void Run()
{
var trafikklys = new Trafikklys();
while (true)
{
Console.Clear();
trafikklys.Show();
Console.ReadKey(true);
trafikklys.GoToNextPhase();
}
}
}

View file

@ -0,0 +1,40 @@
namespace Trafikklys.Demo2;
internal class Trafikklys
{
bool red;
bool yellow;
bool green;
public Trafikklys()
{
red = true;
}
public void Show()
{
TrafikklysKonsoll.Show(red, yellow, green);
}
public void GoToNextPhase()
{
if (red && !yellow)
{
yellow = true;
}
else if (red)
{
red = false;
yellow = false;
green = true;
}else if (green)
{
green = false;
yellow = true;
}else if (yellow)
{
yellow = false;
red = true;
}
}
}

View file

@ -0,0 +1,16 @@
namespace Trafikklys.Demo3;
public class Demo3
{
public static void Run()
{
var trafikklys = new Trafikklys();
while (true)
{
Console.Clear();
trafikklys.Show();
Console.ReadKey(true);
trafikklys.GoToNextPhase();
}
}
}

View file

@ -0,0 +1,38 @@
namespace Trafikklys.Demo3;
public class Trafikklys
{
private bool Red { get; set; }
public bool Yellow { get; private set; }
public bool Green { get; private set; }
public Trafikklys()
{
Red = true;
}
public void Show()
{
TrafikklysKonsoll.Show(Red, Yellow, Green);
}
public void GoToNextPhase()
{
if (Red && !Yellow)
{
Yellow = true;
}else if (Red)
{
Red = false;
Yellow = false;
Green = true;
}else if (Green)
{
Green = false;
Yellow = true;
}else if (Yellow)
{
Yellow = false;
Red = true;
}
}
}

View file

@ -0,0 +1,16 @@
namespace Trafikklys.Demo4;
public class Demo4
{
public static void Run()
{
var trafikklys = new Trafikklys();
while (true)
{
Console.Clear();
trafikklys.Show();
Console.ReadKey(true);
trafikklys.GoToNextPhase();
}
}
}

View file

@ -0,0 +1,27 @@
namespace Trafikklys.Demo4;
public class Trafikklys
{
private int _phase;
public void SetPhase(int phase)
{
if (phase >= 0 && phase <= 3)
{
_phase = phase;
}
}
public void Show()
{
var red = _phase < 2;
var yellow = _phase is 1 or 3;
var green = _phase == 2;
TrafikklysKonsoll.Show(red, yellow, green);
}
public void GoToNextPhase()
{
_phase = _phase < 3 ? _phase + 1 : 0;
}
}

View file

@ -1,3 +1,6 @@
using Trafikklys.Demo1;
using Trafikklys.Demo2;
using Trafikklys.Demo3;
using Trafikklys.Demo4;
Demo1.Run();
Trafikklys.Demo4.Demo4.Run();