Projekt

Obecné

Profil

Stáhnout (8.75 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
            WeatherDataUsed = new List<string>();
86
            ActivityDataUsed = new List<string>();
87

    
88
            WeatherDataUsed.AddRange(weatherFiles);
89
            ActivityDataUsed.AddRange(jisFiles);
90
            ActivityDataUsed.AddRange(loginFiles);
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
            AttendanceList = MergeAttendance(jisList, loginList);
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

    
117
        /// <summary>
118
        /// Merges two lists into one
119
        /// Adds information from the same time period together, doesn't change the rest
120
        /// </summary>
121
        /// <param name="jisList">List to merge</param>
122
        /// <param name="loginList">List to merge</param>
123
        /// <returns>Merged list</returns>
124
        private List<ActivityInfo> MergeAttendance(List<ActivityInfo> jisList, List<ActivityInfo> loginList)
125
        {
126
            List<ActivityInfo> res = new List<ActivityInfo>();
127

    
128
            if (jisList == null && loginList == null)
129
                return res;
130
            else if (jisList == null)
131
                return loginList;
132
            else if (loginList == null)
133
                return jisList;
134

    
135
            int indexJis = 0, indexLogin = 0;
136
            while (true)
137
            {
138
                if (indexJis >= jisList.Count || indexLogin >= loginList.Count)
139
                    break;
140

    
141
                ActivityInfo jis = jisList[indexJis];
142
                int jisTag = TagInfo.IndexOfBuilding(jis.building);
143
                ActivityInfo login = loginList[indexLogin];
144
                int loginTag = TagInfo.IndexOfBuilding(login.building);
145

    
146
                // if same time times -> add to one info
147
                if (jis.startTime == login.startTime)
148
                {
149
                    // need to have same building tags
150
                    if (jisTag == loginTag)
151
                    {
152
                        jis.amount += login.amount;
153
                        res.Add(jis);
154
                        indexJis++; indexLogin++;
155
                    }
156

    
157
                    else if (jisTag < loginTag)
158
                    {
159
                        indexJis++;
160
                        res.Add(jis);
161
                    }
162

    
163
                    else if (jisTag > loginTag)
164
                    {
165
                        indexLogin++;
166
                        res.Add(login);
167
                    }
168

    
169
                }
170

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

    
178
                // if login time is smaller -> add login and move
179
                else if (login.startTime < jis.startTime)
180
                {
181
                    indexLogin++;
182
                    res.Add(login);
183
                }
184

    
185
            }
186

    
187
            // add all not yet processed
188
            if (indexJis < jisList.Count)
189
                res.AddRange(jisList.GetRange(indexJis, jisList.Count - indexJis));
190
                    
191
                    
192
            if (indexLogin < loginList.Count)
193
                res.AddRange(jisList.GetRange(indexLogin, loginList.Count - indexLogin));
194

    
195
            return res;
196
        }
197

    
198
        /// <summary>
199
        /// Sum all info for separate intervals in the list to the appropriate day
200
        /// </summary>
201
        /// <param name="list">List with activity info, info separated into buildings and intervals</param>
202
        /// <returns></returns>
203
        private List<ActivityInfo> AddListToOne(List<ActivityInfo> list)
204
        {
205
            List<ActivityInfo> a2 = new List<ActivityInfo>();
206
            for (int i = 0; i < list.Count; i+=TagInfo.buildings.Length)
207
            {
208
                int amount = 0;
209
                for (int j = 0; j < TagInfo.buildings.Length; j++)
210
                {
211
                    amount += list[i + j].amount;
212
                }
213
                ActivityInfo info = new ActivityInfo("ALL", amount, list[i].startTime, list[i].intervalLength);
214
                a2.Add(info);
215
            }
216
            return a2;
217
        }
218

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

    
232
            for (int i = 0; i < list.Count; i++)
233
                Console.WriteLine(list[i].ToString());
234
        }
235

    
236
        /// <summary>
237
        /// Debug method - writing lists to console
238
        /// </summary>
239
        private void WriteToConsole(List<WeatherInfo> list)
240
        {
241
            // TODO useless in finished app
242
            if (list == null)
243
            {
244
                Console.WriteLine("Unsuccessful parsing");
245
                return;
246
            }
247
            Console.WriteLine(list.Count);
248

    
249
            for (int i = 0; i < list.Count; i++)
250
                Console.WriteLine(list[i].ToString());
251
        }
252

    
253
    }
254
}
(1-1/6)