Projekt

Obecné

Profil

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