Revize 22211075
Přidáno uživatelem Roman Kalivoda před více než 3 roky(ů)
Server/ServerApp/WeatherPredictionParser/JsonParser.cs | ||
---|---|---|
20 | 20 |
/// <author>A. Konig</author> |
21 | 21 |
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 |
|
|
23 | 30 |
/// <summary> Data loader </summary> |
24 | 31 |
DataDownloader loader; |
25 | 32 |
/// <summary> Currently parsed day </summary> |
... | ... | |
39 | 46 |
this.loader = loader; |
40 | 47 |
} |
41 | 48 |
|
42 |
/// <summary> |
|
43 |
/// Get predictions from Predictions that are within specified time span |
|
44 |
/// From-to including |
|
45 |
/// If from == null then all until to |
|
46 |
/// If to == null then all starting from from |
|
47 |
/// </summary> |
|
48 |
/// <param name="from">DateTime from</param> |
|
49 |
/// <param name="to">DateTime to</param> |
|
50 |
/// <returns>List of predictions that fit specified criteria or null if incorrect input</returns> |
|
51 |
public override List<WeatherInfo> GetPredictionForTime(DateTime from, DateTime to) |
|
52 |
{ |
|
53 |
if (Predictions == null) |
|
54 |
return null; |
|
55 |
|
|
56 |
List<WeatherInfo> res = new List<WeatherInfo>(); |
|
57 |
|
|
58 |
if (from == null) |
|
59 |
from = Predictions[0].startTime; |
|
60 |
|
|
61 |
if (to == null) |
|
62 |
from = Predictions[Predictions.Count].startTime; |
|
63 |
|
|
64 |
if (from > to) |
|
65 |
return null; |
|
66 |
|
|
67 |
foreach (WeatherInfo pred in Predictions) |
|
68 |
{ |
|
69 |
int hour = pred.startTime.Hour + pred.intervalLength; |
|
70 |
Console.WriteLine(pred.intervalLength); |
|
71 |
bool addDay = false; |
|
72 |
if (hour >= 24) |
|
73 |
{ |
|
74 |
hour -= 24; |
|
75 |
addDay = true; |
|
76 |
} |
|
77 |
DateTime endTime = new DateTime(pred.startTime.Year, pred.startTime.Month, pred.startTime.Day, hour, pred.startTime.Minute, pred.startTime.Second); |
|
78 |
if (addDay) |
|
79 |
endTime = endTime.AddDays(1); |
|
80 |
|
|
81 |
// if both end and start not outside of interval |
|
82 |
if (!((pred.startTime < from && endTime <= from) || (pred.startTime > to && endTime >= to))) |
|
83 |
res.Add(pred); |
|
84 |
} |
|
85 |
|
|
86 |
return res; |
|
87 |
} |
|
88 |
|
|
89 | 49 |
/// <summary> |
90 | 50 |
/// Parse weather prediction |
91 | 51 |
/// Results is in attributes current for current weather and pred for weather prediction for today, tommorrow and day after tommorrow |
... | ... | |
99 | 59 |
DateTime now = DateTime.Now; |
100 | 60 |
Console.WriteLine(File.Exists(file)); |
101 | 61 |
|
102 |
Current = new WeatherInfo();
|
|
103 |
Predictions = new List<WeatherInfo>();
|
|
62 |
current = new WeatherInfo();
|
|
63 |
predictions = new List<WeatherInfo>();
|
|
104 | 64 |
|
105 | 65 |
if (!File.Exists(file)) |
106 | 66 |
return; |
... | ... | |
124 | 84 |
case "current_condition": |
125 | 85 |
{ |
126 | 86 |
ArrayEnumerator currentWeather = weatherP.Current.Value.EnumerateArray(); |
127 |
Current = ParseCurrentWeather(currentWeather);
|
|
87 |
current = ParseCurrentWeather(currentWeather);
|
|
128 | 88 |
|
129 | 89 |
break; |
130 | 90 |
} |
... | ... | |
150 | 110 |
/// </summary> |
151 | 111 |
private void TestConsoleOutput() |
152 | 112 |
{ |
153 |
Console.WriteLine(Current);
|
|
154 |
foreach (WeatherInfo w in Predictions)
|
|
113 |
Console.WriteLine(current);
|
|
114 |
foreach (WeatherInfo w in predictions)
|
|
155 | 115 |
Console.WriteLine(w); |
156 | 116 |
} |
157 | 117 |
|
... | ... | |
177 | 137 |
private void EncompassSunRiseSetTimes() |
178 | 138 |
{ |
179 | 139 |
// change current weather |
180 |
if ((Current.startTime.TimeOfDay > sunsetTime.TimeOfDay) || (Current.startTime.TimeOfDay < sunriseTime.TimeOfDay))
|
|
181 |
Current.condition = WeatherConditions.Dark;
|
|
140 |
if ((current.startTime.TimeOfDay > sunsetTime.TimeOfDay) || (current.startTime.TimeOfDay < sunriseTime.TimeOfDay))
|
|
141 |
current.condition = WeatherConditions.Dark;
|
|
182 | 142 |
|
183 | 143 |
// change prediction |
184 |
for (int i = 0; i < Predictions.Count - 1; i++)
|
|
144 |
for (int i = 0; i < predictions.Count - 1; i++)
|
|
185 | 145 |
{ |
186 |
WeatherInfo w = Predictions[i];
|
|
187 |
WeatherInfo wNext = Predictions[i + 1];
|
|
146 |
WeatherInfo w = predictions[i];
|
|
147 |
WeatherInfo wNext = predictions[i + 1];
|
|
188 | 148 |
|
189 | 149 |
// if wNext time < than w time then it is prediction from the next day -> add 24 to correctly calculate timespan |
190 | 150 |
int timespan = wNext.startTime.Hour - w.startTime.Hour; |
... | ... | |
218 | 178 |
} |
219 | 179 |
|
220 | 180 |
// last prediction |
221 |
WeatherInfo wLast = Predictions[Predictions.Count - 1];
|
|
181 |
WeatherInfo wLast = predictions[predictions.Count - 1];
|
|
222 | 182 |
TimeSpan endTimeW = new TimeSpan(24, 0, 0); |
223 |
int timespanLast = 24 - wLast.startTime.Hour;
|
|
183 |
int timespanLast = endTimeW.Hours - wLast.startTime.Hour;
|
|
224 | 184 |
wLast.intervalLength = timespanLast; |
225 | 185 |
|
226 | 186 |
// if start under sunset |
... | ... | |
378 | 338 |
} |
379 | 339 |
|
380 | 340 |
// Console.WriteLine(weather.ToString()); |
381 |
Predictions.Add(weather);
|
|
341 |
predictions.Add(weather);
|
|
382 | 342 |
|
383 | 343 |
} |
384 | 344 |
|
... | ... | |
441 | 401 |
return res; |
442 | 402 |
} |
443 | 403 |
|
444 |
|
|
445 | 404 |
} |
446 | 405 |
} |
Také k dispozici: Unified diff
Revert "Merge branch 'PredictorAPI' into 'master'"
This reverts merge request !20