Projekt

Obecné

Profil

Stáhnout (7.8 KB) Statistiky
| Větev: | Tag: | Revize:
1
using ServerApp.Connection;
2
using ServerApp.Connection.XMLProtocolHandler;
3
using ServerApp.DataDownload;
4
using ServerApp.Parser.OutputInfo;
5
using ServerApp.Parser.Parsers;
6
using ServerApp.Predictor;
7
using ServerApp.User;
8
using ServerApp.WeatherPredictionParser;
9
using System;
10
using System.Collections.Generic;
11
using System.IO;
12
using System.Threading;
13

    
14
namespace ServerApp
15
{
16

    
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; } // TODO port cannot be configurable?
23
	}
24

    
25
    public class Program
26
    {
27

    
28
		//public void NotifyUserMessage(string message)
29
		//{
30
		//	Console.WriteLine("Received user message: " + message);
31
		//}
32

    
33
        static void Main(string[] args)
34
        {
35

    
36
			// SETUP FOLDERS
37

    
38
			Config config = FillConfigInfo(args);
39
			if (config == null)
40
			{
41
				Console.ReadLine();
42
				return;
43
			}
44

    
45
			//ConsoleForamtingTest();
46

    
47
			
48

    
49

    
50
			// is this obsolete?
51
			//DataParser p = new DataParser("data/");
52
			//p.Parse();
53

    
54

    
55

    
56
			// data download test
57
			DataDownloader dd = DataDownloadAndRetrievalTest(config);
58

    
59
			// xml building test
60
			//XMLTest();
61

    
62

    
63

    
64
			// PARSE DATA
65

    
66

    
67

    
68
			
69
			// json parser test
70
			//JSONParserTest();
71

    
72
			// model test
73
			IPredictionController controller = PredictionTest(dd);
74

    
75

    
76

    
77
			//parse(new DateTime(2019, 10, 5), , interval = 1, wholeDay = true)
78

    
79

    
80
			// commands accepting test
81
			// create a thread for commands accepting:
82
			CommandsAcceptor ca = new CommandsAcceptor(dd, controller);
83
			Thread inputThread = new Thread(ca.AcceptCommand);
84
			inputThread.Start();
85

    
86
			// connection test
87
			ConnectionTest(controller, config);
88

    
89

    
90
			
91

    
92
			Console.ReadLine();
93
        }
94

    
95
		private static void ConsoleForamtingTest()
96
		{
97

    
98
			Console.WriteLine("List of available commands and their description: ");
99
			Console.WriteLine("--------------------------------------------------");
100
			// data
101
			Console.WriteLine("data [OPTION] [DATE]...");
102
			Console.WriteLine("   Description: Capable of showing saved data files and downloading new ones.");
103
			Console.WriteLine("   -list: Lists currently saved data files. This list does not have to match the files which any of the predictors is trained on!");
104
			Console.WriteLine("      Example: data -list");
105
			Console.WriteLine("   -dl [startDate] [endDate]: Downloads jis, login and weather files from a specified time span. Both [startDate] and [endDate] are inclusive. Date format must be entered as [month]-[year].");
106
			Console.WriteLine("      Example: data -dl 1-2019 12-2020");
107
			Console.WriteLine("");
108

    
109
			// model
110
			Console.WriteLine("model [OPTION]");
111
			Console.WriteLine("   Description: Capable of retraining the prediction model, going back to the previous version and listing files on which the predictor was trained. ");
112
			Console.WriteLine("   -files: Lists the data files on which the currently used model was trained.");
113
			Console.WriteLine("      Example: model -files");
114
			Console.WriteLine("   -retrain: Retrains the model using the files currently saved in the data directory.");
115
			Console.WriteLine("      Example: model -retrain");
116
			Console.WriteLine("   -rollback: Switches the model back to the previous version.");
117
			Console.WriteLine("      Example: model -rollback");
118
		}
119

    
120
		private static DataDownloader DataDownloadAndRetrievalTest(Config config)
121
		{
122
			//test scenario -data download:
123
			DataDownloader dd = new DataDownloader(config.DataRootDir, config.DataWebsite, config.DownloadedFilesNaming);
124
			dd.OverwriteExisting = false;
125
			List<string> savedFiles = new List<string>();
126
			savedFiles.AddRange(dd.DownloadData(DataType.JIS, DataFormat.CSV, new DataDownload.Date(1, 2019), new DataDownload.Date(12, 2020)));
127
			savedFiles.AddRange(dd.DownloadData(DataType.STROJE, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2020)));
128
			savedFiles.AddRange(dd.DownloadData(DataType.POCASI, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2020)));
129

    
130

    
131

    
132
			Console.WriteLine("Saved files: ");
133
			foreach (string s in savedFiles)
134
			{
135
				Console.WriteLine(s);
136
			}
137

    
138
			Console.WriteLine("subdirectories: ");
139
			foreach (KeyValuePair<DataType, string> kvp in dd.DataSubDirectories)
140
			{
141
				Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
142
			}
143

    
144
			List<string> retrievedData = dd.GetData(dd.DataSubDirectories[DataType.JIS], new DataDownload.Date(10, 2019), new DataDownload.Date(12, 2020));
145
			Console.WriteLine("Retrieved data: ");
146
			foreach (string s in retrievedData)
147
			{
148
				Console.WriteLine(s);
149
			}
150
			Console.WriteLine("all from directory:");
151
			retrievedData = dd.GetData(dd.DataSubDirectories[DataType.JIS], null, null);
152
			foreach (string s in retrievedData)
153
			{
154
				Console.WriteLine(s);
155
			}
156

    
157

    
158
			return dd;
159
		}
160

    
161

    
162
		private static void XMLTest()
163
		{
164
			Response response = Response.Randomize();
165
			var xml = XmlCommunication.Serialize(response);
166
			//xml = "haha";
167
			Response response1 = new Response();
168
			Console.WriteLine(xml);
169
			try
170
			{
171
				Response responseDeserialized = XmlCommunication.Deserialize(response1, xml);
172
			}
173
			catch(Exception e)
174
			{
175
				Console.WriteLine("bad format");
176
			}
177
			
178

    
179
			Console.WriteLine("------");
180

    
181

    
182
			Request request = Request.Randomize();
183
			xml = XmlCommunication.Serialize(request);
184
			Console.WriteLine(xml);
185
			Request requestDeserialized = XmlCommunication.Deserialize(request, xml);
186
		}
187

    
188

    
189
		private static void JSONParserTest()
190
		{
191
			JsonParser jsonP = new JsonParser(null);
192
			jsonP.ParsePrediction();
193

    
194
			var res = jsonP.GetPredictionForTime(jsonP.Predictions[5].startTime, jsonP.Predictions[20].startTime);
195
			Console.WriteLine("from " + jsonP.Predictions[5].startTime);
196
			Console.WriteLine("end " + jsonP.Predictions[20].startTime);
197
			foreach (WeatherInfo w in res)
198
				Console.WriteLine(w.ToString());
199
		}
200

    
201
		private static IPredictionController PredictionTest(DataDownloader dd)
202
		{
203
			// TODO nastavit čas
204
			IDataParser p = new DataParser(dd);
205
			IPredictionController predictionController = new PredictionController(p);
206
			predictionController.Train();
207
			//var results = predictionController.Predict()
208

    
209
			return predictionController;
210
		}
211

    
212
		private static void ConnectionTest(IPredictionController predictionController, Config config)
213
		{
214
			ConnectionListener cl = new ConnectionListener(int.Parse(config.Port), predictionController);
215
			cl.StartListening();
216

    
217
			//HttpRequestHandler hrh = new HttpRequestHandler(int.Parse(config.Port));
218
			//hrh.ListenAsynchronously();
219
		}
220

    
221

    
222
		private static Config FillConfigInfo(string[] args)
223
		{
224

    
225
			Config extractedConfigInfo = new Config();
226

    
227
			Console.WriteLine(Directory.GetCurrentDirectory());
228
			
229
			if (args.Length != 1)
230
				Console.WriteLine("Wrong usage of parameters, pass the path to a config file."); // todo better explanation?
231

    
232
			string fullPathConfig = Path.GetFullPath(args[0]);
233
			string[] lines = null;
234
			try {
235
				lines = File.ReadAllLines(fullPathConfig);
236
			}
237
			catch(Exception ex)
238
			{
239
				Console.WriteLine("Could not open " + fullPathConfig);
240
				return null;
241
			}
242
			
243
			for (var i = 0; i < lines.Length; i += 1)
244
			{
245
				string line = lines[i];
246
				Console.WriteLine(line);
247
				if (line.Length == 0 || line == null || line.StartsWith("#"))
248
					continue;
249

    
250
				switch (line)
251
				{
252
					case "!site!":
253
						extractedConfigInfo.DataWebsite = lines[++i].Trim();
254
						break;
255
					case "!naming_convention!":
256
						extractedConfigInfo.DownloadedFilesNaming = lines[++i].Trim();
257
						break;
258
					case "!data_root_dir!":
259
						string dirWithConfig = Path.GetDirectoryName(fullPathConfig);
260
						string rootPath = dirWithConfig + Path.DirectorySeparatorChar + lines[++i].Trim();
261
						extractedConfigInfo.DataRootDir = rootPath;
262
						
263
						break;
264
					case "!port!":
265
						extractedConfigInfo.Port = lines[++i].Trim();
266
						break;
267
					default: break;
268
				}
269
			}
270

    
271
			return extractedConfigInfo;
272
		}
273
    }
274
}
(2-2/5)