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
|
/*
* Copyright (C) 2022,2023 Konsulko Group
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <QDebug>
#include <QtConcurrent>
#include "vehiclesignals.h"
#include "QtKuksaClient.h"
VehicleSignals::VehicleSignals(const VehicleSignalsConfig &config, QObject *parent) :
QObject(parent),
m_config(config)
{
// Create gRPC channel
// NOTE: channel creation and waiting for connected state could be put into
// a thread that is spawned here.
QString host = m_config.hostname();
host += ":";
host += QString::number(m_config.port());
std::shared_ptr<grpc::Channel> channel;
if (!m_config.caCert().isEmpty()) {
qInfo() << "Using TLS";
grpc::SslCredentialsOptions options;
options.pem_root_certs = m_config.caCert().toStdString();
if (!m_config.tlsServerName().isEmpty()) {
grpc::ChannelArguments args;
auto target = m_config.tlsServerName();
qInfo() << "Overriding TLS target name with " << target;
args.SetString(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG, target.toStdString());
channel = grpc::CreateCustomChannel(host.toStdString(), grpc::SslCredentials(options), args);
} else {
channel = grpc::CreateChannel(host.toStdString(), grpc::SslCredentials(options));
}
} else {
channel = grpc::CreateChannel(host.toStdString(), grpc::InsecureChannelCredentials());
}
m_broker = new QtKuksaClient(channel, config);
if (!m_broker)
qCritical() << "gRPC client initialization failed";
QObject::connect(m_broker, &QtKuksaClient::connected, this, &VehicleSignals::onConnected);
}
VehicleSignals::~VehicleSignals()
{
delete m_broker;
}
void VehicleSignals::connect()
{
// QtKuksaClient will call our onConnected slot when the channel
// is connected, and then we pass that along via our connected
// signal.
if (m_broker)
m_broker->connect();
}
void VehicleSignals::authorize()
{
// Databroker has no separate authorize call, so this is a no-op
emit authorized();
}
void VehicleSignals::get(const QString &path, const bool actuator)
{
if (m_broker)
m_broker->get(path, actuator);
}
void VehicleSignals::set(const QString &path, const QString &value, const bool actuator)
{
if (m_broker)
m_broker->set(path, value, actuator);
}
void VehicleSignals::set(const QString &path, const qint32 value, const bool actuator)
{
if (m_broker)
m_broker->set(path, value, actuator);
}
void VehicleSignals::set(const QString &path, const qint64 value, const bool actuator)
{
if (m_broker)
m_broker->set(path, value, actuator);
}
void VehicleSignals::set(const QString &path, const quint32 value, const bool actuator)
{
if (m_broker)
m_broker->set(path, value, actuator);
}
void VehicleSignals::set(const QString &path, const quint64 value, const bool actuator)
{
if (m_broker)
m_broker->set(path, value, actuator);
}
void VehicleSignals::set(const QString &path, const float value, const bool actuator)
{
if (m_broker)
m_broker->set(path, value, actuator);
}
void VehicleSignals::set(const QString &path, const double value, const bool actuator)
{
if (m_broker)
m_broker->set(path, value, actuator);
}
void VehicleSignals::subscribe(const QString &path, bool actuator)
{
if (m_broker)
m_broker->subscribe(path, actuator);
}
void VehicleSignals::subscribe(const QMap<QString, bool> &signals_)
{
if (m_broker)
m_broker->subscribe(signals_);
}
// Slots
void VehicleSignals::onConnected()
{
QObject::connect(m_broker, &QtKuksaClient::getResponse, this, &VehicleSignals::onGetResponse);
QObject::connect(m_broker, &QtKuksaClient::setResponse, this, &VehicleSignals::onSetResponse);
QObject::connect(m_broker, &QtKuksaClient::subscribeResponse, this, &VehicleSignals::onSubscribeResponse);
//QObject::connect(m_broker, &QtKuksaClient::subscribeDone, this, &VehicleSignals::onSubscribeDone);
emit connected();
}
void VehicleSignals::onGetResponse(QString path, QString value, QString timestamp)
{
emit getSuccessResponse(path, value, timestamp);
}
void VehicleSignals::onSetResponse(QString path, QString error)
{
emit setErrorResponse(path, error);
}
void VehicleSignals::onSubscribeResponse(QString path, QString value, QString timestamp)
{
if (m_config.verbose() > 1)
qDebug() << "VehicleSignals::onSubscribeResponse: got " << path << " = " << value;
emit signalNotification(path, value, timestamp);
}
void VehicleSignals::onSubscribeDone(const QMap<QString, bool> &signals_, bool canceled)
{
if (!canceled) {
// queue up a resubscribe attempt
QFuture<void> future = QtConcurrent::run(&VehicleSignals::resubscribe, this, signals_);
}
}
// Private
void VehicleSignals::resubscribe(const QMap<QString, bool> &signals_)
{
// Delay 100 milliseconds between subscribe attempts
QThread::msleep(100);
if (m_broker)
m_broker->subscribe(signals_);
}
|