Projekt

Obecné

Profil

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