Projekt

Obecné

Profil

Stáhnout (8.85 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
        /// <summary>
38
        /// Constructor
39
        /// </summary>
40
        public DataParser(DataDownloader downloader)
41
        {
42
            this.downloader = downloader;
43

    
44
            TagInfo.CreateDictionaries();
45

    
46
            IDataLoader loader = new CsvDataLoader();
47

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

    
53
        /// <summary>
54
        /// Parse data
55
        /// </summary>
56
        /// <param name="interval">Length of an interval</param>
57
        /// <param name="wholeDay">Parse data as one instance per day</param>
58
        /// <param name="endTime">End time of related data</param>
59
        /// <param name="startTime">Start time of related data</param>
60
        override public bool Parse(DateTime startTime, DateTime endTime, int interval = 1, bool wholeDay = true)
61
        {
62
            var cultureInfo = CultureInfo.GetCultureInfo("en-GB");
63
            Thread.CurrentThread.CurrentCulture = cultureInfo;
64
            Thread.CurrentThread.CurrentUICulture = cultureInfo;
65

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

    
71
            // get all files that should be parsed
72
            Date start = new Date((uint)startTime.Month, (uint)startTime.Year);
73
            Date end = new Date((uint)endTime.Month, (uint)endTime.Year);
74
            if (startTime == DateTime.MinValue || endTime == DateTime.MaxValue)
75
            {
76
                start = null;
77
                end = null;
78
            }
79

    
80
            var weatherFiles = downloader.GetData(pathWeather, start, end);
81
            var jisFiles = downloader.GetData(pathJis, start, end);
82
            var loginFiles = downloader.GetData(pathLogIn, start, end);
83

    
84
            WeatherDataUsed = new List<string>();
85
            ActivityDataUsed = new List<string>();
86

    
87
            WeatherDataUsed.AddRange(weatherFiles);
88
            ActivityDataUsed.AddRange(jisFiles);
89
            ActivityDataUsed.AddRange(loginFiles);
90

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

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

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

    
104
            AttendanceList = MergeAttendance(jisList, loginList);
105

    
106
            //Console.WriteLine("MERGED IN ONE LIST");
107
            //WriteToConsole(AttendanceList);
108

    
109
            if (WeatherList.Count == 0 || AttendanceList.Count == 0)
110
                return false;
111

    
112
            return true;
113
        }
114

    
115

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

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

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

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

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

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

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

    
168
                }
169

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

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

    
184
            }
185

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

    
194
            return res;
195
        }
196

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

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

    
231
            for (int i = 0; i < list.Count; i++)
232
            {
233
                if (list[i].startTime.Year >= 2019)
234
                    Console.WriteLine(list[i].ToString());
235
            }
236
        }
237

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

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

    
255
    }
256
}
(1-1/6)