Projekt

Obecné

Profil

Stáhnout (6.7 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

    
47
			
48

    
49

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

    
54

    
55

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

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

    
62

    
63

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

    
68

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

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

    
76

    
77

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

    
80

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

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

    
90

    
91
			
92

    
93
			Console.ReadLine();
94
        }
95

    
96

    
97
		private static DataDownloader DataDownloadAndRetrievalTest(Config config)
98
		{
99
			//test scenario -data download:
100
			DataDownloader dd = new DataDownloader(config.DataRootDir, config.DataWebsite, config.DownloadedFilesNaming);
101
			dd.OverwriteExisting = false;
102
			List<string> savedFiles = new List<string>();
103
			savedFiles.AddRange(dd.DownloadData(DataType.JIS, DataFormat.CSV, new DataDownload.Date(1, 2019), new DataDownload.Date(12, 2020)));
104
			savedFiles.AddRange(dd.DownloadData(DataType.STROJE, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2020)));
105
			savedFiles.AddRange(dd.DownloadData(DataType.POCASI, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2020)));
106

    
107

    
108

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

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

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

    
134

    
135
			return dd;
136
		}
137

    
138

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

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

    
158

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

    
165

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

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

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

    
190
			return predictionController;
191
		}
192

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

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

    
202

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

    
206
			Config extractedConfigInfo = new Config();
207

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

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

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

    
252
			return extractedConfigInfo;
253
		}
254
    }
255
}
(2-2/4)