Projekt

Obecné

Profil

Stáhnout (7.27 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
            //var results = predictionController.Predict()
177
			
178

    
179
			return predictionController;
180
		}
181

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

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

    
191

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

    
195
			Config extractedConfigInfo = new Config();
196

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

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

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

    
247
			int parsedPort;
248

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

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