Skip to content
Newer
Older
100755 231 lines (179 sloc) 6.23 KB
9a780fa Adding licenses to all source files and adding COPYING/LICENSE discla…
cpscotti authored
1 /*
2 * Copyright (c) 2011 Nokia Corporation
3 *
4 * This file is part of the Push Snowboarding Project, More info at:
5 * www.pushsnowboading.com
6 *
7 * Author: Clovis Scotti <scotti@ieee.org>
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 *
26 */
27
dfaf02e (cpscott) "Importing" PushBurton2
cpscotti authored
28 #include "npushreportairtime.h"
29
30 NPushReportAirTime::NPushReportAirTime()
31 {
32 running = false;
33 n_of_airs = 0;
34 }
35
36 NPushReportAirTime::~NPushReportAirTime()
37 {
38 dataAccessMutex.lock();//locks forever so noone "tryes" to go into dead data
39 }
40
41
42 void NPushReportAirTime::append_air(quint64 _air_time, quint64 _tstamp)
43 {
44 QMutexLocker locker(&dataAccessMutex);
45 if(!running)
46 return;
47
48 double dairTime = _air_time/1000.0;
49 double dtstamp = _tstamp/1000.0;
50
51 if(dairTime > longest_air_time)
52 longest_air_time = dairTime;
53
54 total_air_time += dairTime;
55
56 air_times.push_back(dairTime);
57 tstamps.push_back(dtstamp);
58 n_of_airs++;
59 }
60
61 bool NPushReportAirTime::start_run()
62 {
63 QMutexLocker locker(&dataAccessMutex);
64
65 if(running)
66 return false;
67
68 running = true;
69 longest_air_time = 0;
70 total_air_time = 0;
71 n_of_airs = 0;
72
73 air_times.clear();
74 tstamps.clear();
75
76 return true;
77 }
78
79 bool NPushReportAirTime::end_run()
80 {
81 QMutexLocker locker(&dataAccessMutex);
82
83 if(!running)
84 return false;
85
86 running = false;
87 return true;
88 }
89
90
91 bool NPushReportAirTime::save_to_dir(QString& dirName)
92 {
aa93184 Adding ClassDiagram.txt
cpscotti authored
93 QString reportFilename = (dirName + FSC_FSYS_SLASH) + "AirTime.xml";
dfaf02e (cpscott) "Importing" PushBurton2
cpscotti authored
94 QFile reportFile(reportFilename);
95
5bfc3a4 Just adding slash/FS conventions for Linux
cpscotti authored
96 qDebug() << "reportFilename = " << reportFilename;
97 qDebug() << "slash is = " << FSC_FSYS_SLASH;
98
dfaf02e (cpscott) "Importing" PushBurton2
cpscotti authored
99 reportFile.open(QFile::WriteOnly | QFile::Text);
100
101 QXmlStreamWriter xml;
102
103 xml.setDevice(&reportFile);
104 xml.setAutoFormatting(true);
105
106 xml.writeStartDocument();
107
108 dataAccessMutex.lock();
109
110
111 xml.writeStartElement("AirTimeReport");
112
113 xml.writeStartElement("stats");
114
115 xml.writeAttribute("n_of_airs", QString::number(air_times.count()));
116 xml.writeAttribute("biggest_air", QString::number(longest_air_time, 'f', 3));
117 xml.writeAttribute("total_air", QString::number(total_air_time, 'f', 3));
118 xml.writeEndElement();//stats
119
120 xml.writeStartElement("watch");
121 xml.writeAttribute("run_start", QString::number(tRunStart, 'f', 3));
122 xml.writeAttribute("run_end", QString::number(tRunEnd, 'f', 3));
123 xml.writeEndElement();
124
125 if(air_times.count() != tstamps.count()) {
126 xml.writeComment("Corrupted air_times vs tstamps");
127 } else {
128
129 xml.writeStartElement("bar_graph");
130 QList<double>::iterator it;
131 QList<double>::iterator tsit;
132
133 tsit = tstamps.begin();
134 for(it = air_times.begin();it != air_times.end(); ++it) {
135 xml.writeStartElement("air");
136
137 xml.writeAttribute("air_time", QString::number(*it, 'f', 3));
138 xml.writeAttribute("tstamp", QString::number(*tsit, 'f', 3));
139
140 xml.writeEndElement();
141 ++tsit;
142 if(tsit == tstamps.end())
143 break;
144 }
145
146 xml.writeEndElement();//bar_graph
147
148 }
149
150 xml.writeEndElement();//AirTimeReport
151 dataAccessMutex.unlock();
152
153 xml.writeEndDocument();
154
155 reportFile.close();
9cb7e05 Adding (UGLY) confirmation for delete button and fixed many pending w…
Clovis Scotti authored
156 return true;
dfaf02e (cpscott) "Importing" PushBurton2
cpscotti authored
157 }
158
159 bool NPushReportAirTime::load_from_dir(QString& dirName)
160 {
aa93184 Adding ClassDiagram.txt
cpscotti authored
161
162 QString data_in_filename = (dirName + FSC_FSYS_SLASH) + "AirTime.xml";
163
dfaf02e (cpscott) "Importing" PushBurton2
cpscotti authored
164 QFile data_input(data_in_filename);
165 data_input.open(QFile::ReadOnly | QFile::Text);
166
167 QXmlStreamReader xml;
168 xml.setDevice(&data_input);
169
170 if(xml.readNextStartElement())
171 {
172 if(xml.name() != "AirTimeReport") {
173 qDebug() << "Invalid file format";
174 return false;
175 }
176 } else {
177 qDebug() << "Could not read first start Element";
178 return false;
179 }
180
181 do
182 {
183 while(xml.readNextStartElement())
184 {
185 if(xml.name() == "stats")
186 {
187 n_of_airs = xml.attributes().value("n_of_airs").toString().toInt();
188 longest_air_time = xml.attributes().value("biggest_air").toString().toDouble();
189 total_air_time = xml.attributes().value("total_air").toString().toDouble();
190 } else if(xml.name() == "bar_graph") {
191 read_bar_graph_from_xml(xml);
192 } else if(xml.name() == "watch") {
193 tRunStart = xml.attributes().value("run_start").toString().toDouble();
194 tRunEnd = xml.attributes().value("run_end").toString().toDouble();
195 } else {
196 xml.skipCurrentElement();
197 }
198 }
199 } while(xml.name() != "AirTimeReport");
9cb7e05 Adding (UGLY) confirmation for delete button and fixed many pending w…
Clovis Scotti authored
200 return true;
dfaf02e (cpscott) "Importing" PushBurton2
cpscotti authored
201 }
202
203 void NPushReportAirTime::read_bar_graph_from_xml(QXmlStreamReader& xml)
204 {
205 air_times.clear();
206 tstamps.clear();
207
208 do
209 {
210 while(xml.readNextStartElement()) {
211 if(xml.name() == "air") {
212 air_times.push_back(xml.attributes().value("air_time").toString().toDouble());
213 tstamps.push_back(xml.attributes().value("tstamp").toString().toDouble());
214 } else {
215 xml.skipCurrentElement();
216 }
217 }
218 } while(xml.name() != "bar_graph");
219 }
220
221
222 void NPushReportAirTime::set_start_tstamp(double t)
223 {
224 tRunStart = t;
225 }
226
227 void NPushReportAirTime::set_end_tstamp(double t)
228 {
229 tRunEnd = t;
230 }
Something went wrong with that request. Please try again.