Projekt

Obecné

Profil

Stáhnout (7.31 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
            predictionController.Train();
177
            //var results = predictionController.Predict()
178 d358b79e Roman Kalivoda
			
179 cdf3c217 A-Konig
180 6d0d1410 Eliška Mourycová
			return predictionController;
181
		}
182 cdf3c217 A-Konig
183 14bef778 Eliška Mourycová
		private static void ConnectionTest(IPredictionController predictionController, Config config)
184 6d0d1410 Eliška Mourycová
		{
185 da9245bf Eliška Mourycová
			ConnectionListenerAsync cl = new ConnectionListenerAsync(int.Parse(config.Port), predictionController);
186 6d0d1410 Eliška Mourycová
			cl.StartListening();
187 7a998d66 Eliška Mourycová
188 6d0d1410 Eliška Mourycová
			//HttpRequestHandler hrh = new HttpRequestHandler(int.Parse(config.Port));
189
			//hrh.ListenAsynchronously();
190
		}
191 7a998d66 Eliška Mourycová
192
193 6d0d1410 Eliška Mourycová
		private static Config FillConfigInfo(string[] args)
194 7a998d66 Eliška Mourycová
		{
195
196
			Config extractedConfigInfo = new Config();
197
198
			Console.WriteLine(Directory.GetCurrentDirectory());
199 9547fd4a Eliška Mourycová
			Console.WriteLine("Parsing configuration file...");
200 7a998d66 Eliška Mourycová
			
201
			if (args.Length != 1)
202 9cc42e60 Eliška Mourycová
				Console.WriteLine("Wrong usage of parameters, pass only the path to a config file.");
203 7a998d66 Eliška Mourycová
204
			string fullPathConfig = Path.GetFullPath(args[0]);
205
			string[] lines = null;
206
			try {
207
				lines = File.ReadAllLines(fullPathConfig);
208
			}
209
			catch(Exception ex)
210
			{
211
				Console.WriteLine("Could not open " + fullPathConfig);
212
				return null;
213
			}
214
			
215
			for (var i = 0; i < lines.Length; i += 1)
216
			{
217
				string line = lines[i];
218 9547fd4a Eliška Mourycová
				//Console.WriteLine(line);
219 7a998d66 Eliška Mourycová
				if (line.Length == 0 || line == null || line.StartsWith("#"))
220
					continue;
221
222
				switch (line)
223
				{
224
					case "!site!":
225
						extractedConfigInfo.DataWebsite = lines[++i].Trim();
226
						break;
227
					case "!naming_convention!":
228
						extractedConfigInfo.DownloadedFilesNaming = lines[++i].Trim();
229
						break;
230
					case "!data_root_dir!":
231 9547fd4a Eliška Mourycová
						//string rootDir = lines[++i];
232
						//rootDir = rootDir.Replace('\\', Path.DirectorySeparatorChar);
233 7a998d66 Eliška Mourycová
						string dirWithConfig = Path.GetDirectoryName(fullPathConfig);
234
						string rootPath = dirWithConfig + Path.DirectorySeparatorChar + lines[++i].Trim();
235 9547fd4a Eliška Mourycová
						rootPath = Path.GetFullPath(rootPath);
236 7a998d66 Eliška Mourycová
						extractedConfigInfo.DataRootDir = rootPath;
237
						break;
238 38a18391 Eliška Mourycová
					case "!weather_site!":
239
						extractedConfigInfo.WeatherSite = lines[++i].Trim();
240
						break;
241 7a998d66 Eliška Mourycová
					case "!port!":
242
						extractedConfigInfo.Port = lines[++i].Trim();
243
						break;
244
					default: break;
245
				}
246
			}
247
248 9cc42e60 Eliška Mourycová
			int parsedPort;
249
250
			bool success = int.TryParse(extractedConfigInfo.Port, out parsedPort);
251
			if (!success)
252
			{
253
				Console.WriteLine("Configured port " + extractedConfigInfo.Port + " is not an integer number! Abort.");
254
				return null;
255
			}
256
				
257
258 7a998d66 Eliška Mourycová
			return extractedConfigInfo;
259
		}
260 3811845f Alex Konig
    }
261
}