Projekt

Obecné

Profil

Stáhnout (5.99 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
			Console.WriteLine(xml);
136
			Response responseDeserialized = XmlCommunication.Deserialize(response, xml);
137

    
138
			Console.WriteLine("------");
139

    
140

    
141
			Request request = Request.Randomize();
142
			xml = XmlCommunication.Serialize(request);
143
			Console.WriteLine(xml);
144
			Request requestDeserialized = XmlCommunication.Deserialize(request, xml);
145
		}
146

    
147

    
148
		private static void JSONParserTest()
149
		{
150
			JsonParser jsonP = new JsonParser(null);
151
			jsonP.ParsePrediction();
152

    
153
			var res = jsonP.GetPredictionForTime(jsonP.Predictions[5].startTime, jsonP.Predictions[20].startTime);
154
			Console.WriteLine("from " + jsonP.Predictions[5].startTime);
155
			Console.WriteLine("end " + jsonP.Predictions[20].startTime);
156
			foreach (WeatherInfo w in res)
157
				Console.WriteLine(w.ToString());
158
		}
159

    
160
		private static IPredictionController PredictionTest(DataDownloader dd)
161
		{
162
			// TODO nastavit čas
163
			IDataParser p = new DataParser(dd);
164
			IPredictionController predictionController = new PredictionController(p);
165
			predictionController.Train();
166
			//var results = predictionController.Predict()
167

    
168
			return predictionController;
169
		}
170

    
171
		private static void ConnectionTest(/*IPredictionController predictionController,*/ Config config)
172
		{
173
			ConnectionListener cl = new ConnectionListener(int.Parse(config.Port));
174
			cl.StartListening();
175

    
176
			//HttpRequestHandler hrh = new HttpRequestHandler(int.Parse(config.Port));
177
			//hrh.ListenAsynchronously();
178
		}
179

    
180

    
181
		private static Config FillConfigInfo(string[] args)
182
		{
183

    
184
			Config extractedConfigInfo = new Config();
185

    
186
			Console.WriteLine(Directory.GetCurrentDirectory());
187
			
188
			if (args.Length != 1)
189
				Console.WriteLine("Wrong usage of parameters, pass the path to a config file."); // todo better explanation?
190

    
191
			string fullPathConfig = Path.GetFullPath(args[0]);
192
			string[] lines = null;
193
			try {
194
				lines = File.ReadAllLines(fullPathConfig);
195
			}
196
			catch(Exception ex)
197
			{
198
				Console.WriteLine("Could not open " + fullPathConfig);
199
				return null;
200
			}
201
			
202
			for (var i = 0; i < lines.Length; i += 1)
203
			{
204
				string line = lines[i];
205
				Console.WriteLine(line);
206
				if (line.Length == 0 || line == null || line.StartsWith("#"))
207
					continue;
208

    
209
				switch (line)
210
				{
211
					case "!site!":
212
						extractedConfigInfo.DataWebsite = lines[++i].Trim();
213
						break;
214
					case "!naming_convention!":
215
						extractedConfigInfo.DownloadedFilesNaming = lines[++i].Trim();
216
						break;
217
					case "!data_root_dir!":
218
						string dirWithConfig = Path.GetDirectoryName(fullPathConfig);
219
						string rootPath = dirWithConfig + Path.DirectorySeparatorChar + lines[++i].Trim();
220
						extractedConfigInfo.DataRootDir = rootPath;
221
						
222
						break;
223
					case "!port!":
224
						extractedConfigInfo.Port = lines[++i].Trim();
225
						break;
226
					default: break;
227
				}
228
			}
229

    
230
			return extractedConfigInfo;
231
		}
232
    }
233
}
(2-2/5)