Projekt

Obecné

Profil

Stáhnout (6.6 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
			savedFiles.AddRange(dd.DownloadData(DataType.JIS, DataFormat.CSV, new DataDownload.Date(1, 2019), new DataDownload.Date(12, 2020)));
103
			savedFiles.AddRange(dd.DownloadData(DataType.STROJE, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2020)));
104
			savedFiles.AddRange(dd.DownloadData(DataType.POCASI, DataFormat.CSV, new DataDownload.Date(1, 2017), new DataDownload.Date(12, 2020)));
105

    
106

    
107

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

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

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

    
133

    
134
			return dd;
135
		}
136

    
137

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

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

    
157

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

    
164

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

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

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

    
189
			return predictionController;
190
		}
191

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

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

    
201

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

    
205
			Config extractedConfigInfo = new Config();
206

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

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

    
230
				switch (line)
231
				{
232
					case "!site!":
233
						extractedConfigInfo.DataWebsite = lines[++i].Trim();
234
						break;
235
					case "!naming_convention!":
236
						extractedConfigInfo.DownloadedFilesNaming = lines[++i].Trim();
237
						break;
238
					case "!data_root_dir!":
239
						string dirWithConfig = Path.GetDirectoryName(fullPathConfig);
240
						string rootPath = dirWithConfig + Path.DirectorySeparatorChar + lines[++i].Trim();
241
						extractedConfigInfo.DataRootDir = rootPath;
242
						
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
}
(1-1/3)