Projekt

Obecné

Profil

Stáhnout (7.16 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; } 
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
            // setup logging service
39
            XmlConfigurator.Configure();
40

    
41
			// SETUP FOLDERS
42
			Config config = FillConfigInfo(args);
43
			if (config == null)
44
			{
45
				Console.ReadLine();
46
				return;
47
			}
48

    
49
			// data download test
50
			DataDownloader dd = DataDownloadAndRetrievalTest(config);
51

    
52
			WeatherAsStringTest(dd);
53
			Console.ReadLine();
54

    
55
			// xml building test
56
			//XMLTest();
57

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

    
62
			// json parser test
63
			//JSONParserTest();
64

    
65
			// model test
66
			IPredictionController controller = PredictionTest(dd);
67

    
68
			//parse(new DateTime(2019, 10, 5), , interval = 1, wholeDay = true)
69

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

    
76
			// connection test
77
			ConnectionTest(controller, config);
78

    
79
			Console.ReadLine();
80
        }
81

    
82

    
83
		private static DataDownloader DataDownloadAndRetrievalTest(Config config)
84
		{
85
			//test scenario -data download:
86
			DataDownloader dd = new DataDownloader(config.DataRootDir, config.DataWebsite, config.DownloadedFilesNaming, config.WeatherSite);
87
			dd.OverwriteExisting = false;
88
			List<string> savedFiles = new List<string>();
89

    
90
			// -> 12-2019 to exclude corona? But we lose a lot of data
91
			savedFiles.AddRange(dd.DownloadData(DataType.JIS, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
92
			savedFiles.AddRange(dd.DownloadData(DataType.STROJE, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
93
			savedFiles.AddRange(dd.DownloadData(DataType.POCASI, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
94

    
95

    
96

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

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

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

    
122

    
123
			return dd;
124
		}
125

    
126

    
127
		private static void XMLTest()
128
		{
129
			Response response = Response.Randomize();
130
			var xml = XmlCommunication.Serialize(response);
131
			//xml = "haha";
132
			Response response1 = new Response();
133
			Console.WriteLine(xml);
134
			try
135
			{
136
				Response responseDeserialized = XmlCommunication.Deserialize<Response>(xml);
137
			}
138
			catch(Exception e)
139
			{
140
				Console.WriteLine("bad format");
141
			}
142
			
143

    
144
			Console.WriteLine("------");
145

    
146

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

    
153

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

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

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

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

    
183
			return predictionController;
184
		}
185

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

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

    
195

    
196
		private static Config FillConfigInfo(string[] args)
197
		{
198

    
199
			Config extractedConfigInfo = new Config();
200

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

    
207
			string fullPathConfig = Path.GetFullPath(args[0]);
208
			string[] lines = null;
209
			try {
210
				lines = File.ReadAllLines(fullPathConfig);
211
			}
212
			catch(Exception ex)
213
			{
214
				Console.WriteLine("Could not open " + fullPathConfig);
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
			return extractedConfigInfo;
252
		}
253
    }
254
}
(2-2/4)