forked from dresden-elektronik/deconz-ota-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
otau_model.cpp
332 lines (290 loc) · 8.29 KB
/
otau_model.cpp
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include <QFont>
#include "deconz.h"
#include "otau_file.h"
#include "otau_node.h"
#include "otau_model.h"
#include "std_otau_plugin.h"
/*! The constructor.
*/
OtauModel::OtauModel(QObject *parent) :
QAbstractTableModel(parent)
{
}
OtauModel::~OtauModel()
{
for (OtauNode *&n : m_nodes)
{
if (n)
{
delete n;
n = nullptr;
}
}
m_nodes.clear();
}
/*! Returns the model rowcount.
*/
int OtauModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return m_nodes.size();
}
/*! Returns the model columncount.
*/
int OtauModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return SectionCount;
}
/*! Returns the model headerdata for a column.
*/
QVariant OtauModel::headerData(int section, Qt::Orientation orientation, int role) const
{
Q_UNUSED(orientation);
if (role == Qt::DisplayRole && (orientation == Qt::Horizontal))
{
switch (section)
{
case SectionAddress:
return tr("Address");
case SectionManufacturer:
return tr("Vendor");
case SectionImageType:
return tr("Image");
case SectionSoftwareVersion:
return tr("Version");
case SectionProgress:
return tr("Progress");
case SectionDuration:
return tr("Time");
// case SectionStatus:
// return tr("Status");
default:
return tr("Unknown");
}
}
return QVariant();
}
/*! Returns the model data for a specific column.
*/
QVariant OtauModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole)
{
if (index.row() >= rowCount(QModelIndex()))
{
return QVariant();
}
QString str;
OtauNode *node = m_nodes[index.row()];
switch(index.column())
{
case SectionAddress:
if (node->address().hasExt())
{
str = "0x" + QString("%1").arg(node->address().ext(), 16, 16, QLatin1Char('0')).toUpper();
}
else if (node->address().hasNwk())
{
str = "0x" + QString("%1").arg(node->address().nwk(), 4, 16, QLatin1Char('0')).toUpper();
}
break;
case SectionManufacturer:
str = "0x" + QString("%1").arg(node->manufacturerId, 4, 16, QLatin1Char('0'));
break;
case SectionImageType:
str = "0x" + QString("%1").arg(node->imageType(), 4, 16, QLatin1Char('0'));
break;
case SectionSoftwareVersion:
str = "0x" + QString("%1").arg(node->softwareVersion(), 8, 16, QLatin1Char('0'));
break;
case SectionProgress:
if (node->status() == OtauNode::StatusWaitUpgradeEnd)
{
str = tr("Wait to finish");
}
else if (node->zclCommandId == OTAU_UPGRADE_END_RESPONSE_CMD_ID)
{
switch(node->upgradeEndReq.status)
{
case OTAU_SUCCESS: str = tr("Done"); break;
case OTAU_ABORT: str = tr("Abort"); break;
case OTAU_INVALID_IMAGE: str = tr("Invalid image"); break;
case OTAU_REQUIRE_MORE_IMAGE: str = tr("Require more image"); break;
default:
str = tr("Unknown");
break;
}
}
else if (node->zclCommandId == OTAU_QUERY_NEXT_IMAGE_RESPONSE_CMD_ID)
{
if (node->hasData())
{
str = tr("Idle");
}
else
{
str = tr("No file");
}
}
else if (node->permitUpdate())
{
if (node->offset() > 0)
{
if (node->offset() == node->file.totalImageSize)
{
str = tr("Done");
}
else
{
str = QString("%1%").arg((static_cast<double>(node->offset()) / static_cast<double>(node->file.totalImageSize)) * 100.0, 0, 'f', 2);
}
}
else
{
str = tr("Queued");
}
}
else if (node->hasData())
{
str = tr("Paused");
}
else
{
str = tr("No file");
}
break;
case SectionDuration:
{
int min = (node->elapsedTime() / 1000) / 60;
int sec = (node->elapsedTime() / 1000) % 60;
str = QString("%1:%2").arg(min).arg(sec, 2, 10, QLatin1Char('0'));
}
break;
// case SectionStatus:
// str = node->statusString();
// break;
default:
break;
}
return str;
}
else if (role == Qt::ToolTipRole)
{
if (index.row() >= rowCount(QModelIndex()))
{
return QVariant();
}
OtauNode *node = m_nodes[index.row()];
switch (index.column())
{
case SectionSoftwareVersion:
// dresden electronic spezific version format
if (node->softwareVersion() != 0)
{
if ((node->address().ext() & 0x00212EFFFF000000ULL) != 0)
{
return QString("%1.%2 build %3")
.arg((node->softwareVersion() & 0xF0000000U) >> 28)
.arg((node->softwareVersion() & 0x0FF00000U) >> 20)
.arg((node->softwareVersion() & 0x000FFFFFU));
}
}
break;
default:
break;
}
}
else if (role == Qt::FontRole)
{
switch (index.column())
{
case SectionAddress:
case SectionManufacturer:
case SectionImageType:
case SectionSoftwareVersion:
{
QFont font("Monospace");
font.setStyleHint(QFont::TypeWriter);
return font;
}
default:
break;
}
}
return QVariant();
}
/*! Returns a OtauNode.
\param addr - the nodes address which must contain nwk and ext address
\param create - true if a OtauNode shall be created if it does not exist yet
\return pointer to a OtauNode or 0 if not found
*/
OtauNode *OtauModel::getNode(const deCONZ::Address &addr, bool create)
{
std::vector<OtauNode*>::iterator i = m_nodes.begin();
std::vector<OtauNode*>::iterator end = m_nodes.end();
if (!addr.hasExt() && !addr.hasNwk())
{
return 0;
}
for (; i != end; ++i)
{
if (addr.hasNwk() && (*i)->address().hasNwk())
{
if ((*i)->address().nwk() == addr.nwk())
{
return *i;
}
}
if (addr.hasExt() && (*i)->address().hasExt())
{
if ((*i)->address().ext() == addr.ext())
{
if ((*i)->address().nwk() != addr.nwk())
{
// update nwk address
}
return *i;
}
}
}
if (create && addr.hasExt() && addr.hasNwk())
{
// not found create new
uint row = m_nodes.size();
beginInsertRows(QModelIndex(), row, row);
OtauNode *node = new OtauNode(addr);
node->row = row;
node->model = this;
m_nodes.push_back(node);
endInsertRows();
DBG_Printf(DBG_OTA, "OtauNode added %s\n", qPrintable(addr.toStringExt()));
return node;
}
return 0;
}
/*! Returns a OtauNode for a given row.
*/
OtauNode *OtauModel::getNodeAtRow(uint row)
{
if (m_nodes.size() > row)
{
return m_nodes[row];
}
return 0;
}
/*! Notify model/view that the data for a given node has changed.
*/
void OtauModel::nodeDataUpdate(OtauNode *node)
{
if (node && (node->row < m_nodes.size()))
{
emit dataChanged(index(node->row, 0), index(node->row, SectionCount - 1), {Qt::DisplayRole});
}
}
/*! Returns the internal vector of nodes.
*/
std::vector<OtauNode *> &OtauModel::nodes()
{
return m_nodes;
}