Projekt

Obecné

Profil

Stáhnout (6.14 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 static 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
			// commands accepting test
46
			//create a thread for commands accepting:
47
			//Thread inputThread = new Thread(CommandsAcceptor.AcceptCommand);
48
			//inputThread.Start();
49

    
50

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

    
55

    
56

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

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

    
63

    
64

    
65
			// PARSE DATA
66

    
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
			// connection test
82
			ConnectionTest(controller, config);
83
			
84

    
85
			Console.ReadLine();
86
        }
87

    
88

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

    
99

    
100

    
101
			Console.WriteLine("Saved files: ");
102
			foreach (string s in savedFiles)
103
			{
104
				Console.WriteLine(s);
105
			}
106

    
107
			Console.WriteLine("subdirectories: ");
108
			foreach (KeyValuePair<DataType, string> kvp in dd.DataSubDirectories)
109
			{
110
				Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
111
			}
112

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

    
126

    
127
			return dd;
128
		}
129

    
130

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

    
148
			Console.WriteLine("------");
149

    
150

    
151
			Request request = Request.Randomize();
152
			xml = XmlCommunication.Serialize(request);
153
			Console.WriteLine(xml);
154
			Request requestDeserialized = XmlCommunication.Deserialize(request, xml);
155
		}
156

    
157

    
158
		private static void JSONParserTest()
159
		{
160
			JsonParser jsonP = new JsonParser(null);
161
			jsonP.ParsePrediction();
162

    
163
			var res = jsonP.GetPredictionForTime(jsonP.Predictions[5].startTime, jsonP.Predictions[20].startTime);
164
			Console.WriteLine("from " + jsonP.Predictions[5].startTime);
165
			Console.WriteLine("end " + jsonP.Predictions[20].startTime);
166
			foreach (WeatherInfo w in res)
167
				Console.WriteLine(w.ToString());
168
		}
169

    
170
		private static IPredictionController PredictionTest(DataDownloader dd)
171
		{
172
			// TODO nastavit čas
173
			IDataParser p = new DataParser(dd);
174
			IPredictionController predictionController = new PredictionController(p);
175
			predictionController.Train();
176
			//var results = predictionController.Predict()
177

    
178
			return predictionController;
179
		}
180

    
181
		private static void ConnectionTest(IPredictionController predictionController, Config config)
182
		{
183
			ConnectionListener cl = new ConnectionListener(int.Parse(config.Port), predictionController);
184
			cl.StartListening();
185

    
186
			//HttpRequestHandler hrh = new HttpRequestHandler(int.Parse(config.Port));
187
			//hrh.ListenAsynchronously();
188
		}
189

    
190

    
191
		private static Config FillConfigInfo(string[] args)
192
		{
193

    
194
			Config extractedConfigInfo = new Config();
195

    
196
			Console.WriteLine(Directory.GetCurrentDirectory());
197
			
198
			if (args.Length != 1)
199
				Console.WriteLine("Wrong usage of parameters, pass the path to a config file."); // todo better explanation?
200

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

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

    
240
			return extractedConfigInfo;
241
		}
242
    }
243
}
(2-2/5)