Projekt

Obecné

Profil

Stáhnout (7.27 KB) Statistiky
| Větev: | Tag: | Revize:
1 0d31f7e0 Roman Kalivoda
using log4net.Config;
2
using ServerApp.Connection;
3 6b4c34a2 Eliška Mourycová
using ServerApp.Connection.XMLProtocolHandler;
4 7a998d66 Eliška Mourycová
using ServerApp.DataDownload;
5 26ecc756 A-Konig
using ServerApp.Parser.InputData;
6 fffe7190 A-Konig
using ServerApp.Parser.OutputInfo;
7 57a75f60 Roman Kalivoda
using ServerApp.Parser.Parsers;
8 9fc5fa93 Roman Kalivoda
using ServerApp.Predictor;
9 4a417b8b Eliška Mourycová
using ServerApp.User;
10 cdf3c217 A-Konig
using ServerApp.WeatherPredictionParser;
11 3811845f Alex Konig
using System;
12
using System.Collections.Generic;
13 7a998d66 Eliška Mourycová
using System.IO;
14 4a417b8b Eliška Mourycová
using System.Threading;
15 3811845f Alex Konig
16
namespace ServerApp
17
{
18 7a998d66 Eliška Mourycová
19 9cc42e60 Eliška Mourycová
	class Config
20 7a998d66 Eliška Mourycová
	{
21
		public string DataWebsite { get; set; }
22
		public string DownloadedFilesNaming { get; set; }
23
		public string DataRootDir { get; set; }
24 38a18391 Eliška Mourycová
		public string WeatherSite { get; set; }
25 cf35739b Eliška Mourycová
		public string Port { get; set; } 
26 7a998d66 Eliška Mourycová
	}
27
28 4a417b8b Eliška Mourycová
    public class Program
29 3811845f Alex Konig
    {
30 7a998d66 Eliška Mourycová
31 3811845f Alex Konig
        static void Main(string[] args)
32
        {
33 0060a0ae Roman Kalivoda
            // setup logging service
34 0d31f7e0 Roman Kalivoda
            XmlConfigurator.Configure();
35 27aa66b5 A-Konig
36
			// SETUP FOLDERS
37
			Config config = FillConfigInfo(args);
38 3aba3c34 Eliška Mourycová
			if (config == null)
39
			{
40 9cc42e60 Eliška Mourycová
				Console.WriteLine("Configuration file parsing failed. Abort.");
41 3aba3c34 Eliška Mourycová
				Console.ReadLine();
42
				return;
43
			}
44 7a998d66 Eliška Mourycová
45 6d0d1410 Eliška Mourycová
			// data download test
46 14bef778 Eliška Mourycová
			DataDownloader dd = DataDownloadAndRetrievalTest(config);
47 6d0d1410 Eliška Mourycová
48 9cc42e60 Eliška Mourycová
			//WeatherAsStringTest(dd);
49
			//Console.ReadLine();
50 27aa66b5 A-Konig
51 6d0d1410 Eliška Mourycová
			// xml building test
52 92897198 Eliška Mourycová
			//XMLTest();
53 6d0d1410 Eliška Mourycová
54
			// PARSE DATA
55 b78c3571 Eliška Mourycová
			//DataParser parser = new DataParser(dd);
56
			//parser.Parse(new DateTime())
57 6d0d1410 Eliška Mourycová
58
			// json parser test
59
			//JSONParserTest();
60
61
			// model test
62 14bef778 Eliška Mourycová
			IPredictionController controller = PredictionTest(dd);
63 6d0d1410 Eliška Mourycová
64
			//parse(new DateTime(2019, 10, 5), , interval = 1, wholeDay = true)
65
66 c4383c00 Eliška Mourycová
			// commands accepting test
67
			// create a thread for commands accepting:
68
			CommandsAcceptor ca = new CommandsAcceptor(dd, controller);
69
			Thread inputThread = new Thread(ca.AcceptCommand);
70
			inputThread.Start();
71
72 9cc42e60 Eliška Mourycová
73 6d0d1410 Eliška Mourycová
			// connection test
74 8800fb76 Eliška Mourycová
			ConnectionTest(controller, config);
75 c4383c00 Eliška Mourycová
76 9cc42e60 Eliška Mourycová
			//Console.ReadLine();
77 6d0d1410 Eliška Mourycová
        }
78
79
80
		private static DataDownloader DataDownloadAndRetrievalTest(Config config)
81
		{
82 d358b79e Roman Kalivoda
			//test scenario -data download:
83 38a18391 Eliška Mourycová
			DataDownloader dd = new DataDownloader(config.DataRootDir, config.DataWebsite, config.DownloadedFilesNaming, config.WeatherSite);
84 d358b79e Roman Kalivoda
			dd.OverwriteExisting = false;
85
			List<string> savedFiles = new List<string>();
86 9547fd4a Eliška Mourycová
87
			// -> 12-2019 to exclude corona? But we lose a lot of data
88
			savedFiles.AddRange(dd.DownloadData(DataType.JIS, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
89
			savedFiles.AddRange(dd.DownloadData(DataType.STROJE, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
90
			savedFiles.AddRange(dd.DownloadData(DataType.POCASI, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
91 4129ce12 Eliška Mourycová
92
93
94 6d0d1410 Eliška Mourycová
			Console.WriteLine("Saved files: ");
95
			foreach (string s in savedFiles)
96
			{
97
				Console.WriteLine(s);
98
			}
99
100
			Console.WriteLine("subdirectories: ");
101
			foreach (KeyValuePair<DataType, string> kvp in dd.DataSubDirectories)
102
			{
103
				Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
104
			}
105
106
			List<string> retrievedData = dd.GetData(dd.DataSubDirectories[DataType.JIS], new DataDownload.Date(10, 2019), new DataDownload.Date(12, 2020));
107 3aba3c34 Eliška Mourycová
			Console.WriteLine("Retrieved data: ");
108
			foreach (string s in retrievedData)
109
			{
110
				Console.WriteLine(s);
111
			}
112 6d0d1410 Eliška Mourycová
			Console.WriteLine("all from directory:");
113
			retrievedData = dd.GetData(dd.DataSubDirectories[DataType.JIS], null, null);
114
			foreach (string s in retrievedData)
115
			{
116
				Console.WriteLine(s);
117
			}
118 36c0667f Eliška Mourycová
119 cdf3c217 A-Konig
120 6d0d1410 Eliška Mourycová
			return dd;
121
		}
122 cdf3c217 A-Konig
123
124 6d0d1410 Eliška Mourycová
		private static void XMLTest()
125
		{
126
			Response response = Response.Randomize();
127
			var xml = XmlCommunication.Serialize(response);
128 86c29fc1 Eliška Mourycová
			//xml = "haha";
129
			Response response1 = new Response();
130 6d0d1410 Eliška Mourycová
			Console.WriteLine(xml);
131 86c29fc1 Eliška Mourycová
			try
132
			{
133 5cb4249a Eliška Mourycová
				Response responseDeserialized = XmlCommunication.Deserialize<Response>(xml);
134 86c29fc1 Eliška Mourycová
			}
135
			catch(Exception e)
136
			{
137
				Console.WriteLine("bad format");
138
			}
139
			
140 d358b79e Roman Kalivoda
141 6d0d1410 Eliška Mourycová
			Console.WriteLine("------");
142 cdf3c217 A-Konig
143
144 6d0d1410 Eliška Mourycová
			Request request = Request.Randomize();
145
			xml = XmlCommunication.Serialize(request);
146
			Console.WriteLine(xml);
147 5cb4249a Eliška Mourycová
			Request requestDeserialized = XmlCommunication.Deserialize<Request>(xml);
148 6d0d1410 Eliška Mourycová
		}
149
150 d358b79e Roman Kalivoda
151 6d0d1410 Eliška Mourycová
		private static void JSONParserTest()
152
		{
153 847434c6 Roman Kalivoda
            // FIXME pass the right references to the JsonParser constructor
154
			JsonParser jsonP = new JsonParser(null, null);
155 cdf3c217 A-Konig
			jsonP.ParsePrediction();
156
157 fffe7190 A-Konig
			var res = jsonP.GetPredictionForTime(jsonP.Predictions[5].startTime, jsonP.Predictions[20].startTime);
158
			Console.WriteLine("from " + jsonP.Predictions[5].startTime);
159
			Console.WriteLine("end " + jsonP.Predictions[20].startTime);
160
			foreach (WeatherInfo w in res)
161
				Console.WriteLine(w.ToString());
162 6d0d1410 Eliška Mourycová
		}
163 fffe7190 A-Konig
164 38a18391 Eliška Mourycová
		private static void WeatherAsStringTest(DataDownloader dd)
165
		{
166
			Console.WriteLine(dd.DownloadWeatherPrediction());
167
		}
168
169 6d0d1410 Eliška Mourycová
		private static IPredictionController PredictionTest(DataDownloader dd)
170
		{
171 cdf3c217 A-Konig
			// TODO nastavit čas
172 99e5517e A-Konig
			IDataParser p = new DataParser(dd);
173 847434c6 Roman Kalivoda
            // FIXME pass the right references to the JsonParser constructor
174 0d31f7e0 Roman Kalivoda
            IJsonParser jsonP = new JsonParser(dd, new CsvDataLoader());
175 870cd163 Roman Kalivoda
            IPredictionController predictionController = new PredictionController(jsonP, p);
176 ebe96ca4 Roman Kalivoda
            //var results = predictionController.Predict()
177 d358b79e Roman Kalivoda
			
178 cdf3c217 A-Konig
179 6d0d1410 Eliška Mourycová
			return predictionController;
180
		}
181 cdf3c217 A-Konig
182 14bef778 Eliška Mourycová
		private static void ConnectionTest(IPredictionController predictionController, Config config)
183 6d0d1410 Eliška Mourycová
		{
184 da9245bf Eliška Mourycová
			ConnectionListenerAsync cl = new ConnectionListenerAsync(int.Parse(config.Port), predictionController);
185 6d0d1410 Eliška Mourycová
			cl.StartListening();
186 7a998d66 Eliška Mourycová
187 6d0d1410 Eliška Mourycová
			//HttpRequestHandler hrh = new HttpRequestHandler(int.Parse(config.Port));
188
			//hrh.ListenAsynchronously();
189
		}
190 7a998d66 Eliška Mourycová
191
192 6d0d1410 Eliška Mourycová
		private static Config FillConfigInfo(string[] args)
193 7a998d66 Eliška Mourycová
		{
194
195
			Config extractedConfigInfo = new Config();
196
197
			Console.WriteLine(Directory.GetCurrentDirectory());
198 9547fd4a Eliška Mourycová
			Console.WriteLine("Parsing configuration file...");
199 7a998d66 Eliška Mourycová
			
200
			if (args.Length != 1)
201 9cc42e60 Eliška Mourycová
				Console.WriteLine("Wrong usage of parameters, pass only the path to a config file.");
202 7a998d66 Eliška Mourycová
203
			string fullPathConfig = Path.GetFullPath(args[0]);
204
			string[] lines = null;
205
			try {
206
				lines = File.ReadAllLines(fullPathConfig);
207
			}
208
			catch(Exception ex)
209
			{
210
				Console.WriteLine("Could not open " + fullPathConfig);
211
				return null;
212
			}
213
			
214
			for (var i = 0; i < lines.Length; i += 1)
215
			{
216
				string line = lines[i];
217 9547fd4a Eliška Mourycová
				//Console.WriteLine(line);
218 7a998d66 Eliška Mourycová
				if (line.Length == 0 || line == null || line.StartsWith("#"))
219
					continue;
220
221
				switch (line)
222
				{
223
					case "!site!":
224
						extractedConfigInfo.DataWebsite = lines[++i].Trim();
225
						break;
226
					case "!naming_convention!":
227
						extractedConfigInfo.DownloadedFilesNaming = lines[++i].Trim();
228
						break;
229
					case "!data_root_dir!":
230 9547fd4a Eliška Mourycová
						//string rootDir = lines[++i];
231
						//rootDir = rootDir.Replace('\\', Path.DirectorySeparatorChar);
232 7a998d66 Eliška Mourycová
						string dirWithConfig = Path.GetDirectoryName(fullPathConfig);
233
						string rootPath = dirWithConfig + Path.DirectorySeparatorChar + lines[++i].Trim();
234 9547fd4a Eliška Mourycová
						rootPath = Path.GetFullPath(rootPath);
235 7a998d66 Eliška Mourycová
						extractedConfigInfo.DataRootDir = rootPath;
236
						break;
237 38a18391 Eliška Mourycová
					case "!weather_site!":
238
						extractedConfigInfo.WeatherSite = lines[++i].Trim();
239
						break;
240 7a998d66 Eliška Mourycová
					case "!port!":
241
						extractedConfigInfo.Port = lines[++i].Trim();
242
						break;
243
					default: break;
244
				}
245
			}
246
247 9cc42e60 Eliška Mourycová
			int parsedPort;
248
249
			bool success = int.TryParse(extractedConfigInfo.Port, out parsedPort);
250
			if (!success)
251
			{
252
				Console.WriteLine("Configured port " + extractedConfigInfo.Port + " is not an integer number! Abort.");
253
				return null;
254
			}
255
				
256
257 7a998d66 Eliška Mourycová
			return extractedConfigInfo;
258
		}
259 3811845f Alex Konig
    }
260
}