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": "Ubuntu",
|
||||
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
|
||||
"image": "mcr.microsoft.com/devcontainers/base:jammy"
|
||||
|
||||
// Features to add to the dev container. More info: https://containers.dev/features.
|
||||
// "features": {},
|
||||
|
||||
// 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"
|
||||
}
|
||||
{
|
||||
"name": "C# Dev Container",
|
||||
"image": "mcr.microsoft.com/dotnet/sdk:8.0",
|
||||
"customizations": {
|
||||
"vscode": {
|
||||
"extensions": ["ms-dotnettools.csharp"]
|
||||
}
|
||||
},
|
||||
"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">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class Ex1
|
||||
{
|
||||
public static async Task Run()
|
||||
{
|
||||
/* Example of async and await */
|
||||
Console.WriteLine("Start");
|
||||
await DoWorkAsync();
|
||||
Console.WriteLine("Finished");
|
||||
}
|
||||
|
||||
private static async Task DoWorkAsync()
|
||||
{
|
||||
await Task.Delay(2000);
|
||||
}
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class Ex1
|
||||
{
|
||||
public static async Task Run()
|
||||
{
|
||||
/* Example of async and await */
|
||||
Console.WriteLine("Start");
|
||||
await DoWorkAsync();
|
||||
Console.WriteLine("Finished");
|
||||
}
|
||||
|
||||
private static async Task DoWorkAsync()
|
||||
{
|
||||
await Task.Delay(2000);
|
||||
}
|
||||
}
|
|
@ -1,26 +1,26 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class Ex2
|
||||
{
|
||||
public static async Task Run()
|
||||
{
|
||||
/* Example of Tasks, showcasing Ressource Efficency */
|
||||
var task1 = DoMoreWorkAsync("One");
|
||||
var task2 = DoMoreWorkAsync("Two");
|
||||
var task3 = DoMoreWorkAsync("Three");
|
||||
|
||||
await task1;
|
||||
await task2;
|
||||
await task3;
|
||||
Console.WriteLine("Finished example showcasing resource efficency\n\n");
|
||||
}
|
||||
|
||||
private static async Task DoMoreWorkAsync(string name)
|
||||
{
|
||||
Console.WriteLine($"Task {name} is being executed by Thread {Thread.CurrentThread.ManagedThreadId}");
|
||||
await Task.Delay(2000);
|
||||
Console.WriteLine($"Task {name} is done");
|
||||
|
||||
}
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class Ex2
|
||||
{
|
||||
public static async Task Run()
|
||||
{
|
||||
/* Example of Tasks, showcasing Ressource Efficency */
|
||||
var task1 = DoMoreWorkAsync("One");
|
||||
var task2 = DoMoreWorkAsync("Two");
|
||||
var task3 = DoMoreWorkAsync("Three");
|
||||
|
||||
await task1;
|
||||
await task2;
|
||||
await task3;
|
||||
Console.WriteLine("Finished example showcasing resource efficency\n\n");
|
||||
}
|
||||
|
||||
private static async Task DoMoreWorkAsync(string name)
|
||||
{
|
||||
Console.WriteLine($"Task {name} is being executed by Thread {Thread.CurrentThread.ManagedThreadId}");
|
||||
await Task.Delay(2000);
|
||||
Console.WriteLine($"Task {name} is done");
|
||||
|
||||
}
|
||||
}
|
|
@ -1,23 +1,23 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class Ex3
|
||||
{
|
||||
public static async Task Run()
|
||||
{
|
||||
/* Example using a Task<T> type */
|
||||
var lowerCaseString = "i want to be an upper case string";
|
||||
Console.WriteLine("main: " + lowerCaseString);
|
||||
var upperCaseString = await WorkOnStringAsync(lowerCaseString);
|
||||
Console.WriteLine("main: " + upperCaseString);
|
||||
}
|
||||
|
||||
private static async Task<string> WorkOnStringAsync(string s)
|
||||
{
|
||||
Console.WriteLine("Task: Async task started");
|
||||
Console.WriteLine("Task: Waiting for 2 sec");
|
||||
await Task.Delay(2000);
|
||||
Console.WriteLine("Task: Done waiting, returning the upper case string");
|
||||
return s.ToUpper();
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class Ex3
|
||||
{
|
||||
public static async Task Run()
|
||||
{
|
||||
/* Example using a Task<T> type */
|
||||
var lowerCaseString = "i want to be an upper case string";
|
||||
Console.WriteLine("main: " + lowerCaseString);
|
||||
var upperCaseString = await WorkOnStringAsync(lowerCaseString);
|
||||
Console.WriteLine("main: " + upperCaseString);
|
||||
}
|
||||
|
||||
private static async Task<string> WorkOnStringAsync(string s)
|
||||
{
|
||||
Console.WriteLine("Task: Async task started");
|
||||
Console.WriteLine("Task: Waiting for 2 sec");
|
||||
await Task.Delay(2000);
|
||||
Console.WriteLine("Task: Done waiting, returning the upper case string");
|
||||
return s.ToUpper();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,28 +1,28 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class Ex4
|
||||
{
|
||||
public static async Task Run()
|
||||
{
|
||||
/* Example working with Tasks and awaiting them later */
|
||||
var lowerCaseString = "i want to be an upper case string";
|
||||
Console.WriteLine("main: " + lowerCaseString);
|
||||
// Start the task but do not await yet
|
||||
Task<string> upperCaseTask = WorkOnStringAsync(lowerCaseString);
|
||||
Console.WriteLine("main: WorkOnStringAsync has been called, doing other work...");
|
||||
// Now await the result
|
||||
Console.WriteLine("main: Now waiting for the result");
|
||||
var upperCaseString = await upperCaseTask;
|
||||
Console.WriteLine("main: " + upperCaseString);
|
||||
}
|
||||
|
||||
private static async Task<string> WorkOnStringAsync(string s)
|
||||
{
|
||||
Console.WriteLine("Task: Async task started");
|
||||
Console.WriteLine("Task: Waiting for 2 sec");
|
||||
await Task.Delay(2000);
|
||||
Console.WriteLine("Task: Done waiting, returning the upper case string");
|
||||
return s.ToUpper();
|
||||
}
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class Ex4
|
||||
{
|
||||
public static async Task Run()
|
||||
{
|
||||
/* Example working with Tasks and awaiting them later */
|
||||
var lowerCaseString = "i want to be an upper case string";
|
||||
Console.WriteLine("main: " + lowerCaseString);
|
||||
// Start the task but do not await yet
|
||||
Task<string> upperCaseTask = WorkOnStringAsync(lowerCaseString);
|
||||
Console.WriteLine("main: WorkOnStringAsync has been called, doing other work...");
|
||||
// Now await the result
|
||||
Console.WriteLine("main: Now waiting for the result");
|
||||
var upperCaseString = await upperCaseTask;
|
||||
Console.WriteLine("main: " + upperCaseString);
|
||||
}
|
||||
|
||||
private static async Task<string> WorkOnStringAsync(string s)
|
||||
{
|
||||
Console.WriteLine("Task: Async task started");
|
||||
Console.WriteLine("Task: Waiting for 2 sec");
|
||||
await Task.Delay(2000);
|
||||
Console.WriteLine("Task: Done waiting, returning the upper case string");
|
||||
return s.ToUpper();
|
||||
}
|
||||
}
|
|
@ -1,27 +1,27 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class Ex5
|
||||
{
|
||||
public static async Task Run()
|
||||
{
|
||||
/* Example for Task Coordination using Task.WhenAll() */
|
||||
Random random = new();
|
||||
async Task<(int index, int waitingTime)> randomDelay(int index)
|
||||
{
|
||||
var waitingTime = random.Next(500, 2000);
|
||||
await Task.Delay(waitingTime);
|
||||
return (index, waitingTime);
|
||||
}
|
||||
|
||||
var delayTask1 = randomDelay(1);
|
||||
var delayTask2 = randomDelay(2);
|
||||
var delayTask3 = randomDelay(3);
|
||||
|
||||
await Task.WhenAll(delayTask1, delayTask2, delayTask3);
|
||||
|
||||
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 {delayTask3.Result.index} had to wait for {delayTask3.Result.waitingTime} ms");
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class Ex5
|
||||
{
|
||||
public static async Task Run()
|
||||
{
|
||||
/* Example for Task Coordination using Task.WhenAll() */
|
||||
Random random = new();
|
||||
async Task<(int index, int waitingTime)> randomDelay(int index)
|
||||
{
|
||||
var waitingTime = random.Next(500, 2000);
|
||||
await Task.Delay(waitingTime);
|
||||
return (index, waitingTime);
|
||||
}
|
||||
|
||||
var delayTask1 = randomDelay(1);
|
||||
var delayTask2 = randomDelay(2);
|
||||
var delayTask3 = randomDelay(3);
|
||||
|
||||
await Task.WhenAll(delayTask1, delayTask2, delayTask3);
|
||||
|
||||
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 {delayTask3.Result.index} had to wait for {delayTask3.Result.waitingTime} ms");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,27 +1,27 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class Ex6
|
||||
{
|
||||
public static async Task Run()
|
||||
{
|
||||
/* Example for Task Coordination using Task.WhenAny() */
|
||||
Random random = new();
|
||||
async Task<(int index, int waitingTime)> randomDelay(int index)
|
||||
{
|
||||
var waitingTime = random.Next(500, 2000);
|
||||
await Task.Delay(waitingTime);
|
||||
return (index, waitingTime);
|
||||
}
|
||||
|
||||
List<Task<(int index, int waitingTime)>> taskList = new();
|
||||
for (int i = 1; i < 10; i++)
|
||||
{
|
||||
var delayedTask = randomDelay(i);
|
||||
taskList.Add(delayedTask);
|
||||
}
|
||||
var fastestTask = await Task.WhenAny(taskList);
|
||||
Console.WriteLine($"The fastest task was Task {fastestTask.Result.index}. It took {fastestTask.Result.waitingTime} ms to complete.");
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class Ex6
|
||||
{
|
||||
public static async Task Run()
|
||||
{
|
||||
/* Example for Task Coordination using Task.WhenAny() */
|
||||
Random random = new();
|
||||
async Task<(int index, int waitingTime)> randomDelay(int index)
|
||||
{
|
||||
var waitingTime = random.Next(500, 2000);
|
||||
await Task.Delay(waitingTime);
|
||||
return (index, waitingTime);
|
||||
}
|
||||
|
||||
List<Task<(int index, int waitingTime)>> taskList = new();
|
||||
for (int i = 1; i < 10; i++)
|
||||
{
|
||||
var delayedTask = randomDelay(i);
|
||||
taskList.Add(delayedTask);
|
||||
}
|
||||
var fastestTask = await Task.WhenAny(taskList);
|
||||
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.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(string[] args)
|
||||
{
|
||||
if (args.Length == 0)
|
||||
{
|
||||
Console.WriteLine("Usage: dotnet run -- ex1|ex2|ex3|ex4|ex5|ex6");
|
||||
return;
|
||||
}
|
||||
|
||||
switch (args[0].ToLower())
|
||||
{
|
||||
case "ex1":
|
||||
await Ex1.Run();
|
||||
break;
|
||||
case "ex2":
|
||||
await Ex2.Run();
|
||||
break;
|
||||
case "ex3":
|
||||
await Ex3.Run();
|
||||
break;
|
||||
case "ex4":
|
||||
await Ex4.Run();
|
||||
break;
|
||||
case "ex5":
|
||||
await Ex5.Run();
|
||||
break;
|
||||
case "ex6":
|
||||
await Ex6.Run();
|
||||
break;
|
||||
default:
|
||||
Console.WriteLine($"Unknown example: {args[0]}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main(string[] args)
|
||||
{
|
||||
if (args.Length == 0)
|
||||
{
|
||||
Console.WriteLine("Usage: dotnet run -- ex1|ex2|ex3|ex4|ex5|ex6");
|
||||
return;
|
||||
}
|
||||
|
||||
switch (args[0].ToLower())
|
||||
{
|
||||
case "ex1":
|
||||
await Ex1.Run();
|
||||
break;
|
||||
case "ex2":
|
||||
await Ex2.Run();
|
||||
break;
|
||||
case "ex3":
|
||||
await Ex3.Run();
|
||||
break;
|
||||
case "ex4":
|
||||
await Ex4.Run();
|
||||
break;
|
||||
case "ex5":
|
||||
await Ex5.Run();
|
||||
break;
|
||||
case "ex6":
|
||||
await Ex6.Run();
|
||||
break;
|
||||
default:
|
||||
Console.WriteLine($"Unknown example: {args[0]}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,66 +1,66 @@
|
|||
{
|
||||
"format": 1,
|
||||
"restore": {
|
||||
"/workspaces/CSharp/Async_Await/Async_Await.csproj": {}
|
||||
},
|
||||
"projects": {
|
||||
"/workspaces/CSharp/Async_Await/Async_Await.csproj": {
|
||||
"version": "1.0.0",
|
||||
"restore": {
|
||||
"projectUniqueName": "/workspaces/CSharp/Async_Await/Async_Await.csproj",
|
||||
"projectName": "Async_Await",
|
||||
"projectPath": "/workspaces/CSharp/Async_Await/Async_Await.csproj",
|
||||
"packagesPath": "/root/.nuget/packages/",
|
||||
"outputPath": "/workspaces/CSharp/Async_Await/obj/",
|
||||
"projectStyle": "PackageReference",
|
||||
"configFilePaths": [
|
||||
"/root/.nuget/NuGet/NuGet.Config"
|
||||
],
|
||||
"originalTargetFrameworks": [
|
||||
"net8.0"
|
||||
],
|
||||
"sources": {
|
||||
"https://api.nuget.org/v3/index.json": {}
|
||||
},
|
||||
"frameworks": {
|
||||
"net8.0": {
|
||||
"targetAlias": "net8.0",
|
||||
"projectReferences": {}
|
||||
}
|
||||
},
|
||||
"warningProperties": {
|
||||
"warnAsError": [
|
||||
"NU1605"
|
||||
]
|
||||
},
|
||||
"restoreAuditProperties": {
|
||||
"enableAudit": "true",
|
||||
"auditLevel": "low",
|
||||
"auditMode": "direct"
|
||||
}
|
||||
},
|
||||
"frameworks": {
|
||||
"net8.0": {
|
||||
"targetAlias": "net8.0",
|
||||
"imports": [
|
||||
"net461",
|
||||
"net462",
|
||||
"net47",
|
||||
"net471",
|
||||
"net472",
|
||||
"net48",
|
||||
"net481"
|
||||
],
|
||||
"assetTargetFallback": true,
|
||||
"warn": true,
|
||||
"frameworkReferences": {
|
||||
"Microsoft.NETCore.App": {
|
||||
"privateAssets": "all"
|
||||
}
|
||||
},
|
||||
"runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/8.0.410/PortableRuntimeIdentifierGraph.json"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
"format": 1,
|
||||
"restore": {
|
||||
"/workspaces/CSharp/Async_Await/Async_Await.csproj": {}
|
||||
},
|
||||
"projects": {
|
||||
"/workspaces/CSharp/Async_Await/Async_Await.csproj": {
|
||||
"version": "1.0.0",
|
||||
"restore": {
|
||||
"projectUniqueName": "/workspaces/CSharp/Async_Await/Async_Await.csproj",
|
||||
"projectName": "Async_Await",
|
||||
"projectPath": "/workspaces/CSharp/Async_Await/Async_Await.csproj",
|
||||
"packagesPath": "/root/.nuget/packages/",
|
||||
"outputPath": "/workspaces/CSharp/Async_Await/obj/",
|
||||
"projectStyle": "PackageReference",
|
||||
"configFilePaths": [
|
||||
"/root/.nuget/NuGet/NuGet.Config"
|
||||
],
|
||||
"originalTargetFrameworks": [
|
||||
"net8.0"
|
||||
],
|
||||
"sources": {
|
||||
"https://api.nuget.org/v3/index.json": {}
|
||||
},
|
||||
"frameworks": {
|
||||
"net8.0": {
|
||||
"targetAlias": "net8.0",
|
||||
"projectReferences": {}
|
||||
}
|
||||
},
|
||||
"warningProperties": {
|
||||
"warnAsError": [
|
||||
"NU1605"
|
||||
]
|
||||
},
|
||||
"restoreAuditProperties": {
|
||||
"enableAudit": "true",
|
||||
"auditLevel": "low",
|
||||
"auditMode": "direct"
|
||||
}
|
||||
},
|
||||
"frameworks": {
|
||||
"net8.0": {
|
||||
"targetAlias": "net8.0",
|
||||
"imports": [
|
||||
"net461",
|
||||
"net462",
|
||||
"net47",
|
||||
"net471",
|
||||
"net472",
|
||||
"net48",
|
||||
"net481"
|
||||
],
|
||||
"assetTargetFallback": true,
|
||||
"warn": true,
|
||||
"frameworkReferences": {
|
||||
"Microsoft.NETCore.App": {
|
||||
"privateAssets": "all"
|
||||
}
|
||||
},
|
||||
"runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/8.0.410/PortableRuntimeIdentifierGraph.json"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,15 +1,15 @@
|
|||
<?xml version="1.0" encoding="utf-8" standalone="no"?>
|
||||
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
||||
<RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess>
|
||||
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
|
||||
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
|
||||
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">/root/.nuget/packages/</NuGetPackageRoot>
|
||||
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">/root/.nuget/packages/</NuGetPackageFolders>
|
||||
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
|
||||
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.11.1</NuGetToolVersion>
|
||||
</PropertyGroup>
|
||||
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
||||
<SourceRoot Include="/root/.nuget/packages/" />
|
||||
</ItemGroup>
|
||||
<?xml version="1.0" encoding="utf-8" standalone="no"?>
|
||||
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
||||
<RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess>
|
||||
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
|
||||
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
|
||||
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">/root/.nuget/packages/</NuGetPackageRoot>
|
||||
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">/root/.nuget/packages/</NuGetPackageFolders>
|
||||
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
|
||||
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.11.1</NuGetToolVersion>
|
||||
</PropertyGroup>
|
||||
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
||||
<SourceRoot Include="/root/.nuget/packages/" />
|
||||
</ItemGroup>
|
||||
</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" />
|
|
@ -1,4 +1,4 @@
|
|||
// <autogenerated />
|
||||
using System;
|
||||
using System.Reflection;
|
||||
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v8.0", FrameworkDisplayName = ".NET 8.0")]
|
||||
// <autogenerated />
|
||||
using System;
|
||||
using System.Reflection;
|
||||
[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.AssemblyConfigurationAttribute("Debug")]
|
||||
[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.AssemblyTitleAttribute("Async_Await")]
|
||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||
|
|
|
@ -1 +1 @@
|
|||
63804ea50179688365807671500045cda9689bbb802dfd4b9f520cad32043daf
|
||||
715df55e00865760b049b6999c3a198f53ad831234a2f70656b0a5d26a34b8f5
|
||||
|
|
|
@ -8,6 +8,6 @@ build_property.PlatformNeutralAssembly =
|
|||
build_property.EnforceExtendedAnalyzerRules =
|
||||
build_property._SupportedPlatformList = Linux,macOS,Windows
|
||||
build_property.RootNamespace = Async_Await
|
||||
build_property.ProjectDir = /workspaces/CSharp/Async_Await/
|
||||
build_property.ProjectDir = /workspaces/CSharpProjekt/Async_Await/
|
||||
build_property.EnableComHosting =
|
||||
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.deps.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.pdb
|
||||
/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.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.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.genruntimeconfig.cache
|
||||
/workspaces/CSharp/Async_Await/obj/Debug/net8.0/ref/Async_Await.dll
|
||||
/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.runtimeconfig.json
|
||||
/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/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.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.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.genruntimeconfig.cache
|
||||
/workspaces/CSharp/Async_Await/obj/Debug/net8.0/ref/Async_Await.dll
|
||||
|
|
|
@ -1 +1 @@
|
|||
735e7f2d774271d1780a96fe641c2f5ec6cf42f9b6bbfa8ed546e26d04a0fe35
|
||||
735e7f2d774271d1780a96fe641c2f5ec6cf42f9b6bbfa8ed546e26d04a0fe35
|
||||
|
|
|
@ -1,71 +1,71 @@
|
|||
{
|
||||
"version": 3,
|
||||
"targets": {
|
||||
"net8.0": {}
|
||||
},
|
||||
"libraries": {},
|
||||
"projectFileDependencyGroups": {
|
||||
"net8.0": []
|
||||
},
|
||||
"packageFolders": {
|
||||
"/root/.nuget/packages/": {}
|
||||
},
|
||||
"project": {
|
||||
"version": "1.0.0",
|
||||
"restore": {
|
||||
"projectUniqueName": "/workspaces/CSharp/Async_Await/Async_Await.csproj",
|
||||
"projectName": "Async_Await",
|
||||
"projectPath": "/workspaces/CSharp/Async_Await/Async_Await.csproj",
|
||||
"packagesPath": "/root/.nuget/packages/",
|
||||
"outputPath": "/workspaces/CSharp/Async_Await/obj/",
|
||||
"projectStyle": "PackageReference",
|
||||
"configFilePaths": [
|
||||
"/root/.nuget/NuGet/NuGet.Config"
|
||||
],
|
||||
"originalTargetFrameworks": [
|
||||
"net8.0"
|
||||
],
|
||||
"sources": {
|
||||
"https://api.nuget.org/v3/index.json": {}
|
||||
},
|
||||
"frameworks": {
|
||||
"net8.0": {
|
||||
"targetAlias": "net8.0",
|
||||
"projectReferences": {}
|
||||
}
|
||||
},
|
||||
"warningProperties": {
|
||||
"warnAsError": [
|
||||
"NU1605"
|
||||
]
|
||||
},
|
||||
"restoreAuditProperties": {
|
||||
"enableAudit": "true",
|
||||
"auditLevel": "low",
|
||||
"auditMode": "direct"
|
||||
}
|
||||
},
|
||||
"frameworks": {
|
||||
"net8.0": {
|
||||
"targetAlias": "net8.0",
|
||||
"imports": [
|
||||
"net461",
|
||||
"net462",
|
||||
"net47",
|
||||
"net471",
|
||||
"net472",
|
||||
"net48",
|
||||
"net481"
|
||||
],
|
||||
"assetTargetFallback": true,
|
||||
"warn": true,
|
||||
"frameworkReferences": {
|
||||
"Microsoft.NETCore.App": {
|
||||
"privateAssets": "all"
|
||||
}
|
||||
},
|
||||
"runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/8.0.410/PortableRuntimeIdentifierGraph.json"
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
"version": 3,
|
||||
"targets": {
|
||||
"net8.0": {}
|
||||
},
|
||||
"libraries": {},
|
||||
"projectFileDependencyGroups": {
|
||||
"net8.0": []
|
||||
},
|
||||
"packageFolders": {
|
||||
"/root/.nuget/packages/": {}
|
||||
},
|
||||
"project": {
|
||||
"version": "1.0.0",
|
||||
"restore": {
|
||||
"projectUniqueName": "/workspaces/CSharp/Async_Await/Async_Await.csproj",
|
||||
"projectName": "Async_Await",
|
||||
"projectPath": "/workspaces/CSharp/Async_Await/Async_Await.csproj",
|
||||
"packagesPath": "/root/.nuget/packages/",
|
||||
"outputPath": "/workspaces/CSharp/Async_Await/obj/",
|
||||
"projectStyle": "PackageReference",
|
||||
"configFilePaths": [
|
||||
"/root/.nuget/NuGet/NuGet.Config"
|
||||
],
|
||||
"originalTargetFrameworks": [
|
||||
"net8.0"
|
||||
],
|
||||
"sources": {
|
||||
"https://api.nuget.org/v3/index.json": {}
|
||||
},
|
||||
"frameworks": {
|
||||
"net8.0": {
|
||||
"targetAlias": "net8.0",
|
||||
"projectReferences": {}
|
||||
}
|
||||
},
|
||||
"warningProperties": {
|
||||
"warnAsError": [
|
||||
"NU1605"
|
||||
]
|
||||
},
|
||||
"restoreAuditProperties": {
|
||||
"enableAudit": "true",
|
||||
"auditLevel": "low",
|
||||
"auditMode": "direct"
|
||||
}
|
||||
},
|
||||
"frameworks": {
|
||||
"net8.0": {
|
||||
"targetAlias": "net8.0",
|
||||
"imports": [
|
||||
"net461",
|
||||
"net462",
|
||||
"net47",
|
||||
"net471",
|
||||
"net472",
|
||||
"net48",
|
||||
"net481"
|
||||
],
|
||||
"assetTargetFallback": true,
|
||||
"warn": true,
|
||||
"frameworkReferences": {
|
||||
"Microsoft.NETCore.App": {
|
||||
"privateAssets": "all"
|
||||
}
|
||||
},
|
||||
"runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/8.0.410/PortableRuntimeIdentifierGraph.json"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,8 +1,8 @@
|
|||
{
|
||||
"version": 2,
|
||||
"dgSpecHash": "WpEPBuOGmH8=",
|
||||
"success": true,
|
||||
"projectFilePath": "/workspaces/CSharp/Async_Await/Async_Await.csproj",
|
||||
"expectedPackageFiles": [],
|
||||
"logs": []
|
||||
{
|
||||
"version": 2,
|
||||
"dgSpecHash": "WpEPBuOGmH8=",
|
||||
"success": true,
|
||||
"projectFilePath": "/workspaces/CSharp/Async_Await/Async_Await.csproj",
|
||||
"expectedPackageFiles": [],
|
||||
"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
|
||||
|
||||
## ✅ Voraussetzungen
|
||||
|
||||
- [.NET SDK](https://dotnet.microsoft.com/en-us/download) installiert
|
||||
Prüfen mit:
|
||||
```bash
|
||||
dotnet --version
|
||||
```
|
||||
- **Visual Studio Code** installiert
|
||||
- In VS Code folgende Extensions installieren:
|
||||
- **C# Dev Kit**
|
||||
|
||||
---
|
||||
|
||||
## 📁 Neues C# Konsolenprojekt erstellen
|
||||
|
||||
```bash
|
||||
dotnet new console -n HelloWorld
|
||||
cd HelloWorld
|
||||
code .
|
||||
```
|
||||
|
||||
| Befehl | Bedeutung |
|
||||
|--------|-----------|
|
||||
| `dotnet new console` | Erstellt ein Konsolenprojekt |
|
||||
| `-n HelloWorld` | Legt den Projektnamen fest |
|
||||
| `code .` | Öffnet das Projekt in VS Code |
|
||||
|
||||
---
|
||||
|
||||
## 📦 Projektstruktur
|
||||
|
||||
Nach dem Öffnen in VS Code solltest du sehen:
|
||||
|
||||
```
|
||||
HelloWorld/
|
||||
├── Program.cs // Hauptprogramm
|
||||
├── HelloWorld.csproj // Projektdefinition
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ▶️ Projekt ausführen
|
||||
|
||||
### Über Terminal:
|
||||
|
||||
```bash
|
||||
dotnet run
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📌 Nützliche Befehle
|
||||
|
||||
| Befehl | Beschreibung |
|
||||
| --------------------- | ----------------------------------------- |
|
||||
| `dotnet build` | Projekt kompilieren |
|
||||
| `dotnet run` | Projekt bauen und ausführen |
|
||||
| `dotnet clean` | Build-Ordner bereinigen |
|
||||
| `dotnet new` | Neue Projektvorlagen anzeigen |
|
||||
| `dotnet list package` | Installierte Pakete anzeigen |
|
||||
| ``dotnet test`` | Führt automatisierte Tests im Projekt aus |
|
||||
|
||||
|
||||
# 🛠️ C# Projekt in Visual Studio Code einrichten und ausführen
|
||||
|
||||
## ✅ Voraussetzungen
|
||||
|
||||
- [.NET SDK](https://dotnet.microsoft.com/en-us/download) installiert
|
||||
Prüfen mit:
|
||||
```bash
|
||||
dotnet --version
|
||||
```
|
||||
- **Visual Studio Code** installiert
|
||||
- In VS Code folgende Extensions installieren:
|
||||
- **C# Dev Kit**
|
||||
|
||||
---
|
||||
|
||||
## 📁 Neues C# Konsolenprojekt erstellen
|
||||
|
||||
```bash
|
||||
dotnet new console -n HelloWorld
|
||||
cd HelloWorld
|
||||
code .
|
||||
```
|
||||
|
||||
| Befehl | Bedeutung |
|
||||
|--------|-----------|
|
||||
| `dotnet new console` | Erstellt ein Konsolenprojekt |
|
||||
| `-n HelloWorld` | Legt den Projektnamen fest |
|
||||
| `code .` | Öffnet das Projekt in VS Code |
|
||||
|
||||
---
|
||||
|
||||
## 📦 Projektstruktur
|
||||
|
||||
Nach dem Öffnen in VS Code solltest du sehen:
|
||||
|
||||
```
|
||||
HelloWorld/
|
||||
├── Program.cs // Hauptprogramm
|
||||
├── HelloWorld.csproj // Projektdefinition
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ▶️ Projekt ausführen
|
||||
|
||||
### Über Terminal:
|
||||
|
||||
```bash
|
||||
dotnet run
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📌 Nützliche Befehle
|
||||
|
||||
| Befehl | Beschreibung |
|
||||
| --------------------- | ----------------------------------------- |
|
||||
| `dotnet build` | Projekt kompilieren |
|
||||
| `dotnet run` | Projekt bauen und ausführen |
|
||||
| `dotnet clean` | Build-Ordner bereinigen |
|
||||
| `dotnet new` | Neue Projektvorlagen anzeigen |
|
||||
| `dotnet list package` | Installierte Pakete anzeigen |
|
||||
| ``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.Client;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace MqttSensorApp
|
||||
{
|
||||
// TODO: Aufgabe 3: Implementieren Sie die SensorData-Klasse
|
||||
public class SensorData
|
||||
{
|
||||
// TODO: Ergänzen Sie Properties für Temperature, Humidity und Location
|
||||
// Nutzen Sie JsonPropertyName-Attribute für die JSON-Deserialisierung
|
||||
|
||||
}
|
||||
|
||||
public class Program
|
||||
{
|
||||
private static List<double> receivedTemperatures = new List<double>();
|
||||
|
||||
public static async Task Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("MQTT Sensor Data Processor gestartet...");
|
||||
|
||||
// MQTT Client Setup (bereits implementiert)
|
||||
var factory = new MqttFactory();
|
||||
var client = factory.CreateMqttClient();
|
||||
|
||||
// Event Handler für empfangene Nachrichten
|
||||
client.ApplicationMessageReceivedAsync += async e =>
|
||||
{
|
||||
var payload = System.Text.Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
|
||||
await ProcessMessage(payload);
|
||||
};
|
||||
|
||||
// Verbindungsoptionen
|
||||
var options = new MqttClientOptionsBuilder()
|
||||
.WithTcpServer("broker.hivemq.com", 1883)
|
||||
.WithClientId($"SensorClient_{Guid.NewGuid()}")
|
||||
.WithCleanSession()
|
||||
.Build();
|
||||
|
||||
try
|
||||
{
|
||||
// Verbindung aufbauen
|
||||
await client.ConnectAsync(options);
|
||||
Console.WriteLine("Mit MQTT Broker verbunden");
|
||||
|
||||
// Topic abonnieren
|
||||
await client.SubscribeAsync("sensor/data/room1");
|
||||
Console.WriteLine("Topic 'sensor/data/room1' abonniert");
|
||||
|
||||
// Simulierte Sensordaten senden (für Testing)
|
||||
await SendTestData(client);
|
||||
|
||||
// Programm laufen lassen
|
||||
Console.WriteLine("Drücken Sie eine Taste zum Beenden...");
|
||||
Console.ReadKey();
|
||||
|
||||
// Verbindung trennen
|
||||
await client.DisconnectAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Fehler: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task ProcessMessage(string jsonPayload)
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.WriteLine($"Empfangen: {jsonPayload}");
|
||||
|
||||
// TODO: Aufgabe 3: Deserialisieren Sie den JSON zu SensorData
|
||||
// var sensorData = JsonSerializer.Deserialize<SensorData>(jsonPayload);
|
||||
|
||||
// Temporäre Lösung für Aufgabe 1 und 2
|
||||
var data = JsonSerializer.Deserialize<Dictionary<string, object>>(jsonPayload);
|
||||
|
||||
if (data != null && data.ContainsKey("temperature"))
|
||||
{
|
||||
var temp = Convert.ToDouble(data["temperature"].ToString());
|
||||
receivedTemperatures.Add(temp);
|
||||
|
||||
// Nachricht analysieren
|
||||
var analysis = AnalyzeMessage(temp);
|
||||
Console.WriteLine($"Analyse: {analysis}");
|
||||
}
|
||||
|
||||
// Alle 5 Nachrichten: Hohe Temperaturen anzeigen
|
||||
if (receivedTemperatures.Count % 5 == 0)
|
||||
{
|
||||
var highTemps = FilterHighTemperatures(receivedTemperatures);
|
||||
Console.WriteLine($"Hohe Temperaturen (>25°C): {string.Join(", ", highTemps.Select(t => $"{t:F1}°C"))}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Fehler beim Verarbeiten der Nachricht: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Aufgabe 1: LINQ-Abfrage implementieren
|
||||
private static IEnumerable<double> FilterHighTemperatures(List<double> temperatures)
|
||||
{
|
||||
// TODO: Verwenden Sie LINQ (Where-Klausel), um alle Temperaturen > 25°C zu filtern
|
||||
// Beispiel: return temperatures.Where(...);
|
||||
|
||||
return new List<double>(); // Platzhalter, ersetzen Sie diese Zeile
|
||||
}
|
||||
|
||||
// TODO: Aufgabe 2: Pattern Matching implementieren
|
||||
private static string AnalyzeMessage(object value)
|
||||
{
|
||||
// TODO: Implementieren Sie Pattern Matching mit switch expression
|
||||
// Behandeln Sie folgende Fälle:
|
||||
// - double über 30: "WARNUNG: Sehr hohe Temperatur!"
|
||||
// - double zwischen 25-30: "Hohe Temperatur"
|
||||
// - double unter 25: "Normale Temperatur"
|
||||
// - string: "Text-Nachricht erhalten"
|
||||
// - null: "Keine Daten"
|
||||
// - default: "Unbekannter Datentyp"
|
||||
|
||||
return "TODO: Pattern Matching implementieren"; // Platzhalter
|
||||
}
|
||||
|
||||
// Hilfsmethode: Testnachrichten senden
|
||||
private static async Task SendTestData(IMqttClient client)
|
||||
{
|
||||
var testData = new[]
|
||||
{
|
||||
new { temperature = 22.5, humidity = 65.0, location = "Room1" },
|
||||
new { temperature = 28.3, humidity = 58.2, location = "Room1" },
|
||||
new { temperature = 19.8, humidity = 72.1, location = "Room1" },
|
||||
new { temperature = 31.2, humidity = 45.3, location = "Room1" },
|
||||
new { temperature = 26.7, humidity = 62.8, location = "Room1" }
|
||||
};
|
||||
|
||||
foreach (var data in testData)
|
||||
{
|
||||
var json = JsonSerializer.Serialize(data);
|
||||
var message = new MqttApplicationMessageBuilder()
|
||||
.WithTopic("sensor/data/room1")
|
||||
.WithPayload(json)
|
||||
.Build();
|
||||
|
||||
await client.PublishAsync(message);
|
||||
await Task.Delay(2000); // 2 Sekunden Pause
|
||||
}
|
||||
}
|
||||
}
|
||||
using MQTTnet;
|
||||
using MQTTnet.Client;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace MqttSensorApp
|
||||
{
|
||||
// TODO: Aufgabe 3: Implementieren Sie die SensorData-Klasse
|
||||
public class SensorData
|
||||
{
|
||||
// TODO: Ergänzen Sie Properties für Temperature, Humidity und Location
|
||||
// Nutzen Sie JsonPropertyName-Attribute für die JSON-Deserialisierung
|
||||
|
||||
}
|
||||
|
||||
public class Program
|
||||
{
|
||||
private static List<double> receivedTemperatures = new List<double>();
|
||||
|
||||
public static async Task Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("MQTT Sensor Data Processor gestartet...");
|
||||
|
||||
// MQTT Client Setup (bereits implementiert)
|
||||
var factory = new MqttFactory();
|
||||
var client = factory.CreateMqttClient();
|
||||
|
||||
// Event Handler für empfangene Nachrichten
|
||||
client.ApplicationMessageReceivedAsync += async e =>
|
||||
{
|
||||
var payload = System.Text.Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
|
||||
await ProcessMessage(payload);
|
||||
};
|
||||
|
||||
// Verbindungsoptionen
|
||||
var options = new MqttClientOptionsBuilder()
|
||||
.WithTcpServer("broker.hivemq.com", 1883)
|
||||
.WithClientId($"SensorClient_{Guid.NewGuid()}")
|
||||
.WithCleanSession()
|
||||
.Build();
|
||||
|
||||
try
|
||||
{
|
||||
// Verbindung aufbauen
|
||||
await client.ConnectAsync(options);
|
||||
Console.WriteLine("Mit MQTT Broker verbunden");
|
||||
|
||||
// Topic abonnieren
|
||||
await client.SubscribeAsync("sensor/data/room1");
|
||||
Console.WriteLine("Topic 'sensor/data/room1' abonniert");
|
||||
|
||||
// Simulierte Sensordaten senden (für Testing)
|
||||
await SendTestData(client);
|
||||
|
||||
// Programm laufen lassen
|
||||
Console.WriteLine("Drücken Sie eine Taste zum Beenden...");
|
||||
Console.ReadKey();
|
||||
|
||||
// Verbindung trennen
|
||||
await client.DisconnectAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Fehler: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task ProcessMessage(string jsonPayload)
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.WriteLine($"Empfangen: {jsonPayload}");
|
||||
|
||||
// TODO: Aufgabe 3: Deserialisieren Sie den JSON zu SensorData
|
||||
// var sensorData = JsonSerializer.Deserialize<SensorData>(jsonPayload);
|
||||
|
||||
// Temporäre Lösung für Aufgabe 1 und 2
|
||||
var data = JsonSerializer.Deserialize<Dictionary<string, object>>(jsonPayload);
|
||||
|
||||
if (data != null && data.ContainsKey("temperature"))
|
||||
{
|
||||
var temp = Convert.ToDouble(data["temperature"].ToString());
|
||||
receivedTemperatures.Add(temp);
|
||||
|
||||
// Nachricht analysieren
|
||||
var analysis = AnalyzeMessage(temp);
|
||||
Console.WriteLine($"Analyse: {analysis}");
|
||||
}
|
||||
|
||||
// Alle 5 Nachrichten: Hohe Temperaturen anzeigen
|
||||
if (receivedTemperatures.Count % 5 == 0)
|
||||
{
|
||||
var highTemps = FilterHighTemperatures(receivedTemperatures);
|
||||
Console.WriteLine($"Hohe Temperaturen (>25°C): {string.Join(", ", highTemps.Select(t => $"{t:F1}°C"))}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Fehler beim Verarbeiten der Nachricht: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Aufgabe 1: LINQ-Abfrage implementieren
|
||||
private static IEnumerable<double> FilterHighTemperatures(List<double> temperatures)
|
||||
{
|
||||
// TODO: Verwenden Sie LINQ (Where-Klausel), um alle Temperaturen > 25°C zu filtern
|
||||
// Beispiel: return temperatures.Where(...);
|
||||
|
||||
return new List<double>(); // Platzhalter, ersetzen Sie diese Zeile
|
||||
}
|
||||
|
||||
// TODO: Aufgabe 2: Pattern Matching implementieren
|
||||
private static string AnalyzeMessage(object value)
|
||||
{
|
||||
// TODO: Implementieren Sie Pattern Matching mit switch expression
|
||||
// Behandeln Sie folgende Fälle:
|
||||
// - double über 30: "WARNUNG: Sehr hohe Temperatur!"
|
||||
// - double zwischen 25-30: "Hohe Temperatur"
|
||||
// - double unter 25: "Normale Temperatur"
|
||||
// - string: "Text-Nachricht erhalten"
|
||||
// - null: "Keine Daten"
|
||||
// - default: "Unbekannter Datentyp"
|
||||
|
||||
return "TODO: Pattern Matching implementieren"; // Platzhalter
|
||||
}
|
||||
|
||||
// Hilfsmethode: Testnachrichten senden
|
||||
private static async Task SendTestData(IMqttClient client)
|
||||
{
|
||||
var testData = new[]
|
||||
{
|
||||
new { temperature = 22.5, humidity = 65.0, location = "Room1" },
|
||||
new { temperature = 28.3, humidity = 58.2, location = "Room1" },
|
||||
new { temperature = 19.8, humidity = 72.1, location = "Room1" },
|
||||
new { temperature = 31.2, humidity = 45.3, location = "Room1" },
|
||||
new { temperature = 26.7, humidity = 62.8, location = "Room1" }
|
||||
};
|
||||
|
||||
foreach (var data in testData)
|
||||
{
|
||||
var json = JsonSerializer.Serialize(data);
|
||||
var message = new MqttApplicationMessageBuilder()
|
||||
.WithTopic("sensor/data/room1")
|
||||
.WithPayload(json)
|
||||
.Build();
|
||||
|
||||
await client.PublishAsync(message);
|
||||
await Task.Delay(2000); // 2 Sekunden Pause
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,151 +1,151 @@
|
|||
using MQTTnet;
|
||||
using MQTTnet.Client;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace MqttSensorApp
|
||||
{
|
||||
// LÖSUNG Aufgabe 3: SensorData-Klasse implementieren
|
||||
public class SensorData
|
||||
{
|
||||
[JsonPropertyName("temperature")]
|
||||
public double Temperature { get; set; }
|
||||
|
||||
[JsonPropertyName("humidity")]
|
||||
public double Humidity { get; set; }
|
||||
|
||||
[JsonPropertyName("location")]
|
||||
public string Location { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public class Program
|
||||
{
|
||||
private static List<double> receivedTemperatures = new List<double>();
|
||||
|
||||
public static async Task Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("MQTT Sensor Data Processor gestartet...");
|
||||
|
||||
// MQTT Client Setup (bereits implementiert)
|
||||
var factory = new MqttFactory();
|
||||
var client = factory.CreateMqttClient();
|
||||
|
||||
// Event Handler für empfangene Nachrichten
|
||||
client.ApplicationMessageReceivedAsync += async e =>
|
||||
{
|
||||
var payload = System.Text.Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
|
||||
await ProcessMessage(payload);
|
||||
};
|
||||
|
||||
// Verbindungsoptionen
|
||||
var options = new MqttClientOptionsBuilder()
|
||||
.WithTcpServer("broker.hivemq.com", 1883)
|
||||
.WithClientId($"SensorClient_{Guid.NewGuid()}")
|
||||
.WithCleanSession()
|
||||
.Build();
|
||||
|
||||
try
|
||||
{
|
||||
// Verbindung aufbauen
|
||||
await client.ConnectAsync(options);
|
||||
Console.WriteLine("Mit MQTT Broker verbunden");
|
||||
|
||||
// Topic abonnieren
|
||||
await client.SubscribeAsync("sensor/data/room1");
|
||||
Console.WriteLine("Topic 'sensor/data/room1' abonniert");
|
||||
|
||||
// Simulierte Sensordaten senden (für Testing)
|
||||
await SendTestData(client);
|
||||
|
||||
// Programm laufen lassen
|
||||
Console.WriteLine("Drücken Sie eine Taste zum Beenden...");
|
||||
Console.ReadKey();
|
||||
|
||||
// Verbindung trennen
|
||||
await client.DisconnectAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Fehler: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task ProcessMessage(string jsonPayload)
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.WriteLine($"Empfangen: {jsonPayload}");
|
||||
|
||||
// LÖSUNG Aufgabe 3: Deserialisieren des JSON zu SensorData
|
||||
var sensorData = JsonSerializer.Deserialize<SensorData>(jsonPayload);
|
||||
|
||||
if (sensorData != null)
|
||||
{
|
||||
receivedTemperatures.Add(sensorData.Temperature);
|
||||
|
||||
// LÖSUNG Aufgabe 2: Pattern Matching anwenden
|
||||
var analysis = AnalyzeMessage(sensorData.Temperature);
|
||||
Console.WriteLine($"Analyse: {analysis}");
|
||||
}
|
||||
|
||||
// Alle 5 Nachrichten: Hohe Temperaturen anzeigen
|
||||
if (receivedTemperatures.Count % 5 == 0)
|
||||
{
|
||||
var highTemps = FilterHighTemperatures(receivedTemperatures);
|
||||
Console.WriteLine($"Hohe Temperaturen (>25°C): {string.Join(", ", highTemps.Select(t => $"{t:F1}°C"))}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Fehler beim Verarbeiten der Nachricht: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
// LÖSUNG Aufgabe 1: LINQ-Abfrage implementieren
|
||||
private static IEnumerable<double> FilterHighTemperatures(List<double> temperatures)
|
||||
{
|
||||
// LINQ Where-Klausel verwenden, um Temperaturen > 25°C zu filtern
|
||||
return temperatures.Where(temp => temp > 25.0);
|
||||
}
|
||||
|
||||
// LÖSUNG Aufgabe 2: Pattern Matching implementieren
|
||||
private static string AnalyzeMessage(object value)
|
||||
{
|
||||
// Switch expression mit Pattern Matching
|
||||
return value switch
|
||||
{
|
||||
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 => "Normale Temperatur",
|
||||
string text => "Text-Nachricht erhalten",
|
||||
null => "Keine Daten",
|
||||
_ => "Unbekannter Datentyp"
|
||||
};
|
||||
}
|
||||
|
||||
// Hilfsmethode: Testnachrichten senden
|
||||
private static async Task SendTestData(IMqttClient client)
|
||||
{
|
||||
var testData = new[]
|
||||
{
|
||||
new { temperature = 22.5, humidity = 65.0, location = "Room1" },
|
||||
new { temperature = 28.3, humidity = 58.2, location = "Room1" },
|
||||
new { temperature = 19.8, humidity = 72.1, location = "Room1" },
|
||||
new { temperature = 31.2, humidity = 45.3, location = "Room1" },
|
||||
new { temperature = 26.7, humidity = 62.8, location = "Room1" }
|
||||
};
|
||||
|
||||
foreach (var data in testData)
|
||||
{
|
||||
var json = JsonSerializer.Serialize(data);
|
||||
var message = new MqttApplicationMessageBuilder()
|
||||
.WithTopic("sensor/data/room1")
|
||||
.WithPayload(json)
|
||||
.Build();
|
||||
|
||||
await client.PublishAsync(message);
|
||||
await Task.Delay(2000); // 2 Sekunden Pause
|
||||
}
|
||||
}
|
||||
}
|
||||
using MQTTnet;
|
||||
using MQTTnet.Client;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace MqttSensorApp
|
||||
{
|
||||
// LÖSUNG Aufgabe 3: SensorData-Klasse implementieren
|
||||
public class SensorData
|
||||
{
|
||||
[JsonPropertyName("temperature")]
|
||||
public double Temperature { get; set; }
|
||||
|
||||
[JsonPropertyName("humidity")]
|
||||
public double Humidity { get; set; }
|
||||
|
||||
[JsonPropertyName("location")]
|
||||
public string Location { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public class Program
|
||||
{
|
||||
private static List<double> receivedTemperatures = new List<double>();
|
||||
|
||||
public static async Task Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("MQTT Sensor Data Processor gestartet...");
|
||||
|
||||
// MQTT Client Setup (bereits implementiert)
|
||||
var factory = new MqttFactory();
|
||||
var client = factory.CreateMqttClient();
|
||||
|
||||
// Event Handler für empfangene Nachrichten
|
||||
client.ApplicationMessageReceivedAsync += async e =>
|
||||
{
|
||||
var payload = System.Text.Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
|
||||
await ProcessMessage(payload);
|
||||
};
|
||||
|
||||
// Verbindungsoptionen
|
||||
var options = new MqttClientOptionsBuilder()
|
||||
.WithTcpServer("broker.hivemq.com", 1883)
|
||||
.WithClientId($"SensorClient_{Guid.NewGuid()}")
|
||||
.WithCleanSession()
|
||||
.Build();
|
||||
|
||||
try
|
||||
{
|
||||
// Verbindung aufbauen
|
||||
await client.ConnectAsync(options);
|
||||
Console.WriteLine("Mit MQTT Broker verbunden");
|
||||
|
||||
// Topic abonnieren
|
||||
await client.SubscribeAsync("sensor/data/room1");
|
||||
Console.WriteLine("Topic 'sensor/data/room1' abonniert");
|
||||
|
||||
// Simulierte Sensordaten senden (für Testing)
|
||||
await SendTestData(client);
|
||||
|
||||
// Programm laufen lassen
|
||||
Console.WriteLine("Drücken Sie eine Taste zum Beenden...");
|
||||
Console.ReadKey();
|
||||
|
||||
// Verbindung trennen
|
||||
await client.DisconnectAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Fehler: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task ProcessMessage(string jsonPayload)
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.WriteLine($"Empfangen: {jsonPayload}");
|
||||
|
||||
// LÖSUNG Aufgabe 3: Deserialisieren des JSON zu SensorData
|
||||
var sensorData = JsonSerializer.Deserialize<SensorData>(jsonPayload);
|
||||
|
||||
if (sensorData != null)
|
||||
{
|
||||
receivedTemperatures.Add(sensorData.Temperature);
|
||||
|
||||
// LÖSUNG Aufgabe 2: Pattern Matching anwenden
|
||||
var analysis = AnalyzeMessage(sensorData.Temperature);
|
||||
Console.WriteLine($"Analyse: {analysis}");
|
||||
}
|
||||
|
||||
// Alle 5 Nachrichten: Hohe Temperaturen anzeigen
|
||||
if (receivedTemperatures.Count % 5 == 0)
|
||||
{
|
||||
var highTemps = FilterHighTemperatures(receivedTemperatures);
|
||||
Console.WriteLine($"Hohe Temperaturen (>25°C): {string.Join(", ", highTemps.Select(t => $"{t:F1}°C"))}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Fehler beim Verarbeiten der Nachricht: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
// LÖSUNG Aufgabe 1: LINQ-Abfrage implementieren
|
||||
private static IEnumerable<double> FilterHighTemperatures(List<double> temperatures)
|
||||
{
|
||||
// LINQ Where-Klausel verwenden, um Temperaturen > 25°C zu filtern
|
||||
return temperatures.Where(temp => temp > 25.0);
|
||||
}
|
||||
|
||||
// LÖSUNG Aufgabe 2: Pattern Matching implementieren
|
||||
private static string AnalyzeMessage(object value)
|
||||
{
|
||||
// Switch expression mit Pattern Matching
|
||||
return value switch
|
||||
{
|
||||
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 => "Normale Temperatur",
|
||||
string text => "Text-Nachricht erhalten",
|
||||
null => "Keine Daten",
|
||||
_ => "Unbekannter Datentyp"
|
||||
};
|
||||
}
|
||||
|
||||
// Hilfsmethode: Testnachrichten senden
|
||||
private static async Task SendTestData(IMqttClient client)
|
||||
{
|
||||
var testData = new[]
|
||||
{
|
||||
new { temperature = 22.5, humidity = 65.0, location = "Room1" },
|
||||
new { temperature = 28.3, humidity = 58.2, location = "Room1" },
|
||||
new { temperature = 19.8, humidity = 72.1, location = "Room1" },
|
||||
new { temperature = 31.2, humidity = 45.3, location = "Room1" },
|
||||
new { temperature = 26.7, humidity = 62.8, location = "Room1" }
|
||||
};
|
||||
|
||||
foreach (var data in testData)
|
||||
{
|
||||
var json = JsonSerializer.Serialize(data);
|
||||
var message = new MqttApplicationMessageBuilder()
|
||||
.WithTopic("sensor/data/room1")
|
||||
.WithPayload(json)
|
||||
.Build();
|
||||
|
||||
await client.PublishAsync(message);
|
||||
await Task.Delay(2000); // 2 Sekunden Pause
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue