Projekt

Obecné

Profil

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