summaryrefslogtreecommitdiffstats
path: root/vehicle-signals/QtKuksaClient.h
blob: 1f24e09766a97978aab9047431269a422ae1da01 (plain)
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
/*
 * Copyright (C) 2023 Konsulko Group
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#ifndef QT_KUKSA_CLIENT_H
#define QT_KUKSA_CLIENT_H

#include <QObject>
#include <QMap>
#include <QMutex>
#include <grpcpp/grpcpp.h>
#include "kuksa/val/v1/val.grpc.pb.h"

// Just pull in the whole namespace since Datapoint contains a lot of
// definitions that may potentially be needed.
using namespace kuksa::val::v1;

using grpc::Status;

#include "VehicleSignalsConfig.h"

// KUKSA.val databroker gRPC API Qt client class

class QtKuksaClient : public QObject
{
	Q_OBJECT

public:
	explicit QtKuksaClient(const std::shared_ptr< ::grpc::ChannelInterface>& channel,
			       const VehicleSignalsConfig &config,
			       QObject * parent = Q_NULLPTR);

	Q_INVOKABLE void connect();

	Q_INVOKABLE void get(const QString &path, const bool actuator = false);

	Q_INVOKABLE void set(const QString &path, const QString &value, const bool actuator = false);
	Q_INVOKABLE void set(const QString &path, const qint32 value, const bool actuator = false);
	Q_INVOKABLE void set(const QString &path, const qint64 value, const bool actuator = false);
	Q_INVOKABLE void set(const QString &path, const quint32 value, const bool actuator = false);
	Q_INVOKABLE void set(const QString &path, const quint64 value, const bool actuator = false);
	Q_INVOKABLE void set(const QString &path, const float value, const bool actuator = false);
	Q_INVOKABLE void set(const QString &path, const double value, const bool actuator = false);
  
	Q_INVOKABLE void subscribe(const QString &path, const bool actuator = false);
	Q_INVOKABLE void subscribe(const QMap<QString, bool> &signals_);

signals:
	void connected();
	void getResponse(QString path, QString value, QString timestamp);
        void setResponse(QString path, QString error);
        void subscribeResponse(QString path, QString value, QString timestamp);
        void subscribeDone(const QMap<QString, bool> &signals_, bool canceled);

private:
	class SubscribeReader;

	std::shared_ptr< ::grpc::ChannelInterface> m_channel;
	VehicleSignalsConfig m_config;
	std::shared_ptr<VAL::Stub> m_stub;
	bool m_connected;
	QMutex m_connected_mutex;

	void waitForConnected();

	void set(const QString &path, const Datapoint &dp, const bool actuator);

	void handleGetResponse(const GetResponse *response);

	void handleSetResponse(const SetResponse *response);

	void handleSubscribeResponse(const SubscribeResponse *response);

	void handleSubscribeDone(const QMap<QString, bool> &signals_, const Status &status);

	void handleCriticalFailure(const QString &error);
	void handleCriticalFailure(const char *error) { handleCriticalFailure(QString(error)); };

	void resubscribe(const QMap<QString, bool> &signals_);

	bool convertDatapoint(const Datapoint &dp, QString &value, QString &timestamp);
};

#endif // QT_KUKSA_CLIENT_H