Projekt

Obecné

Profil

Stáhnout (8.5 KB) Statistiky
| Větev: | Tag: | Revize:
1 cdf3c217 A-Konig
//
2
// Author: A. Konig
3
//
4
5
using ServerApp.DataDownload;
6
using ServerApp.Parser.InputData;
7 98b568bc A-Konig
using ServerApp.Parser.OutputInfo;
8 93452c41 A-Konig
using System;
9
using System.Collections.Generic;
10
using System.Globalization;
11
using System.Threading;
12
13 734533a8 A-Konig
namespace ServerApp.Parser.Parsers
14 93452c41 A-Konig
{
15 734533a8 A-Konig
    /// <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 cdf3c217 A-Konig
    /// <author>A. Konig</author>
20 99e5517e A-Konig
    public class DataParser : IDataParser
21 93452c41 A-Konig
    {
22 cdf3c217 A-Konig
        /// <summary> Data downloader </summary>
23
        DataDownloader downloader;
24
25 734533a8 A-Konig
        /// <summary> Weather data parser </summary>
26 93452c41 A-Konig
        WeatherParser weatherParser;
27 734533a8 A-Konig
        /// <summary> Jis data parser </summary>
28 93452c41 A-Konig
        JisParser jisParser;
29 734533a8 A-Konig
        /// <summary> Login data parser </summary>
30 93452c41 A-Konig
        LogInParser loginParser;
31
32 734533a8 A-Konig
        /// <summary> ActivityInfo representing jis activity </summary>
33 d39750f3 A-Konig
        List<ActivityInfo> jisList;
34 734533a8 A-Konig
        /// <summary> ActivityInfo representing login activity</summary>
35 d39750f3 A-Konig
        List<ActivityInfo> loginList;
36 78428231 A-Konig
37 cdf3c217 A-Konig
38 734533a8 A-Konig
        /// <summary>
39
        /// Constructor
40
        /// </summary>
41 cdf3c217 A-Konig
        public DataParser(DataDownloader downloader)
42 93452c41 A-Konig
        {
43 cdf3c217 A-Konig
            this.downloader = downloader;
44
45 93452c41 A-Konig
            TagInfo.CreateDictionaries();
46 734533a8 A-Konig
47 cdf3c217 A-Konig
            IDataLoader loader = new CsvDataLoader();
48 93452c41 A-Konig
49 98b568bc A-Konig
            weatherParser = new WeatherParser(loader);
50
            jisParser = new JisParser(loader);
51
            loginParser = new LogInParser(loader);
52 93452c41 A-Konig
        }
53
54 734533a8 A-Konig
        /// <summary>
55
        /// Parse data
56
        /// </summary>
57
        /// <param name="interval">Length of an interval</param>
58
        /// <param name="wholeDay">Parse data as one instance per day</param>
59 cdf3c217 A-Konig
        /// <param name="endTime">End time of related data</param>
60
        /// <param name="startTime">Start time of related data</param>
61
        override public bool Parse(DateTime startTime, DateTime endTime, int interval = 1, bool wholeDay = true)
62 93452c41 A-Konig
        {
63
            var cultureInfo = CultureInfo.GetCultureInfo("en-GB");
64
            Thread.CurrentThread.CurrentCulture = cultureInfo;
65
            Thread.CurrentThread.CurrentUICulture = cultureInfo;
66
67 cdf3c217 A-Konig
            // get path to folder w/ datafiles
68 9fb55c71 A-Konig
            string pathWeather = downloader.DataSubDirectories[DataType.POCASI];
69
            string pathJis = downloader.DataSubDirectories[DataType.JIS];
70
            string pathLogIn = downloader.DataSubDirectories[DataType.STROJE];
71 cdf3c217 A-Konig
72
            // get all files that should be parsed
73 9fb55c71 A-Konig
            Date start = null;
74
            Date end = null;
75
            if (startTime != null || endTime != null)
76
            {
77
                start = new Date((uint)startTime.Month, (uint)startTime.Year);
78
                end = new Date((uint)endTime.Month, (uint)endTime.Year);
79
            }
80
81 cdf3c217 A-Konig
            var weatherFiles = downloader.GetData(pathWeather, start, end);
82
            var jisFiles = downloader.GetData(pathJis, start, end);
83
            var loginFiles = downloader.GetData(pathLogIn, start, end);
84 93452c41 A-Konig
85 99e5517e A-Konig
            WeatherList = weatherParser.ParseWeatherData(weatherFiles, startTime, endTime, wholeDay, interval);
86 cdf3c217 A-Konig
            jisList = jisParser.ParseJisData(jisFiles, startTime, endTime, wholeDay, interval);
87
            loginList = loginParser.ParseLogInData(loginFiles, startTime, endTime, wholeDay, interval);
88 93452c41 A-Konig
89 260b1217 A-Konig
            //Console.WriteLine("WEATHER");
90
            //WriteToConsole(weatherList);
91 9fb55c71 A-Konig
            //Console.WriteLine("JIS");
92
            //WriteToConsole(jisList);
93 ecdce631 A-Konig
            //Console.WriteLine("LOGIN");
94
            //WriteToConsole(loginList);
95 d39750f3 A-Konig
96
            //WriteToConsole(AddListToOne(jisList));
97
98 99e5517e A-Konig
            AttendanceList = MergeAttendance(jisList, loginList);
99 d39750f3 A-Konig
100 9fb55c71 A-Konig
            //Console.WriteLine("MERGED IN ONE LIST");
101
            //WriteToConsole(attendanceList);
102 d39750f3 A-Konig
103 99e5517e A-Konig
            if (WeatherList.Count == 0 || AttendanceList.Count == 0)
104 d39750f3 A-Konig
                return false;
105
106
            return true;
107
        }
108
109 99e5517e A-Konig
110 d39750f3 A-Konig
        /// <summary>
111 99e5517e A-Konig
        /// Merges two lists into one
112 d39750f3 A-Konig
        /// Adds information from the same time period together, doesn't change the rest
113
        /// </summary>
114 99e5517e A-Konig
        /// <param name="jisList">List to merge</param>
115
        /// <param name="loginList">List to merge</param>
116
        /// <returns>Merged list</returns>
117
        private List<ActivityInfo> MergeAttendance(List<ActivityInfo> jisList, List<ActivityInfo> loginList)
118 d39750f3 A-Konig
        {
119 99e5517e A-Konig
            List<ActivityInfo> res = new List<ActivityInfo>();
120
121
            if (jisList == null && loginList == null)
122
                return res;
123
            else if (jisList == null)
124
                return loginList;
125
            else if (loginList == null)
126
                return jisList;
127 d39750f3 A-Konig
128
            int indexJis = 0, indexLogin = 0;
129
            while (true)
130
            {
131 4cb8ae48 A-Konig
                if (indexJis >= jisList.Count || indexLogin >= loginList.Count)
132 d39750f3 A-Konig
                    break;
133
134
                ActivityInfo jis = jisList[indexJis];
135
                int jisTag = TagInfo.IndexOfBuilding(jis.building);
136
                ActivityInfo login = loginList[indexLogin];
137
                int loginTag = TagInfo.IndexOfBuilding(login.building);
138
139
                // if same time times -> add to one info
140
                if (jis.startTime == login.startTime)
141
                {
142
                    // need to have same building tags
143
                    if (jisTag == loginTag)
144
                    {
145
                        jis.amount += login.amount;
146 99e5517e A-Konig
                        res.Add(jis);
147 d39750f3 A-Konig
                        indexJis++; indexLogin++;
148
                    }
149
150 99e5517e A-Konig
                    else if (jisTag < loginTag)
151 d39750f3 A-Konig
                    {
152
                        indexJis++;
153 99e5517e A-Konig
                        res.Add(jis);
154 d39750f3 A-Konig
                    }
155
156 99e5517e A-Konig
                    else if (jisTag > loginTag)
157 d39750f3 A-Konig
                    {
158
                        indexLogin++;
159 99e5517e A-Konig
                        res.Add(login);
160 d39750f3 A-Konig
                    }
161
162
                }
163
164
                // if jis time is smaller -> add jis and move
165 99e5517e A-Konig
                else if (jis.startTime < login.startTime)
166 d39750f3 A-Konig
                {
167
                    indexJis++;
168 99e5517e A-Konig
                    res.Add(jis);
169 d39750f3 A-Konig
                }
170
171
                // if login time is smaller -> add login and move
172 99e5517e A-Konig
                else if (login.startTime < jis.startTime)
173 d39750f3 A-Konig
                {
174
                    indexLogin++;
175 99e5517e A-Konig
                    res.Add(login);
176 d39750f3 A-Konig
                }
177
178
            }
179
180 4cb8ae48 A-Konig
            // add all not yet processed
181
            if (indexJis < jisList.Count)
182
                res.AddRange(jisList.GetRange(indexJis, jisList.Count - indexJis));
183
                    
184
                    
185
            if (indexLogin < loginList.Count)
186
                res.AddRange(jisList.GetRange(indexLogin, loginList.Count - indexLogin));
187
188 99e5517e A-Konig
            return res;
189 d39750f3 A-Konig
        }
190
191
        /// <summary>
192
        /// Sum all info for separate intervals in the list to the appropriate day
193
        /// </summary>
194
        /// <param name="list">List with activity info, info separated into buildings and intervals</param>
195
        /// <returns></returns>
196
        private List<ActivityInfo> AddListToOne(List<ActivityInfo> list)
197
        {
198
            List<ActivityInfo> a2 = new List<ActivityInfo>();
199
            for (int i = 0; i < list.Count; i+=TagInfo.buildings.Length)
200
            {
201
                int amount = 0;
202
                for (int j = 0; j < TagInfo.buildings.Length; j++)
203
                {
204
                    amount += list[i + j].amount;
205
                }
206
                ActivityInfo info = new ActivityInfo("ALL", amount, list[i].startTime, list[i].intervalLength);
207
                a2.Add(info);
208
            }
209
            return a2;
210
        }
211
212
        /// <summary>
213
        /// Debug method - writing lists to console
214
        /// </summary>
215
        private void WriteToConsole(List<ActivityInfo> list)
216
        {
217
            // TODO useless in finished app
218
            if (list == null)
219
            {
220
                Console.WriteLine("Unsuccessful parsing");
221
                return;
222
            }
223
            Console.WriteLine(list.Count);
224
225
            for (int i = 0; i < list.Count; i++)
226
                Console.WriteLine(list[i].ToString());
227 93452c41 A-Konig
        }
228
229 734533a8 A-Konig
        /// <summary>
230
        /// Debug method - writing lists to console
231
        /// </summary>
232 d39750f3 A-Konig
        private void WriteToConsole(List<WeatherInfo> list)
233 93452c41 A-Konig
        {
234 734533a8 A-Konig
            // TODO useless in finished app
235 93452c41 A-Konig
            if (list == null)
236
            {
237
                Console.WriteLine("Unsuccessful parsing");
238
                return;
239
            }
240
            Console.WriteLine(list.Count);
241
242
            for (int i = 0; i < list.Count; i++)
243
                Console.WriteLine(list[i].ToString());
244
        }
245
246
    }
247
}