Projekt

Obecné

Profil

Stáhnout (7.54 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 7a998d66 Eliška Mourycová
52 f7eb37b3 Eliška Mourycová
			// data download init
53
			Console.WriteLine("Downloading open data...");
54
			DataDownloader dd = DataDownloaderInit(config);
55
			Console.WriteLine("Data downloaded and saved.");
56 6d0d1410 Eliška Mourycová
57
58 f7eb37b3 Eliška Mourycová
			// model init
59
			Console.WriteLine("Training the predictor...");
60
			IPredictionController controller = PredictionControllerInit(dd);
61
			Console.WriteLine("Predictor training finished.");
62 6d0d1410 Eliška Mourycová
63
64 fbc74182 Eliška Mourycová
			// commands accepting
65 c4383c00 Eliška Mourycová
			// create a thread for commands accepting:
66
			CommandsAcceptor ca = new CommandsAcceptor(dd, controller);
67
			Thread inputThread = new Thread(ca.AcceptCommand);
68
			inputThread.Start();
69
70 9cc42e60 Eliška Mourycová
71 f7eb37b3 Eliška Mourycová
			// connection init
72
			ConnectionInit(controller, config);
73 c4383c00 Eliška Mourycová
74 6d0d1410 Eliška Mourycová
        }
75
76 127a92b1 Eliška Mourycová
		/// <summary>
77
		/// Initializes DataDownloader. Downloads default span of data and saves the files.
78
		/// </summary>
79
		/// <param name="config">The Config instance</param>
80
		/// <returns>New instance of DataDownloader</returns>
81 f7eb37b3 Eliška Mourycová
		private static DataDownloader DataDownloaderInit(Config config)
82 6d0d1410 Eliška Mourycová
		{
83 127a92b1 Eliška Mourycová
			
84 38a18391 Eliška Mourycová
			DataDownloader dd = new DataDownloader(config.DataRootDir, config.DataWebsite, config.DownloadedFilesNaming, config.WeatherSite);
85 d358b79e Roman Kalivoda
			dd.OverwriteExisting = false;
86
			List<string> savedFiles = new List<string>();
87 9547fd4a Eliška Mourycová
88 127a92b1 Eliška Mourycová
			// -> 12-2019 to exclude corona period which could mess with the predictor
89 9547fd4a Eliška Mourycová
			savedFiles.AddRange(dd.DownloadData(DataType.JIS, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
90
			savedFiles.AddRange(dd.DownloadData(DataType.STROJE, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
91
			savedFiles.AddRange(dd.DownloadData(DataType.POCASI, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
92 4129ce12 Eliška Mourycová
93
94
95 f7eb37b3 Eliška Mourycová
			//Console.WriteLine("Saved files: ");
96
			//foreach (string s in savedFiles)
97
			//{
98
			//	Console.WriteLine(s);
99
			//}
100 6d0d1410 Eliška Mourycová
101 f7eb37b3 Eliška Mourycová
			//Console.WriteLine("subdirectories: ");
102
			//foreach (KeyValuePair<DataType, string> kvp in dd.DataSubDirectories)
103
			//{
104
			//	Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
105
			//}
106 6d0d1410 Eliška Mourycová
107 f7eb37b3 Eliška Mourycová
			//List<string> retrievedData = dd.GetData(dd.DataSubDirectories[DataType.JIS], new DataDownload.Date(10, 2019), new DataDownload.Date(12, 2020));
108
			//Console.WriteLine("Retrieved data: ");
109
			//foreach (string s in retrievedData)
110
			//{
111
			//	Console.WriteLine(s);
112
			//}
113
			//Console.WriteLine("all from directory:");
114
			//retrievedData = dd.GetData(dd.DataSubDirectories[DataType.JIS], null, null);
115
			//foreach (string s in retrievedData)
116
			//{
117
			//	Console.WriteLine(s);
118
			//}
119 36c0667f Eliška Mourycová
120 cdf3c217 A-Konig
121 6d0d1410 Eliška Mourycová
			return dd;
122
		}
123 cdf3c217 A-Konig
124
125 6d0d1410 Eliška Mourycová
		private static void JSONParserTest()
126
		{
127 847434c6 Roman Kalivoda
            // FIXME pass the right references to the JsonParser constructor
128
			JsonParser jsonP = new JsonParser(null, null);
129 cdf3c217 A-Konig
			jsonP.ParsePrediction();
130
131 fffe7190 A-Konig
			var res = jsonP.GetPredictionForTime(jsonP.Predictions[5].startTime, jsonP.Predictions[20].startTime);
132
			Console.WriteLine("from " + jsonP.Predictions[5].startTime);
133
			Console.WriteLine("end " + jsonP.Predictions[20].startTime);
134
			foreach (WeatherInfo w in res)
135
				Console.WriteLine(w.ToString());
136 6d0d1410 Eliška Mourycová
		}
137 fffe7190 A-Konig
138 38a18391 Eliška Mourycová
		private static void WeatherAsStringTest(DataDownloader dd)
139
		{
140
			Console.WriteLine(dd.DownloadWeatherPrediction());
141
		}
142
143 127a92b1 Eliška Mourycová
		/// <summary>
144
		/// Initializes and trains the predictor if need be.
145
		/// </summary>
146
		/// <param name="dd">The DataDownloader instance.</param>
147
		/// <returns>New instance of the predictor.</returns>
148 f7eb37b3 Eliška Mourycová
		private static IPredictionController PredictionControllerInit(DataDownloader dd)
149 6d0d1410 Eliška Mourycová
		{
150 99e5517e A-Konig
			IDataParser p = new DataParser(dd);
151 847434c6 Roman Kalivoda
            // FIXME pass the right references to the JsonParser constructor
152 0d31f7e0 Roman Kalivoda
            IJsonParser jsonP = new JsonParser(dd, new CsvDataLoader());
153 870cd163 Roman Kalivoda
            IPredictionController predictionController = new PredictionController(jsonP, p);
154 ebe96ca4 Roman Kalivoda
            //var results = predictionController.Predict()
155 d358b79e Roman Kalivoda
			
156 cdf3c217 A-Konig
157 6d0d1410 Eliška Mourycová
			return predictionController;
158
		}
159 cdf3c217 A-Konig
160 127a92b1 Eliška Mourycová
		/// <summary>
161
		/// Initializes waiting for the clients' requests.
162
		/// </summary>
163
		/// <param name="predictionController">The initialized and trained instance of the predictor.</param>
164
		/// <param name="config">The Config class instance.</param>
165 f7eb37b3 Eliška Mourycová
		private static void ConnectionInit(IPredictionController predictionController, Config config)
166 6d0d1410 Eliška Mourycová
		{
167 da9245bf Eliška Mourycová
			ConnectionListenerAsync cl = new ConnectionListenerAsync(int.Parse(config.Port), predictionController);
168 6d0d1410 Eliška Mourycová
			cl.StartListening();
169 7a998d66 Eliška Mourycová
170 6d0d1410 Eliška Mourycová
		}
171 7a998d66 Eliška Mourycová
172 127a92b1 Eliška Mourycová
		/// <summary>
173
		/// Parses the configuration file and extracts valuable information from it.
174
		/// </summary>
175
		/// <param name="args">The arguments passed to the program.</param>
176
		/// <returns>Filled instance of the Config class, null if there was a problem.</returns>
177 6d0d1410 Eliška Mourycová
		private static Config FillConfigInfo(string[] args)
178 7a998d66 Eliška Mourycová
		{
179
180
			Config extractedConfigInfo = new Config();
181
182 9547fd4a Eliška Mourycová
			Console.WriteLine("Parsing configuration file...");
183 7a998d66 Eliška Mourycová
			
184
			if (args.Length != 1)
185 9cc42e60 Eliška Mourycová
				Console.WriteLine("Wrong usage of parameters, pass only the path to a config file.");
186 7a998d66 Eliška Mourycová
187
			string fullPathConfig = Path.GetFullPath(args[0]);
188
			string[] lines = null;
189
			try {
190
				lines = File.ReadAllLines(fullPathConfig);
191
			}
192
			catch(Exception ex)
193
			{
194
				Console.WriteLine("Could not open " + fullPathConfig);
195 127a92b1 Eliška Mourycová
				Console.WriteLine(ex.Message);
196 7a998d66 Eliška Mourycová
				return null;
197
			}
198
			
199
			for (var i = 0; i < lines.Length; i += 1)
200
			{
201
				string line = lines[i];
202 9547fd4a Eliška Mourycová
				//Console.WriteLine(line);
203 7a998d66 Eliška Mourycová
				if (line.Length == 0 || line == null || line.StartsWith("#"))
204
					continue;
205
206
				switch (line)
207
				{
208
					case "!site!":
209
						extractedConfigInfo.DataWebsite = lines[++i].Trim();
210
						break;
211
					case "!naming_convention!":
212
						extractedConfigInfo.DownloadedFilesNaming = lines[++i].Trim();
213
						break;
214
					case "!data_root_dir!":
215 9547fd4a Eliška Mourycová
						//string rootDir = lines[++i];
216
						//rootDir = rootDir.Replace('\\', Path.DirectorySeparatorChar);
217 7a998d66 Eliška Mourycová
						string dirWithConfig = Path.GetDirectoryName(fullPathConfig);
218
						string rootPath = dirWithConfig + Path.DirectorySeparatorChar + lines[++i].Trim();
219 9547fd4a Eliška Mourycová
						rootPath = Path.GetFullPath(rootPath);
220 7a998d66 Eliška Mourycová
						extractedConfigInfo.DataRootDir = rootPath;
221
						break;
222 38a18391 Eliška Mourycová
					case "!weather_site!":
223
						extractedConfigInfo.WeatherSite = lines[++i].Trim();
224
						break;
225 7a998d66 Eliška Mourycová
					case "!port!":
226
						extractedConfigInfo.Port = lines[++i].Trim();
227
						break;
228
					default: break;
229
				}
230
			}
231
232 9cc42e60 Eliška Mourycová
			int parsedPort;
233
234
			bool success = int.TryParse(extractedConfigInfo.Port, out parsedPort);
235
			if (!success)
236
			{
237 127a92b1 Eliška Mourycová
				Console.WriteLine("Configured port " + extractedConfigInfo.Port + " is not an integer! Abort.");
238 9cc42e60 Eliška Mourycová
				return null;
239
			}
240
				
241
242 7a998d66 Eliška Mourycová
			return extractedConfigInfo;
243
		}
244 3811845f Alex Konig
    }
245
}