Projekt

Obecné

Profil

Stáhnout (4.89 KB) Statistiky
| Větev: | Tag: | Revize:
1
using System;
2
using System.Collections.Generic;
3
using System.Globalization;
4
using System.IO;
5

    
6
namespace ServerApp.Parser.InputData
7
{
8
    /// <summary>
9
    /// Class responsible for loading data from file
10
    /// </summary>
11
    /// <author>Alex Konig</author>
12
    class CsvDataLoader : DataLoader
13
    {
14
        /// <summary> Culture info for parsing numbers </summary>
15
        static CultureInfo cultureInfo = new CultureInfo("de-DE");
16

    
17
        /// <summary>
18
        /// Load csv file into array
19
        /// </summary>
20
        /// <param name="name">Path to file</param>
21
        /// <returns>Array with loaded data</returns>
22
        private string[] LoadCsv(string name)
23
        {
24
            if (!File.Exists(name))
25
                return null;
26

    
27
            string[] lines = File.ReadAllLines(name);
28
            return lines;
29
        }
30

    
31
        /// <summary>
32
        /// Load jis data from file
33
        /// </summary>
34
        /// <param name="pathToFile">Path to file</param>
35
        /// <returns></returns>
36
        public List<JisInstance> LoadJisFile(string pathToFile)
37
        {
38
            var lines = LoadCsv(pathToFile);
39
            List<JisInstance> list = new List<JisInstance>();
40

    
41
            if (lines == null)
42
                return list;
43

    
44
            for (int i = 0; i < lines.Length; i++)
45
            {
46
                var data = lines[i].Split(';');
47

    
48
                if (data.Length < 3)
49
                    continue;
50

    
51
                data[0] = data[0].Substring(1, data[0].Length - 2);
52
                data[1] = data[1].Substring(1, data[1].Length - 2);
53
                
54
                var dateTime = DateTime.ParseExact(data[1], "G", cultureInfo);
55

    
56
                int amount = 0;
57
                int.TryParse(data[2], out amount);
58

    
59
                JisInstance inst = new JisInstance(data[0], dateTime, amount);
60
                list.Add(inst);
61
            }
62

    
63
            return list;
64
        }
65

    
66
        /// <summary>
67
        /// Load login data from file
68
        /// </summary>
69
        /// <param name="pathToFile">Path to file</param>
70
        /// <returns></returns>
71
        public List<LogInInstance> LoadLoginFile(string pathToFile)
72
        {
73
            var lines = LoadCsv(pathToFile);
74
            List<LogInInstance> list = new List<LogInInstance>();
75

    
76
            if (lines == null)
77
                return list;
78

    
79
            for (int i = 0; i < lines.Length; i++)
80
            {
81
                var data = lines[i].Split(';');
82

    
83
                if (data.Length < 9)
84
                    continue;
85

    
86
                data[0] = data[0].Substring(1, data[0].Length - 2);
87
                data[3] = data[3].Substring(1, data[3].Length - 2);
88
                data[4] = data[4].Substring(1, data[4].Length - 2);
89
                data[5] = data[5].Substring(1, data[5].Length - 2);
90
                data[6] = data[6].Substring(1, data[6].Length - 2);
91
                data[7] = data[7].Substring(1, data[7].Length - 2);
92
                data[8] = data[8].Substring(1, data[8].Length - 2);
93

    
94
                var dateTime = DateTime.ParseExact(data[0], "G", cultureInfo);
95
                var startTime = DateTime.ParseExact(data[3], "t", cultureInfo);
96
                var endTime = DateTime.ParseExact(data[4], "t", cultureInfo);
97

    
98
                int count = 0;
99
                int.TryParse(data[1], out count);
100

    
101
                int lesson = 0;
102
                int.TryParse(data[2], out lesson);
103

    
104
                LogInInstance inst = new LogInInstance(dateTime, count, lesson, startTime, endTime, data[5], data[6], data[7], data[8]);
105
                list.Add(inst);
106
            }
107

    
108
            return list;
109
        }
110

    
111
        /// <summary>
112
        /// Load weather data from file
113
        /// </summary>
114
        /// <param name="pathToFile">Path to file</param>
115
        /// <returns></returns>
116
        public List<WeatherInstance> LoadWeatherFile(string pathToFile)
117
        {
118
            var lines = LoadCsv(pathToFile);
119
            List<WeatherInstance> list = new List<WeatherInstance>();
120

    
121
            if (lines == null)
122
                return list;
123

    
124
            for (int i = 0; i < lines.Length; i++)
125
            {
126
                var data = lines[i].Split(';');
127
                
128
                if (data.Length < 5)
129
                    continue;
130

    
131
                data[0] = data[0].Substring(1, data[0].Length - 2);
132
                
133
                var dateTime = DateTime.ParseExact(data[0], "G", cultureInfo);
134
                
135
                double temp = 0;
136
                double.TryParse(data[1], out temp);
137
                
138
                double wind = 0;
139
                double.TryParse(data[2], out wind);
140
                
141
                int rain = 0;
142
                int.TryParse(data[3], out rain);
143
                
144
                double lum = 0;
145
                double.TryParse(data[4], out lum);
146

    
147
                WeatherInstance inst = new WeatherInstance(dateTime, temp, wind, rain, lum);
148
                list.Add(inst);
149
            }
150

    
151
            return list;
152
        }
153

    
154
    }
155
}
(1-1/5)