Revize 26ecc756
Přidáno uživatelem Alex Konig před téměř 4 roky(ů)
Server/ServerApp/DataDownload/DataDownloader.cs | ||
---|---|---|
95 | 95 |
|
96 | 96 |
DateTime now = DateTime.Now; |
97 | 97 |
WebClient webClient = new WebClient(); |
98 |
string data = webClient.DownloadString(predictionSite);
|
|
98 |
webClient.DownloadFile(predictionSite, $"data/{now.Year}{now.Month}{now.Day}.json");
|
|
99 | 99 |
|
100 |
return data;
|
|
100 |
return $"data/{now.Year}{now.Month}{now.Day}.json";
|
|
101 | 101 |
} |
102 | 102 |
|
103 | 103 |
/// <summary> |
Server/ServerApp/Parser/InputData/CsvDataLoader.cs | ||
---|---|---|
18 | 18 |
/// <summary> Culture info for parsing numbers </summary> |
19 | 19 |
static CultureInfo cultureInfo = new CultureInfo("de-DE"); |
20 | 20 |
|
21 |
/// <summary> |
|
22 |
/// Load file into string |
|
23 |
/// </summary> |
|
24 |
/// <param name="path">Paht to file</param> |
|
25 |
/// <returns>Null if file does not exist, or file in string</returns> |
|
26 |
public string LoadPredictionFile(string path) |
|
27 |
{ |
|
28 |
if (path == null || !File.Exists(path)) |
|
29 |
return null; |
|
30 |
|
|
31 |
return File.ReadAllText(path); |
|
32 |
} |
|
33 |
|
|
21 | 34 |
/// <summary> |
22 | 35 |
/// Load csv file into array |
23 | 36 |
/// </summary> |
Server/ServerApp/Parser/InputData/IDataLoader.cs | ||
---|---|---|
32 | 32 |
/// <returns></returns> |
33 | 33 |
List<WeatherInstance> LoadWeatherFile(string pathToFile); |
34 | 34 |
|
35 |
/// <summary> |
|
36 |
/// Load file into string |
|
37 |
/// </summary> |
|
38 |
/// <param name="path">Paht to file</param> |
|
39 |
/// <returns>Null if file does not exist, or file in string</returns> |
|
40 |
string LoadPredictionFile(string path); |
|
41 |
|
|
42 |
|
|
35 | 43 |
} |
36 | 44 |
} |
Server/ServerApp/Program.cs | ||
---|---|---|
1 | 1 |
using ServerApp.Connection; |
2 | 2 |
using ServerApp.Connection.XMLProtocolHandler; |
3 | 3 |
using ServerApp.DataDownload; |
4 |
using ServerApp.Parser.InputData; |
|
4 | 5 |
using ServerApp.Parser.OutputInfo; |
5 | 6 |
using ServerApp.Parser.Parsers; |
6 | 7 |
using ServerApp.Predictor; |
... | ... | |
131 | 132 |
|
132 | 133 |
// PARSE DATA |
133 | 134 |
|
134 |
JsonParser jsonP = new JsonParser(dd); |
|
135 |
JsonParser jsonP = new JsonParser(dd, new CsvDataLoader());
|
|
135 | 136 |
jsonP.ParsePrediction(); |
136 | 137 |
|
137 | 138 |
var res = jsonP.GetPredictionForTime(jsonP.Predictions[5].startTime, jsonP.Predictions[20].startTime); |
Server/ServerApp/WeatherPredictionParser/JsonParser.cs | ||
---|---|---|
3 | 3 |
// |
4 | 4 |
|
5 | 5 |
using ServerApp.DataDownload; |
6 |
using ServerApp.Parser.InputData; |
|
6 | 7 |
using ServerApp.Parser.OutputInfo; |
7 | 8 |
using System; |
8 | 9 |
using System.Collections.Generic; |
... | ... | |
20 | 21 |
/// <author>A. Konig</author> |
21 | 22 |
public class JsonParser : IJsonParser |
22 | 23 |
{ |
24 |
/// <summary> Data downloader </summary> |
|
25 |
DataDownloader downloader; |
|
23 | 26 |
/// <summary> Data loader </summary> |
24 |
DataDownloader loader;
|
|
27 |
IDataLoader loader;
|
|
25 | 28 |
/// <summary> Currently parsed day </summary> |
26 | 29 |
DateTime currParsedDay; |
27 | 30 |
/// <summary> Sunrise time of currently parsed day </summary> |
... | ... | |
32 | 35 |
/// <summary> |
33 | 36 |
/// Constructor |
34 | 37 |
/// </summary> |
35 |
/// <param name="loader"></param> |
|
36 |
public JsonParser(DataDownloader loader) |
|
38 |
/// <param name="downloader"></param>
|
|
39 |
public JsonParser(DataDownloader downloader, IDataLoader loader)
|
|
37 | 40 |
{ |
41 |
this.downloader = downloader; |
|
38 | 42 |
this.loader = loader; |
39 | 43 |
} |
40 | 44 |
|
... | ... | |
107 | 111 |
sunsetTime = new List<DateTime>(); |
108 | 112 |
|
109 | 113 |
// get file |
110 |
string data = loader.DownloadWeatherPrediction(); |
|
114 |
string file = downloader.DownloadWeatherPrediction(); |
|
115 |
string data = loader.LoadPredictionFile(file); |
|
116 |
|
|
117 |
if (data == null) |
|
118 |
return; |
|
119 |
|
|
111 | 120 |
DateTime now = DateTime.Now; |
112 | 121 |
|
113 | 122 |
Current = new WeatherInfo(); |
Server/TestProject/ParserTests/TestingParser.cs | ||
---|---|---|
1268 | 1268 |
string data = SetBasicData(); |
1269 | 1269 |
|
1270 | 1270 |
Mock<DataDownloader> dl = new Mock<DataDownloader>("", "", ""); |
1271 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns(data);
|
|
1271 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns("test");
|
|
1272 | 1272 |
|
1273 |
JsonParser target = new JsonParser(dl.Object); |
|
1273 |
Mock<IDataLoader> l = new Mock<IDataLoader>(); |
|
1274 |
l.Setup(m => m.LoadPredictionFile("test")).Returns(data); |
|
1275 |
|
|
1276 |
JsonParser target = new JsonParser(dl.Object, l.Object); |
|
1274 | 1277 |
|
1275 | 1278 |
target.ParsePrediction(); |
1276 | 1279 |
WeatherInfo current = target.Current; |
... | ... | |
1284 | 1287 |
string data = SetBasicData(); |
1285 | 1288 |
|
1286 | 1289 |
Mock<DataDownloader> dl = new Mock<DataDownloader>("", "", ""); |
1287 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns(data); |
|
1290 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns("test"); |
|
1291 |
|
|
1292 |
Mock<IDataLoader> l = new Mock<IDataLoader>(); |
|
1293 |
l.Setup(m => m.LoadPredictionFile("test")).Returns(data); |
|
1288 | 1294 |
|
1289 |
JsonParser target = new JsonParser(dl.Object); |
|
1295 |
JsonParser target = new JsonParser(dl.Object, l.Object);
|
|
1290 | 1296 |
|
1291 | 1297 |
target.ParsePrediction(); |
1292 | 1298 |
List<WeatherInfo> retVal = target.Predictions; |
... | ... | |
1306 | 1312 |
string data = SetAstronomyData(); |
1307 | 1313 |
|
1308 | 1314 |
Mock<DataDownloader> dl = new Mock<DataDownloader>("", "", ""); |
1309 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns(data);
|
|
1315 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns("test");
|
|
1310 | 1316 |
|
1311 |
JsonParser target = new JsonParser(dl.Object); |
|
1317 |
Mock<IDataLoader> l = new Mock<IDataLoader>(); |
|
1318 |
l.Setup(m => m.LoadPredictionFile("test")).Returns(data); |
|
1319 |
|
|
1320 |
JsonParser target = new JsonParser(dl.Object, l.Object); |
|
1312 | 1321 |
|
1313 | 1322 |
target.ParsePrediction(); |
1314 | 1323 |
List<WeatherInfo> retVal = target.Predictions; |
... | ... | |
1333 | 1342 |
{ |
1334 | 1343 |
string data = ""; |
1335 | 1344 |
Mock<DataDownloader> dl = new Mock<DataDownloader>("", "", ""); |
1336 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns(data); |
|
1345 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns("test"); |
|
1346 |
|
|
1347 |
Mock<IDataLoader> l = new Mock<IDataLoader>(); |
|
1348 |
l.Setup(m => m.LoadPredictionFile("test")).Returns(data); |
|
1337 | 1349 |
|
1338 |
JsonParser target = new JsonParser(dl.Object); |
|
1350 |
JsonParser target = new JsonParser(dl.Object, l.Object);
|
|
1339 | 1351 |
|
1340 | 1352 |
List<WeatherInfo> pred = new List<WeatherInfo>(); |
1341 | 1353 |
pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 0, 0, 0), 8, 1, 2, 60_000, 3)); |
... | ... | |
1367 | 1379 |
{ |
1368 | 1380 |
string data = ""; |
1369 | 1381 |
Mock<DataDownloader> dl = new Mock<DataDownloader>("", "", ""); |
1370 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns(data);
|
|
1382 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns("test");
|
|
1371 | 1383 |
|
1372 |
JsonParser target = new JsonParser(dl.Object); |
|
1384 |
Mock<IDataLoader> l = new Mock<IDataLoader>(); |
|
1385 |
l.Setup(m => m.LoadPredictionFile("test")).Returns(data); |
|
1386 |
|
|
1387 |
JsonParser target = new JsonParser(dl.Object, l.Object); |
|
1373 | 1388 |
|
1374 | 1389 |
List<WeatherInfo> pred = new List<WeatherInfo>(); |
1375 | 1390 |
pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 0, 0, 0), 8, 1, 2, 60_000, 3)); |
... | ... | |
1394 | 1409 |
{ |
1395 | 1410 |
string data = ""; |
1396 | 1411 |
Mock<DataDownloader> dl = new Mock<DataDownloader>("", "", ""); |
1397 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns(data); |
|
1412 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns("test"); |
|
1413 |
|
|
1414 |
Mock<IDataLoader> l = new Mock<IDataLoader>(); |
|
1415 |
l.Setup(m => m.LoadPredictionFile("test")).Returns(data); |
|
1398 | 1416 |
|
1399 |
JsonParser target = new JsonParser(dl.Object); |
|
1417 |
JsonParser target = new JsonParser(dl.Object, l.Object);
|
|
1400 | 1418 |
|
1401 | 1419 |
List<WeatherInfo> pred = new List<WeatherInfo>(); |
1402 | 1420 |
pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 0, 0, 0), 8, 1, 2, 60_000, 3)); |
... | ... | |
1426 | 1444 |
{ |
1427 | 1445 |
string data = ""; |
1428 | 1446 |
Mock<DataDownloader> dl = new Mock<DataDownloader>("", "", ""); |
1429 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns(data);
|
|
1447 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns("test");
|
|
1430 | 1448 |
|
1431 |
JsonParser target = new JsonParser(dl.Object); |
|
1449 |
Mock<IDataLoader> l = new Mock<IDataLoader>(); |
|
1450 |
l.Setup(m => m.LoadPredictionFile("test")).Returns(data); |
|
1451 |
|
|
1452 |
JsonParser target = new JsonParser(dl.Object, l.Object); |
|
1432 | 1453 |
|
1433 | 1454 |
List<WeatherInfo> pred = new List<WeatherInfo>(); |
1434 | 1455 |
pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 0, 0, 0), 8, 1, 2, 60_000, 3)); |
... | ... | |
1455 | 1476 |
// TODO make an input file |
1456 | 1477 |
string data = ""; |
1457 | 1478 |
Mock<DataDownloader> dl = new Mock<DataDownloader>("", "", ""); |
1458 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns(data); |
|
1479 |
dl.Setup(m => m.DownloadWeatherPrediction()).Returns("test"); |
|
1480 |
|
|
1481 |
Mock<IDataLoader> l = new Mock<IDataLoader>(); |
|
1482 |
l.Setup(m => m.LoadPredictionFile("test")).Returns(data); |
|
1459 | 1483 |
|
1460 |
JsonParser target = new JsonParser(dl.Object); |
|
1484 |
JsonParser target = new JsonParser(dl.Object, l.Object);
|
|
1461 | 1485 |
|
1462 | 1486 |
List<WeatherInfo> pred = new List<WeatherInfo>(); |
1463 | 1487 |
pred.Add(new WeatherInfo(new DateTime(2000, 1, 1, 0, 0, 0), 8, 1, 2, 60_000, 3)); |
Také k dispozici: Unified diff
re #8962 Adding reading of file to string to CsvDataLoader