Projekt

Obecné

Profil

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

    
16
namespace ServerApp
17
{
18

    
19
	class Config
20
	{
21
		public string DataWebsite { get; set; }
22
		public string DownloadedFilesNaming { get; set; }
23
		public string DataRootDir { get; set; }
24
		public string WeatherSite { get; set; }
25
		public string Port { get; set; } 
26
	}
27

    
28
    public class Program
29
    {
30

    
31
        static void Main(string[] args)
32
        {
33
            // setup logging service
34
            XmlConfigurator.Configure();
35

    
36
			// SETUP FOLDERS
37
			Config config = FillConfigInfo(args);
38
			if (config == null)
39
			{
40
				Console.WriteLine("Configuration file parsing failed. Abort.");
41
				Console.ReadLine();
42
				return;
43
			}
44

    
45
			// data download test
46
			DataDownloader dd = DataDownloadAndRetrievalTest(config);
47

    
48
			//WeatherAsStringTest(dd);
49
			//Console.ReadLine();
50

    
51
			// xml building test
52
			//XMLTest();
53

    
54
			// PARSE DATA
55
			//DataParser parser = new DataParser(dd);
56
			//parser.Parse(new DateTime())
57

    
58
			// json parser test
59
			//JSONParserTest();
60

    
61
			// model test
62
			IPredictionController controller = PredictionTest(dd);
63

    
64
			//parse(new DateTime(2019, 10, 5), , interval = 1, wholeDay = true)
65

    
66
			// commands accepting test
67
			// create a thread for commands accepting:
68
			CommandsAcceptor ca = new CommandsAcceptor(dd, controller);
69
			Thread inputThread = new Thread(ca.AcceptCommand);
70
			inputThread.Start();
71

    
72

    
73
			// connection test
74
			ConnectionTest(controller, config);
75

    
76
			//Console.ReadLine();
77
        }
78

    
79

    
80
		private static DataDownloader DataDownloadAndRetrievalTest(Config config)
81
		{
82
			//test scenario -data download:
83
			DataDownloader dd = new DataDownloader(config.DataRootDir, config.DataWebsite, config.DownloadedFilesNaming, config.WeatherSite);
84
			dd.OverwriteExisting = false;
85
			List<string> savedFiles = new List<string>();
86

    
87
			// -> 12-2019 to exclude corona? But we lose a lot of data
88
			savedFiles.AddRange(dd.DownloadData(DataType.JIS, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
89
			savedFiles.AddRange(dd.DownloadData(DataType.STROJE, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
90
			savedFiles.AddRange(dd.DownloadData(DataType.POCASI, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
91

    
92

    
93

    
94
			Console.WriteLine("Saved files: ");
95
			foreach (string s in savedFiles)
96
			{
97
				Console.WriteLine(s);
98
			}
99

    
100
			Console.WriteLine("subdirectories: ");
101
			foreach (KeyValuePair<DataType, string> kvp in dd.DataSubDirectories)
102
			{
103
				Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
104
			}
105

    
106
			List<string> retrievedData = dd.GetData(dd.DataSubDirectories[DataType.JIS], new DataDownload.Date(10, 2019), new DataDownload.Date(12, 2020));
107
			Console.WriteLine("Retrieved data: ");
108
			foreach (string s in retrievedData)
109
			{
110
				Console.WriteLine(s);
111
			}
112
			Console.WriteLine("all from directory:");
113
			retrievedData = dd.GetData(dd.DataSubDirectories[DataType.JIS], null, null);
114
			foreach (string s in retrievedData)
115
			{
116
				Console.WriteLine(s);
117
			}
118

    
119

    
120
			return dd;
121
		}
122

    
123

    
124
		private static void XMLTest()
125
		{
126
			Response response = Response.Randomize();
127
			var xml = XmlCommunication.Serialize(response);
128
			//xml = "haha";
129
			Response response1 = new Response();
130
			Console.WriteLine(xml);
131
			try
132
			{
133
				Response responseDeserialized = XmlCommunication.Deserialize<Response>(xml);
134
			}
135
			catch(Exception e)
136
			{
137
				Console.WriteLine("bad format");
138
			}
139
			
140

    
141
			Console.WriteLine("------");
142

    
143

    
144
			Request request = Request.Randomize();
145
			xml = XmlCommunication.Serialize(request);
146
			Console.WriteLine(xml);
147
			Request requestDeserialized = XmlCommunication.Deserialize<Request>(xml);
148
		}
149

    
150

    
151
		private static void JSONParserTest()
152
		{
153
            // FIXME pass the right references to the JsonParser constructor
154
			JsonParser jsonP = new JsonParser(null, null);
155
			jsonP.ParsePrediction();
156

    
157
			var res = jsonP.GetPredictionForTime(jsonP.Predictions[5].startTime, jsonP.Predictions[20].startTime);
158
			Console.WriteLine("from " + jsonP.Predictions[5].startTime);
159
			Console.WriteLine("end " + jsonP.Predictions[20].startTime);
160
			foreach (WeatherInfo w in res)
161
				Console.WriteLine(w.ToString());
162
		}
163

    
164
		private static void WeatherAsStringTest(DataDownloader dd)
165
		{
166
			Console.WriteLine(dd.DownloadWeatherPrediction());
167
		}
168

    
169
		private static IPredictionController PredictionTest(DataDownloader dd)
170
		{
171
			// TODO nastavit čas
172
			IDataParser p = new DataParser(dd);
173
            // FIXME pass the right references to the JsonParser constructor
174
            IJsonParser jsonP = new JsonParser(dd, new CsvDataLoader());
175
            IPredictionController predictionController = new PredictionController(jsonP, p);
176
            predictionController.Train();
177
            //var results = predictionController.Predict()
178
			
179

    
180
			return predictionController;
181
		}
182

    
183
		private static void ConnectionTest(IPredictionController predictionController, Config config)
184
		{
185
			ConnectionListenerAsync cl = new ConnectionListenerAsync(int.Parse(config.Port), predictionController);
186
			cl.StartListening();
187

    
188
			//HttpRequestHandler hrh = new HttpRequestHandler(int.Parse(config.Port));
189
			//hrh.ListenAsynchronously();
190
		}
191

    
192

    
193
		private static Config FillConfigInfo(string[] args)
194
		{
195

    
196
			Config extractedConfigInfo = new Config();
197

    
198
			Console.WriteLine(Directory.GetCurrentDirectory());
199
			Console.WriteLine("Parsing configuration file...");
200
			
201
			if (args.Length != 1)
202
				Console.WriteLine("Wrong usage of parameters, pass only the path to a config file.");
203

    
204
			string fullPathConfig = Path.GetFullPath(args[0]);
205
			string[] lines = null;
206
			try {
207
				lines = File.ReadAllLines(fullPathConfig);
208
			}
209
			catch(Exception ex)
210
			{
211
				Console.WriteLine("Could not open " + fullPathConfig);
212
				return null;
213
			}
214
			
215
			for (var i = 0; i < lines.Length; i += 1)
216
			{
217
				string line = lines[i];
218
				//Console.WriteLine(line);
219
				if (line.Length == 0 || line == null || line.StartsWith("#"))
220
					continue;
221

    
222
				switch (line)
223
				{
224
					case "!site!":
225
						extractedConfigInfo.DataWebsite = lines[++i].Trim();
226
						break;
227
					case "!naming_convention!":
228
						extractedConfigInfo.DownloadedFilesNaming = lines[++i].Trim();
229
						break;
230
					case "!data_root_dir!":
231
						//string rootDir = lines[++i];
232
						//rootDir = rootDir.Replace('\\', Path.DirectorySeparatorChar);
233
						string dirWithConfig = Path.GetDirectoryName(fullPathConfig);
234
						string rootPath = dirWithConfig + Path.DirectorySeparatorChar + lines[++i].Trim();
235
						rootPath = Path.GetFullPath(rootPath);
236
						extractedConfigInfo.DataRootDir = rootPath;
237
						break;
238
					case "!weather_site!":
239
						extractedConfigInfo.WeatherSite = lines[++i].Trim();
240
						break;
241
					case "!port!":
242
						extractedConfigInfo.Port = lines[++i].Trim();
243
						break;
244
					default: break;
245
				}
246
			}
247

    
248
			int parsedPort;
249

    
250
			bool success = int.TryParse(extractedConfigInfo.Port, out parsedPort);
251
			if (!success)
252
			{
253
				Console.WriteLine("Configured port " + extractedConfigInfo.Port + " is not an integer number! Abort.");
254
				return null;
255
			}
256
				
257

    
258
			return extractedConfigInfo;
259
		}
260
    }
261
}
(2-2/4)