Projekt

Obecné

Profil

Stáhnout (5.09 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 734533a8 A-Konig
        /// <summary>
22
        /// Load csv file into array
23
        /// </summary>
24
        /// <param name="name">Path to file</param>
25
        /// <returns>Array with loaded data</returns>
26 98b568bc A-Konig
        private string[] LoadCsv(string name)
27 5d9a5bd9 A-Konig
        {
28 d39750f3 A-Konig
            if (!File.Exists(name))
29
                return null;
30
31 5d9a5bd9 A-Konig
            string[] lines = File.ReadAllLines(name);
32
            return lines;
33
        }
34
35 734533a8 A-Konig
        /// <summary>
36
        /// Load jis data from file
37
        /// </summary>
38
        /// <param name="pathToFile">Path to file</param>
39
        /// <returns></returns>
40 98b568bc A-Konig
        public List<JisInstance> LoadJisFile(string pathToFile)
41 5d9a5bd9 A-Konig
        {
42
            var lines = LoadCsv(pathToFile);
43 a53b1de8 A-Konig
            if (lines == null)
44
                return null;
45
46 5d9a5bd9 A-Konig
            List<JisInstance> list = new List<JisInstance>();
47
48 d39750f3 A-Konig
            if (lines == null)
49
                return list;
50
51 5d9a5bd9 A-Konig
            for (int i = 0; i < lines.Length; i++)
52
            {
53
                var data = lines[i].Split(';');
54 d39750f3 A-Konig
55
                if (data.Length < 3)
56
                    continue;
57
58 5d9a5bd9 A-Konig
                data[0] = data[0].Substring(1, data[0].Length - 2);
59
                data[1] = data[1].Substring(1, data[1].Length - 2);
60
                
61
                var dateTime = DateTime.ParseExact(data[1], "G", cultureInfo);
62
63
                int amount = 0;
64
                int.TryParse(data[2], out amount);
65
66
                JisInstance inst = new JisInstance(data[0], dateTime, amount);
67
                list.Add(inst);
68
            }
69
70
            return list;
71
        }
72
73 734533a8 A-Konig
        /// <summary>
74
        /// Load login data from file
75
        /// </summary>
76
        /// <param name="pathToFile">Path to file</param>
77
        /// <returns></returns>
78 98b568bc A-Konig
        public List<LogInInstance> LoadLoginFile(string pathToFile)
79 5d9a5bd9 A-Konig
        {
80
            var lines = LoadCsv(pathToFile);
81 a53b1de8 A-Konig
            if (lines == null)
82
                return null;
83
84 5d9a5bd9 A-Konig
            List<LogInInstance> list = new List<LogInInstance>();
85
86 d39750f3 A-Konig
            if (lines == null)
87
                return list;
88
89 5d9a5bd9 A-Konig
            for (int i = 0; i < lines.Length; i++)
90
            {
91
                var data = lines[i].Split(';');
92 d39750f3 A-Konig
93
                if (data.Length < 9)
94
                    continue;
95
96 5d9a5bd9 A-Konig
                data[0] = data[0].Substring(1, data[0].Length - 2);
97
                data[3] = data[3].Substring(1, data[3].Length - 2);
98
                data[4] = data[4].Substring(1, data[4].Length - 2);
99 0da0ac88 A-Konig
                data[5] = data[5].Substring(1, data[5].Length - 2);
100
                data[6] = data[6].Substring(1, data[6].Length - 2);
101
                data[7] = data[7].Substring(1, data[7].Length - 2);
102
                data[8] = data[8].Substring(1, data[8].Length - 2);
103 5d9a5bd9 A-Konig
104
                var dateTime = DateTime.ParseExact(data[0], "G", cultureInfo);
105
                var startTime = DateTime.ParseExact(data[3], "t", cultureInfo);
106
                var endTime = DateTime.ParseExact(data[4], "t", cultureInfo);
107
108
                int count = 0;
109
                int.TryParse(data[1], out count);
110
111
                int lesson = 0;
112
                int.TryParse(data[2], out lesson);
113
114
                LogInInstance inst = new LogInInstance(dateTime, count, lesson, startTime, endTime, data[5], data[6], data[7], data[8]);
115
                list.Add(inst);
116
            }
117
118
            return list;
119
        }
120
121 734533a8 A-Konig
        /// <summary>
122
        /// Load weather data from file
123
        /// </summary>
124
        /// <param name="pathToFile">Path to file</param>
125
        /// <returns></returns>
126 98b568bc A-Konig
        public List<WeatherInstance> LoadWeatherFile(string pathToFile)
127 5d9a5bd9 A-Konig
        {
128
            var lines = LoadCsv(pathToFile);
129 a53b1de8 A-Konig
            if (lines == null)
130
                return null;
131
132 5d9a5bd9 A-Konig
            List<WeatherInstance> list = new List<WeatherInstance>();
133 d39750f3 A-Konig
134
            if (lines == null)
135
                return list;
136
137 5d9a5bd9 A-Konig
            for (int i = 0; i < lines.Length; i++)
138
            {
139
                var data = lines[i].Split(';');
140 d39750f3 A-Konig
                
141
                if (data.Length < 5)
142
                    continue;
143
144 5d9a5bd9 A-Konig
                data[0] = data[0].Substring(1, data[0].Length - 2);
145
                
146
                var dateTime = DateTime.ParseExact(data[0], "G", cultureInfo);
147
                
148
                double temp = 0;
149
                double.TryParse(data[1], out temp);
150
                
151
                double wind = 0;
152
                double.TryParse(data[2], out wind);
153
                
154
                int rain = 0;
155
                int.TryParse(data[3], out rain);
156
                
157
                double lum = 0;
158
                double.TryParse(data[4], out lum);
159
160
                WeatherInstance inst = new WeatherInstance(dateTime, temp, wind, rain, lum);
161
                list.Add(inst);
162
            }
163
164
            return list;
165
        }
166
167
    }
168
}