Projekt

Obecné

Profil

Stáhnout (7.8 KB) Statistiky
| Větev: | Tag: | Revize:
1 7a998d66 Eliška Mourycová
using ServerApp.Connection;
2 6b4c34a2 Eliška Mourycová
using ServerApp.Connection.XMLProtocolHandler;
3 7a998d66 Eliška Mourycová
using ServerApp.DataDownload;
4 fffe7190 A-Konig
using ServerApp.Parser.OutputInfo;
5 57a75f60 Roman Kalivoda
using ServerApp.Parser.Parsers;
6 9fc5fa93 Roman Kalivoda
using ServerApp.Predictor;
7 4a417b8b Eliška Mourycová
using ServerApp.User;
8 cdf3c217 A-Konig
using ServerApp.WeatherPredictionParser;
9 3811845f Alex Konig
using System;
10
using System.Collections.Generic;
11 7a998d66 Eliška Mourycová
using System.IO;
12 4a417b8b Eliška Mourycová
using System.Threading;
13 3811845f Alex Konig
14
namespace ServerApp
15
{
16 7a998d66 Eliška Mourycová
17
	class Config // TBD where this should go
18
	{
19
		public string DataWebsite { get; set; }
20
		public string DownloadedFilesNaming { get; set; }
21
		public string DataRootDir { get; set; }
22 92897198 Eliška Mourycová
		public string Port { get; set; } // TODO port cannot be configurable?
23 7a998d66 Eliška Mourycová
	}
24
25 4a417b8b Eliška Mourycová
    public class Program
26 3811845f Alex Konig
    {
27 7a998d66 Eliška Mourycová
28 40f56e57 Eliška Mourycová
		//public void NotifyUserMessage(string message)
29
		//{
30
		//	Console.WriteLine("Received user message: " + message);
31
		//}
32 7a998d66 Eliška Mourycová
33 3811845f Alex Konig
        static void Main(string[] args)
34
        {
35
36 cdf3c217 A-Konig
			// SETUP FOLDERS
37 c9eed50c A-Konig
38 3aba3c34 Eliška Mourycová
			Config config = FillConfigInfo(args);
39
			if (config == null)
40
			{
41
				Console.ReadLine();
42
				return;
43
			}
44 7a998d66 Eliška Mourycová
45 40f56e57 Eliška Mourycová
			//ConsoleForamtingTest();
46
47 c4383c00 Eliška Mourycová
			
48 7a998d66 Eliška Mourycová
49
50 6d0d1410 Eliška Mourycová
			// is this obsolete?
51 7a998d66 Eliška Mourycová
			//DataParser p = new DataParser("data/");
52
			//p.Parse();
53
54 6d0d1410 Eliška Mourycová
55
56
			// data download test
57 14bef778 Eliška Mourycová
			DataDownloader dd = DataDownloadAndRetrievalTest(config);
58 6d0d1410 Eliška Mourycová
59
			// xml building test
60 92897198 Eliška Mourycová
			//XMLTest();
61 6d0d1410 Eliška Mourycová
62
63
64
			// PARSE DATA
65
66
67
68
			
69
			// json parser test
70
			//JSONParserTest();
71
72
			// model test
73 14bef778 Eliška Mourycová
			IPredictionController controller = PredictionTest(dd);
74 6d0d1410 Eliška Mourycová
75
76
77
			//parse(new DateTime(2019, 10, 5), , interval = 1, wholeDay = true)
78
79
80 c4383c00 Eliška Mourycová
			// commands accepting test
81
			// create a thread for commands accepting:
82
			CommandsAcceptor ca = new CommandsAcceptor(dd, controller);
83
			Thread inputThread = new Thread(ca.AcceptCommand);
84
			inputThread.Start();
85
86 6d0d1410 Eliška Mourycová
			// connection test
87 14bef778 Eliška Mourycová
			ConnectionTest(controller, config);
88 c4383c00 Eliška Mourycová
89
90 6d0d1410 Eliška Mourycová
			
91
92
			Console.ReadLine();
93
        }
94
95 40f56e57 Eliška Mourycová
		private static void ConsoleForamtingTest()
96
		{
97
98
			Console.WriteLine("List of available commands and their description: ");
99
			Console.WriteLine("--------------------------------------------------");
100
			// data
101
			Console.WriteLine("data [OPTION] [DATE]...");
102
			Console.WriteLine("   Description: Capable of showing saved data files and downloading new ones.");
103
			Console.WriteLine("   -list: Lists currently saved data files. This list does not have to match the files which any of the predictors is trained on!");
104
			Console.WriteLine("      Example: data -list");
105
			Console.WriteLine("   -dl [startDate] [endDate]: Downloads jis, login and weather files from a specified time span. Both [startDate] and [endDate] are inclusive. Date format must be entered as [month]-[year].");
106
			Console.WriteLine("      Example: data -dl 1-2019 12-2020");
107
			Console.WriteLine("");
108
109
			// model
110
			Console.WriteLine("model [OPTION]");
111
			Console.WriteLine("   Description: Capable of retraining the prediction model, going back to the previous version and listing files on which the predictor was trained. ");
112
			Console.WriteLine("   -files: Lists the data files on which the currently used model was trained.");
113
			Console.WriteLine("      Example: model -files");
114
			Console.WriteLine("   -retrain: Retrains the model using the files currently saved in the data directory.");
115
			Console.WriteLine("      Example: model -retrain");
116
			Console.WriteLine("   -rollback: Switches the model back to the previous version.");
117
			Console.WriteLine("      Example: model -rollback");
118
		}
119 6d0d1410 Eliška Mourycová
120
		private static DataDownloader DataDownloadAndRetrievalTest(Config config)
121
		{
122 d358b79e Roman Kalivoda
			//test scenario -data download:
123
			DataDownloader dd = new DataDownloader(config.DataRootDir, config.DataWebsite, config.DownloadedFilesNaming);
124
			dd.OverwriteExisting = false;
125
			List<string> savedFiles = new List<string>();
126
			savedFiles.AddRange(dd.DownloadData(DataType.JIS, DataFormat.CSV, new DataDownload.Date(1, 2019), new DataDownload.Date(12, 2020)));
127
			savedFiles.AddRange(dd.DownloadData(DataType.STROJE, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2020)));
128
			savedFiles.AddRange(dd.DownloadData(DataType.POCASI, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2020)));
129 4129ce12 Eliška Mourycová
130
131
132 6d0d1410 Eliška Mourycová
			Console.WriteLine("Saved files: ");
133
			foreach (string s in savedFiles)
134
			{
135
				Console.WriteLine(s);
136
			}
137
138
			Console.WriteLine("subdirectories: ");
139
			foreach (KeyValuePair<DataType, string> kvp in dd.DataSubDirectories)
140
			{
141
				Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
142
			}
143
144
			List<string> retrievedData = dd.GetData(dd.DataSubDirectories[DataType.JIS], new DataDownload.Date(10, 2019), new DataDownload.Date(12, 2020));
145 3aba3c34 Eliška Mourycová
			Console.WriteLine("Retrieved data: ");
146
			foreach (string s in retrievedData)
147
			{
148
				Console.WriteLine(s);
149
			}
150 6d0d1410 Eliška Mourycová
			Console.WriteLine("all from directory:");
151
			retrievedData = dd.GetData(dd.DataSubDirectories[DataType.JIS], null, null);
152
			foreach (string s in retrievedData)
153
			{
154
				Console.WriteLine(s);
155
			}
156
157
158
			return dd;
159
		}
160
161
162
		private static void XMLTest()
163
		{
164
			Response response = Response.Randomize();
165
			var xml = XmlCommunication.Serialize(response);
166 86c29fc1 Eliška Mourycová
			//xml = "haha";
167
			Response response1 = new Response();
168 6d0d1410 Eliška Mourycová
			Console.WriteLine(xml);
169 86c29fc1 Eliška Mourycová
			try
170
			{
171
				Response responseDeserialized = XmlCommunication.Deserialize(response1, xml);
172
			}
173
			catch(Exception e)
174
			{
175
				Console.WriteLine("bad format");
176
			}
177
			
178 36c0667f Eliška Mourycová
179 6d0d1410 Eliška Mourycová
			Console.WriteLine("------");
180 cdf3c217 A-Konig
181
182 6d0d1410 Eliška Mourycová
			Request request = Request.Randomize();
183
			xml = XmlCommunication.Serialize(request);
184
			Console.WriteLine(xml);
185
			Request requestDeserialized = XmlCommunication.Deserialize(request, xml);
186
		}
187
188 d358b79e Roman Kalivoda
189 6d0d1410 Eliška Mourycová
		private static void JSONParserTest()
190
		{
191
			JsonParser jsonP = new JsonParser(null);
192 cdf3c217 A-Konig
			jsonP.ParsePrediction();
193
194 fffe7190 A-Konig
			var res = jsonP.GetPredictionForTime(jsonP.Predictions[5].startTime, jsonP.Predictions[20].startTime);
195
			Console.WriteLine("from " + jsonP.Predictions[5].startTime);
196
			Console.WriteLine("end " + jsonP.Predictions[20].startTime);
197
			foreach (WeatherInfo w in res)
198
				Console.WriteLine(w.ToString());
199 6d0d1410 Eliška Mourycová
		}
200 fffe7190 A-Konig
201 6d0d1410 Eliška Mourycová
		private static IPredictionController PredictionTest(DataDownloader dd)
202
		{
203 cdf3c217 A-Konig
			// TODO nastavit čas
204 99e5517e A-Konig
			IDataParser p = new DataParser(dd);
205 6d0d1410 Eliška Mourycová
			IPredictionController predictionController = new PredictionController(p);
206
			predictionController.Train();
207
			//var results = predictionController.Predict()
208 4cb8ae48 A-Konig
209 6d0d1410 Eliška Mourycová
			return predictionController;
210
		}
211 cdf3c217 A-Konig
212 14bef778 Eliška Mourycová
		private static void ConnectionTest(IPredictionController predictionController, Config config)
213 6d0d1410 Eliška Mourycová
		{
214 14bef778 Eliška Mourycová
			ConnectionListener cl = new ConnectionListener(int.Parse(config.Port), predictionController);
215 6d0d1410 Eliška Mourycová
			cl.StartListening();
216 7a998d66 Eliška Mourycová
217 6d0d1410 Eliška Mourycová
			//HttpRequestHandler hrh = new HttpRequestHandler(int.Parse(config.Port));
218
			//hrh.ListenAsynchronously();
219
		}
220 7a998d66 Eliška Mourycová
221
222 6d0d1410 Eliška Mourycová
		private static Config FillConfigInfo(string[] args)
223 7a998d66 Eliška Mourycová
		{
224
225
			Config extractedConfigInfo = new Config();
226
227
			Console.WriteLine(Directory.GetCurrentDirectory());
228
			
229
			if (args.Length != 1)
230
				Console.WriteLine("Wrong usage of parameters, pass the path to a config file."); // todo better explanation?
231
232
			string fullPathConfig = Path.GetFullPath(args[0]);
233
			string[] lines = null;
234
			try {
235
				lines = File.ReadAllLines(fullPathConfig);
236
			}
237
			catch(Exception ex)
238
			{
239
				Console.WriteLine("Could not open " + fullPathConfig);
240
				return null;
241
			}
242
			
243
			for (var i = 0; i < lines.Length; i += 1)
244
			{
245
				string line = lines[i];
246
				Console.WriteLine(line);
247
				if (line.Length == 0 || line == null || line.StartsWith("#"))
248
					continue;
249
250
				switch (line)
251
				{
252
					case "!site!":
253
						extractedConfigInfo.DataWebsite = lines[++i].Trim();
254
						break;
255
					case "!naming_convention!":
256
						extractedConfigInfo.DownloadedFilesNaming = lines[++i].Trim();
257
						break;
258
					case "!data_root_dir!":
259
						string dirWithConfig = Path.GetDirectoryName(fullPathConfig);
260
						string rootPath = dirWithConfig + Path.DirectorySeparatorChar + lines[++i].Trim();
261
						extractedConfigInfo.DataRootDir = rootPath;
262
						
263
						break;
264
					case "!port!":
265
						extractedConfigInfo.Port = lines[++i].Trim();
266
						break;
267
					default: break;
268
				}
269
			}
270
271
			return extractedConfigInfo;
272
		}
273 3811845f Alex Konig
    }
274
}