Projekt

Obecné

Profil

Stáhnout (4.44 KB) Statistiky
| Větev: | Tag: | Revize:
1 5d9a5bd9 A-Konig
using System;
2
using System.Collections.Generic;
3
using System.Globalization;
4
using System.IO;
5
6 734533a8 A-Konig
namespace ServerApp.Parser.InputData
7 5d9a5bd9 A-Konig
{
8 734533a8 A-Konig
    /// <summary>
9
    /// Class responsible for loading data from file
10
    /// </summary>
11 98b568bc A-Konig
    /// <author>Alex Konig</author>
12
    class CsvDataLoader : DataLoader
13 5d9a5bd9 A-Konig
    {
14 734533a8 A-Konig
        /// <summary> Culture info for parsing numbers </summary>
15 5d9a5bd9 A-Konig
        static CultureInfo cultureInfo = new CultureInfo("de-DE");
16
17 734533a8 A-Konig
        /// <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 98b568bc A-Konig
        private string[] LoadCsv(string name)
23 5d9a5bd9 A-Konig
        {
24
            string[] lines = File.ReadAllLines(name);
25
            return lines;
26
        }
27
28 734533a8 A-Konig
        /// <summary>
29
        /// Load jis data from file
30
        /// </summary>
31
        /// <param name="pathToFile">Path to file</param>
32
        /// <returns></returns>
33 98b568bc A-Konig
        public List<JisInstance> LoadJisFile(string pathToFile)
34 5d9a5bd9 A-Konig
        {
35
            var lines = LoadCsv(pathToFile);
36
            List<JisInstance> list = new List<JisInstance>();
37
38
            for (int i = 0; i < lines.Length; i++)
39
            {
40
                var data = lines[i].Split(';');
41
                data[0] = data[0].Substring(1, data[0].Length - 2);
42
                data[1] = data[1].Substring(1, data[1].Length - 2);
43
                
44
                var dateTime = DateTime.ParseExact(data[1], "G", cultureInfo);
45
46
                int amount = 0;
47
                int.TryParse(data[2], out amount);
48
49
                JisInstance inst = new JisInstance(data[0], dateTime, amount);
50
                list.Add(inst);
51
            }
52
53
            return list;
54
        }
55
56 734533a8 A-Konig
        /// <summary>
57
        /// Load login data from file
58
        /// </summary>
59
        /// <param name="pathToFile">Path to file</param>
60
        /// <returns></returns>
61 98b568bc A-Konig
        public List<LogInInstance> LoadLoginFile(string pathToFile)
62 5d9a5bd9 A-Konig
        {
63
            var lines = LoadCsv(pathToFile);
64
            List<LogInInstance> list = new List<LogInInstance>();
65
66
            for (int i = 0; i < lines.Length; i++)
67
            {
68
                var data = lines[i].Split(';');
69
                data[0] = data[0].Substring(1, data[0].Length - 2);
70
                data[3] = data[3].Substring(1, data[3].Length - 2);
71
                data[4] = data[4].Substring(1, data[4].Length - 2);
72 0da0ac88 A-Konig
                data[5] = data[5].Substring(1, data[5].Length - 2);
73
                data[6] = data[6].Substring(1, data[6].Length - 2);
74
                data[7] = data[7].Substring(1, data[7].Length - 2);
75
                data[8] = data[8].Substring(1, data[8].Length - 2);
76 5d9a5bd9 A-Konig
77
                var dateTime = DateTime.ParseExact(data[0], "G", cultureInfo);
78
                var startTime = DateTime.ParseExact(data[3], "t", cultureInfo);
79
                var endTime = DateTime.ParseExact(data[4], "t", cultureInfo);
80
81
                int count = 0;
82
                int.TryParse(data[1], out count);
83
84
                int lesson = 0;
85
                int.TryParse(data[2], out lesson);
86
87
                LogInInstance inst = new LogInInstance(dateTime, count, lesson, startTime, endTime, data[5], data[6], data[7], data[8]);
88
                list.Add(inst);
89
            }
90
91
            return list;
92
        }
93
94 734533a8 A-Konig
        /// <summary>
95
        /// Load weather data from file
96
        /// </summary>
97
        /// <param name="pathToFile">Path to file</param>
98
        /// <returns></returns>
99 98b568bc A-Konig
        public List<WeatherInstance> LoadWeatherFile(string pathToFile)
100 5d9a5bd9 A-Konig
        {
101
            var lines = LoadCsv(pathToFile);
102
            
103
            List<WeatherInstance> list = new List<WeatherInstance>();
104
            for (int i = 0; i < lines.Length; i++)
105
            {
106
                var data = lines[i].Split(';');
107
                data[0] = data[0].Substring(1, data[0].Length - 2);
108
                
109
                var dateTime = DateTime.ParseExact(data[0], "G", cultureInfo);
110
                
111
                double temp = 0;
112
                double.TryParse(data[1], out temp);
113
                
114
                double wind = 0;
115
                double.TryParse(data[2], out wind);
116
                
117
                int rain = 0;
118
                int.TryParse(data[3], out rain);
119
                
120
                double lum = 0;
121
                double.TryParse(data[4], out lum);
122
123
                WeatherInstance inst = new WeatherInstance(dateTime, temp, wind, rain, lum);
124
                list.Add(inst);
125
            }
126
127
            return list;
128
        }
129
130
    }
131
}