Projekt

Obecné

Profil

Stáhnout (6.41 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
			Config config = FillConfigInfo(args);
38
			if (config == null)
39
			{
40
				Console.ReadLine();
41
				return;
42
			}
43

    
44

    
45
			
46

    
47

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

    
52

    
53

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

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

    
60

    
61

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

    
66

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

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

    
74

    
75

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

    
78

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

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

    
88

    
89
			
90

    
91
			Console.ReadLine();
92
        }
93

    
94

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

    
105

    
106

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

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

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

    
132

    
133
			return dd;
134
		}
135

    
136

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

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

    
156

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

    
163

    
164
		private static void JSONParserTest()
165
		{
166
			JsonParser jsonP = new JsonParser(null);
167
			jsonP.ParsePrediction();
168

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

    
176
		private static IPredictionController PredictionTest(DataDownloader dd)
177
		{
178
			// TODO nastavit čas
179
			IDataParser p = new DataParser(dd);
180
            IJsonParser jsonP = new JsonParser(dd);
181
            IPredictionController predictionController = new PredictionController(jsonP, 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)