Projekt

Obecné

Profil

Stáhnout (6.84 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
            XmlConfigurator.Configure();
38
            // SETUP FOLDERS
39
            Config config = FillConfigInfo(args);
40
			if (config == null)
41
			{
42
				Console.ReadLine();
43
				return;
44
			}
45

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

    
49
			// xml building test
50
			//XMLTest();
51

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

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

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

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

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

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

    
73
			Console.ReadLine();
74
        }
75

    
76

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

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

    
89

    
90

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

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

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

    
116

    
117
			return dd;
118
		}
119

    
120

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

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

    
140

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

    
147

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

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

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

    
172
			return predictionController;
173
		}
174

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

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

    
184

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

    
188
			Config extractedConfigInfo = new Config();
189

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

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

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

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