Projekt

Obecné

Profil

Stáhnout (7.17 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 // TBD where this should go
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; } // TODO port cannot be configurable?
26
	}
27

    
28
    public class Program
29
    {
30

    
31
		//public void NotifyUserMessage(string message)
32
		//{
33
		//	Console.WriteLine("Received user message: " + message);
34
		//}
35

    
36
        static void Main(string[] args)
37
        {
38
            XmlConfigurator.Configure();
39
            // SETUP FOLDERS
40
            Config config = FillConfigInfo(args);
41
			if (config == null)
42
			{
43
				Console.ReadLine();
44
				return;
45
			}
46

    
47
			// data download test
48
			DataDownloader dd = DataDownloadAndRetrievalTest(config);
49

    
50
			WeatherAsStringTest(dd);
51
			Console.ReadLine();
52

    
53
			// xml building test
54
			//XMLTest();
55

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

    
60
			// json parser test
61
			//JSONParserTest();
62

    
63
			// model test
64
			IPredictionController controller = PredictionTest(dd);
65

    
66
			//parse(new DateTime(2019, 10, 5), , interval = 1, wholeDay = true)
67

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

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

    
77
			Console.ReadLine();
78
        }
79

    
80

    
81
		private static DataDownloader DataDownloadAndRetrievalTest(Config config)
82
		{
83
			//test scenario -data download:
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? But we lose a lot of data
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 XMLTest()
126
		{
127
			Response response = Response.Randomize();
128
			var xml = XmlCommunication.Serialize(response);
129
			//xml = "haha";
130
			Response response1 = new Response();
131
			Console.WriteLine(xml);
132
			try
133
			{
134
				Response responseDeserialized = XmlCommunication.Deserialize<Response>(xml);
135
			}
136
			catch(Exception e)
137
			{
138
				Console.WriteLine("bad format");
139
			}
140
			
141

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

    
144

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

    
151

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

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

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

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

    
181
			return predictionController;
182
		}
183

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

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

    
193

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

    
197
			Config extractedConfigInfo = new Config();
198

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

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

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

    
249
			return extractedConfigInfo;
250
		}
251
    }
252
}
(2-2/4)