summaryrefslogtreecommitdiffstats
path: root/src/BobinkClient.h
blob: b8e562465322c26eefc171f520179bcd6ff2211d (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
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
#ifndef BOBINKCLIENT_H
#define BOBINKCLIENT_H

#include <QEventLoop>
#include <QObject>
#include <QOpcUaApplicationDescription>
#include <QOpcUaClient>
#include <QOpcUaEndpointDescription>
#include <QOpcUaErrorState>
#include <QOpcUaPkiConfiguration>
#include <QOpcUaProvider>
#include <QQmlEngine>
#include <QTimer>

class BobinkAuth;

class BobinkClient : public QObject
{
  Q_OBJECT
  QML_ELEMENT
  QML_SINGLETON

  Q_PROPERTY (bool connected READ connected NOTIFY connectedChanged)
  Q_PROPERTY (QString serverUrl READ serverUrl WRITE setServerUrl NOTIFY
                  serverUrlChanged)
  Q_PROPERTY (BobinkAuth *auth READ auth WRITE setAuth NOTIFY authChanged)

  Q_PROPERTY (QString discoveryUrl READ discoveryUrl WRITE setDiscoveryUrl
                  NOTIFY discoveryUrlChanged)
  Q_PROPERTY (int discoveryInterval READ discoveryInterval WRITE
                  setDiscoveryInterval NOTIFY discoveryIntervalChanged)
  Q_PROPERTY (bool discovering READ discovering NOTIFY discoveringChanged)
  Q_PROPERTY (QVariantList servers READ servers NOTIFY serversChanged)

  Q_PROPERTY (QString pkiDir READ pkiDir WRITE setPkiDir NOTIFY pkiDirChanged)

public:
  explicit BobinkClient (QObject *parent = nullptr);
  ~BobinkClient () override;

  static BobinkClient *instance ();
  static BobinkClient *create (QQmlEngine *qmlEngine, QJSEngine *jsEngine);

  bool connected () const;

  QString serverUrl () const;
  void setServerUrl (const QString &url);

  BobinkAuth *auth () const;
  void setAuth (BobinkAuth *auth);

  QOpcUaClient *opcuaClient () const;

  QString discoveryUrl () const;
  void setDiscoveryUrl (const QString &url);

  int discoveryInterval () const;
  void setDiscoveryInterval (int ms);

  bool discovering () const;

  const QList<QOpcUaApplicationDescription> &discoveredServers () const;
  QVariantList servers () const;

  QString pkiDir () const;
  void setPkiDir (const QString &path);

  Q_INVOKABLE void connectToServer ();
  Q_INVOKABLE void disconnectFromServer ();
  Q_INVOKABLE void acceptCertificate ();
  Q_INVOKABLE void rejectCertificate ();

  Q_INVOKABLE void startDiscovery ();
  Q_INVOKABLE void stopDiscovery ();
  Q_INVOKABLE void applyPki ();

signals:
  void connectedChanged ();
  void serverUrlChanged ();
  void authChanged ();
  void certificateTrustRequested (const QString &certInfo);
  void connectionError (const QString &message);

  void discoveryUrlChanged ();
  void discoveryIntervalChanged ();
  void discoveringChanged ();
  void serversChanged ();
  void pkiDirChanged ();

private slots:
  void handleStateChanged (QOpcUaClient::ClientState state);
  void
  handleEndpointsReceived (const QList<QOpcUaEndpointDescription> &endpoints,
                           QOpcUa::UaStatusCode statusCode,
                           const QUrl &requestUrl);
  void handleConnectError (QOpcUaErrorState *errorState);
  void handleFindServersFinished (
      const QList<QOpcUaApplicationDescription> &servers,
      QOpcUa::UaStatusCode statusCode, const QUrl &requestUrl);
  void doDiscovery ();

private:
  void setupClient ();

  static BobinkClient *s_instance;
  QOpcUaProvider *m_provider = nullptr;
  QOpcUaClient *m_client = nullptr;
  BobinkAuth *m_auth = nullptr;
  QString m_serverUrl;
  bool m_connected = false;
  QEventLoop *m_certLoop = nullptr;
  bool m_certAccepted = false;

  QString m_discoveryUrl;
  int m_discoveryInterval = 10000;
  QTimer m_discoveryTimer;
  bool m_discovering = false;
  QList<QOpcUaApplicationDescription> m_discoveredServers;

  QString m_pkiDir;
};

#endif // BOBINKCLIENT_H