Revize 4b847de5
Přidáno uživatelem Alex Konig před téměř 4 roky(ů)
Server/ServerApp/WeatherPredictionParser/JsonParser.cs | ||
---|---|---|
18 | 18 |
/// Class representing a parser for json prediction data |
19 | 19 |
/// </summary> |
20 | 20 |
/// <author>A. Konig</author> |
21 |
class JsonParser : IJsonParser |
|
21 |
public class JsonParser : IJsonParser
|
|
22 | 22 |
{ |
23 |
/// <summary> Current weather </summary> |
|
24 |
WeatherInfo current; |
|
25 |
public new WeatherInfo Current { get => current; } |
|
26 |
/// <summary> Prediction for today, tommorrow and day after tommorrow </summary> |
|
27 |
List<WeatherInfo> predictions; |
|
28 |
public new List<WeatherInfo> Predictions { get => predictions; set => predictions = value; } |
|
29 |
|
|
30 | 23 |
/// <summary> Data loader </summary> |
31 | 24 |
DataDownloader loader; |
32 | 25 |
/// <summary> Currently parsed day </summary> |
... | ... | |
55 | 48 |
// TODO ask DataDownloader for download said file and return path to it |
56 | 49 |
|
57 | 50 |
// get file |
58 |
string file = DownloadWeatherPrediction(); |
|
51 |
string file = loader.DownloadWeatherPrediction();
|
|
59 | 52 |
DateTime now = DateTime.Now; |
60 | 53 |
Console.WriteLine(File.Exists(file)); |
61 | 54 |
|
62 |
current = new WeatherInfo();
|
|
63 |
predictions = new List<WeatherInfo>();
|
|
55 |
Current = new WeatherInfo();
|
|
56 |
Predictions = new List<WeatherInfo>();
|
|
64 | 57 |
|
65 | 58 |
if (!File.Exists(file)) |
66 | 59 |
return; |
... | ... | |
84 | 77 |
case "current_condition": |
85 | 78 |
{ |
86 | 79 |
ArrayEnumerator currentWeather = weatherP.Current.Value.EnumerateArray(); |
87 |
current = ParseCurrentWeather(currentWeather);
|
|
80 |
Current = ParseCurrentWeather(currentWeather);
|
|
88 | 81 |
|
89 | 82 |
break; |
90 | 83 |
} |
... | ... | |
110 | 103 |
/// </summary> |
111 | 104 |
private void TestConsoleOutput() |
112 | 105 |
{ |
113 |
Console.WriteLine(current);
|
|
114 |
foreach (WeatherInfo w in predictions)
|
|
106 |
Console.WriteLine(Current);
|
|
107 |
foreach (WeatherInfo w in Predictions)
|
|
115 | 108 |
Console.WriteLine(w); |
116 | 109 |
} |
117 | 110 |
|
118 |
// TODO move to data loader |
|
119 |
/// <summary> |
|
120 |
/// Downloads json file |
|
121 |
/// </summary> |
|
122 |
/// <returns> Path to file </returns> |
|
123 |
private string DownloadWeatherPrediction() |
|
124 |
{ |
|
125 |
DateTime now = DateTime.Now; |
|
126 |
WebClient webClient = new WebClient(); |
|
127 |
webClient.DownloadFile("http://wttr.in/Plzen,czechia?format=j1", $"data/{now.Year}{now.Month}{now.Day}.json"); |
|
128 |
|
|
129 |
return $"data/{now.Year}{now.Month}{now.Day}.json"; |
|
130 |
} |
|
131 |
|
|
132 | 111 |
/// <summary> |
133 | 112 |
/// Change data in a way that they now reflect sunrise and sunset times |
134 | 113 |
/// If current time under sunrise or over sunset -> WeatherConditions is Dark |
... | ... | |
137 | 116 |
private void EncompassSunRiseSetTimes() |
138 | 117 |
{ |
139 | 118 |
// change current weather |
140 |
if ((current.startTime.TimeOfDay > sunsetTime.TimeOfDay) || (current.startTime.TimeOfDay < sunriseTime.TimeOfDay))
|
|
141 |
current.condition = WeatherConditions.Dark;
|
|
119 |
if ((Current.startTime.TimeOfDay > sunsetTime.TimeOfDay) || (Current.startTime.TimeOfDay < sunriseTime.TimeOfDay))
|
|
120 |
Current.condition = WeatherConditions.Dark;
|
|
142 | 121 |
|
143 | 122 |
// change prediction |
144 |
for (int i = 0; i < predictions.Count - 1; i++)
|
|
123 |
for (int i = 0; i < Predictions.Count - 1; i++)
|
|
145 | 124 |
{ |
146 |
WeatherInfo w = predictions[i];
|
|
147 |
WeatherInfo wNext = predictions[i + 1];
|
|
125 |
WeatherInfo w = Predictions[i];
|
|
126 |
WeatherInfo wNext = Predictions[i + 1];
|
|
148 | 127 |
|
149 | 128 |
// if wNext time < than w time then it is prediction from the next day -> add 24 to correctly calculate timespan |
150 | 129 |
int timespan = wNext.startTime.Hour - w.startTime.Hour; |
... | ... | |
178 | 157 |
} |
179 | 158 |
|
180 | 159 |
// last prediction |
181 |
WeatherInfo wLast = predictions[predictions.Count - 1];
|
|
160 |
WeatherInfo wLast = Predictions[Predictions.Count - 1];
|
|
182 | 161 |
TimeSpan endTimeW = new TimeSpan(24, 0, 0); |
183 | 162 |
int timespanLast = endTimeW.Hours - wLast.startTime.Hour; |
184 | 163 |
wLast.intervalLength = timespanLast; |
... | ... | |
338 | 317 |
} |
339 | 318 |
|
340 | 319 |
// Console.WriteLine(weather.ToString()); |
341 |
predictions.Add(weather); |
|
342 |
|
|
320 |
Predictions.Add(weather); |
|
343 | 321 |
} |
344 | 322 |
|
345 | 323 |
} |
Také k dispozici: Unified diff
re #8933 Testing LoginParser