Projekt

Obecné

Profil

Stáhnout (8.06 KB) Statistiky
| Větev: | Tag: | Revize:
1
//
2
// Author: A. Konig
3
//
4

    
5
using ServerApp.DataDownload;
6
using ServerApp.Parser.InputData;
7
using ServerApp.Parser.OutputInfo;
8
using System;
9
using System.Collections.Generic;
10
using System.Globalization;
11
using System.Threading;
12

    
13
namespace ServerApp.Parser.Parsers
14
{
15
    /// <summary>
16
    /// Class parsing data files into instances of ActivityInfo and WeatherInfo divided by given time interval
17
    /// Data parsed from 7am (included) to 18pm (included)
18
    /// </summary>
19
    /// <author>A. Konig</author>
20
    class DataParser : IDataParser
21
    {
22
        /// <summary> Data downloader </summary>
23
        DataDownloader downloader;
24

    
25
        /// <summary> Weather data parser </summary>
26
        WeatherParser weatherParser;
27
        /// <summary> Jis data parser </summary>
28
        JisParser jisParser;
29
        /// <summary> Login data parser </summary>
30
        LogInParser loginParser;
31

    
32
        /// <summary> WeatherInfo </summary>
33
        List<WeatherInfo> weatherList;
34
        public new List<WeatherInfo> WeatherList { get => weatherList; }
35
        /// <summary> ActivityInfo repersenting overall activity </summary>
36
        List<ActivityInfo> attendanceList;
37
        public new List<ActivityInfo> AttendanceList { get => attendanceList; }
38

    
39
        /// <summary> ActivityInfo representing jis activity </summary>
40
        List<ActivityInfo> jisList;
41
        /// <summary> ActivityInfo representing login activity</summary>
42
        List<ActivityInfo> loginList;
43

    
44

    
45
        /// <summary>
46
        /// Constructor
47
        /// </summary>
48
        public DataParser(DataDownloader downloader)
49
        {
50
            this.downloader = downloader;
51

    
52
            TagInfo.CreateDictionaries();
53

    
54
            IDataLoader loader = new CsvDataLoader();
55

    
56
            weatherParser = new WeatherParser(loader);
57
            jisParser = new JisParser(loader);
58
            loginParser = new LogInParser(loader);
59
        }
60

    
61
        /// <summary>
62
        /// Parse data
63
        /// </summary>
64
        /// <param name="interval">Length of an interval</param>
65
        /// <param name="wholeDay">Parse data as one instance per day</param>
66
        /// <param name="endTime">End time of related data</param>
67
        /// <param name="startTime">Start time of related data</param>
68
        override public bool Parse(DateTime startTime, DateTime endTime, int interval = 1, bool wholeDay = true)
69
        {
70
            var cultureInfo = CultureInfo.GetCultureInfo("en-GB");
71
            Thread.CurrentThread.CurrentCulture = cultureInfo;
72
            Thread.CurrentThread.CurrentUICulture = cultureInfo;
73

    
74
            // get path to folder w/ datafiles
75
            string pathWeather = downloader.DataSubDirectories[DataType.POCASI];
76
            string pathJis = downloader.DataSubDirectories[DataType.JIS];
77
            string pathLogIn = downloader.DataSubDirectories[DataType.STROJE];
78

    
79
            // get all files that should be parsed
80
            Date start = null;
81
            Date end = null;
82
            if (startTime != null || endTime != null)
83
            {
84
                start = new Date((uint)startTime.Month, (uint)startTime.Year);
85
                end = new Date((uint)endTime.Month, (uint)endTime.Year);
86
            }
87

    
88
            var weatherFiles = downloader.GetData(pathWeather, start, end);
89
            var jisFiles = downloader.GetData(pathJis, start, end);
90
            var loginFiles = downloader.GetData(pathLogIn, start, end);
91

    
92
            weatherList = weatherParser.ParseWeatherData(weatherFiles, startTime, endTime, wholeDay, interval);
93
            jisList = jisParser.ParseJisData(jisFiles, startTime, endTime, wholeDay, interval);
94
            loginList = loginParser.ParseLogInData(loginFiles, startTime, endTime, wholeDay, interval);
95

    
96
            //Console.WriteLine("WEATHER");
97
            //WriteToConsole(weatherList);
98
            //Console.WriteLine("JIS");
99
            //WriteToConsole(jisList);
100
            //Console.WriteLine("LOGIN");
101
            //WriteToConsole(loginList);
102

    
103
            //WriteToConsole(AddListToOne(jisList));
104

    
105
            MergeAttendance();
106

    
107
            //Console.WriteLine("MERGED IN ONE LIST");
108
            //WriteToConsole(attendanceList);
109

    
110
            if (weatherList.Count == 0 || attendanceList.Count == 0)
111
                return false;
112

    
113
            return true;
114
        }
115

    
116
        /// <summary>
117
        /// Merges ActivityInfo lists with jis and login activity into one
118
        /// Adds information from the same time period together, doesn't change the rest
119
        /// </summary>
120
        private void MergeAttendance()
121
        {
122
            attendanceList = new List<ActivityInfo>();
123

    
124
            int indexJis = 0, indexLogin = 0;
125
            while (true)
126
            {
127
                if (indexJis >= jisList.Count && indexLogin >= loginList.Count)
128
                    break;
129

    
130
                ActivityInfo jis = jisList[indexJis];
131
                int jisTag = TagInfo.IndexOfBuilding(jis.building);
132
                ActivityInfo login = loginList[indexLogin];
133
                int loginTag = TagInfo.IndexOfBuilding(login.building);
134

    
135
                // if same time times -> add to one info
136
                if (jis.startTime == login.startTime)
137
                {
138
                    // need to have same building tags
139
                    if (jisTag == loginTag)
140
                    {
141
                        jis.amount += login.amount;
142
                        attendanceList.Add(jis);
143
                        indexJis++; indexLogin++;
144
                    }
145

    
146
                    if (jisTag < loginTag)
147
                    {
148
                        indexJis++;
149
                        attendanceList.Add(jis);
150
                    }
151

    
152
                    if (jisTag > loginTag)
153
                    {
154
                        indexLogin++;
155
                        attendanceList.Add(login);
156
                    }
157

    
158
                }
159

    
160
                // if jis time is smaller -> add jis and move
161
                if (jis.startTime < login.startTime)
162
                {
163
                    indexJis++;
164
                    attendanceList.Add(jis);
165
                }
166

    
167
                // if login time is smaller -> add login and move
168
                if (login.startTime < jis.startTime)
169
                {
170
                    indexLogin++;
171
                    attendanceList.Add(login);
172
                }
173

    
174
            }
175

    
176
        }
177

    
178
        /// <summary>
179
        /// Sum all info for separate intervals in the list to the appropriate day
180
        /// </summary>
181
        /// <param name="list">List with activity info, info separated into buildings and intervals</param>
182
        /// <returns></returns>
183
        private List<ActivityInfo> AddListToOne(List<ActivityInfo> list)
184
        {
185
            List<ActivityInfo> a2 = new List<ActivityInfo>();
186
            for (int i = 0; i < list.Count; i+=TagInfo.buildings.Length)
187
            {
188
                int amount = 0;
189
                for (int j = 0; j < TagInfo.buildings.Length; j++)
190
                {
191
                    amount += list[i + j].amount;
192
                }
193
                ActivityInfo info = new ActivityInfo("ALL", amount, list[i].startTime, list[i].intervalLength);
194
                a2.Add(info);
195
            }
196
            return a2;
197
        }
198

    
199
        /// <summary>
200
        /// Debug method - writing lists to console
201
        /// </summary>
202
        private void WriteToConsole(List<ActivityInfo> list)
203
        {
204
            // TODO useless in finished app
205
            if (list == null)
206
            {
207
                Console.WriteLine("Unsuccessful parsing");
208
                return;
209
            }
210
            Console.WriteLine(list.Count);
211

    
212
            for (int i = 0; i < list.Count; i++)
213
                Console.WriteLine(list[i].ToString());
214
        }
215

    
216
        /// <summary>
217
        /// Debug method - writing lists to console
218
        /// </summary>
219
        private void WriteToConsole(List<WeatherInfo> list)
220
        {
221
            // TODO useless in finished app
222
            if (list == null)
223
            {
224
                Console.WriteLine("Unsuccessful parsing");
225
                return;
226
            }
227
            Console.WriteLine(list.Count);
228

    
229
            for (int i = 0; i < list.Count; i++)
230
                Console.WriteLine(list[i].ToString());
231
        }
232

    
233
    }
234
}
(1-1/6)