forked from Serial-Studio/Serial-Studio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataProvider.cpp
More file actions
179 lines (155 loc) · 5.01 KB
/
DataProvider.cpp
File metadata and controls
179 lines (155 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Copyright (c) 2020-2021 Alex Spataru <https://github.com/alex-spataru>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "Group.h"
#include "Logger.h"
#include "Dataset.h"
#include "CsvPlayer.h"
#include "DataProvider.h"
#include "JsonGenerator.h"
#include "SerialManager.h"
#include <QJsonArray>
#include <QJsonObject>
/*
* Only instance of the class
*/
static DataProvider *INSTANCE = nullptr;
/**
* Constructor of the class
*/
DataProvider::DataProvider()
{
auto cp = CsvPlayer::getInstance();
auto jp = JsonGenerator::getInstance();
auto sm = SerialManager::getInstance();
connect(cp, SIGNAL(openChanged()), this, SLOT(resetData()));
connect(jp, SIGNAL(packetReceived()), this, SLOT(update()));
connect(sm, SIGNAL(connectedChanged()), this, SLOT(resetData()));
LOG_INFO() << "Initialized QML Bridge module";
}
/**
* Returns the only instance of the class
*/
DataProvider *DataProvider::getInstance()
{
if (!INSTANCE)
INSTANCE = new DataProvider();
return INSTANCE;
}
/**
* @return The title of the project/frame sent by the serial device
*/
QString DataProvider::projectTitle() const
{
return m_title;
}
/**
* @return The number of groups contained in the last frame.
*/
int DataProvider::groupCount() const
{
return groups().count();
}
/**
* @return A list with all the @c Group objects generated
* after reading the last received frame.
*/
QList<Group *> DataProvider::groups() const
{
return m_groups;
}
/**
* Returns a pointer to the group object registered with the given @a index.
* If @a index is invalid, then @c Q_NULLPTR is returned.
*/
Group *DataProvider::getGroup(const int index)
{
if (index < groupCount() && index >= 0)
return groups().at(index);
return Q_NULLPTR;
}
/**
* Extracts project title & groups from the latest JSON frame interpretted
* by the @a JsonParser class.
*
* Afterwards, the function shall generate a list with @c Group objects from
* the data. Each @c Group object shall generate a list with the @c Dataset
* object asociated with the group.
*
* @note The function shall only re-generate @c Group & @c Dataset objects if
* the current JSON data frame is valid.
*/
void DataProvider::update()
{
// Get JSON document
auto document = JsonGenerator::getInstance()->document();
// Validate JSON document
if (!document.isEmpty())
{
// Get JSON object information
auto object = document.object();
auto title = object.value("t").toString();
auto groups = object.value("g").toArray();
// We need to have a project title and at least one group
if (!title.isEmpty() && !groups.isEmpty())
{
// Update title
m_title = title;
// Delete existing groups
for (int i = 0; i < groupCount(); ++i)
m_groups.at(i)->deleteLater();
// Clear group list
m_groups.clear();
// Generate groups & datasets from data frame
for (auto i = 0; i < groups.count(); ++i)
{
auto group = new Group(this);
if (group->read(groups.at(i).toObject()))
m_groups.append(group);
else
group->deleteLater();
}
// Update UI
emit updated();
}
}
}
/**
* Clears the project title, deletes all groups & notifies the UI. This function
* is used when the serial device connection state is changed to avoid errors.
*/
void DataProvider::resetData()
{
// Stop if dev. man is not disconnected or if CSV file is open
if (SerialManager::getInstance()->connected() || CsvPlayer::getInstance()->isOpen())
return;
// Delete existing groups
for (int i = 0; i < groupCount(); ++i)
m_groups.at(i)->deleteLater();
// Clear group list
m_title.clear();
m_groups.clear();
// Update UI
emit updated();
emit dataReset();
// Log to console
LOG_INFO() << "QML bridge data reset";
}