Projekt

Obecné

Profil

Stáhnout (7.17 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
	class Config // TBD where this should go
20
	{
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 92897198 Eliška Mourycová
		public string Port { get; set; } // TODO port cannot be configurable?
26 7a998d66 Eliška Mourycová
	}
27
28 4a417b8b Eliška Mourycová
    public class Program
29 3811845f Alex Konig
    {
30 7a998d66 Eliška Mourycová
31 40f56e57 Eliška Mourycová
		//public void NotifyUserMessage(string message)
32
		//{
33
		//	Console.WriteLine("Received user message: " + message);
34
		//}
35 7a998d66 Eliška Mourycová
36 3811845f Alex Konig
        static void Main(string[] args)
37
        {
38 0d31f7e0 Roman Kalivoda
            XmlConfigurator.Configure();
39
            // SETUP FOLDERS
40
            Config config = FillConfigInfo(args);
41 3aba3c34 Eliška Mourycová
			if (config == null)
42
			{
43
				Console.ReadLine();
44
				return;
45
			}
46 7a998d66 Eliška Mourycová
47 6d0d1410 Eliška Mourycová
			// data download test
48 14bef778 Eliška Mourycová
			DataDownloader dd = DataDownloadAndRetrievalTest(config);
49 6d0d1410 Eliška Mourycová
50 38a18391 Eliška Mourycová
			WeatherAsStringTest(dd);
51
			Console.ReadLine();
52
53 6d0d1410 Eliška Mourycová
			// xml building test
54 92897198 Eliška Mourycová
			//XMLTest();
55 6d0d1410 Eliška Mourycová
56
			// PARSE DATA
57 b78c3571 Eliška Mourycová
			//DataParser parser = new DataParser(dd);
58
			//parser.Parse(new DateTime())
59 6d0d1410 Eliška Mourycová
60
			// json parser test
61
			//JSONParserTest();
62
63
			// model test
64 14bef778 Eliška Mourycová
			IPredictionController controller = PredictionTest(dd);
65 6d0d1410 Eliška Mourycová
66
			//parse(new DateTime(2019, 10, 5), , interval = 1, wholeDay = true)
67
68 c4383c00 Eliška Mourycová
			// commands accepting test
69
			// create a thread for commands accepting:
70
			CommandsAcceptor ca = new CommandsAcceptor(dd, controller);
71
			Thread inputThread = new Thread(ca.AcceptCommand);
72
			inputThread.Start();
73
74 6d0d1410 Eliška Mourycová
			// connection test
75 14bef778 Eliška Mourycová
			ConnectionTest(controller, config);
76 c4383c00 Eliška Mourycová
77 6d0d1410 Eliška Mourycová
			Console.ReadLine();
78
        }
79
80
81
		private static DataDownloader DataDownloadAndRetrievalTest(Config config)
82
		{
83 d358b79e Roman Kalivoda
			//test scenario -data download:
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
			// -> 12-2019 to exclude corona? But we lose a lot of data
89
			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 6d0d1410 Eliška Mourycová
			Console.WriteLine("Saved files: ");
96
			foreach (string s in savedFiles)
97
			{
98
				Console.WriteLine(s);
99
			}
100
101
			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
107
			List<string> retrievedData = dd.GetData(dd.DataSubDirectories[DataType.JIS], new DataDownload.Date(10, 2019), new DataDownload.Date(12, 2020));
108 3aba3c34 Eliška Mourycová
			Console.WriteLine("Retrieved data: ");
109
			foreach (string s in retrievedData)
110
			{
111
				Console.WriteLine(s);
112
			}
113 6d0d1410 Eliška Mourycová
			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 XMLTest()
126
		{
127
			Response response = Response.Randomize();
128
			var xml = XmlCommunication.Serialize(response);
129 86c29fc1 Eliška Mourycová
			//xml = "haha";
130
			Response response1 = new Response();
131 6d0d1410 Eliška Mourycová
			Console.WriteLine(xml);
132 86c29fc1 Eliška Mourycová
			try
133
			{
134 5cb4249a Eliška Mourycová
				Response responseDeserialized = XmlCommunication.Deserialize<Response>(xml);
135 86c29fc1 Eliška Mourycová
			}
136
			catch(Exception e)
137
			{
138
				Console.WriteLine("bad format");
139
			}
140
			
141 d358b79e Roman Kalivoda
142 6d0d1410 Eliška Mourycová
			Console.WriteLine("------");
143 cdf3c217 A-Konig
144
145 6d0d1410 Eliška Mourycová
			Request request = Request.Randomize();
146
			xml = XmlCommunication.Serialize(request);
147
			Console.WriteLine(xml);
148 5cb4249a Eliška Mourycová
			Request requestDeserialized = XmlCommunication.Deserialize<Request>(xml);
149 6d0d1410 Eliška Mourycová
		}
150
151 d358b79e Roman Kalivoda
152 6d0d1410 Eliška Mourycová
		private static void JSONParserTest()
153
		{
154 847434c6 Roman Kalivoda
            // FIXME pass the right references to the JsonParser constructor
155
			JsonParser jsonP = new JsonParser(null, null);
156 cdf3c217 A-Konig
			jsonP.ParsePrediction();
157
158 fffe7190 A-Konig
			var res = jsonP.GetPredictionForTime(jsonP.Predictions[5].startTime, jsonP.Predictions[20].startTime);
159
			Console.WriteLine("from " + jsonP.Predictions[5].startTime);
160
			Console.WriteLine("end " + jsonP.Predictions[20].startTime);
161
			foreach (WeatherInfo w in res)
162
				Console.WriteLine(w.ToString());
163 6d0d1410 Eliška Mourycová
		}
164 fffe7190 A-Konig
165 38a18391 Eliška Mourycová
		private static void WeatherAsStringTest(DataDownloader dd)
166
		{
167
			Console.WriteLine(dd.DownloadWeatherPrediction());
168
		}
169
170 6d0d1410 Eliška Mourycová
		private static IPredictionController PredictionTest(DataDownloader dd)
171
		{
172 cdf3c217 A-Konig
			// TODO nastavit čas
173 99e5517e A-Konig
			IDataParser p = new DataParser(dd);
174 847434c6 Roman Kalivoda
            // FIXME pass the right references to the JsonParser constructor
175 0d31f7e0 Roman Kalivoda
            IJsonParser jsonP = new JsonParser(dd, new CsvDataLoader());
176 870cd163 Roman Kalivoda
            IPredictionController predictionController = new PredictionController(jsonP, p);
177 ebe96ca4 Roman Kalivoda
            predictionController.Train();
178
            //var results = predictionController.Predict()
179 d358b79e Roman Kalivoda
			
180 cdf3c217 A-Konig
181 6d0d1410 Eliška Mourycová
			return predictionController;
182
		}
183 cdf3c217 A-Konig
184 14bef778 Eliška Mourycová
		private static void ConnectionTest(IPredictionController predictionController, Config config)
185 6d0d1410 Eliška Mourycová
		{
186 da9245bf Eliška Mourycová
			ConnectionListenerAsync cl = new ConnectionListenerAsync(int.Parse(config.Port), predictionController);
187 6d0d1410 Eliška Mourycová
			cl.StartListening();
188 7a998d66 Eliška Mourycová
189 6d0d1410 Eliška Mourycová
			//HttpRequestHandler hrh = new HttpRequestHandler(int.Parse(config.Port));
190
			//hrh.ListenAsynchronously();
191
		}
192 7a998d66 Eliška Mourycová
193
194 6d0d1410 Eliška Mourycová
		private static Config FillConfigInfo(string[] args)
195 7a998d66 Eliška Mourycová
		{
196
197
			Config extractedConfigInfo = new Config();
198
199
			Console.WriteLine(Directory.GetCurrentDirectory());
200 9547fd4a Eliška Mourycová
			Console.WriteLine("Parsing configuration file...");
201 7a998d66 Eliška Mourycová
			
202
			if (args.Length != 1)
203
				Console.WriteLine("Wrong usage of parameters, pass the path to a config file."); // todo better explanation?
204
205
			string fullPathConfig = Path.GetFullPath(args[0]);
206
			string[] lines = null;
207
			try {
208
				lines = File.ReadAllLines(fullPathConfig);
209
			}
210
			catch(Exception ex)
211
			{
212
				Console.WriteLine("Could not open " + fullPathConfig);
213
				return null;
214
			}
215
			
216
			for (var i = 0; i < lines.Length; i += 1)
217
			{
218
				string line = lines[i];
219 9547fd4a Eliška Mourycová
				//Console.WriteLine(line);
220 7a998d66 Eliška Mourycová
				if (line.Length == 0 || line == null || line.StartsWith("#"))
221
					continue;
222
223
				switch (line)
224
				{
225
					case "!site!":
226
						extractedConfigInfo.DataWebsite = lines[++i].Trim();
227
						break;
228
					case "!naming_convention!":
229
						extractedConfigInfo.DownloadedFilesNaming = lines[++i].Trim();
230
						break;
231
					case "!data_root_dir!":
232 9547fd4a Eliška Mourycová
						//string rootDir = lines[++i];
233
						//rootDir = rootDir.Replace('\\', Path.DirectorySeparatorChar);
234 7a998d66 Eliška Mourycová
						string dirWithConfig = Path.GetDirectoryName(fullPathConfig);
235
						string rootPath = dirWithConfig + Path.DirectorySeparatorChar + lines[++i].Trim();
236 9547fd4a Eliška Mourycová
						rootPath = Path.GetFullPath(rootPath);
237 7a998d66 Eliška Mourycová
						extractedConfigInfo.DataRootDir = rootPath;
238
						break;
239 38a18391 Eliška Mourycová
					case "!weather_site!":
240
						extractedConfigInfo.WeatherSite = lines[++i].Trim();
241
						break;
242 7a998d66 Eliška Mourycová
					case "!port!":
243
						extractedConfigInfo.Port = lines[++i].Trim();
244
						break;
245
					default: break;
246
				}
247
			}
248
249
			return extractedConfigInfo;
250
		}
251 3811845f Alex Konig
    }
252
}