Projekt

Obecné

Profil

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