Projekt

Obecné

Profil

Stáhnout (8.5 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
    public 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> ActivityInfo representing jis activity </summary>
33
        List<ActivityInfo> jisList;
34
        /// <summary> ActivityInfo representing login activity</summary>
35
        List<ActivityInfo> loginList;
36

    
37

    
38
        /// <summary>
39
        /// Constructor
40
        /// </summary>
41
        public DataParser(DataDownloader downloader)
42
        {
43
            this.downloader = downloader;
44

    
45
            TagInfo.CreateDictionaries();
46

    
47
            IDataLoader loader = new CsvDataLoader();
48

    
49
            weatherParser = new WeatherParser(loader);
50
            jisParser = new JisParser(loader);
51
            loginParser = new LogInParser(loader);
52
        }
53

    
54
        /// <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
        /// <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
        {
63
            var cultureInfo = CultureInfo.GetCultureInfo("en-GB");
64
            Thread.CurrentThread.CurrentCulture = cultureInfo;
65
            Thread.CurrentThread.CurrentUICulture = cultureInfo;
66

    
67
            // get path to folder w/ datafiles
68
            string pathWeather = downloader.DataSubDirectories[DataType.POCASI];
69
            string pathJis = downloader.DataSubDirectories[DataType.JIS];
70
            string pathLogIn = downloader.DataSubDirectories[DataType.STROJE];
71

    
72
            // get all files that should be parsed
73
            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
            var weatherFiles = downloader.GetData(pathWeather, start, end);
82
            var jisFiles = downloader.GetData(pathJis, start, end);
83
            var loginFiles = downloader.GetData(pathLogIn, start, end);
84

    
85
            WeatherList = weatherParser.ParseWeatherData(weatherFiles, startTime, endTime, wholeDay, interval);
86
            jisList = jisParser.ParseJisData(jisFiles, startTime, endTime, wholeDay, interval);
87
            loginList = loginParser.ParseLogInData(loginFiles, startTime, endTime, wholeDay, interval);
88

    
89
            //Console.WriteLine("WEATHER");
90
            //WriteToConsole(weatherList);
91
            //Console.WriteLine("JIS");
92
            //WriteToConsole(jisList);
93
            //Console.WriteLine("LOGIN");
94
            //WriteToConsole(loginList);
95

    
96
            //WriteToConsole(AddListToOne(jisList));
97

    
98
            AttendanceList = MergeAttendance(jisList, loginList);
99

    
100
            //Console.WriteLine("MERGED IN ONE LIST");
101
            //WriteToConsole(attendanceList);
102

    
103
            if (WeatherList.Count == 0 || AttendanceList.Count == 0)
104
                return false;
105

    
106
            return true;
107
        }
108

    
109

    
110
        /// <summary>
111
        /// Merges two lists into one
112
        /// Adds information from the same time period together, doesn't change the rest
113
        /// </summary>
114
        /// <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
        {
119
            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

    
128
            int indexJis = 0, indexLogin = 0;
129
            while (true)
130
            {
131
                if (indexJis >= jisList.Count || indexLogin >= loginList.Count)
132
                    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
                        res.Add(jis);
147
                        indexJis++; indexLogin++;
148
                    }
149

    
150
                    else if (jisTag < loginTag)
151
                    {
152
                        indexJis++;
153
                        res.Add(jis);
154
                    }
155

    
156
                    else if (jisTag > loginTag)
157
                    {
158
                        indexLogin++;
159
                        res.Add(login);
160
                    }
161

    
162
                }
163

    
164
                // if jis time is smaller -> add jis and move
165
                else if (jis.startTime < login.startTime)
166
                {
167
                    indexJis++;
168
                    res.Add(jis);
169
                }
170

    
171
                // if login time is smaller -> add login and move
172
                else if (login.startTime < jis.startTime)
173
                {
174
                    indexLogin++;
175
                    res.Add(login);
176
                }
177

    
178
            }
179

    
180
            // 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
            return res;
189
        }
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
        }
228

    
229
        /// <summary>
230
        /// Debug method - writing lists to console
231
        /// </summary>
232
        private void WriteToConsole(List<WeatherInfo> list)
233
        {
234
            // TODO useless in finished app
235
            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
}
(1-1/6)