Projekt

Obecné

Profil

Stáhnout (4.91 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 csv file into array
23
        /// </summary>
24
        /// <param name="name">Path to file</param>
25
        /// <returns>Array with loaded data</returns>
26
        private string[] LoadCsv(string name)
27
        {
28
            if (!File.Exists(name))
29
                return null;
30

    
31
            string[] lines = File.ReadAllLines(name);
32
            return lines;
33
        }
34

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

    
45
            if (lines == null)
46
                return list;
47

    
48
            for (int i = 0; i < lines.Length; i++)
49
            {
50
                var data = lines[i].Split(';');
51

    
52
                if (data.Length < 3)
53
                    continue;
54

    
55
                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
        /// <summary>
71
        /// Load login data from file
72
        /// </summary>
73
        /// <param name="pathToFile">Path to file</param>
74
        /// <returns></returns>
75
        public List<LogInInstance> LoadLoginFile(string pathToFile)
76
        {
77
            var lines = LoadCsv(pathToFile);
78
            List<LogInInstance> list = new List<LogInInstance>();
79

    
80
            if (lines == null)
81
                return list;
82

    
83
            for (int i = 0; i < lines.Length; i++)
84
            {
85
                var data = lines[i].Split(';');
86

    
87
                if (data.Length < 9)
88
                    continue;
89

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

    
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
        /// <summary>
116
        /// Load weather data from file
117
        /// </summary>
118
        /// <param name="pathToFile">Path to file</param>
119
        /// <returns></returns>
120
        public List<WeatherInstance> LoadWeatherFile(string pathToFile)
121
        {
122
            var lines = LoadCsv(pathToFile);
123
            List<WeatherInstance> list = new List<WeatherInstance>();
124

    
125
            if (lines == null)
126
                return list;
127

    
128
            for (int i = 0; i < lines.Length; i++)
129
            {
130
                var data = lines[i].Split(';');
131
                
132
                if (data.Length < 5)
133
                    continue;
134

    
135
                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
}
(1-1/5)