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
|
//logger.Debug("ahoj");
|
52
|
//logger.Warn("ahoj2");
|
53
|
//Console.ReadLine();
|
54
|
|
55
|
// data download init
|
56
|
Console.WriteLine("Downloading open data...");
|
57
|
DataDownloader dd = DataDownloaderInit(config);
|
58
|
Console.WriteLine("Data downloaded and saved.");
|
59
|
|
60
|
//WeatherAsStringTest(dd);
|
61
|
//Console.ReadLine();
|
62
|
|
63
|
// xml building test
|
64
|
//XMLTest();
|
65
|
|
66
|
|
67
|
// json parser test
|
68
|
//JSONParserTest();
|
69
|
|
70
|
// model init
|
71
|
Console.WriteLine("Training the predictor...");
|
72
|
IPredictionController controller = PredictionControllerInit(dd);
|
73
|
Console.WriteLine("Predictor training finished.");
|
74
|
|
75
|
|
76
|
// commands accepting
|
77
|
// create a thread for commands accepting:
|
78
|
CommandsAcceptor ca = new CommandsAcceptor(dd, controller);
|
79
|
Thread inputThread = new Thread(ca.AcceptCommand);
|
80
|
inputThread.Start();
|
81
|
|
82
|
|
83
|
// connection init
|
84
|
ConnectionInit(controller, config);
|
85
|
|
86
|
}
|
87
|
|
88
|
|
89
|
private static DataDownloader DataDownloaderInit(Config config)
|
90
|
{
|
91
|
//test scenario -data download:
|
92
|
DataDownloader dd = new DataDownloader(config.DataRootDir, config.DataWebsite, config.DownloadedFilesNaming, config.WeatherSite);
|
93
|
dd.OverwriteExisting = false;
|
94
|
List<string> savedFiles = new List<string>();
|
95
|
|
96
|
// -> 12-2019 to exclude corona? But we lose a lot of data
|
97
|
savedFiles.AddRange(dd.DownloadData(DataType.JIS, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
|
98
|
savedFiles.AddRange(dd.DownloadData(DataType.STROJE, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
|
99
|
savedFiles.AddRange(dd.DownloadData(DataType.POCASI, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
|
100
|
|
101
|
|
102
|
|
103
|
//Console.WriteLine("Saved files: ");
|
104
|
//foreach (string s in savedFiles)
|
105
|
//{
|
106
|
// Console.WriteLine(s);
|
107
|
//}
|
108
|
|
109
|
//Console.WriteLine("subdirectories: ");
|
110
|
//foreach (KeyValuePair<DataType, string> kvp in dd.DataSubDirectories)
|
111
|
//{
|
112
|
// Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
|
113
|
//}
|
114
|
|
115
|
//List<string> retrievedData = dd.GetData(dd.DataSubDirectories[DataType.JIS], new DataDownload.Date(10, 2019), new DataDownload.Date(12, 2020));
|
116
|
//Console.WriteLine("Retrieved data: ");
|
117
|
//foreach (string s in retrievedData)
|
118
|
//{
|
119
|
// Console.WriteLine(s);
|
120
|
//}
|
121
|
//Console.WriteLine("all from directory:");
|
122
|
//retrievedData = dd.GetData(dd.DataSubDirectories[DataType.JIS], null, null);
|
123
|
//foreach (string s in retrievedData)
|
124
|
//{
|
125
|
// Console.WriteLine(s);
|
126
|
//}
|
127
|
|
128
|
|
129
|
return dd;
|
130
|
}
|
131
|
|
132
|
|
133
|
private static void XMLTest()
|
134
|
{
|
135
|
Response response = Response.Randomize();
|
136
|
var xml = XmlCommunication.Serialize(response);
|
137
|
//xml = "haha";
|
138
|
Response response1 = new Response();
|
139
|
Console.WriteLine(xml);
|
140
|
try
|
141
|
{
|
142
|
Response responseDeserialized = XmlCommunication.Deserialize<Response>(xml);
|
143
|
}
|
144
|
catch(Exception e)
|
145
|
{
|
146
|
Console.WriteLine("bad format");
|
147
|
}
|
148
|
|
149
|
|
150
|
Console.WriteLine("------");
|
151
|
|
152
|
|
153
|
Request request = Request.Randomize();
|
154
|
xml = XmlCommunication.Serialize(request);
|
155
|
Console.WriteLine(xml);
|
156
|
Request requestDeserialized = XmlCommunication.Deserialize<Request>(xml);
|
157
|
}
|
158
|
|
159
|
|
160
|
private static void JSONParserTest()
|
161
|
{
|
162
|
// FIXME pass the right references to the JsonParser constructor
|
163
|
JsonParser jsonP = new JsonParser(null, null);
|
164
|
jsonP.ParsePrediction();
|
165
|
|
166
|
var res = jsonP.GetPredictionForTime(jsonP.Predictions[5].startTime, jsonP.Predictions[20].startTime);
|
167
|
Console.WriteLine("from " + jsonP.Predictions[5].startTime);
|
168
|
Console.WriteLine("end " + jsonP.Predictions[20].startTime);
|
169
|
foreach (WeatherInfo w in res)
|
170
|
Console.WriteLine(w.ToString());
|
171
|
}
|
172
|
|
173
|
private static void WeatherAsStringTest(DataDownloader dd)
|
174
|
{
|
175
|
Console.WriteLine(dd.DownloadWeatherPrediction());
|
176
|
}
|
177
|
|
178
|
private static IPredictionController PredictionControllerInit(DataDownloader dd)
|
179
|
{
|
180
|
IDataParser p = new DataParser(dd);
|
181
|
// FIXME pass the right references to the JsonParser constructor
|
182
|
IJsonParser jsonP = new JsonParser(dd, new CsvDataLoader());
|
183
|
IPredictionController predictionController = new PredictionController(jsonP, p);
|
184
|
//var results = predictionController.Predict()
|
185
|
|
186
|
|
187
|
return predictionController;
|
188
|
}
|
189
|
|
190
|
private static void ConnectionInit(IPredictionController predictionController, Config config)
|
191
|
{
|
192
|
ConnectionListenerAsync cl = new ConnectionListenerAsync(int.Parse(config.Port), predictionController);
|
193
|
cl.StartListening();
|
194
|
|
195
|
//HttpRequestHandler hrh = new HttpRequestHandler(int.Parse(config.Port));
|
196
|
//hrh.ListenAsynchronously();
|
197
|
}
|
198
|
|
199
|
|
200
|
private static Config FillConfigInfo(string[] args)
|
201
|
{
|
202
|
|
203
|
Config extractedConfigInfo = new Config();
|
204
|
|
205
|
Console.WriteLine("Parsing configuration file...");
|
206
|
|
207
|
if (args.Length != 1)
|
208
|
Console.WriteLine("Wrong usage of parameters, pass only the path to a config file.");
|
209
|
|
210
|
string fullPathConfig = Path.GetFullPath(args[0]);
|
211
|
string[] lines = null;
|
212
|
try {
|
213
|
lines = File.ReadAllLines(fullPathConfig);
|
214
|
}
|
215
|
catch(Exception ex)
|
216
|
{
|
217
|
Console.WriteLine("Could not open " + fullPathConfig);
|
218
|
return null;
|
219
|
}
|
220
|
|
221
|
for (var i = 0; i < lines.Length; i += 1)
|
222
|
{
|
223
|
string line = lines[i];
|
224
|
//Console.WriteLine(line);
|
225
|
if (line.Length == 0 || line == null || line.StartsWith("#"))
|
226
|
continue;
|
227
|
|
228
|
switch (line)
|
229
|
{
|
230
|
case "!site!":
|
231
|
extractedConfigInfo.DataWebsite = lines[++i].Trim();
|
232
|
break;
|
233
|
case "!naming_convention!":
|
234
|
extractedConfigInfo.DownloadedFilesNaming = lines[++i].Trim();
|
235
|
break;
|
236
|
case "!data_root_dir!":
|
237
|
//string rootDir = lines[++i];
|
238
|
//rootDir = rootDir.Replace('\\', Path.DirectorySeparatorChar);
|
239
|
string dirWithConfig = Path.GetDirectoryName(fullPathConfig);
|
240
|
string rootPath = dirWithConfig + Path.DirectorySeparatorChar + lines[++i].Trim();
|
241
|
rootPath = Path.GetFullPath(rootPath);
|
242
|
extractedConfigInfo.DataRootDir = rootPath;
|
243
|
break;
|
244
|
case "!weather_site!":
|
245
|
extractedConfigInfo.WeatherSite = lines[++i].Trim();
|
246
|
break;
|
247
|
case "!port!":
|
248
|
extractedConfigInfo.Port = lines[++i].Trim();
|
249
|
break;
|
250
|
default: break;
|
251
|
}
|
252
|
}
|
253
|
|
254
|
int parsedPort;
|
255
|
|
256
|
bool success = int.TryParse(extractedConfigInfo.Port, out parsedPort);
|
257
|
if (!success)
|
258
|
{
|
259
|
Console.WriteLine("Configured port " + extractedConfigInfo.Port + " is not an integer number! Abort.");
|
260
|
return null;
|
261
|
}
|
262
|
|
263
|
|
264
|
return extractedConfigInfo;
|
265
|
}
|
266
|
}
|
267
|
}
|