Projekt

Obecné

Profil

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

    
17
namespace ServerApp
18
{
19

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

    
29
    public class Program
30
    {
31
		// logger class instance
32
		private static readonly ILog logger = LogManager.GetLogger(typeof(Program));
33

    
34
		static void Main(string[] args)
35
        {
36
			Console.WriteLine("Server start.");
37

    
38
            // setup logging service
39
            XmlConfigurator.Configure();
40

    
41
			// SETUP FOLDERS
42
			Config config = FillConfigInfo(args);
43
			if (config == null)
44
			{
45
				Console.WriteLine("Configuration file parsing failed. Abort.");
46
				Console.ReadLine();
47
				return;
48
			}
49
			Console.WriteLine("Config parsing successful.");
50

    
51

    
52
			// data download init
53
			Console.WriteLine("Downloading open data...");
54
			DataDownloader dd = DataDownloaderInit(config);
55
			Console.WriteLine("Data downloaded and saved.");
56

    
57

    
58
			// model init
59
			Console.WriteLine("Training the predictor...");
60
			IPredictionController controller = PredictionControllerInit(dd);
61
			Console.WriteLine("Predictor training finished.");
62

    
63

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

    
70

    
71
			// connection init
72
			ConnectionInit(controller, config);
73

    
74
        }
75

    
76
		/// <summary>
77
		/// Initializes DataDownloader. Downloads default span of data and saves the files.
78
		/// </summary>
79
		/// <param name="config">The Config instance</param>
80
		/// <returns>New instance of DataDownloader</returns>
81
		private static DataDownloader DataDownloaderInit(Config config)
82
		{
83
			
84
			DataDownloader dd = new DataDownloader(config.DataRootDir, config.DataWebsite, config.DownloadedFilesNaming, config.WeatherSite);
85
			dd.OverwriteExisting = false;
86
			List<string> savedFiles = new List<string>();
87

    
88
			// -> 12-2019 to exclude corona period which could mess with the predictor
89
			savedFiles.AddRange(dd.DownloadData(DataType.JIS, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
90
			savedFiles.AddRange(dd.DownloadData(DataType.STROJE, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
91
			savedFiles.AddRange(dd.DownloadData(DataType.POCASI, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
92

    
93

    
94

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

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

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

    
120

    
121
			return dd;
122
		}
123

    
124

    
125
		private static void JSONParserTest()
126
		{
127
            // FIXME pass the right references to the JsonParser constructor
128
			JsonParser jsonP = new JsonParser(null, null);
129
			jsonP.ParsePrediction();
130

    
131
			var res = jsonP.GetPredictionForTime(jsonP.Predictions[5].startTime, jsonP.Predictions[20].startTime);
132
			Console.WriteLine("from " + jsonP.Predictions[5].startTime);
133
			Console.WriteLine("end " + jsonP.Predictions[20].startTime);
134
			foreach (WeatherInfo w in res)
135
				Console.WriteLine(w.ToString());
136
		}
137

    
138
		private static void WeatherAsStringTest(DataDownloader dd)
139
		{
140
			Console.WriteLine(dd.DownloadWeatherPrediction());
141
		}
142

    
143
		/// <summary>
144
		/// Initializes and trains the predictor if need be.
145
		/// </summary>
146
		/// <param name="dd">The DataDownloader instance.</param>
147
		/// <returns>New instance of the predictor.</returns>
148
		private static IPredictionController PredictionControllerInit(DataDownloader dd)
149
		{
150
			IDataParser p = new DataParser(dd);
151
            // FIXME pass the right references to the JsonParser constructor
152
            IJsonParser jsonP = new JsonParser(dd, new CsvDataLoader());
153
            IPredictionController predictionController = new PredictionController(jsonP, p);
154
            //var results = predictionController.Predict()
155
			
156

    
157
			return predictionController;
158
		}
159

    
160
		/// <summary>
161
		/// Initializes waiting for the clients' requests.
162
		/// </summary>
163
		/// <param name="predictionController">The initialized and trained instance of the predictor.</param>
164
		/// <param name="config">The Config class instance.</param>
165
		private static void ConnectionInit(IPredictionController predictionController, Config config)
166
		{
167
			ConnectionListenerAsync cl = new ConnectionListenerAsync(int.Parse(config.Port), predictionController);
168
			cl.StartListening();
169

    
170
		}
171

    
172
		/// <summary>
173
		/// Parses the configuration file and extracts valuable information from it.
174
		/// </summary>
175
		/// <param name="args">The arguments passed to the program.</param>
176
		/// <returns>Filled instance of the Config class, null if there was a problem.</returns>
177
		private static Config FillConfigInfo(string[] args)
178
		{
179

    
180
			Config extractedConfigInfo = new Config();
181

    
182
			Console.WriteLine("Parsing configuration file...");
183
			
184
			if (args.Length != 1)
185
				Console.WriteLine("Wrong usage of parameters, pass only the path to a config file.");
186

    
187
			string fullPathConfig = Path.GetFullPath(args[0]);
188
			string[] lines = null;
189
			try {
190
				lines = File.ReadAllLines(fullPathConfig);
191
			}
192
			catch(Exception ex)
193
			{
194
				Console.WriteLine("Could not open " + fullPathConfig);
195
				Console.WriteLine(ex.Message);
196
				return null;
197
			}
198
			
199
			for (var i = 0; i < lines.Length; i += 1)
200
			{
201
				string line = lines[i];
202
				//Console.WriteLine(line);
203
				if (line.Length == 0 || line == null || line.StartsWith("#"))
204
					continue;
205

    
206
				switch (line)
207
				{
208
					case "!site!":
209
						extractedConfigInfo.DataWebsite = lines[++i].Trim();
210
						break;
211
					case "!naming_convention!":
212
						extractedConfigInfo.DownloadedFilesNaming = lines[++i].Trim();
213
						break;
214
					case "!data_root_dir!":
215
						//string rootDir = lines[++i];
216
						//rootDir = rootDir.Replace('\\', Path.DirectorySeparatorChar);
217
						string dirWithConfig = Path.GetDirectoryName(fullPathConfig);
218
						string rootPath = dirWithConfig + Path.DirectorySeparatorChar + lines[++i].Trim();
219
						rootPath = Path.GetFullPath(rootPath);
220
						extractedConfigInfo.DataRootDir = rootPath;
221
						break;
222
					case "!weather_site!":
223
						extractedConfigInfo.WeatherSite = lines[++i].Trim();
224
						break;
225
					case "!port!":
226
						extractedConfigInfo.Port = lines[++i].Trim();
227
						break;
228
					default: break;
229
				}
230
			}
231

    
232
			int parsedPort;
233

    
234
			bool success = int.TryParse(extractedConfigInfo.Port, out parsedPort);
235
			if (!success)
236
			{
237
				Console.WriteLine("Configured port " + extractedConfigInfo.Port + " is not an integer! Abort.");
238
				return null;
239
			}
240
				
241

    
242
			return extractedConfigInfo;
243
		}
244
    }
245
}
(2-2/4)