Projekt

Obecné

Profil

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