Dev Container für C# eingerichtet
parent
9d9bacd718
commit
4da955ac72
|
@ -1,22 +1,10 @@
|
||||||
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
|
{
|
||||||
// README at: https://github.com/devcontainers/templates/tree/main/src/ubuntu
|
"name": "C# Dev Container",
|
||||||
{
|
"image": "mcr.microsoft.com/dotnet/sdk:8.0",
|
||||||
"name": "Ubuntu",
|
"customizations": {
|
||||||
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
|
"vscode": {
|
||||||
"image": "mcr.microsoft.com/devcontainers/base:jammy"
|
"extensions": ["ms-dotnettools.csharp"]
|
||||||
|
}
|
||||||
// Features to add to the dev container. More info: https://containers.dev/features.
|
},
|
||||||
// "features": {},
|
"remoteUser": "root"
|
||||||
|
}
|
||||||
// Use 'forwardPorts' to make a list of ports inside the container available locally.
|
|
||||||
// "forwardPorts": [],
|
|
||||||
|
|
||||||
// Use 'postCreateCommand' to run commands after the container is created.
|
|
||||||
// "postCreateCommand": "uname -a",
|
|
||||||
|
|
||||||
// Configure tool-specific properties.
|
|
||||||
// "customizations": {},
|
|
||||||
|
|
||||||
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
|
|
||||||
// "remoteUser": "root"
|
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
// Use IntelliSense to find out which attributes exist for C# debugging
|
||||||
|
// Use hover for the description of the existing attributes
|
||||||
|
// For further information visit https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md
|
||||||
|
"name": ".NET Core Launch (console)",
|
||||||
|
"type": "coreclr",
|
||||||
|
"request": "launch",
|
||||||
|
"preLaunchTask": "build",
|
||||||
|
// If you have changed target frameworks, make sure to update the program path.
|
||||||
|
"program": "${workspaceFolder}/Async_Await/bin/Debug/net8.0/Async_Await.dll",
|
||||||
|
"args": [],
|
||||||
|
"cwd": "${workspaceFolder}/Async_Await",
|
||||||
|
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
|
||||||
|
"console": "internalConsole",
|
||||||
|
"stopAtEntry": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": ".NET Core Attach",
|
||||||
|
"type": "coreclr",
|
||||||
|
"request": "attach"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
|
@ -0,0 +1,41 @@
|
||||||
|
{
|
||||||
|
"version": "2.0.0",
|
||||||
|
"tasks": [
|
||||||
|
{
|
||||||
|
"label": "build",
|
||||||
|
"command": "dotnet",
|
||||||
|
"type": "process",
|
||||||
|
"args": [
|
||||||
|
"build",
|
||||||
|
"${workspaceFolder}/CSharpProjekt.sln",
|
||||||
|
"/property:GenerateFullPaths=true",
|
||||||
|
"/consoleloggerparameters:NoSummary;ForceNoAlign"
|
||||||
|
],
|
||||||
|
"problemMatcher": "$msCompile"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "publish",
|
||||||
|
"command": "dotnet",
|
||||||
|
"type": "process",
|
||||||
|
"args": [
|
||||||
|
"publish",
|
||||||
|
"${workspaceFolder}/CSharpProjekt.sln",
|
||||||
|
"/property:GenerateFullPaths=true",
|
||||||
|
"/consoleloggerparameters:NoSummary;ForceNoAlign"
|
||||||
|
],
|
||||||
|
"problemMatcher": "$msCompile"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "watch",
|
||||||
|
"command": "dotnet",
|
||||||
|
"type": "process",
|
||||||
|
"args": [
|
||||||
|
"watch",
|
||||||
|
"run",
|
||||||
|
"--project",
|
||||||
|
"${workspaceFolder}/CSharpProjekt.sln"
|
||||||
|
],
|
||||||
|
"problemMatcher": "$msCompile"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
|
@ -1,10 +1,10 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
public class Ex1
|
public class Ex1
|
||||||
{
|
{
|
||||||
public static async Task Run()
|
public static async Task Run()
|
||||||
{
|
{
|
||||||
/* Example of async and await */
|
/* Example of async and await */
|
||||||
Console.WriteLine("Start");
|
Console.WriteLine("Start");
|
||||||
await DoWorkAsync();
|
await DoWorkAsync();
|
||||||
Console.WriteLine("Finished");
|
Console.WriteLine("Finished");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task DoWorkAsync()
|
private static async Task DoWorkAsync()
|
||||||
{
|
{
|
||||||
await Task.Delay(2000);
|
await Task.Delay(2000);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,26 +1,26 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
public class Ex2
|
public class Ex2
|
||||||
{
|
{
|
||||||
public static async Task Run()
|
public static async Task Run()
|
||||||
{
|
{
|
||||||
/* Example of Tasks, showcasing Ressource Efficency */
|
/* Example of Tasks, showcasing Ressource Efficency */
|
||||||
var task1 = DoMoreWorkAsync("One");
|
var task1 = DoMoreWorkAsync("One");
|
||||||
var task2 = DoMoreWorkAsync("Two");
|
var task2 = DoMoreWorkAsync("Two");
|
||||||
var task3 = DoMoreWorkAsync("Three");
|
var task3 = DoMoreWorkAsync("Three");
|
||||||
|
|
||||||
await task1;
|
await task1;
|
||||||
await task2;
|
await task2;
|
||||||
await task3;
|
await task3;
|
||||||
Console.WriteLine("Finished example showcasing resource efficency\n\n");
|
Console.WriteLine("Finished example showcasing resource efficency\n\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task DoMoreWorkAsync(string name)
|
private static async Task DoMoreWorkAsync(string name)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Task {name} is being executed by Thread {Thread.CurrentThread.ManagedThreadId}");
|
Console.WriteLine($"Task {name} is being executed by Thread {Thread.CurrentThread.ManagedThreadId}");
|
||||||
await Task.Delay(2000);
|
await Task.Delay(2000);
|
||||||
Console.WriteLine($"Task {name} is done");
|
Console.WriteLine($"Task {name} is done");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,23 +1,23 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
public class Ex3
|
public class Ex3
|
||||||
{
|
{
|
||||||
public static async Task Run()
|
public static async Task Run()
|
||||||
{
|
{
|
||||||
/* Example using a Task<T> type */
|
/* Example using a Task<T> type */
|
||||||
var lowerCaseString = "i want to be an upper case string";
|
var lowerCaseString = "i want to be an upper case string";
|
||||||
Console.WriteLine("main: " + lowerCaseString);
|
Console.WriteLine("main: " + lowerCaseString);
|
||||||
var upperCaseString = await WorkOnStringAsync(lowerCaseString);
|
var upperCaseString = await WorkOnStringAsync(lowerCaseString);
|
||||||
Console.WriteLine("main: " + upperCaseString);
|
Console.WriteLine("main: " + upperCaseString);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task<string> WorkOnStringAsync(string s)
|
private static async Task<string> WorkOnStringAsync(string s)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Task: Async task started");
|
Console.WriteLine("Task: Async task started");
|
||||||
Console.WriteLine("Task: Waiting for 2 sec");
|
Console.WriteLine("Task: Waiting for 2 sec");
|
||||||
await Task.Delay(2000);
|
await Task.Delay(2000);
|
||||||
Console.WriteLine("Task: Done waiting, returning the upper case string");
|
Console.WriteLine("Task: Done waiting, returning the upper case string");
|
||||||
return s.ToUpper();
|
return s.ToUpper();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,28 +1,28 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
public class Ex4
|
public class Ex4
|
||||||
{
|
{
|
||||||
public static async Task Run()
|
public static async Task Run()
|
||||||
{
|
{
|
||||||
/* Example working with Tasks and awaiting them later */
|
/* Example working with Tasks and awaiting them later */
|
||||||
var lowerCaseString = "i want to be an upper case string";
|
var lowerCaseString = "i want to be an upper case string";
|
||||||
Console.WriteLine("main: " + lowerCaseString);
|
Console.WriteLine("main: " + lowerCaseString);
|
||||||
// Start the task but do not await yet
|
// Start the task but do not await yet
|
||||||
Task<string> upperCaseTask = WorkOnStringAsync(lowerCaseString);
|
Task<string> upperCaseTask = WorkOnStringAsync(lowerCaseString);
|
||||||
Console.WriteLine("main: WorkOnStringAsync has been called, doing other work...");
|
Console.WriteLine("main: WorkOnStringAsync has been called, doing other work...");
|
||||||
// Now await the result
|
// Now await the result
|
||||||
Console.WriteLine("main: Now waiting for the result");
|
Console.WriteLine("main: Now waiting for the result");
|
||||||
var upperCaseString = await upperCaseTask;
|
var upperCaseString = await upperCaseTask;
|
||||||
Console.WriteLine("main: " + upperCaseString);
|
Console.WriteLine("main: " + upperCaseString);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task<string> WorkOnStringAsync(string s)
|
private static async Task<string> WorkOnStringAsync(string s)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Task: Async task started");
|
Console.WriteLine("Task: Async task started");
|
||||||
Console.WriteLine("Task: Waiting for 2 sec");
|
Console.WriteLine("Task: Waiting for 2 sec");
|
||||||
await Task.Delay(2000);
|
await Task.Delay(2000);
|
||||||
Console.WriteLine("Task: Done waiting, returning the upper case string");
|
Console.WriteLine("Task: Done waiting, returning the upper case string");
|
||||||
return s.ToUpper();
|
return s.ToUpper();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,27 +1,27 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
public class Ex5
|
public class Ex5
|
||||||
{
|
{
|
||||||
public static async Task Run()
|
public static async Task Run()
|
||||||
{
|
{
|
||||||
/* Example for Task Coordination using Task.WhenAll() */
|
/* Example for Task Coordination using Task.WhenAll() */
|
||||||
Random random = new();
|
Random random = new();
|
||||||
async Task<(int index, int waitingTime)> randomDelay(int index)
|
async Task<(int index, int waitingTime)> randomDelay(int index)
|
||||||
{
|
{
|
||||||
var waitingTime = random.Next(500, 2000);
|
var waitingTime = random.Next(500, 2000);
|
||||||
await Task.Delay(waitingTime);
|
await Task.Delay(waitingTime);
|
||||||
return (index, waitingTime);
|
return (index, waitingTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
var delayTask1 = randomDelay(1);
|
var delayTask1 = randomDelay(1);
|
||||||
var delayTask2 = randomDelay(2);
|
var delayTask2 = randomDelay(2);
|
||||||
var delayTask3 = randomDelay(3);
|
var delayTask3 = randomDelay(3);
|
||||||
|
|
||||||
await Task.WhenAll(delayTask1, delayTask2, delayTask3);
|
await Task.WhenAll(delayTask1, delayTask2, delayTask3);
|
||||||
|
|
||||||
Console.WriteLine($"Task {delayTask1.Result.index} had to wait for {delayTask1.Result.waitingTime} ms");
|
Console.WriteLine($"Task {delayTask1.Result.index} had to wait for {delayTask1.Result.waitingTime} ms");
|
||||||
Console.WriteLine($"Task {delayTask2.Result.index} had to wait for {delayTask2.Result.waitingTime} ms");
|
Console.WriteLine($"Task {delayTask2.Result.index} had to wait for {delayTask2.Result.waitingTime} ms");
|
||||||
Console.WriteLine($"Task {delayTask3.Result.index} had to wait for {delayTask3.Result.waitingTime} ms");
|
Console.WriteLine($"Task {delayTask3.Result.index} had to wait for {delayTask3.Result.waitingTime} ms");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,27 +1,27 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
public class Ex6
|
public class Ex6
|
||||||
{
|
{
|
||||||
public static async Task Run()
|
public static async Task Run()
|
||||||
{
|
{
|
||||||
/* Example for Task Coordination using Task.WhenAny() */
|
/* Example for Task Coordination using Task.WhenAny() */
|
||||||
Random random = new();
|
Random random = new();
|
||||||
async Task<(int index, int waitingTime)> randomDelay(int index)
|
async Task<(int index, int waitingTime)> randomDelay(int index)
|
||||||
{
|
{
|
||||||
var waitingTime = random.Next(500, 2000);
|
var waitingTime = random.Next(500, 2000);
|
||||||
await Task.Delay(waitingTime);
|
await Task.Delay(waitingTime);
|
||||||
return (index, waitingTime);
|
return (index, waitingTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Task<(int index, int waitingTime)>> taskList = new();
|
List<Task<(int index, int waitingTime)>> taskList = new();
|
||||||
for (int i = 1; i < 10; i++)
|
for (int i = 1; i < 10; i++)
|
||||||
{
|
{
|
||||||
var delayedTask = randomDelay(i);
|
var delayedTask = randomDelay(i);
|
||||||
taskList.Add(delayedTask);
|
taskList.Add(delayedTask);
|
||||||
}
|
}
|
||||||
var fastestTask = await Task.WhenAny(taskList);
|
var fastestTask = await Task.WhenAny(taskList);
|
||||||
Console.WriteLine($"The fastest task was Task {fastestTask.Result.index}. It took {fastestTask.Result.waitingTime} ms to complete.");
|
Console.WriteLine($"The fastest task was Task {fastestTask.Result.index}. It took {fastestTask.Result.waitingTime} ms to complete.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,40 +1,40 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
static async Task Main(string[] args)
|
static async Task Main(string[] args)
|
||||||
{
|
{
|
||||||
if (args.Length == 0)
|
if (args.Length == 0)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Usage: dotnet run -- ex1|ex2|ex3|ex4|ex5|ex6");
|
Console.WriteLine("Usage: dotnet run -- ex1|ex2|ex3|ex4|ex5|ex6");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (args[0].ToLower())
|
switch (args[0].ToLower())
|
||||||
{
|
{
|
||||||
case "ex1":
|
case "ex1":
|
||||||
await Ex1.Run();
|
await Ex1.Run();
|
||||||
break;
|
break;
|
||||||
case "ex2":
|
case "ex2":
|
||||||
await Ex2.Run();
|
await Ex2.Run();
|
||||||
break;
|
break;
|
||||||
case "ex3":
|
case "ex3":
|
||||||
await Ex3.Run();
|
await Ex3.Run();
|
||||||
break;
|
break;
|
||||||
case "ex4":
|
case "ex4":
|
||||||
await Ex4.Run();
|
await Ex4.Run();
|
||||||
break;
|
break;
|
||||||
case "ex5":
|
case "ex5":
|
||||||
await Ex5.Run();
|
await Ex5.Run();
|
||||||
break;
|
break;
|
||||||
case "ex6":
|
case "ex6":
|
||||||
await Ex6.Run();
|
await Ex6.Run();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
Console.WriteLine($"Unknown example: {args[0]}");
|
Console.WriteLine($"Unknown example: {args[0]}");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,66 +1,66 @@
|
||||||
{
|
{
|
||||||
"format": 1,
|
"format": 1,
|
||||||
"restore": {
|
"restore": {
|
||||||
"/workspaces/CSharp/Async_Await/Async_Await.csproj": {}
|
"/workspaces/CSharp/Async_Await/Async_Await.csproj": {}
|
||||||
},
|
},
|
||||||
"projects": {
|
"projects": {
|
||||||
"/workspaces/CSharp/Async_Await/Async_Await.csproj": {
|
"/workspaces/CSharp/Async_Await/Async_Await.csproj": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"restore": {
|
"restore": {
|
||||||
"projectUniqueName": "/workspaces/CSharp/Async_Await/Async_Await.csproj",
|
"projectUniqueName": "/workspaces/CSharp/Async_Await/Async_Await.csproj",
|
||||||
"projectName": "Async_Await",
|
"projectName": "Async_Await",
|
||||||
"projectPath": "/workspaces/CSharp/Async_Await/Async_Await.csproj",
|
"projectPath": "/workspaces/CSharp/Async_Await/Async_Await.csproj",
|
||||||
"packagesPath": "/root/.nuget/packages/",
|
"packagesPath": "/root/.nuget/packages/",
|
||||||
"outputPath": "/workspaces/CSharp/Async_Await/obj/",
|
"outputPath": "/workspaces/CSharp/Async_Await/obj/",
|
||||||
"projectStyle": "PackageReference",
|
"projectStyle": "PackageReference",
|
||||||
"configFilePaths": [
|
"configFilePaths": [
|
||||||
"/root/.nuget/NuGet/NuGet.Config"
|
"/root/.nuget/NuGet/NuGet.Config"
|
||||||
],
|
],
|
||||||
"originalTargetFrameworks": [
|
"originalTargetFrameworks": [
|
||||||
"net8.0"
|
"net8.0"
|
||||||
],
|
],
|
||||||
"sources": {
|
"sources": {
|
||||||
"https://api.nuget.org/v3/index.json": {}
|
"https://api.nuget.org/v3/index.json": {}
|
||||||
},
|
},
|
||||||
"frameworks": {
|
"frameworks": {
|
||||||
"net8.0": {
|
"net8.0": {
|
||||||
"targetAlias": "net8.0",
|
"targetAlias": "net8.0",
|
||||||
"projectReferences": {}
|
"projectReferences": {}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"warningProperties": {
|
"warningProperties": {
|
||||||
"warnAsError": [
|
"warnAsError": [
|
||||||
"NU1605"
|
"NU1605"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"restoreAuditProperties": {
|
"restoreAuditProperties": {
|
||||||
"enableAudit": "true",
|
"enableAudit": "true",
|
||||||
"auditLevel": "low",
|
"auditLevel": "low",
|
||||||
"auditMode": "direct"
|
"auditMode": "direct"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"frameworks": {
|
"frameworks": {
|
||||||
"net8.0": {
|
"net8.0": {
|
||||||
"targetAlias": "net8.0",
|
"targetAlias": "net8.0",
|
||||||
"imports": [
|
"imports": [
|
||||||
"net461",
|
"net461",
|
||||||
"net462",
|
"net462",
|
||||||
"net47",
|
"net47",
|
||||||
"net471",
|
"net471",
|
||||||
"net472",
|
"net472",
|
||||||
"net48",
|
"net48",
|
||||||
"net481"
|
"net481"
|
||||||
],
|
],
|
||||||
"assetTargetFallback": true,
|
"assetTargetFallback": true,
|
||||||
"warn": true,
|
"warn": true,
|
||||||
"frameworkReferences": {
|
"frameworkReferences": {
|
||||||
"Microsoft.NETCore.App": {
|
"Microsoft.NETCore.App": {
|
||||||
"privateAssets": "all"
|
"privateAssets": "all"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/8.0.410/PortableRuntimeIdentifierGraph.json"
|
"runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/8.0.410/PortableRuntimeIdentifierGraph.json"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,15 +1,15 @@
|
||||||
<?xml version="1.0" encoding="utf-8" standalone="no"?>
|
<?xml version="1.0" encoding="utf-8" standalone="no"?>
|
||||||
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
||||||
<RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess>
|
<RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess>
|
||||||
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
|
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
|
||||||
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
|
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
|
||||||
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">/root/.nuget/packages/</NuGetPackageRoot>
|
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">/root/.nuget/packages/</NuGetPackageRoot>
|
||||||
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">/root/.nuget/packages/</NuGetPackageFolders>
|
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">/root/.nuget/packages/</NuGetPackageFolders>
|
||||||
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
|
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
|
||||||
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.11.1</NuGetToolVersion>
|
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.11.1</NuGetToolVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
||||||
<SourceRoot Include="/root/.nuget/packages/" />
|
<SourceRoot Include="/root/.nuget/packages/" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
|
@ -1,2 +1,2 @@
|
||||||
<?xml version="1.0" encoding="utf-8" standalone="no"?>
|
<?xml version="1.0" encoding="utf-8" standalone="no"?>
|
||||||
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" />
|
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" />
|
|
@ -1,4 +1,4 @@
|
||||||
// <autogenerated />
|
// <autogenerated />
|
||||||
using System;
|
using System;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v8.0", FrameworkDisplayName = ".NET 8.0")]
|
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v8.0", FrameworkDisplayName = ".NET 8.0")]
|
||||||
|
|
|
@ -13,7 +13,7 @@ using System.Reflection;
|
||||||
[assembly: System.Reflection.AssemblyCompanyAttribute("Async_Await")]
|
[assembly: System.Reflection.AssemblyCompanyAttribute("Async_Await")]
|
||||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
||||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
|
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+9d9bacd718b457b1e622e2000d7baf963457a871")]
|
||||||
[assembly: System.Reflection.AssemblyProductAttribute("Async_Await")]
|
[assembly: System.Reflection.AssemblyProductAttribute("Async_Await")]
|
||||||
[assembly: System.Reflection.AssemblyTitleAttribute("Async_Await")]
|
[assembly: System.Reflection.AssemblyTitleAttribute("Async_Await")]
|
||||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
63804ea50179688365807671500045cda9689bbb802dfd4b9f520cad32043daf
|
715df55e00865760b049b6999c3a198f53ad831234a2f70656b0a5d26a34b8f5
|
||||||
|
|
|
@ -8,6 +8,6 @@ build_property.PlatformNeutralAssembly =
|
||||||
build_property.EnforceExtendedAnalyzerRules =
|
build_property.EnforceExtendedAnalyzerRules =
|
||||||
build_property._SupportedPlatformList = Linux,macOS,Windows
|
build_property._SupportedPlatformList = Linux,macOS,Windows
|
||||||
build_property.RootNamespace = Async_Await
|
build_property.RootNamespace = Async_Await
|
||||||
build_property.ProjectDir = /workspaces/CSharp/Async_Await/
|
build_property.ProjectDir = /workspaces/CSharpProjekt/Async_Await/
|
||||||
build_property.EnableComHosting =
|
build_property.EnableComHosting =
|
||||||
build_property.EnableGeneratedComInterfaceComImportInterop =
|
build_property.EnableGeneratedComInterfaceComImportInterop =
|
||||||
|
|
Binary file not shown.
|
@ -1 +1 @@
|
||||||
f56eca8a218035aaee0739bd26bba4d23e0f75fd08331f478a25b9179244fa4b
|
f56eca8a218035aaee0739bd26bba4d23e0f75fd08331f478a25b9179244fa4b
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
/workspaces/CSharp/Async_Await/bin/Debug/net8.0/Async_Await
|
/workspaces/CSharp/Async_Await/bin/Debug/net8.0/Async_Await
|
||||||
/workspaces/CSharp/Async_Await/bin/Debug/net8.0/Async_Await.deps.json
|
/workspaces/CSharp/Async_Await/bin/Debug/net8.0/Async_Await.deps.json
|
||||||
/workspaces/CSharp/Async_Await/bin/Debug/net8.0/Async_Await.runtimeconfig.json
|
/workspaces/CSharp/Async_Await/bin/Debug/net8.0/Async_Await.runtimeconfig.json
|
||||||
/workspaces/CSharp/Async_Await/bin/Debug/net8.0/Async_Await.dll
|
/workspaces/CSharp/Async_Await/bin/Debug/net8.0/Async_Await.dll
|
||||||
/workspaces/CSharp/Async_Await/bin/Debug/net8.0/Async_Await.pdb
|
/workspaces/CSharp/Async_Await/bin/Debug/net8.0/Async_Await.pdb
|
||||||
/workspaces/CSharp/Async_Await/obj/Debug/net8.0/Async_Await.GeneratedMSBuildEditorConfig.editorconfig
|
/workspaces/CSharp/Async_Await/obj/Debug/net8.0/Async_Await.GeneratedMSBuildEditorConfig.editorconfig
|
||||||
/workspaces/CSharp/Async_Await/obj/Debug/net8.0/Async_Await.AssemblyInfoInputs.cache
|
/workspaces/CSharp/Async_Await/obj/Debug/net8.0/Async_Await.AssemblyInfoInputs.cache
|
||||||
/workspaces/CSharp/Async_Await/obj/Debug/net8.0/Async_Await.AssemblyInfo.cs
|
/workspaces/CSharp/Async_Await/obj/Debug/net8.0/Async_Await.AssemblyInfo.cs
|
||||||
/workspaces/CSharp/Async_Await/obj/Debug/net8.0/Async_Await.csproj.CoreCompileInputs.cache
|
/workspaces/CSharp/Async_Await/obj/Debug/net8.0/Async_Await.csproj.CoreCompileInputs.cache
|
||||||
/workspaces/CSharp/Async_Await/obj/Debug/net8.0/Async_Await.dll
|
/workspaces/CSharp/Async_Await/obj/Debug/net8.0/Async_Await.dll
|
||||||
/workspaces/CSharp/Async_Await/obj/Debug/net8.0/refint/Async_Await.dll
|
/workspaces/CSharp/Async_Await/obj/Debug/net8.0/refint/Async_Await.dll
|
||||||
/workspaces/CSharp/Async_Await/obj/Debug/net8.0/Async_Await.pdb
|
/workspaces/CSharp/Async_Await/obj/Debug/net8.0/Async_Await.pdb
|
||||||
/workspaces/CSharp/Async_Await/obj/Debug/net8.0/Async_Await.genruntimeconfig.cache
|
/workspaces/CSharp/Async_Await/obj/Debug/net8.0/Async_Await.genruntimeconfig.cache
|
||||||
/workspaces/CSharp/Async_Await/obj/Debug/net8.0/ref/Async_Await.dll
|
/workspaces/CSharp/Async_Await/obj/Debug/net8.0/ref/Async_Await.dll
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
735e7f2d774271d1780a96fe641c2f5ec6cf42f9b6bbfa8ed546e26d04a0fe35
|
735e7f2d774271d1780a96fe641c2f5ec6cf42f9b6bbfa8ed546e26d04a0fe35
|
||||||
|
|
|
@ -1,71 +1,71 @@
|
||||||
{
|
{
|
||||||
"version": 3,
|
"version": 3,
|
||||||
"targets": {
|
"targets": {
|
||||||
"net8.0": {}
|
"net8.0": {}
|
||||||
},
|
},
|
||||||
"libraries": {},
|
"libraries": {},
|
||||||
"projectFileDependencyGroups": {
|
"projectFileDependencyGroups": {
|
||||||
"net8.0": []
|
"net8.0": []
|
||||||
},
|
},
|
||||||
"packageFolders": {
|
"packageFolders": {
|
||||||
"/root/.nuget/packages/": {}
|
"/root/.nuget/packages/": {}
|
||||||
},
|
},
|
||||||
"project": {
|
"project": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"restore": {
|
"restore": {
|
||||||
"projectUniqueName": "/workspaces/CSharp/Async_Await/Async_Await.csproj",
|
"projectUniqueName": "/workspaces/CSharp/Async_Await/Async_Await.csproj",
|
||||||
"projectName": "Async_Await",
|
"projectName": "Async_Await",
|
||||||
"projectPath": "/workspaces/CSharp/Async_Await/Async_Await.csproj",
|
"projectPath": "/workspaces/CSharp/Async_Await/Async_Await.csproj",
|
||||||
"packagesPath": "/root/.nuget/packages/",
|
"packagesPath": "/root/.nuget/packages/",
|
||||||
"outputPath": "/workspaces/CSharp/Async_Await/obj/",
|
"outputPath": "/workspaces/CSharp/Async_Await/obj/",
|
||||||
"projectStyle": "PackageReference",
|
"projectStyle": "PackageReference",
|
||||||
"configFilePaths": [
|
"configFilePaths": [
|
||||||
"/root/.nuget/NuGet/NuGet.Config"
|
"/root/.nuget/NuGet/NuGet.Config"
|
||||||
],
|
],
|
||||||
"originalTargetFrameworks": [
|
"originalTargetFrameworks": [
|
||||||
"net8.0"
|
"net8.0"
|
||||||
],
|
],
|
||||||
"sources": {
|
"sources": {
|
||||||
"https://api.nuget.org/v3/index.json": {}
|
"https://api.nuget.org/v3/index.json": {}
|
||||||
},
|
},
|
||||||
"frameworks": {
|
"frameworks": {
|
||||||
"net8.0": {
|
"net8.0": {
|
||||||
"targetAlias": "net8.0",
|
"targetAlias": "net8.0",
|
||||||
"projectReferences": {}
|
"projectReferences": {}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"warningProperties": {
|
"warningProperties": {
|
||||||
"warnAsError": [
|
"warnAsError": [
|
||||||
"NU1605"
|
"NU1605"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"restoreAuditProperties": {
|
"restoreAuditProperties": {
|
||||||
"enableAudit": "true",
|
"enableAudit": "true",
|
||||||
"auditLevel": "low",
|
"auditLevel": "low",
|
||||||
"auditMode": "direct"
|
"auditMode": "direct"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"frameworks": {
|
"frameworks": {
|
||||||
"net8.0": {
|
"net8.0": {
|
||||||
"targetAlias": "net8.0",
|
"targetAlias": "net8.0",
|
||||||
"imports": [
|
"imports": [
|
||||||
"net461",
|
"net461",
|
||||||
"net462",
|
"net462",
|
||||||
"net47",
|
"net47",
|
||||||
"net471",
|
"net471",
|
||||||
"net472",
|
"net472",
|
||||||
"net48",
|
"net48",
|
||||||
"net481"
|
"net481"
|
||||||
],
|
],
|
||||||
"assetTargetFallback": true,
|
"assetTargetFallback": true,
|
||||||
"warn": true,
|
"warn": true,
|
||||||
"frameworkReferences": {
|
"frameworkReferences": {
|
||||||
"Microsoft.NETCore.App": {
|
"Microsoft.NETCore.App": {
|
||||||
"privateAssets": "all"
|
"privateAssets": "all"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/8.0.410/PortableRuntimeIdentifierGraph.json"
|
"runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/8.0.410/PortableRuntimeIdentifierGraph.json"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,8 +1,8 @@
|
||||||
{
|
{
|
||||||
"version": 2,
|
"version": 2,
|
||||||
"dgSpecHash": "WpEPBuOGmH8=",
|
"dgSpecHash": "WpEPBuOGmH8=",
|
||||||
"success": true,
|
"success": true,
|
||||||
"projectFilePath": "/workspaces/CSharp/Async_Await/Async_Await.csproj",
|
"projectFilePath": "/workspaces/CSharp/Async_Await/Async_Await.csproj",
|
||||||
"expectedPackageFiles": [],
|
"expectedPackageFiles": [],
|
||||||
"logs": []
|
"logs": []
|
||||||
}
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 17
|
||||||
|
VisualStudioVersion = 17.5.2.0
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Async_Await", "Async_Await\Async_Await.csproj", "{43D83C40-3641-95EC-E9D8-56C37192E099}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{43D83C40-3641-95EC-E9D8-56C37192E099}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{43D83C40-3641-95EC-E9D8-56C37192E099}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{43D83C40-3641-95EC-E9D8-56C37192E099}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{43D83C40-3641-95EC-E9D8-56C37192E099}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {01C1DFBB-F25B-4F05-9057-239C7852F3E2}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
|
@ -1,65 +1,65 @@
|
||||||
|
|
||||||
# 🛠️ C# Projekt in Visual Studio Code einrichten und ausführen
|
# 🛠️ C# Projekt in Visual Studio Code einrichten und ausführen
|
||||||
|
|
||||||
## ✅ Voraussetzungen
|
## ✅ Voraussetzungen
|
||||||
|
|
||||||
- [.NET SDK](https://dotnet.microsoft.com/en-us/download) installiert
|
- [.NET SDK](https://dotnet.microsoft.com/en-us/download) installiert
|
||||||
Prüfen mit:
|
Prüfen mit:
|
||||||
```bash
|
```bash
|
||||||
dotnet --version
|
dotnet --version
|
||||||
```
|
```
|
||||||
- **Visual Studio Code** installiert
|
- **Visual Studio Code** installiert
|
||||||
- In VS Code folgende Extensions installieren:
|
- In VS Code folgende Extensions installieren:
|
||||||
- **C# Dev Kit**
|
- **C# Dev Kit**
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 📁 Neues C# Konsolenprojekt erstellen
|
## 📁 Neues C# Konsolenprojekt erstellen
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
dotnet new console -n HelloWorld
|
dotnet new console -n HelloWorld
|
||||||
cd HelloWorld
|
cd HelloWorld
|
||||||
code .
|
code .
|
||||||
```
|
```
|
||||||
|
|
||||||
| Befehl | Bedeutung |
|
| Befehl | Bedeutung |
|
||||||
|--------|-----------|
|
|--------|-----------|
|
||||||
| `dotnet new console` | Erstellt ein Konsolenprojekt |
|
| `dotnet new console` | Erstellt ein Konsolenprojekt |
|
||||||
| `-n HelloWorld` | Legt den Projektnamen fest |
|
| `-n HelloWorld` | Legt den Projektnamen fest |
|
||||||
| `code .` | Öffnet das Projekt in VS Code |
|
| `code .` | Öffnet das Projekt in VS Code |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 📦 Projektstruktur
|
## 📦 Projektstruktur
|
||||||
|
|
||||||
Nach dem Öffnen in VS Code solltest du sehen:
|
Nach dem Öffnen in VS Code solltest du sehen:
|
||||||
|
|
||||||
```
|
```
|
||||||
HelloWorld/
|
HelloWorld/
|
||||||
├── Program.cs // Hauptprogramm
|
├── Program.cs // Hauptprogramm
|
||||||
├── HelloWorld.csproj // Projektdefinition
|
├── HelloWorld.csproj // Projektdefinition
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## ▶️ Projekt ausführen
|
## ▶️ Projekt ausführen
|
||||||
|
|
||||||
### Über Terminal:
|
### Über Terminal:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
dotnet run
|
dotnet run
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 📌 Nützliche Befehle
|
## 📌 Nützliche Befehle
|
||||||
|
|
||||||
| Befehl | Beschreibung |
|
| Befehl | Beschreibung |
|
||||||
| --------------------- | ----------------------------------------- |
|
| --------------------- | ----------------------------------------- |
|
||||||
| `dotnet build` | Projekt kompilieren |
|
| `dotnet build` | Projekt kompilieren |
|
||||||
| `dotnet run` | Projekt bauen und ausführen |
|
| `dotnet run` | Projekt bauen und ausführen |
|
||||||
| `dotnet clean` | Build-Ordner bereinigen |
|
| `dotnet clean` | Build-Ordner bereinigen |
|
||||||
| `dotnet new` | Neue Projektvorlagen anzeigen |
|
| `dotnet new` | Neue Projektvorlagen anzeigen |
|
||||||
| `dotnet list package` | Installierte Pakete anzeigen |
|
| `dotnet list package` | Installierte Pakete anzeigen |
|
||||||
| ``dotnet test`` | Führt automatisierte Tests im Projekt aus |
|
| ``dotnet test`` | Führt automatisierte Tests im Projekt aus |
|
||||||
|
|
||||||
|
|
300
mqtt_task_pr3.cs
300
mqtt_task_pr3.cs
|
@ -1,151 +1,151 @@
|
||||||
using MQTTnet;
|
using MQTTnet;
|
||||||
using MQTTnet.Client;
|
using MQTTnet.Client;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace MqttSensorApp
|
namespace MqttSensorApp
|
||||||
{
|
{
|
||||||
// TODO: Aufgabe 3: Implementieren Sie die SensorData-Klasse
|
// TODO: Aufgabe 3: Implementieren Sie die SensorData-Klasse
|
||||||
public class SensorData
|
public class SensorData
|
||||||
{
|
{
|
||||||
// TODO: Ergänzen Sie Properties für Temperature, Humidity und Location
|
// TODO: Ergänzen Sie Properties für Temperature, Humidity und Location
|
||||||
// Nutzen Sie JsonPropertyName-Attribute für die JSON-Deserialisierung
|
// Nutzen Sie JsonPropertyName-Attribute für die JSON-Deserialisierung
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
private static List<double> receivedTemperatures = new List<double>();
|
private static List<double> receivedTemperatures = new List<double>();
|
||||||
|
|
||||||
public static async Task Main(string[] args)
|
public static async Task Main(string[] args)
|
||||||
{
|
{
|
||||||
Console.WriteLine("MQTT Sensor Data Processor gestartet...");
|
Console.WriteLine("MQTT Sensor Data Processor gestartet...");
|
||||||
|
|
||||||
// MQTT Client Setup (bereits implementiert)
|
// MQTT Client Setup (bereits implementiert)
|
||||||
var factory = new MqttFactory();
|
var factory = new MqttFactory();
|
||||||
var client = factory.CreateMqttClient();
|
var client = factory.CreateMqttClient();
|
||||||
|
|
||||||
// Event Handler für empfangene Nachrichten
|
// Event Handler für empfangene Nachrichten
|
||||||
client.ApplicationMessageReceivedAsync += async e =>
|
client.ApplicationMessageReceivedAsync += async e =>
|
||||||
{
|
{
|
||||||
var payload = System.Text.Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
|
var payload = System.Text.Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
|
||||||
await ProcessMessage(payload);
|
await ProcessMessage(payload);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Verbindungsoptionen
|
// Verbindungsoptionen
|
||||||
var options = new MqttClientOptionsBuilder()
|
var options = new MqttClientOptionsBuilder()
|
||||||
.WithTcpServer("broker.hivemq.com", 1883)
|
.WithTcpServer("broker.hivemq.com", 1883)
|
||||||
.WithClientId($"SensorClient_{Guid.NewGuid()}")
|
.WithClientId($"SensorClient_{Guid.NewGuid()}")
|
||||||
.WithCleanSession()
|
.WithCleanSession()
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Verbindung aufbauen
|
// Verbindung aufbauen
|
||||||
await client.ConnectAsync(options);
|
await client.ConnectAsync(options);
|
||||||
Console.WriteLine("Mit MQTT Broker verbunden");
|
Console.WriteLine("Mit MQTT Broker verbunden");
|
||||||
|
|
||||||
// Topic abonnieren
|
// Topic abonnieren
|
||||||
await client.SubscribeAsync("sensor/data/room1");
|
await client.SubscribeAsync("sensor/data/room1");
|
||||||
Console.WriteLine("Topic 'sensor/data/room1' abonniert");
|
Console.WriteLine("Topic 'sensor/data/room1' abonniert");
|
||||||
|
|
||||||
// Simulierte Sensordaten senden (für Testing)
|
// Simulierte Sensordaten senden (für Testing)
|
||||||
await SendTestData(client);
|
await SendTestData(client);
|
||||||
|
|
||||||
// Programm laufen lassen
|
// Programm laufen lassen
|
||||||
Console.WriteLine("Drücken Sie eine Taste zum Beenden...");
|
Console.WriteLine("Drücken Sie eine Taste zum Beenden...");
|
||||||
Console.ReadKey();
|
Console.ReadKey();
|
||||||
|
|
||||||
// Verbindung trennen
|
// Verbindung trennen
|
||||||
await client.DisconnectAsync();
|
await client.DisconnectAsync();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Fehler: {ex.Message}");
|
Console.WriteLine($"Fehler: {ex.Message}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task ProcessMessage(string jsonPayload)
|
private static async Task ProcessMessage(string jsonPayload)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Empfangen: {jsonPayload}");
|
Console.WriteLine($"Empfangen: {jsonPayload}");
|
||||||
|
|
||||||
// TODO: Aufgabe 3: Deserialisieren Sie den JSON zu SensorData
|
// TODO: Aufgabe 3: Deserialisieren Sie den JSON zu SensorData
|
||||||
// var sensorData = JsonSerializer.Deserialize<SensorData>(jsonPayload);
|
// var sensorData = JsonSerializer.Deserialize<SensorData>(jsonPayload);
|
||||||
|
|
||||||
// Temporäre Lösung für Aufgabe 1 und 2
|
// Temporäre Lösung für Aufgabe 1 und 2
|
||||||
var data = JsonSerializer.Deserialize<Dictionary<string, object>>(jsonPayload);
|
var data = JsonSerializer.Deserialize<Dictionary<string, object>>(jsonPayload);
|
||||||
|
|
||||||
if (data != null && data.ContainsKey("temperature"))
|
if (data != null && data.ContainsKey("temperature"))
|
||||||
{
|
{
|
||||||
var temp = Convert.ToDouble(data["temperature"].ToString());
|
var temp = Convert.ToDouble(data["temperature"].ToString());
|
||||||
receivedTemperatures.Add(temp);
|
receivedTemperatures.Add(temp);
|
||||||
|
|
||||||
// Nachricht analysieren
|
// Nachricht analysieren
|
||||||
var analysis = AnalyzeMessage(temp);
|
var analysis = AnalyzeMessage(temp);
|
||||||
Console.WriteLine($"Analyse: {analysis}");
|
Console.WriteLine($"Analyse: {analysis}");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Alle 5 Nachrichten: Hohe Temperaturen anzeigen
|
// Alle 5 Nachrichten: Hohe Temperaturen anzeigen
|
||||||
if (receivedTemperatures.Count % 5 == 0)
|
if (receivedTemperatures.Count % 5 == 0)
|
||||||
{
|
{
|
||||||
var highTemps = FilterHighTemperatures(receivedTemperatures);
|
var highTemps = FilterHighTemperatures(receivedTemperatures);
|
||||||
Console.WriteLine($"Hohe Temperaturen (>25°C): {string.Join(", ", highTemps.Select(t => $"{t:F1}°C"))}");
|
Console.WriteLine($"Hohe Temperaturen (>25°C): {string.Join(", ", highTemps.Select(t => $"{t:F1}°C"))}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Fehler beim Verarbeiten der Nachricht: {ex.Message}");
|
Console.WriteLine($"Fehler beim Verarbeiten der Nachricht: {ex.Message}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Aufgabe 1: LINQ-Abfrage implementieren
|
// TODO: Aufgabe 1: LINQ-Abfrage implementieren
|
||||||
private static IEnumerable<double> FilterHighTemperatures(List<double> temperatures)
|
private static IEnumerable<double> FilterHighTemperatures(List<double> temperatures)
|
||||||
{
|
{
|
||||||
// TODO: Verwenden Sie LINQ (Where-Klausel), um alle Temperaturen > 25°C zu filtern
|
// TODO: Verwenden Sie LINQ (Where-Klausel), um alle Temperaturen > 25°C zu filtern
|
||||||
// Beispiel: return temperatures.Where(...);
|
// Beispiel: return temperatures.Where(...);
|
||||||
|
|
||||||
return new List<double>(); // Platzhalter, ersetzen Sie diese Zeile
|
return new List<double>(); // Platzhalter, ersetzen Sie diese Zeile
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Aufgabe 2: Pattern Matching implementieren
|
// TODO: Aufgabe 2: Pattern Matching implementieren
|
||||||
private static string AnalyzeMessage(object value)
|
private static string AnalyzeMessage(object value)
|
||||||
{
|
{
|
||||||
// TODO: Implementieren Sie Pattern Matching mit switch expression
|
// TODO: Implementieren Sie Pattern Matching mit switch expression
|
||||||
// Behandeln Sie folgende Fälle:
|
// Behandeln Sie folgende Fälle:
|
||||||
// - double über 30: "WARNUNG: Sehr hohe Temperatur!"
|
// - double über 30: "WARNUNG: Sehr hohe Temperatur!"
|
||||||
// - double zwischen 25-30: "Hohe Temperatur"
|
// - double zwischen 25-30: "Hohe Temperatur"
|
||||||
// - double unter 25: "Normale Temperatur"
|
// - double unter 25: "Normale Temperatur"
|
||||||
// - string: "Text-Nachricht erhalten"
|
// - string: "Text-Nachricht erhalten"
|
||||||
// - null: "Keine Daten"
|
// - null: "Keine Daten"
|
||||||
// - default: "Unbekannter Datentyp"
|
// - default: "Unbekannter Datentyp"
|
||||||
|
|
||||||
return "TODO: Pattern Matching implementieren"; // Platzhalter
|
return "TODO: Pattern Matching implementieren"; // Platzhalter
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hilfsmethode: Testnachrichten senden
|
// Hilfsmethode: Testnachrichten senden
|
||||||
private static async Task SendTestData(IMqttClient client)
|
private static async Task SendTestData(IMqttClient client)
|
||||||
{
|
{
|
||||||
var testData = new[]
|
var testData = new[]
|
||||||
{
|
{
|
||||||
new { temperature = 22.5, humidity = 65.0, location = "Room1" },
|
new { temperature = 22.5, humidity = 65.0, location = "Room1" },
|
||||||
new { temperature = 28.3, humidity = 58.2, location = "Room1" },
|
new { temperature = 28.3, humidity = 58.2, location = "Room1" },
|
||||||
new { temperature = 19.8, humidity = 72.1, location = "Room1" },
|
new { temperature = 19.8, humidity = 72.1, location = "Room1" },
|
||||||
new { temperature = 31.2, humidity = 45.3, location = "Room1" },
|
new { temperature = 31.2, humidity = 45.3, location = "Room1" },
|
||||||
new { temperature = 26.7, humidity = 62.8, location = "Room1" }
|
new { temperature = 26.7, humidity = 62.8, location = "Room1" }
|
||||||
};
|
};
|
||||||
|
|
||||||
foreach (var data in testData)
|
foreach (var data in testData)
|
||||||
{
|
{
|
||||||
var json = JsonSerializer.Serialize(data);
|
var json = JsonSerializer.Serialize(data);
|
||||||
var message = new MqttApplicationMessageBuilder()
|
var message = new MqttApplicationMessageBuilder()
|
||||||
.WithTopic("sensor/data/room1")
|
.WithTopic("sensor/data/room1")
|
||||||
.WithPayload(json)
|
.WithPayload(json)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
await client.PublishAsync(message);
|
await client.PublishAsync(message);
|
||||||
await Task.Delay(2000); // 2 Sekunden Pause
|
await Task.Delay(2000); // 2 Sekunden Pause
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,151 +1,151 @@
|
||||||
using MQTTnet;
|
using MQTTnet;
|
||||||
using MQTTnet.Client;
|
using MQTTnet.Client;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace MqttSensorApp
|
namespace MqttSensorApp
|
||||||
{
|
{
|
||||||
// LÖSUNG Aufgabe 3: SensorData-Klasse implementieren
|
// LÖSUNG Aufgabe 3: SensorData-Klasse implementieren
|
||||||
public class SensorData
|
public class SensorData
|
||||||
{
|
{
|
||||||
[JsonPropertyName("temperature")]
|
[JsonPropertyName("temperature")]
|
||||||
public double Temperature { get; set; }
|
public double Temperature { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("humidity")]
|
[JsonPropertyName("humidity")]
|
||||||
public double Humidity { get; set; }
|
public double Humidity { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("location")]
|
[JsonPropertyName("location")]
|
||||||
public string Location { get; set; } = string.Empty;
|
public string Location { get; set; } = string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
private static List<double> receivedTemperatures = new List<double>();
|
private static List<double> receivedTemperatures = new List<double>();
|
||||||
|
|
||||||
public static async Task Main(string[] args)
|
public static async Task Main(string[] args)
|
||||||
{
|
{
|
||||||
Console.WriteLine("MQTT Sensor Data Processor gestartet...");
|
Console.WriteLine("MQTT Sensor Data Processor gestartet...");
|
||||||
|
|
||||||
// MQTT Client Setup (bereits implementiert)
|
// MQTT Client Setup (bereits implementiert)
|
||||||
var factory = new MqttFactory();
|
var factory = new MqttFactory();
|
||||||
var client = factory.CreateMqttClient();
|
var client = factory.CreateMqttClient();
|
||||||
|
|
||||||
// Event Handler für empfangene Nachrichten
|
// Event Handler für empfangene Nachrichten
|
||||||
client.ApplicationMessageReceivedAsync += async e =>
|
client.ApplicationMessageReceivedAsync += async e =>
|
||||||
{
|
{
|
||||||
var payload = System.Text.Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
|
var payload = System.Text.Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
|
||||||
await ProcessMessage(payload);
|
await ProcessMessage(payload);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Verbindungsoptionen
|
// Verbindungsoptionen
|
||||||
var options = new MqttClientOptionsBuilder()
|
var options = new MqttClientOptionsBuilder()
|
||||||
.WithTcpServer("broker.hivemq.com", 1883)
|
.WithTcpServer("broker.hivemq.com", 1883)
|
||||||
.WithClientId($"SensorClient_{Guid.NewGuid()}")
|
.WithClientId($"SensorClient_{Guid.NewGuid()}")
|
||||||
.WithCleanSession()
|
.WithCleanSession()
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Verbindung aufbauen
|
// Verbindung aufbauen
|
||||||
await client.ConnectAsync(options);
|
await client.ConnectAsync(options);
|
||||||
Console.WriteLine("Mit MQTT Broker verbunden");
|
Console.WriteLine("Mit MQTT Broker verbunden");
|
||||||
|
|
||||||
// Topic abonnieren
|
// Topic abonnieren
|
||||||
await client.SubscribeAsync("sensor/data/room1");
|
await client.SubscribeAsync("sensor/data/room1");
|
||||||
Console.WriteLine("Topic 'sensor/data/room1' abonniert");
|
Console.WriteLine("Topic 'sensor/data/room1' abonniert");
|
||||||
|
|
||||||
// Simulierte Sensordaten senden (für Testing)
|
// Simulierte Sensordaten senden (für Testing)
|
||||||
await SendTestData(client);
|
await SendTestData(client);
|
||||||
|
|
||||||
// Programm laufen lassen
|
// Programm laufen lassen
|
||||||
Console.WriteLine("Drücken Sie eine Taste zum Beenden...");
|
Console.WriteLine("Drücken Sie eine Taste zum Beenden...");
|
||||||
Console.ReadKey();
|
Console.ReadKey();
|
||||||
|
|
||||||
// Verbindung trennen
|
// Verbindung trennen
|
||||||
await client.DisconnectAsync();
|
await client.DisconnectAsync();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Fehler: {ex.Message}");
|
Console.WriteLine($"Fehler: {ex.Message}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task ProcessMessage(string jsonPayload)
|
private static async Task ProcessMessage(string jsonPayload)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Empfangen: {jsonPayload}");
|
Console.WriteLine($"Empfangen: {jsonPayload}");
|
||||||
|
|
||||||
// LÖSUNG Aufgabe 3: Deserialisieren des JSON zu SensorData
|
// LÖSUNG Aufgabe 3: Deserialisieren des JSON zu SensorData
|
||||||
var sensorData = JsonSerializer.Deserialize<SensorData>(jsonPayload);
|
var sensorData = JsonSerializer.Deserialize<SensorData>(jsonPayload);
|
||||||
|
|
||||||
if (sensorData != null)
|
if (sensorData != null)
|
||||||
{
|
{
|
||||||
receivedTemperatures.Add(sensorData.Temperature);
|
receivedTemperatures.Add(sensorData.Temperature);
|
||||||
|
|
||||||
// LÖSUNG Aufgabe 2: Pattern Matching anwenden
|
// LÖSUNG Aufgabe 2: Pattern Matching anwenden
|
||||||
var analysis = AnalyzeMessage(sensorData.Temperature);
|
var analysis = AnalyzeMessage(sensorData.Temperature);
|
||||||
Console.WriteLine($"Analyse: {analysis}");
|
Console.WriteLine($"Analyse: {analysis}");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Alle 5 Nachrichten: Hohe Temperaturen anzeigen
|
// Alle 5 Nachrichten: Hohe Temperaturen anzeigen
|
||||||
if (receivedTemperatures.Count % 5 == 0)
|
if (receivedTemperatures.Count % 5 == 0)
|
||||||
{
|
{
|
||||||
var highTemps = FilterHighTemperatures(receivedTemperatures);
|
var highTemps = FilterHighTemperatures(receivedTemperatures);
|
||||||
Console.WriteLine($"Hohe Temperaturen (>25°C): {string.Join(", ", highTemps.Select(t => $"{t:F1}°C"))}");
|
Console.WriteLine($"Hohe Temperaturen (>25°C): {string.Join(", ", highTemps.Select(t => $"{t:F1}°C"))}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Fehler beim Verarbeiten der Nachricht: {ex.Message}");
|
Console.WriteLine($"Fehler beim Verarbeiten der Nachricht: {ex.Message}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// LÖSUNG Aufgabe 1: LINQ-Abfrage implementieren
|
// LÖSUNG Aufgabe 1: LINQ-Abfrage implementieren
|
||||||
private static IEnumerable<double> FilterHighTemperatures(List<double> temperatures)
|
private static IEnumerable<double> FilterHighTemperatures(List<double> temperatures)
|
||||||
{
|
{
|
||||||
// LINQ Where-Klausel verwenden, um Temperaturen > 25°C zu filtern
|
// LINQ Where-Klausel verwenden, um Temperaturen > 25°C zu filtern
|
||||||
return temperatures.Where(temp => temp > 25.0);
|
return temperatures.Where(temp => temp > 25.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// LÖSUNG Aufgabe 2: Pattern Matching implementieren
|
// LÖSUNG Aufgabe 2: Pattern Matching implementieren
|
||||||
private static string AnalyzeMessage(object value)
|
private static string AnalyzeMessage(object value)
|
||||||
{
|
{
|
||||||
// Switch expression mit Pattern Matching
|
// Switch expression mit Pattern Matching
|
||||||
return value switch
|
return value switch
|
||||||
{
|
{
|
||||||
double temp when temp > 30.0 => "WARNUNG: Sehr hohe Temperatur!",
|
double temp when temp > 30.0 => "WARNUNG: Sehr hohe Temperatur!",
|
||||||
double temp when temp >= 25.0 && temp <= 30.0 => "Hohe Temperatur",
|
double temp when temp >= 25.0 && temp <= 30.0 => "Hohe Temperatur",
|
||||||
double temp when temp < 25.0 => "Normale Temperatur",
|
double temp when temp < 25.0 => "Normale Temperatur",
|
||||||
string text => "Text-Nachricht erhalten",
|
string text => "Text-Nachricht erhalten",
|
||||||
null => "Keine Daten",
|
null => "Keine Daten",
|
||||||
_ => "Unbekannter Datentyp"
|
_ => "Unbekannter Datentyp"
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hilfsmethode: Testnachrichten senden
|
// Hilfsmethode: Testnachrichten senden
|
||||||
private static async Task SendTestData(IMqttClient client)
|
private static async Task SendTestData(IMqttClient client)
|
||||||
{
|
{
|
||||||
var testData = new[]
|
var testData = new[]
|
||||||
{
|
{
|
||||||
new { temperature = 22.5, humidity = 65.0, location = "Room1" },
|
new { temperature = 22.5, humidity = 65.0, location = "Room1" },
|
||||||
new { temperature = 28.3, humidity = 58.2, location = "Room1" },
|
new { temperature = 28.3, humidity = 58.2, location = "Room1" },
|
||||||
new { temperature = 19.8, humidity = 72.1, location = "Room1" },
|
new { temperature = 19.8, humidity = 72.1, location = "Room1" },
|
||||||
new { temperature = 31.2, humidity = 45.3, location = "Room1" },
|
new { temperature = 31.2, humidity = 45.3, location = "Room1" },
|
||||||
new { temperature = 26.7, humidity = 62.8, location = "Room1" }
|
new { temperature = 26.7, humidity = 62.8, location = "Room1" }
|
||||||
};
|
};
|
||||||
|
|
||||||
foreach (var data in testData)
|
foreach (var data in testData)
|
||||||
{
|
{
|
||||||
var json = JsonSerializer.Serialize(data);
|
var json = JsonSerializer.Serialize(data);
|
||||||
var message = new MqttApplicationMessageBuilder()
|
var message = new MqttApplicationMessageBuilder()
|
||||||
.WithTopic("sensor/data/room1")
|
.WithTopic("sensor/data/room1")
|
||||||
.WithPayload(json)
|
.WithPayload(json)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
await client.PublishAsync(message);
|
await client.PublishAsync(message);
|
||||||
await Task.Delay(2000); // 2 Sekunden Pause
|
await Task.Delay(2000); // 2 Sekunden Pause
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue