Projekt

Obecné

Profil

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