Projekt

Obecné

Profil

Stáhnout (6.11 KB) Statistiky
| Větev: | Tag: | Revize:
1
//
2
// Author: A. Konig
3
//
4

    
5
using System;
6
using System.Collections.Generic;
7
using System.Globalization;
8
using System.IO;
9

    
10
namespace ServerApp.Parser.InputData
11
{
12
    /// <summary>
13
    /// Class responsible for loading data from file
14
    /// </summary>
15
    /// <author>A. Konig</author>
16
    class CsvDataLoader : IDataLoader
17
    {
18
        /// <summary> Culture info for parsing numbers </summary>
19
        static CultureInfo cultureInfo = new CultureInfo("de-DE");
20

    
21
        /// <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
        /// <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
        private string[] LoadCsv(string name)
40
        {
41
            if (!File.Exists(name))
42
                return null;
43

    
44
            string[] lines = File.ReadAllLines(name);
45
            return lines;
46
        }
47

    
48
        /// <summary>
49
        /// Load jis data from file
50
        /// </summary>
51
        /// <param name="pathToFile">Path to file</param>
52
        /// <returns></returns>
53
        public List<JisInstance> LoadJisFile(string pathToFile)
54
        {
55
            var lines = LoadCsv(pathToFile);
56
            if (lines == null)
57
                return null;
58

    
59
            List<JisInstance> list = new List<JisInstance>();
60

    
61
            if (lines == null)
62
                return list;
63

    
64
            for (int i = 0; i < lines.Length; i++)
65
            {
66
                var data = lines[i].Split(';');
67

    
68
                if (data.Length < 3)
69
                    continue;
70

    
71
                data[0] = data[0].Substring(1, data[0].Length - 2);
72
                data[1] = data[1].Substring(1, data[1].Length - 2);
73

    
74
                var dateTime = new DateTime();
75
                try
76
                {
77
                    dateTime = DateTime.ParseExact(data[1], "G", cultureInfo);
78
                }
79
                catch
80
                {
81
                    continue;
82
                }
83

    
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
        /// <summary>
95
        /// Load login data from file
96
        /// </summary>
97
        /// <param name="pathToFile">Path to file</param>
98
        /// <returns></returns>
99
        public List<LogInInstance> LoadLoginFile(string pathToFile)
100
        {
101
            var lines = LoadCsv(pathToFile);
102
            if (lines == null)
103
                return null;
104

    
105
            List<LogInInstance> list = new List<LogInInstance>();
106

    
107
            if (lines == null)
108
                return list;
109

    
110
            for (int i = 0; i < lines.Length; i++)
111
            {
112
                var data = lines[i].Split(';');
113

    
114
                if (data.Length < 9)
115
                    continue;
116

    
117
                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
                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

    
125
                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

    
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
        /// <summary>
153
        /// Load weather data from file
154
        /// </summary>
155
        /// <param name="pathToFile">Path to file</param>
156
        /// <returns></returns>
157
        public List<WeatherInstance> LoadWeatherFile(string pathToFile)
158
        {
159
            var lines = LoadCsv(pathToFile);
160
            if (lines == null)
161
                return null;
162

    
163
            List<WeatherInstance> list = new List<WeatherInstance>();
164

    
165
            if (lines == null)
166
                return list;
167

    
168
            for (int i = 0; i < lines.Length; i++)
169
            {
170
                var data = lines[i].Split(';');
171
                
172
                if (data.Length < 5)
173
                    continue;
174

    
175
                data[0] = data[0].Substring(1, data[0].Length - 2);
176

    
177
                var dateTime = new DateTime();
178
                try
179
                {
180
                    dateTime = DateTime.ParseExact(data[0], "G", cultureInfo);
181
                }
182
                catch
183
                {
184
                    continue;
185
                }
186
                
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
}
(1-1/5)