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
|
/// <summary>
|
20
|
/// This class stores information extracted from the configuration file
|
21
|
/// </summary>
|
22
|
class Config
|
23
|
{
|
24
|
/// <summary>
|
25
|
/// The site to download OpenData from
|
26
|
/// </summary>
|
27
|
public string DataWebsite { get; set; }
|
28
|
|
29
|
/// <summary>
|
30
|
/// The naming convention for the open data files
|
31
|
/// </summary>
|
32
|
public string DownloadedFilesNaming { get; set; }
|
33
|
|
34
|
/// <summary>
|
35
|
/// The directory, where data files should be stored
|
36
|
/// </summary>
|
37
|
public string DataRootDir { get; set; }
|
38
|
|
39
|
/// <summary>
|
40
|
/// The site to download weather prediction from
|
41
|
/// </summary>
|
42
|
public string WeatherSite { get; set; }
|
43
|
|
44
|
/// <summary>
|
45
|
/// The port to listen on for clients' requests
|
46
|
/// </summary>
|
47
|
public string Port { get; set; }
|
48
|
}
|
49
|
|
50
|
public class Program
|
51
|
{
|
52
|
|
53
|
static void Main(string[] args)
|
54
|
{
|
55
|
Console.WriteLine("Server start.");
|
56
|
|
57
|
// setup logging service
|
58
|
XmlConfigurator.Configure();
|
59
|
|
60
|
// SETUP FOLDERS
|
61
|
Config config = FillConfigInfo(args);
|
62
|
if (config == null)
|
63
|
{
|
64
|
Console.WriteLine("Configuration file parsing failed. Abort.");
|
65
|
Console.ReadLine();
|
66
|
return;
|
67
|
}
|
68
|
Console.WriteLine("Config parsing successful.");
|
69
|
|
70
|
|
71
|
// data download init
|
72
|
Console.WriteLine("Downloading open data...");
|
73
|
DataDownloader dd = DataDownloaderInit(config);
|
74
|
Console.WriteLine("Data downloaded and saved.");
|
75
|
|
76
|
|
77
|
// model init
|
78
|
Console.WriteLine("Training the predictor...");
|
79
|
IPredictionController controller = PredictionControllerInit(dd);
|
80
|
Console.WriteLine("Predictor training finished.");
|
81
|
|
82
|
|
83
|
// commands accepting
|
84
|
// create a thread for commands accepting:
|
85
|
CommandsAcceptor ca = new CommandsAcceptor(dd, controller);
|
86
|
Thread inputThread = new Thread(ca.AcceptCommand);
|
87
|
inputThread.Start();
|
88
|
|
89
|
|
90
|
// connection init
|
91
|
ConnectionInit(controller, config);
|
92
|
|
93
|
}
|
94
|
|
95
|
/// <summary>
|
96
|
/// Initializes DataDownloader. Downloads default span of data and saves the files.
|
97
|
/// </summary>
|
98
|
/// <param name="config">The Config instance</param>
|
99
|
/// <returns>New instance of DataDownloader</returns>
|
100
|
private static DataDownloader DataDownloaderInit(Config config)
|
101
|
{
|
102
|
|
103
|
DataDownloader dd = new DataDownloader(config.DataRootDir, config.DataWebsite, config.DownloadedFilesNaming, config.WeatherSite);
|
104
|
dd.OverwriteExisting = false;
|
105
|
List<string> savedFiles = new List<string>();
|
106
|
|
107
|
// -> 12-2019 to exclude corona period which could mess with the predictor
|
108
|
savedFiles.AddRange(dd.DownloadData(DataType.JIS, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
|
109
|
savedFiles.AddRange(dd.DownloadData(DataType.STROJE, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
|
110
|
savedFiles.AddRange(dd.DownloadData(DataType.POCASI, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
|
111
|
|
112
|
|
113
|
|
114
|
//Console.WriteLine("Saved files: ");
|
115
|
//foreach (string s in savedFiles)
|
116
|
//{
|
117
|
// Console.WriteLine(s);
|
118
|
//}
|
119
|
|
120
|
//Console.WriteLine("subdirectories: ");
|
121
|
//foreach (KeyValuePair<DataType, string> kvp in dd.DataSubDirectories)
|
122
|
//{
|
123
|
// Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
|
124
|
//}
|
125
|
|
126
|
//List<string> retrievedData = dd.GetData(dd.DataSubDirectories[DataType.JIS], new DataDownload.Date(10, 2019), new DataDownload.Date(12, 2020));
|
127
|
//Console.WriteLine("Retrieved data: ");
|
128
|
//foreach (string s in retrievedData)
|
129
|
//{
|
130
|
// Console.WriteLine(s);
|
131
|
//}
|
132
|
//Console.WriteLine("all from directory:");
|
133
|
//retrievedData = dd.GetData(dd.DataSubDirectories[DataType.JIS], null, null);
|
134
|
//foreach (string s in retrievedData)
|
135
|
//{
|
136
|
// Console.WriteLine(s);
|
137
|
//}
|
138
|
|
139
|
|
140
|
return dd;
|
141
|
}
|
142
|
|
143
|
|
144
|
private static void JSONParserTest()
|
145
|
{
|
146
|
// FIXME pass the right references to the JsonParser constructor
|
147
|
JsonParser jsonP = new JsonParser(null, null);
|
148
|
jsonP.ParsePrediction();
|
149
|
|
150
|
var res = jsonP.GetPredictionForTime(jsonP.Predictions[5].startTime, jsonP.Predictions[20].startTime);
|
151
|
Console.WriteLine("from " + jsonP.Predictions[5].startTime);
|
152
|
Console.WriteLine("end " + jsonP.Predictions[20].startTime);
|
153
|
foreach (WeatherInfo w in res)
|
154
|
Console.WriteLine(w.ToString());
|
155
|
}
|
156
|
|
157
|
private static void WeatherAsStringTest(DataDownloader dd)
|
158
|
{
|
159
|
Console.WriteLine(dd.DownloadWeatherPrediction());
|
160
|
}
|
161
|
|
162
|
/// <summary>
|
163
|
/// Initializes and trains the predictor if need be.
|
164
|
/// </summary>
|
165
|
/// <param name="dd">The DataDownloader instance.</param>
|
166
|
/// <returns>New instance of the predictor.</returns>
|
167
|
private static IPredictionController PredictionControllerInit(DataDownloader dd)
|
168
|
{
|
169
|
IDataParser p = new DataParser(dd);
|
170
|
// FIXME pass the right references to the JsonParser constructor
|
171
|
IJsonParser jsonP = new JsonParser(dd, new CsvDataLoader());
|
172
|
IPredictionController predictionController = new PredictionController(jsonP, p);
|
173
|
//var results = predictionController.Predict()
|
174
|
|
175
|
|
176
|
return predictionController;
|
177
|
}
|
178
|
|
179
|
/// <summary>
|
180
|
/// Initializes waiting for the clients' requests.
|
181
|
/// </summary>
|
182
|
/// <param name="predictionController">The initialized and trained instance of the predictor.</param>
|
183
|
/// <param name="config">The Config class instance.</param>
|
184
|
private static void ConnectionInit(IPredictionController predictionController, Config config)
|
185
|
{
|
186
|
ConnectionListenerAsync cl = new ConnectionListenerAsync(int.Parse(config.Port), predictionController);
|
187
|
cl.StartListening();
|
188
|
|
189
|
}
|
190
|
|
191
|
/// <summary>
|
192
|
/// Parses the configuration file and extracts valuable information from it.
|
193
|
/// </summary>
|
194
|
/// <param name="args">The arguments passed to the program.</param>
|
195
|
/// <returns>Filled instance of the Config class, null if there was a problem.</returns>
|
196
|
private static Config FillConfigInfo(string[] args)
|
197
|
{
|
198
|
|
199
|
Config extractedConfigInfo = new Config();
|
200
|
|
201
|
Console.WriteLine("Parsing configuration file...");
|
202
|
|
203
|
if (args.Length != 1)
|
204
|
Console.WriteLine("Wrong usage of parameters, pass only the path to a config file.");
|
205
|
|
206
|
string fullPathConfig = Path.GetFullPath(args[0]);
|
207
|
string[] lines = null;
|
208
|
try {
|
209
|
lines = File.ReadAllLines(fullPathConfig);
|
210
|
}
|
211
|
catch(Exception ex)
|
212
|
{
|
213
|
Console.WriteLine("Could not open " + fullPathConfig);
|
214
|
Console.WriteLine(ex.Message);
|
215
|
return null;
|
216
|
}
|
217
|
|
218
|
for (var i = 0; i < lines.Length; i += 1)
|
219
|
{
|
220
|
string line = lines[i];
|
221
|
//Console.WriteLine(line);
|
222
|
if (line.Length == 0 || line == null || line.StartsWith("#"))
|
223
|
continue;
|
224
|
|
225
|
switch (line)
|
226
|
{
|
227
|
case "!site!":
|
228
|
extractedConfigInfo.DataWebsite = lines[++i].Trim();
|
229
|
break;
|
230
|
case "!naming_convention!":
|
231
|
extractedConfigInfo.DownloadedFilesNaming = lines[++i].Trim();
|
232
|
break;
|
233
|
case "!data_root_dir!":
|
234
|
//string rootDir = lines[++i];
|
235
|
//rootDir = rootDir.Replace('\\', Path.DirectorySeparatorChar);
|
236
|
string dirWithConfig = Path.GetDirectoryName(fullPathConfig);
|
237
|
string rootPath = dirWithConfig + Path.DirectorySeparatorChar + lines[++i].Trim();
|
238
|
rootPath = Path.GetFullPath(rootPath);
|
239
|
extractedConfigInfo.DataRootDir = rootPath;
|
240
|
break;
|
241
|
case "!weather_site!":
|
242
|
extractedConfigInfo.WeatherSite = lines[++i].Trim();
|
243
|
break;
|
244
|
case "!port!":
|
245
|
extractedConfigInfo.Port = lines[++i].Trim();
|
246
|
break;
|
247
|
default: break;
|
248
|
}
|
249
|
}
|
250
|
|
251
|
int parsedPort;
|
252
|
|
253
|
bool success = int.TryParse(extractedConfigInfo.Port, out parsedPort);
|
254
|
if (!success)
|
255
|
{
|
256
|
Console.WriteLine("Configured port " + extractedConfigInfo.Port + " is not an integer! Abort.");
|
257
|
return null;
|
258
|
}
|
259
|
|
260
|
|
261
|
return extractedConfigInfo;
|
262
|
}
|
263
|
}
|
264
|
}
|