Projekt

Obecné

Profil

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

    
14
namespace ServerApp
15
{
16

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

    
25
    public class Program
26
    {
27

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

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

    
36
			// SETUP FOLDERS
37

    
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(response1, 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
			JsonParser jsonP = new JsonParser(null);
168
			jsonP.ParsePrediction();
169

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

    
177
		private static IPredictionController PredictionTest(DataDownloader dd)
178
		{
179
			// TODO nastavit čas
180
			IDataParser p = new DataParser(dd);
181
			IPredictionController predictionController = new PredictionController(p);
182
			predictionController.Train();
183
			//var results = predictionController.Predict()
184
			
185

    
186
			return predictionController;
187
		}
188

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

    
194
			//HttpRequestHandler hrh = new HttpRequestHandler(int.Parse(config.Port));
195
			//hrh.ListenAsynchronously();
196
		}
197

    
198

    
199
		private static Config FillConfigInfo(string[] args)
200
		{
201

    
202
			Config extractedConfigInfo = new Config();
203

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

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

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

    
248
			return extractedConfigInfo;
249
		}
250
    }
251
}
(2-2/5)