Projekt

Obecné

Profil

Stáhnout (6.86 KB) Statistiky
| Větev: | Tag: | Revize:
1
using ServerApp.Connection;
2
using ServerApp.Connection.XMLProtocolHandler;
3
using ServerApp.DataDownload;
4
using ServerApp.Parser.InputData;
5
using ServerApp.Parser.OutputInfo;
6
using ServerApp.Parser.Parsers;
7
using ServerApp.Predictor;
8
using ServerApp.User;
9
using ServerApp.WeatherPredictionParser;
10
using System;
11
using System.Collections.Generic;
12
using System.IO;
13
using System.Threading;
14

    
15
namespace ServerApp
16
{
17

    
18
	class Config // TBD where this should go
19
	{
20
		public string DataWebsite { get; set; }
21
		public string DownloadedFilesNaming { get; set; }
22
		public string DataRootDir { get; set; }
23
		public string Port { get; set; } // TODO port cannot be configurable?
24
	}
25

    
26
    public class Program
27
    {
28

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

    
34
        static void Main(string[] args)
35
        {
36

    
37
			// SETUP FOLDERS
38
			Config config = FillConfigInfo(args);
39
			if (config == null)
40
			{
41
				Console.ReadLine();
42
				return;
43
			}
44

    
45

    
46
			
47

    
48

    
49
			// is this obsolete?
50
			//DataParser p = new DataParser("data/");
51
			//p.Parse();
52

    
53

    
54

    
55
			// data download test
56
			DataDownloader dd = DataDownloadAndRetrievalTest(config);
57

    
58
			// xml building test
59
			//XMLTest();
60

    
61

    
62

    
63
			// PARSE DATA
64
			//DataParser parser = new DataParser(dd);
65
			//parser.Parse(new DateTime())
66

    
67

    
68
			
69
			// json parser test
70
			//JSONParserTest();
71

    
72
			// model test
73
			IPredictionController controller = PredictionTest(dd);
74

    
75

    
76

    
77
			//parse(new DateTime(2019, 10, 5), , interval = 1, wholeDay = true)
78

    
79

    
80
			// commands accepting test
81
			// create a thread for commands accepting:
82
			CommandsAcceptor ca = new CommandsAcceptor(dd, controller);
83
			Thread inputThread = new Thread(ca.AcceptCommand);
84
			inputThread.Start();
85

    
86
			// connection test
87
			ConnectionTest(controller, config);
88

    
89

    
90
			
91

    
92
			Console.ReadLine();
93
        }
94

    
95

    
96
		private static DataDownloader DataDownloadAndRetrievalTest(Config config)
97
		{
98
			//test scenario -data download:
99
			DataDownloader dd = new DataDownloader(config.DataRootDir, config.DataWebsite, config.DownloadedFilesNaming);
100
			dd.OverwriteExisting = false;
101
			List<string> savedFiles = new List<string>();
102

    
103
			// -> 12-2019 to exclude corona? But we lose a lot of data
104
			savedFiles.AddRange(dd.DownloadData(DataType.JIS, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
105
			savedFiles.AddRange(dd.DownloadData(DataType.STROJE, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
106
			savedFiles.AddRange(dd.DownloadData(DataType.POCASI, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2019)));
107

    
108

    
109

    
110
			Console.WriteLine("Saved files: ");
111
			foreach (string s in savedFiles)
112
			{
113
				Console.WriteLine(s);
114
			}
115

    
116
			Console.WriteLine("subdirectories: ");
117
			foreach (KeyValuePair<DataType, string> kvp in dd.DataSubDirectories)
118
			{
119
				Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
120
			}
121

    
122
			List<string> retrievedData = dd.GetData(dd.DataSubDirectories[DataType.JIS], new DataDownload.Date(10, 2019), new DataDownload.Date(12, 2020));
123
			Console.WriteLine("Retrieved data: ");
124
			foreach (string s in retrievedData)
125
			{
126
				Console.WriteLine(s);
127
			}
128
			Console.WriteLine("all from directory:");
129
			retrievedData = dd.GetData(dd.DataSubDirectories[DataType.JIS], null, null);
130
			foreach (string s in retrievedData)
131
			{
132
				Console.WriteLine(s);
133
			}
134

    
135

    
136
			return dd;
137
		}
138

    
139

    
140
		private static void XMLTest()
141
		{
142
			Response response = Response.Randomize();
143
			var xml = XmlCommunication.Serialize(response);
144
			//xml = "haha";
145
			Response response1 = new Response();
146
			Console.WriteLine(xml);
147
			try
148
			{
149
				Response responseDeserialized = XmlCommunication.Deserialize<Response>(xml);
150
			}
151
			catch(Exception e)
152
			{
153
				Console.WriteLine("bad format");
154
			}
155
			
156

    
157
			Console.WriteLine("------");
158

    
159

    
160
			Request request = Request.Randomize();
161
			xml = XmlCommunication.Serialize(request);
162
			Console.WriteLine(xml);
163
			Request requestDeserialized = XmlCommunication.Deserialize<Request>(xml);
164
		}
165

    
166

    
167
		private static void JSONParserTest()
168
		{
169
            // FIXME pass the right references to the JsonParser constructor
170
			JsonParser jsonP = new JsonParser(null, null);
171
			jsonP.ParsePrediction();
172

    
173
			var res = jsonP.GetPredictionForTime(jsonP.Predictions[5].startTime, jsonP.Predictions[20].startTime);
174
			Console.WriteLine("from " + jsonP.Predictions[5].startTime);
175
			Console.WriteLine("end " + jsonP.Predictions[20].startTime);
176
			foreach (WeatherInfo w in res)
177
				Console.WriteLine(w.ToString());
178
		}
179

    
180
		private static IPredictionController PredictionTest(DataDownloader dd)
181
		{
182
			// TODO nastavit čas
183
			IDataParser p = new DataParser(dd);
184
            // FIXME pass the right references to the JsonParser constructor
185
            IJsonParser jsonP = new JsonParser(dd, null);
186
            IPredictionController predictionController = new PredictionController(jsonP, p);
187
            predictionController.Train();
188
            //var results = predictionController.Predict()
189
			
190

    
191
			return predictionController;
192
		}
193

    
194
		private static void ConnectionTest(IPredictionController predictionController, Config config)
195
		{
196
			ConnectionListenerAsync cl = new ConnectionListenerAsync(int.Parse(config.Port), predictionController);
197
			cl.StartListening();
198

    
199
			//HttpRequestHandler hrh = new HttpRequestHandler(int.Parse(config.Port));
200
			//hrh.ListenAsynchronously();
201
		}
202

    
203

    
204
		private static Config FillConfigInfo(string[] args)
205
		{
206

    
207
			Config extractedConfigInfo = new Config();
208

    
209
			Console.WriteLine(Directory.GetCurrentDirectory());
210
			Console.WriteLine("Parsing configuration file...");
211
			
212
			if (args.Length != 1)
213
				Console.WriteLine("Wrong usage of parameters, pass the path to a config file."); // todo better explanation?
214

    
215
			string fullPathConfig = Path.GetFullPath(args[0]);
216
			string[] lines = null;
217
			try {
218
				lines = File.ReadAllLines(fullPathConfig);
219
			}
220
			catch(Exception ex)
221
			{
222
				Console.WriteLine("Could not open " + fullPathConfig);
223
				return null;
224
			}
225
			
226
			for (var i = 0; i < lines.Length; i += 1)
227
			{
228
				string line = lines[i];
229
				//Console.WriteLine(line);
230
				if (line.Length == 0 || line == null || line.StartsWith("#"))
231
					continue;
232

    
233
				switch (line)
234
				{
235
					case "!site!":
236
						extractedConfigInfo.DataWebsite = lines[++i].Trim();
237
						break;
238
					case "!naming_convention!":
239
						extractedConfigInfo.DownloadedFilesNaming = lines[++i].Trim();
240
						break;
241
					case "!data_root_dir!":
242
						//string rootDir = lines[++i];
243
						//rootDir = rootDir.Replace('\\', Path.DirectorySeparatorChar);
244
						string dirWithConfig = Path.GetDirectoryName(fullPathConfig);
245
						string rootPath = dirWithConfig + Path.DirectorySeparatorChar + lines[++i].Trim();
246
						rootPath = Path.GetFullPath(rootPath);
247
						extractedConfigInfo.DataRootDir = rootPath;
248
						break;
249
					case "!port!":
250
						extractedConfigInfo.Port = lines[++i].Trim();
251
						break;
252
					default: break;
253
				}
254
			}
255

    
256
			return extractedConfigInfo;
257
		}
258
    }
259
}
(1-1/3)