Projekt

Obecné

Profil

Stáhnout (5.99 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
			//DataDownloader dd = DataDownloadAndRetrievalTest(config);
59
60
			// xml building test
61
			//XMLTest();
62
63
64
65
			// PARSE DATA
66
67
68
69
			
70
			// json parser test
71
			//JSONParserTest();
72
73
			// model test
74
			//IPredictionController controller = PredictionTest(dd);
75
76
77
78
			//parse(new DateTime(2019, 10, 5), , interval = 1, wholeDay = true)
79
80
81
			// connection test
82
			ConnectionTest(/*controller,*/ config);
83
			
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
			Console.WriteLine(xml);
136
			Response responseDeserialized = XmlCommunication.Deserialize(response, xml);
137 36c0667f Eliška Mourycová
138 6d0d1410 Eliška Mourycová
			Console.WriteLine("------");
139 cdf3c217 A-Konig
140
141 6d0d1410 Eliška Mourycová
			Request request = Request.Randomize();
142
			xml = XmlCommunication.Serialize(request);
143
			Console.WriteLine(xml);
144
			Request requestDeserialized = XmlCommunication.Deserialize(request, xml);
145
		}
146
147 d358b79e Roman Kalivoda
148 6d0d1410 Eliška Mourycová
		private static void JSONParserTest()
149
		{
150
			JsonParser jsonP = new JsonParser(null);
151 cdf3c217 A-Konig
			jsonP.ParsePrediction();
152
153 fffe7190 A-Konig
			var res = jsonP.GetPredictionForTime(jsonP.Predictions[5].startTime, jsonP.Predictions[20].startTime);
154
			Console.WriteLine("from " + jsonP.Predictions[5].startTime);
155
			Console.WriteLine("end " + jsonP.Predictions[20].startTime);
156
			foreach (WeatherInfo w in res)
157
				Console.WriteLine(w.ToString());
158 6d0d1410 Eliška Mourycová
		}
159 fffe7190 A-Konig
160 6d0d1410 Eliška Mourycová
		private static IPredictionController PredictionTest(DataDownloader dd)
161
		{
162 cdf3c217 A-Konig
			// TODO nastavit čas
163 99e5517e A-Konig
			IDataParser p = new DataParser(dd);
164 6d0d1410 Eliška Mourycová
			IPredictionController predictionController = new PredictionController(p);
165
			predictionController.Train();
166
			//var results = predictionController.Predict()
167 4cb8ae48 A-Konig
168 6d0d1410 Eliška Mourycová
			return predictionController;
169
		}
170 cdf3c217 A-Konig
171 6d0d1410 Eliška Mourycová
		private static void ConnectionTest(/*IPredictionController predictionController,*/ Config config)
172
		{
173
			ConnectionListener cl = new ConnectionListener(int.Parse(config.Port));
174
			cl.StartListening();
175 7a998d66 Eliška Mourycová
176 6d0d1410 Eliška Mourycová
			//HttpRequestHandler hrh = new HttpRequestHandler(int.Parse(config.Port));
177
			//hrh.ListenAsynchronously();
178
		}
179 7a998d66 Eliška Mourycová
180
181 6d0d1410 Eliška Mourycová
		private static Config FillConfigInfo(string[] args)
182 7a998d66 Eliška Mourycová
		{
183
184
			Config extractedConfigInfo = new Config();
185
186
			Console.WriteLine(Directory.GetCurrentDirectory());
187
			
188
			if (args.Length != 1)
189
				Console.WriteLine("Wrong usage of parameters, pass the path to a config file."); // todo better explanation?
190
191
			string fullPathConfig = Path.GetFullPath(args[0]);
192
			string[] lines = null;
193
			try {
194
				lines = File.ReadAllLines(fullPathConfig);
195
			}
196
			catch(Exception ex)
197
			{
198
				Console.WriteLine("Could not open " + fullPathConfig);
199
				return null;
200
			}
201
			
202
			for (var i = 0; i < lines.Length; i += 1)
203
			{
204
				string line = lines[i];
205
				Console.WriteLine(line);
206
				if (line.Length == 0 || line == null || line.StartsWith("#"))
207
					continue;
208
209
				switch (line)
210
				{
211
					case "!site!":
212
						extractedConfigInfo.DataWebsite = lines[++i].Trim();
213
						break;
214
					case "!naming_convention!":
215
						extractedConfigInfo.DownloadedFilesNaming = lines[++i].Trim();
216
						break;
217
					case "!data_root_dir!":
218
						string dirWithConfig = Path.GetDirectoryName(fullPathConfig);
219
						string rootPath = dirWithConfig + Path.DirectorySeparatorChar + lines[++i].Trim();
220
						extractedConfigInfo.DataRootDir = rootPath;
221
						
222
						break;
223
					case "!port!":
224
						extractedConfigInfo.Port = lines[++i].Trim();
225
						break;
226
					default: break;
227
				}
228
			}
229
230
			return extractedConfigInfo;
231
		}
232 3811845f Alex Konig
    }
233
}