Projekt

Obecné

Profil

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