-
Notifications
You must be signed in to change notification settings - Fork 15
/
diagnostics_service.cpp
158 lines (138 loc) · 5.09 KB
/
diagnostics_service.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
#include <QDateTime>
#include <QTimer>
#include <veutil/qt/ve_qitem.hpp>
#include "dbus_service.h"
#include "dbus_services.h"
#include "diagnostics_service.h"
#include "mappings.h"
DiagnosticsService::DiagnosticsService(DBusServices *services, Mappings *mappings, VeQItem *root,
QObject *parent):
QObject(parent),
mLastErrorTimer(new QTimer(this)),
mServices(services),
mMappings(mappings),
mRoot(root),
mLastError(root->itemGetOrCreate("LastError/Message")),
mLastErrorTimeStamp(root->itemGetOrCreate("LastError/Timestamp")),
mServiceCount(root->itemGetOrCreate("Services/Count"))
{
QsLogging::Logger &logger = QsLogging::Logger::instance();
QsLogging::DestinationPtr dest(new DiagnosticsDestination(this));
logger.addDestination(dest);
mServiceCount->produceValue(0);
mServiceCount->produceText("0");
mRoot->produceValue(QVariant(), VeQItem::Synchronized);
mLastErrorTimer->setInterval(1000);
mLastErrorTimer->setSingleShot(true);
connect(services, SIGNAL(dbusServiceFound(DBusService *)),
this, SLOT(onServiceFound(DBusService *)));
connect(mLastErrorTimer, SIGNAL(timeout()), this, SLOT(onLastErrorTimer()));
}
void DiagnosticsService::setError(const QString &error)
{
mLastErrorText = error;
mLastErrorTime = QDateTime::currentDateTimeUtc();
if (!mLastErrorTimer->isActive())
mLastErrorTimer->start();
}
void DiagnosticsService::onServiceFound(DBusService *service)
{
connect(service->getDeviceInstance(), SIGNAL(valueChanged(QVariant)),
this, SLOT(onDeviceInstanceChanged()));
connect(service->getServiceRoot(), SIGNAL(stateChanged(VeQItem::State)),
this, SLOT(onServiceStateChanged()));
}
void DiagnosticsService::onDeviceInstanceChanged()
{
VeQItem *deviceInstanceItem = static_cast<VeQItem *>(sender());
VeQItem *serviceRoot = deviceInstanceItem->itemParent();
VeQItem *serviceEntry = getServiceItem(serviceRoot, deviceInstanceItem);
if (serviceEntry == 0)
serviceEntry = createServiceItem(deviceInstanceItem);
if (serviceEntry == 0)
return;
updateService(serviceEntry, serviceRoot);
}
void DiagnosticsService::onServiceStateChanged()
{
VeQItem *item = static_cast<VeQItem *>(sender());
VeQItem *serviceEntry = 0;
if (item->getState() == VeQItem::Offline) {
serviceEntry = getServiceItem(item);
} else {
VeQItem *deviceInstanceItem = item->itemGetOrCreate("DeviceInstance");
serviceEntry = getServiceItem(item, deviceInstanceItem);
if (serviceEntry == 0)
serviceEntry = createServiceItem(deviceInstanceItem);
}
if (serviceEntry == 0)
return;
updateService(serviceEntry, item);
}
void DiagnosticsService::onLastErrorTimer()
{
mLastError->produceValue(mLastErrorText);
mLastError->produceText(mLastErrorText);
mLastErrorTimeStamp->produceValue(mLastErrorTime.toMSecsSinceEpoch() / 1000);
mLastErrorTimeStamp->produceText(mLastErrorTime.toLocalTime().toString());
}
VeQItem *DiagnosticsService::getServiceItem(VeQItem *serviceRoot, VeQItem *deviceInstance)
{
QVariant v = deviceInstance->getValue();
if (!v.isValid())
return 0;
int unitId = mMappings->getUnitId(v.toInt());
QString id = serviceRoot->id();
// EV Check for a service with the same type and the same device instance.
// If present, replace that service.
QString serviceType = DBusService::getDeviceType(id);
int serviceCount = mServiceCount->getValue().toInt();
for (int i=0; i<serviceCount; ++i) {
VeQItem *serviceEntry = mRoot->itemGetOrCreate(QString("Services/%1").arg(i), false);
VeQItem *serviceName = serviceEntry->itemGetOrCreate("ServiceName");
QString st = DBusService::getDeviceType(serviceName->getValue().toString());
if (st == serviceType) {
VeQItem *di = serviceEntry->itemGetOrCreate("UnitId");
if (di->getValue().toInt() == unitId)
return serviceEntry;
}
}
return 0;
}
VeQItem *DiagnosticsService::getServiceItem(VeQItem *serviceRoot)
{
QString serviceName = serviceRoot->id();
int serviceCount = mServiceCount->getValue().toInt();
for (int i=0; i<serviceCount; ++i) {
VeQItem *se = mRoot->itemGetOrCreate(QString("Services/%1").arg(i), false);
VeQItem *sn = se->itemGetOrCreate("ServiceName");
if (serviceName == sn->getValue().toString())
return se;
}
return 0;
}
VeQItem *DiagnosticsService::createServiceItem(VeQItem *deviceInstance)
{
QVariant v = deviceInstance->getValue();
if (!v.isValid())
return 0;
int unitId = mMappings->getUnitId(v.toInt());
int serviceCount = mServiceCount->getValue().toInt();
VeQItem *serviceEntry = mRoot->itemGetOrCreate(QString("Services/%1").arg(serviceCount), false);
VeQItem *unitIdItem = serviceEntry->itemGetOrCreate("UnitId");
unitIdItem->produceValue(unitId);
unitIdItem->produceText(QString::number(unitId));
mServiceCount->setValue(serviceCount + 1);
return serviceEntry;
}
void DiagnosticsService::updateService(VeQItem *serviceEntry, VeQItem *serviceRoot)
{
QString id = serviceRoot->id();
VeQItem *serviceName = serviceEntry->itemGetOrCreate("ServiceName");
serviceName->produceValue(id);
serviceName->produceText(id);
VeQItem *isActive = serviceEntry->itemGetOrCreate("IsActive");
int a = serviceRoot->getState() == VeQItem::Offline ? 0 : 1;
isActive->produceValue(a);
isActive->produceText(QString::number(a));
}