Hyelicht 2.0
Controller application for the Hyelicht shelf. Paint on the shelf with colors, turn on the fireplace mode, and more.
Loading...
Searching...
No Matches
httpserver.h
1/*
2 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
3 * SPDX-FileCopyrightText: 2021-2024 Eike Hein <sho@eikehein.com>
4 */
5
6#pragma once
7
8#include <QHostAddress>
9#include <QObject>
10#include <QPointer>
11#include <QQmlParserStatus>
12
13#include "shelfmodel.h"
14
15#ifdef HYELICHT_BUILD_ONBOARD
16class QHttpHeaders;
17class QHttpServer;
18class QHttpServerRequest;
19class QHttpServerResponder;
20class QTcpServer;
21#endif
22
24
41class HttpServer : public QObject, public QQmlParserStatus
42{
43 Q_OBJECT
44 Q_INTERFACES(QQmlParserStatus)
45
46
55 Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
56
58
66
68
75 Q_PROPERTY(int port READ port WRITE setPort NOTIFY portChanged)
76
78
86 Q_PROPERTY(ShelfModel* model READ model WRITE setModel NOTIFY modelChanged)
87
88 public:
90
93 explicit HttpServer(QObject *parent = nullptr);
94 ~HttpServer() override;
95
97
103 bool enabled() const;
104
106
111 void setEnabled(bool enabled);
112
114
121 QString listenAddress() const;
122
124
130 void setListenAddress(const QString &listenAddress);
131
133
140 int port() const;
141
143
149 void setPort(int port);
150
152
158 ShelfModel *model() const;
159
161
169
171 void classBegin() override;
173 void componentComplete() override;
174
175 Q_SIGNALS:
177
181 void enabledChanged() const;
182
184
189
191
195 void portChanged() const;
196
198
203
204 private Q_SLOTS:
205 void updateHandler();
206
207 private:
208 void updateServer();
209#ifdef HYELICHT_BUILD_ONBOARD
210 std::function<void(const QHttpServerRequest &, QHttpServerResponder &)> propHandler(const char *prop);
211 std::function<void(const QHttpServerRequest &, QHttpServerResponder &)> modelRowHandler(const char *prop,
212 const int index, const int role);
213 void propToJSon(QHttpServerResponder &responder, const QHttpHeaders &headers,
214 const QObject *obj, const char *name);
215 void jsonToProp(const QHttpServerRequest &request, QHttpServerResponder &responder,
216 const QHttpHeaders &headers, QObject *obj, const char *name);
217#endif
218 QJsonObject rowToJson(const QModelIndex &modelIndex);
219 QHash<int, QVariant> jsonToModelRole(const QJsonDocument &document,
220 const QString &roleName);
221
222 bool m_enabled;
223
224 QString m_listenAddress;
225 int m_port;
226
227#ifdef HYELICHT_BUILD_ONBOARD
228 QPointer<QTcpServer> m_tcpServer;
229 QPointer<QHttpServer> m_httpServer;
230#endif
231
232 QPointer<ShelfModel> m_model;
233
234 bool m_createdByQml;
235 bool m_complete;
236};
HTTP REST API binding and server for ShelfModel.
Definition httpserver.h:42
void componentComplete() override
Implements the QQmlParserStatus interface.
Definition httpserver.cpp:129
void setModel(ShelfModel *model)
Set the ShelfModel instance this server provides a HTTP REST API binding for.
Definition httpserver.cpp:100
void modelChanged()
The ShelfModel instance this server provides a HTTP REST API binding for has changed.
void classBegin() override
Implements the QQmlParserStatus interface.
Definition httpserver.cpp:124
void listenAddressChanged() const
The listen address for the server has changed.
void setListenAddress(const QString &listenAddress)
Set the listen address for the server.
Definition httpserver.cpp:64
void portChanged() const
The port the server listens on has changed.
QString listenAddress
Listen address for the server.
Definition httpserver.h:65
bool enabled
Toggle the server on or off.
Definition httpserver.h:55
int port
Port the server listens on.
Definition httpserver.h:75
void setEnabled(bool enabled)
Turn the server on or off.
Definition httpserver.cpp:46
void setPort(int port)
Set the port the server listens on.
Definition httpserver.cpp:82
ShelfModel * model
ShelfModel instance this server provides a HTTP REST API binding for.
Definition httpserver.h:86
void enabledChanged() const
The server has turned on or off.
Data model and business logic specific to the Hyelicht shelf.
Definition shelfmodel.h:48