Projekt

Obecné

Profil

Stáhnout (6.88 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 Port { get; set; } // TODO port cannot be configurable?
25
	}
26

    
27
    public class Program
28
    {
29

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

    
35
        static void Main(string[] args)
36
        {
37
            // setup logging service
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
			// xml building test
51
			//XMLTest();
52

    
53
			// PARSE DATA
54
			//DataParser parser = new DataParser(dd);
55
			//parser.Parse(new DateTime())
56

    
57
			// json parser test
58
			//JSONParserTest();
59

    
60
			// model test
61
			IPredictionController controller = PredictionTest(dd);
62

    
63
			//parse(new DateTime(2019, 10, 5), , interval = 1, wholeDay = true)
64

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

    
71
			// connection test
72
			ConnectionTest(controller, config);
73

    
74
			Console.ReadLine();
75
        }
76

    
77

    
78
		private static DataDownloader DataDownloadAndRetrievalTest(Config config)
79
		{
80
			//test scenario -data download:
81
			DataDownloader dd = new DataDownloader(config.DataRootDir, config.DataWebsite, config.DownloadedFilesNaming);
82
			dd.OverwriteExisting = false;
83
			List<string> savedFiles = new List<string>();
84

    
85
			// -> 12-2019 to exclude corona? But we lose a lot of data
86
			savedFiles.AddRange(dd.DownloadData(DataType.JIS, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
87
			savedFiles.AddRange(dd.DownloadData(DataType.STROJE, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
88
			savedFiles.AddRange(dd.DownloadData(DataType.POCASI, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
89

    
90

    
91

    
92
			Console.WriteLine("Saved files: ");
93
			foreach (string s in savedFiles)
94
			{
95
				Console.WriteLine(s);
96
			}
97

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

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

    
117

    
118
			return dd;
119
		}
120

    
121

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

    
139
			Console.WriteLine("------");
140

    
141

    
142
			Request request = Request.Randomize();
143
			xml = XmlCommunication.Serialize(request);
144
			Console.WriteLine(xml);
145
			Request requestDeserialized = XmlCommunication.Deserialize<Request>(xml);
146
		}
147

    
148

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

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

    
162
		private static IPredictionController PredictionTest(DataDownloader dd)
163
		{
164
			// TODO nastavit čas
165
			IDataParser p = new DataParser(dd);
166
            // FIXME pass the right references to the JsonParser constructor
167
            IJsonParser jsonP = new JsonParser(dd, new CsvDataLoader());
168
            IPredictionController predictionController = new PredictionController(jsonP, p);
169
            predictionController.Train();
170
            //var results = predictionController.Predict()
171
			
172

    
173
			return predictionController;
174
		}
175

    
176
		private static void ConnectionTest(IPredictionController predictionController, Config config)
177
		{
178
			ConnectionListenerAsync cl = new ConnectionListenerAsync(int.Parse(config.Port), predictionController);
179
			cl.StartListening();
180

    
181
			//HttpRequestHandler hrh = new HttpRequestHandler(int.Parse(config.Port));
182
			//hrh.ListenAsynchronously();
183
		}
184

    
185

    
186
		private static Config FillConfigInfo(string[] args)
187
		{
188

    
189
			Config extractedConfigInfo = new Config();
190

    
191
			Console.WriteLine(Directory.GetCurrentDirectory());
192
			Console.WriteLine("Parsing configuration file...");
193
			
194
			if (args.Length != 1)
195
				Console.WriteLine("Wrong usage of parameters, pass the path to a config file."); // todo better explanation?
196

    
197
			string fullPathConfig = Path.GetFullPath(args[0]);
198
			string[] lines = null;
199
			try {
200
				lines = File.ReadAllLines(fullPathConfig);
201
			}
202
			catch(Exception ex)
203
			{
204
				Console.WriteLine("Could not open " + fullPathConfig);
205
				return null;
206
			}
207
			
208
			for (var i = 0; i < lines.Length; i += 1)
209
			{
210
				string line = lines[i];
211
				//Console.WriteLine(line);
212
				if (line.Length == 0 || line == null || line.StartsWith("#"))
213
					continue;
214

    
215
				switch (line)
216
				{
217
					case "!site!":
218
						extractedConfigInfo.DataWebsite = lines[++i].Trim();
219
						break;
220
					case "!naming_convention!":
221
						extractedConfigInfo.DownloadedFilesNaming = lines[++i].Trim();
222
						break;
223
					case "!data_root_dir!":
224
						//string rootDir = lines[++i];
225
						//rootDir = rootDir.Replace('\\', Path.DirectorySeparatorChar);
226
						string dirWithConfig = Path.GetDirectoryName(fullPathConfig);
227
						string rootPath = dirWithConfig + Path.DirectorySeparatorChar + lines[++i].Trim();
228
						rootPath = Path.GetFullPath(rootPath);
229
						extractedConfigInfo.DataRootDir = rootPath;
230
						break;
231
					case "!port!":
232
						extractedConfigInfo.Port = lines[++i].Trim();
233
						break;
234
					default: break;
235
				}
236
			}
237

    
238
			return extractedConfigInfo;
239
		}
240
    }
241
}
(2-2/4)