Skip to content

Commit ca63b6b

Browse files
committed
New version of bad example
1 parent 5ba05f3 commit ca63b6b

9 files changed

Lines changed: 96 additions & 440 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using System.Net;
3+
using System.Text.Json;
4+
using System.IO;
5+
6+
using Microsoft.Extensions.Configuration;
7+
8+
class Program
9+
{
10+
static void Main()
11+
{
12+
var config = new ConfigurationBuilder()
13+
.AddUserSecrets<Program>()
14+
.Build();
15+
16+
var apiKey = config["OpenWeatherMap:ApiKey"];
17+
var thingSpeakApiKey = config["ThingSpeak:ApiKey"]; // ThingSpeak Write API Key
18+
string city = "Freiberg";
19+
20+
WebClient client = new WebClient();
21+
string city_url = $"http://api.openweathermap.org/geo/1.0/direct?q={city}&limit=1&appid={apiKey}";
22+
string city_json = client.DownloadString(city_url);
23+
Console.WriteLine(city_json);
24+
25+
JsonDocument cityDoc = JsonDocument.Parse(city_json);
26+
JsonElement cityRoot = cityDoc.RootElement;
27+
double lat = cityRoot[0].GetProperty("lat").GetDouble();
28+
double lon = cityRoot[0].GetProperty("lon").GetDouble();
29+
30+
string weather_url = $"http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={apiKey}&units=metric";
31+
Console.WriteLine(weather_url);
32+
33+
string weather_json = client.DownloadString(weather_url);
34+
Console.WriteLine(weather_json);
35+
36+
JsonDocument weatherDoc = JsonDocument.Parse(weather_json);
37+
JsonElement weatherRoot = weatherDoc.RootElement;
38+
string wetter = weatherRoot.GetProperty("weather")[0].GetProperty("description").GetString();
39+
double temp = weatherRoot.GetProperty("main").GetProperty("temp").GetDouble();
40+
double wind = weatherRoot.GetProperty("wind").GetProperty("speed").GetDouble();
41+
42+
Console.WriteLine($"\nWetter in {city}:");
43+
Console.WriteLine($"Beschreibung: {wetter}");
44+
Console.WriteLine($"Temperatur: {temp}°C");
45+
Console.WriteLine($"Windgeschwindigkeit: {wind} m/s");
46+
47+
// CSV-Datei erstellen/erweitern
48+
string csvFilePath = "wetterdaten.csv";
49+
string csvLine = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss},{city},{wetter},{temp},{wind}";
50+
51+
// Prüfen ob CSV-Datei bereits existiert
52+
if (!File.Exists(csvFilePath))
53+
{
54+
// Header schreiben falls Datei nicht existiert
55+
string header = "Datum,Stadt,Beschreibung,Temperatur_C,Windgeschwindigkeit_ms";
56+
File.WriteAllText(csvFilePath, header + Environment.NewLine);
57+
}
58+
59+
// Wetterdaten anhängen
60+
File.AppendAllText(csvFilePath, csvLine + Environment.NewLine);
61+
62+
Console.WriteLine($"\nWetterdaten wurden in '{csvFilePath}' gespeichert.");
63+
64+
// ThingSpeak Daten senden
65+
if (!string.IsNullOrEmpty(thingSpeakApiKey))
66+
{
67+
string thingSpeakUrl = $"https://api.thingspeak.com/update?api_key={thingSpeakApiKey}&field1={temp}&field2={wind}";
68+
69+
try
70+
{
71+
string thingSpeakResponse = client.DownloadString(thingSpeakUrl);
72+
Console.WriteLine($"ThingSpeak Response: {thingSpeakResponse}");
73+
74+
if (int.TryParse(thingSpeakResponse, out int entryId) && entryId > 0)
75+
{
76+
Console.WriteLine($"Daten erfolgreich an ThingSpeak gesendet! Entry ID: {entryId}");
77+
}
78+
else
79+
{
80+
Console.WriteLine("Fehler beim Senden an ThingSpeak.");
81+
}
82+
}
83+
catch (Exception ex)
84+
{
85+
Console.WriteLine($"Fehler beim Senden an ThingSpeak: {ex.Message}");
86+
}
87+
}
88+
else
89+
{
90+
Console.WriteLine("ThingSpeak API Key nicht gefunden. Daten werden nicht gesendet.");
91+
}
92+
}
93+
}

code/29_CsharpApplications/secrets/secrets/secrets.csproj renamed to code/29_CsharpApplications/openweather/openweather_badSolution/openweather_badSolution.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net9.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
8-
<UserSecretsId>e43e84ba-9606-47e6-a315-765ffecaa275</UserSecretsId>
8+
<UserSecretsId>a883fa52-47c7-4e0a-ac41-6440467bd809</UserSecretsId>
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
12+
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.7" />
1313
</ItemGroup>
1414

1515
</Project>

code/29_CsharpApplications/refactoring/thingspeak_solution/Program.cs

Lines changed: 0 additions & 142 deletions
This file was deleted.

code/29_CsharpApplications/refactoring/thingspeak_solution/Program.txt

Lines changed: 0 additions & 135 deletions
This file was deleted.

code/29_CsharpApplications/refactoring/thingspeak_solution/ThingSpeak.zip

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)