-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdbus_services.cpp
76 lines (67 loc) · 1.78 KB
/
dbus_services.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
#include <QsLog.h>
#include <veutil/qt/ve_qitem.hpp>
#include "dbus_service.h"
#include "dbus_services.h"
DBusServices::DBusServices(VeQItem *root, QObject *parent) :
QObject(parent),
mRoot(root)
{
}
DBusServices::~DBusServices()
{
foreach (const QList<DBusService *> &services, mServiceByType) {
foreach (DBusService *service, services)
delete service;
}
}
void DBusServices::initialScan()
{
for (int i=0;;++i) {
VeQItem *item = mRoot->itemChild(i);
if (item == 0)
break;
onServiceAdded(item);
}
connect(mRoot, SIGNAL(childAdded(VeQItem *)),
this, SLOT(onServiceAdded(VeQItem *)));
}
DBusService *DBusServices::getService(QString deviceType, int deviceInstance)
{
QList<DBusService *> services = mServiceByType.value(deviceType);
if (services.isEmpty())
return 0;
DBusService *last = 0;
DBusService *lastConnected = 0;
foreach (DBusService *service, services) {
VeQItem *serviceInstance = service->getDeviceInstance();
QVariant v = serviceInstance->getValue();
if (v.toInt() == deviceInstance) {
switch (serviceInstance->getState()) {
case VeQItem::Synchronized:
lastConnected = service;
// Fall through
default:
last = service;
break;
}
}
}
return lastConnected == 0 ? last : lastConnected;
}
void DBusServices::onServiceAdded(VeQItem *item)
{
QString name = item->id();
// Skip our own service
if (name == "com.victronenergy.modbustcp")
return;
DBusService *service = mServicesByName.value(name);
if (service != 0)
return;
QLOG_TRACE() << "[DBusServices] Add new service " << name;
service = new DBusService(item);
mServicesByName.insert(name, service);
QString deviceType = service->getDeviceType(name);
QList<DBusService *> &device = mServiceByType[deviceType];
device.append(service);
emit dbusServiceFound(service);
}