-
Notifications
You must be signed in to change notification settings - Fork 1
/
beanpacketsmonitormodel.cpp
164 lines (147 loc) · 4.75 KB
/
beanpacketsmonitormodel.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
#include "beanpacketsmonitormodel.h"
#include "beanpacket.h"
#include <QDebug>
#define MARK 0
#define DSTID 1
#define MSGID 2
#define DLC 3
#define DATA 4
#define CRC 5
#define PRIO 6
#define REPEAT 7
#define COUNTER 8
#define COMMENT 9
BeanPacketsMonitorModel::BeanPacketsMonitorModel(QObject *parent)
: QAbstractTableModel(parent) {
packets = new QList<BeanPacket *>;
}
QVariant BeanPacketsMonitorModel::headerData(int section, Qt::Orientation orientation, int role) const {
if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
return QVariant();
switch (section) {
case MARK:
return QString("");
case DSTID:
return QString("Dst");
case MSGID:
return QString("Msg");
case DLC:
return QString("Len");
case DATA:
return QString("Data");
case CRC:
return QString("CRC");
case PRIO:
return QString("Pri");
case REPEAT:
return QString("Repeat");
case COUNTER:
return QString("Counter");
case COMMENT:
return QString("Comment");
default:
return QVariant();
}
}
int BeanPacketsMonitorModel::rowCount(const QModelIndex &parent) const {
return packets->size();
}
int BeanPacketsMonitorModel::columnCount(const QModelIndex &parent) const {
return COMMENT + 1;
}
QVariant BeanPacketsMonitorModel::data(const QModelIndex &index, int role) const {
if (!index.isValid())
return QVariant();
if (role == Qt::DisplayRole) {
if (index.row() <= packets->size()) {
const BeanPacket *packet = packets->at(index.row());
switch (index.column()) {
case DSTID:
return QVariant(packet->getDstIdStr());
case MSGID:
return QVariant(packet->getMsgIdStr());
case PRIO:
return QVariant(packet->getPrioStr());
case DATA:
return QVariant(packet->getDataStr());
case DLC:
return QVariant(packet->getDlcStr());
case CRC:
return QVariant(packet->getCrcStr());
case REPEAT:
return QVariant(packet->getRepeatStr());
case COUNTER:
return QVariant(packet->getCounterStr());
case COMMENT:
return QVariant(packet->debug);
default:
return QVariant();
}
}
} else if (role == Qt::TextAlignmentRole) {
switch (index.column()) {
case DATA:
return Qt::AlignLeft + Qt::AlignVCenter;
case PRIO:
case DLC:
case CRC:
return Qt::AlignCenter;
case MSGID:
case DSTID:
default:
return Qt::AlignRight + Qt::AlignVCenter;
}
}
return QVariant();
}
void BeanPacketsMonitorModel::clearPackets() {
qDeleteAll(*packets);
packets->clear();
layoutChanged();
}
void BeanPacketsMonitorModel::appendPacket(BeanPacket *packet, bool groupMsg) {
// either add, either change existing
qDebug() << "Monitor: packets size before:" << packets->size();
BeanPacket *targetPacket = nullptr;
// search for existing dst
for (int i = 0; i < packets->size(); i++) {
auto curPacket = packets->at(i);
qDebug() << packet->dstId << "? local item text:" << curPacket;
if (curPacket->dstId == packet->dstId) { // dst matches
bool matched = true;
if (groupMsg) {
if (curPacket->msgId != packet->msgId) { // msg matches
matched = false;
}
}
if (matched) {
qDebug() << "row " << i;
targetPacket = curPacket;
break;
}
}
}
if (targetPacket) { // update existing
targetPacket->msgId = packet->msgId;
targetPacket->crc = packet->crc;
targetPacket->prio = packet->prio;
targetPacket->dlc = packet->dlc;
targetPacket->parsedDlc = packet->parsedDlc;
memcpy(&(targetPacket->data), &(packet->data), 11);
if (packet->timeEpoch >= targetPacket->timeEpoch) {
targetPacket->repeat = packet->timeEpoch - targetPacket->timeEpoch;
}
targetPacket->timeEpoch = packet->timeEpoch;
targetPacket->counter++;
} else {
auto copyPacket = new BeanPacket(packet);
packets->append(copyPacket);
}
layoutChanged();
}
BeanPacket *BeanPacketsMonitorModel::getPacketAt(int i) {
if (packets) {
return packets->at(i);
}
return nullptr;
}