Projekt

Obecné

Profil

Stáhnout (6.11 KB) Statistiky
| Větev: | Tag: | Revize:
1 cdf3c217 A-Konig
//
2
// Author: A. Konig
3
//
4
5
using System;
6 5d9a5bd9 A-Konig
using System.Collections.Generic;
7
using System.Globalization;
8
using System.IO;
9
10 734533a8 A-Konig
namespace ServerApp.Parser.InputData
11 5d9a5bd9 A-Konig
{
12 734533a8 A-Konig
    /// <summary>
13
    /// Class responsible for loading data from file
14
    /// </summary>
15 cdf3c217 A-Konig
    /// <author>A. Konig</author>
16
    class CsvDataLoader : IDataLoader
17 5d9a5bd9 A-Konig
    {
18 734533a8 A-Konig
        /// <summary> Culture info for parsing numbers </summary>
19 5d9a5bd9 A-Konig
        static CultureInfo cultureInfo = new CultureInfo("de-DE");
20
21 26ecc756 A-Konig
        /// <summary>
22
        /// Load file into string
23
        /// </summary>
24
        /// <param name="path">Paht to file</param>
25
        /// <returns>Null if file does not exist, or file in string</returns>
26
        public string LoadPredictionFile(string path)
27
        {
28
            if (path == null || !File.Exists(path))
29
                return null;
30
31
            return File.ReadAllText(path);
32
        }
33
34 734533a8 A-Konig
        /// <summary>
35
        /// Load csv file into array
36
        /// </summary>
37
        /// <param name="name">Path to file</param>
38
        /// <returns>Array with loaded data</returns>
39 98b568bc A-Konig
        private string[] LoadCsv(string name)
40 5d9a5bd9 A-Konig
        {
41 d39750f3 A-Konig
            if (!File.Exists(name))
42
                return null;
43
44 5d9a5bd9 A-Konig
            string[] lines = File.ReadAllLines(name);
45
            return lines;
46
        }
47
48 734533a8 A-Konig
        /// <summary>
49
        /// Load jis data from file
50
        /// </summary>
51
        /// <param name="pathToFile">Path to file</param>
52
        /// <returns></returns>
53 98b568bc A-Konig
        public List<JisInstance> LoadJisFile(string pathToFile)
54 5d9a5bd9 A-Konig
        {
55
            var lines = LoadCsv(pathToFile);
56 a53b1de8 A-Konig
            if (lines == null)
57
                return null;
58
59 5d9a5bd9 A-Konig
            List<JisInstance> list = new List<JisInstance>();
60
61 d39750f3 A-Konig
            if (lines == null)
62
                return list;
63
64 5d9a5bd9 A-Konig
            for (int i = 0; i < lines.Length; i++)
65
            {
66
                var data = lines[i].Split(';');
67 d39750f3 A-Konig
68
                if (data.Length < 3)
69
                    continue;
70
71 5d9a5bd9 A-Konig
                data[0] = data[0].Substring(1, data[0].Length - 2);
72
                data[1] = data[1].Substring(1, data[1].Length - 2);
73 23e8ae04 A-Konig
74
                var dateTime = new DateTime();
75
                try
76
                {
77
                    dateTime = DateTime.ParseExact(data[1], "G", cultureInfo);
78
                }
79
                catch
80
                {
81
                    continue;
82
                }
83 5d9a5bd9 A-Konig
84
                int amount = 0;
85
                int.TryParse(data[2], out amount);
86
87
                JisInstance inst = new JisInstance(data[0], dateTime, amount);
88
                list.Add(inst);
89
            }
90
91
            return list;
92
        }
93
94 734533a8 A-Konig
        /// <summary>
95
        /// Load login data from file
96
        /// </summary>
97
        /// <param name="pathToFile">Path to file</param>
98
        /// <returns></returns>
99 98b568bc A-Konig
        public List<LogInInstance> LoadLoginFile(string pathToFile)
100 5d9a5bd9 A-Konig
        {
101
            var lines = LoadCsv(pathToFile);
102 a53b1de8 A-Konig
            if (lines == null)
103
                return null;
104
105 5d9a5bd9 A-Konig
            List<LogInInstance> list = new List<LogInInstance>();
106
107 d39750f3 A-Konig
            if (lines == null)
108
                return list;
109
110 5d9a5bd9 A-Konig
            for (int i = 0; i < lines.Length; i++)
111
            {
112
                var data = lines[i].Split(';');
113 d39750f3 A-Konig
114
                if (data.Length < 9)
115
                    continue;
116
117 5d9a5bd9 A-Konig
                data[0] = data[0].Substring(1, data[0].Length - 2);
118
                data[3] = data[3].Substring(1, data[3].Length - 2);
119
                data[4] = data[4].Substring(1, data[4].Length - 2);
120 0da0ac88 A-Konig
                data[5] = data[5].Substring(1, data[5].Length - 2);
121
                data[6] = data[6].Substring(1, data[6].Length - 2);
122
                data[7] = data[7].Substring(1, data[7].Length - 2);
123
                data[8] = data[8].Substring(1, data[8].Length - 2);
124 5d9a5bd9 A-Konig
125 23e8ae04 A-Konig
                var dateTime = new DateTime();
126
                var startTime = new DateTime();
127
                var endTime = new DateTime();
128
                try
129
                {
130
                    dateTime = DateTime.ParseExact(data[0], "G", cultureInfo);
131
                    startTime = DateTime.ParseExact(data[3], "t", cultureInfo);
132
                    endTime = DateTime.ParseExact(data[4], "t", cultureInfo);
133
                }
134
                catch
135
                {
136
                    continue;
137
                }
138 5d9a5bd9 A-Konig
139
                int count = 0;
140
                int.TryParse(data[1], out count);
141
142
                int lesson = 0;
143
                int.TryParse(data[2], out lesson);
144
145
                LogInInstance inst = new LogInInstance(dateTime, count, lesson, startTime, endTime, data[5], data[6], data[7], data[8]);
146
                list.Add(inst);
147
            }
148
149
            return list;
150
        }
151
152 734533a8 A-Konig
        /// <summary>
153
        /// Load weather data from file
154
        /// </summary>
155
        /// <param name="pathToFile">Path to file</param>
156
        /// <returns></returns>
157 98b568bc A-Konig
        public List<WeatherInstance> LoadWeatherFile(string pathToFile)
158 5d9a5bd9 A-Konig
        {
159
            var lines = LoadCsv(pathToFile);
160 a53b1de8 A-Konig
            if (lines == null)
161
                return null;
162
163 5d9a5bd9 A-Konig
            List<WeatherInstance> list = new List<WeatherInstance>();
164 d39750f3 A-Konig
165
            if (lines == null)
166
                return list;
167
168 5d9a5bd9 A-Konig
            for (int i = 0; i < lines.Length; i++)
169
            {
170
                var data = lines[i].Split(';');
171 d39750f3 A-Konig
                
172
                if (data.Length < 5)
173
                    continue;
174
175 5d9a5bd9 A-Konig
                data[0] = data[0].Substring(1, data[0].Length - 2);
176 23e8ae04 A-Konig
177
                var dateTime = new DateTime();
178
                try
179
                {
180
                    dateTime = DateTime.ParseExact(data[0], "G", cultureInfo);
181
                }
182
                catch
183
                {
184
                    continue;
185
                }
186 5d9a5bd9 A-Konig
                
187
                double temp = 0;
188
                double.TryParse(data[1], out temp);
189
                
190
                double wind = 0;
191
                double.TryParse(data[2], out wind);
192
                
193
                int rain = 0;
194
                int.TryParse(data[3], out rain);
195
                
196
                double lum = 0;
197
                double.TryParse(data[4], out lum);
198
199
                WeatherInstance inst = new WeatherInstance(dateTime, temp, wind, rain, lum);
200
                list.Add(inst);
201
            }
202
203
            return list;
204
        }
205
206
    }
207
}