Projekt

Obecné

Profil

Stáhnout (6.19 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; }
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
			// 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

    
64

    
65

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

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

    
73

    
74

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

    
77

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

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

    
87

    
88
			
89

    
90
			Console.ReadLine();
91
        }
92

    
93

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

    
104

    
105

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

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

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

    
131

    
132
			return dd;
133
		}
134

    
135

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

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

    
155

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

    
162

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

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

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

    
183
			return predictionController;
184
		}
185

    
186
		private static void ConnectionTest(IPredictionController predictionController, Config config)
187
		{
188
			ConnectionListener cl = new ConnectionListener(int.Parse(config.Port), predictionController);
189
			cl.StartListening();
190

    
191
			//HttpRequestHandler hrh = new HttpRequestHandler(int.Parse(config.Port));
192
			//hrh.ListenAsynchronously();
193
		}
194

    
195

    
196
		private static Config FillConfigInfo(string[] args)
197
		{
198

    
199
			Config extractedConfigInfo = new Config();
200

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

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

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

    
245
			return extractedConfigInfo;
246
		}
247
    }
248
}
(2-2/5)