Initial commit
This commit is contained in:
commit
b7d90b9afe
16245 files changed
+3396666
No files matched your search
Executable
+143
@@ -0,0 +1,143 @@
|
||||
#include "egcu_simulator.h"
|
||||
|
||||
QVector<int> relayStates = {0,0,0,0,0,0,0,0};
|
||||
|
||||
QByteArray createHttpResponse(int statusCode, const QString &contentType, const QString &body)
|
||||
{
|
||||
QString statusText;
|
||||
switch (statusCode)
|
||||
{
|
||||
case 200: statusText = QStringLiteral("OK"); break;
|
||||
case 404: statusText = QStringLiteral("Not Found"); break;
|
||||
case 405: statusText = QStringLiteral("Method Not Allowed"); break;
|
||||
default: statusText = QStringLiteral("Error"); break;
|
||||
}
|
||||
|
||||
QString response;
|
||||
response.append(QString(QStringLiteral("HTTP/1.1 %1 %2\r\n")).arg(statusCode).arg(statusText));
|
||||
response.append(QString(QStringLiteral("Content-Type: %1\r\n")).arg(contentType));
|
||||
response.append(QString(QStringLiteral("Content-Length: %1\r\n")).arg(body.toUtf8().size()));
|
||||
response.append(QString(QStringLiteral("Connection: close\r\n\r\n")));
|
||||
QByteArray responseBa = response.toUtf8();
|
||||
responseBa.append(body.toUtf8());
|
||||
return responseBa;
|
||||
}
|
||||
|
||||
QString sendState()
|
||||
{
|
||||
int i = 0;
|
||||
QString stateString;
|
||||
for (int value : qAsConst(relayStates))
|
||||
{
|
||||
QString state = QStringLiteral("\"");
|
||||
state += QString::number(i);
|
||||
|
||||
if (value == 0)
|
||||
state +=QStringLiteral("\":\"off\",");
|
||||
else
|
||||
state +=QStringLiteral("\":\"on\",");
|
||||
i++;
|
||||
|
||||
stateString += state;
|
||||
}
|
||||
|
||||
stateString.chop(1);
|
||||
|
||||
//return createHttpResponse(200, "text/html", "{\"ID\":\"0x520\",\"Name\":\"Relay Report\",\"State\":{\"0\":\"on\",\"1\":\"off\",\"2\":\"off\",\"3\":\"off\",\"4\":\"off\",\"5\":\"off\",\"6\":\"off\",\"7\":\"off\"}}");
|
||||
QString resultString = QStringLiteral("{\"ID\":\"0x520\",\"Name\":\"Relay Report\",\"State\":{");
|
||||
resultString += stateString;
|
||||
resultString += QStringLiteral("}}");
|
||||
qDebug() << "resultString=" << resultString << "\n";
|
||||
return resultString;
|
||||
}
|
||||
|
||||
|
||||
QByteArray processRequest(const QByteArray &request)
|
||||
{
|
||||
qDebug() << "processRequest:\n" << request;
|
||||
QString requestStr = QString::fromUtf8( request);
|
||||
// Parsen der HTTP-Request-Zeile
|
||||
QStringList lines = requestStr.split(QStringLiteral("\r\n"));
|
||||
QStringList requestLine = lines.first().split(QStringLiteral(" "));
|
||||
QString method = requestLine.value(0);
|
||||
QString path = requestLine.value(1);
|
||||
|
||||
// GET-Anfrage verarbeiten
|
||||
if (method == QStringLiteral("GET"))
|
||||
{
|
||||
qDebug() << "GET path='" << path << "'\n";
|
||||
if (path == QStringLiteral("/"))
|
||||
return createHttpResponse(200, QStringLiteral("text/html"), QStringLiteral("<h1>EGCU-Simulator</h1>"));
|
||||
if (path == QStringLiteral("/relay.php?state=3"))
|
||||
{
|
||||
return createHttpResponse(200, QStringLiteral("text/html"), sendState());
|
||||
}
|
||||
else if (path.contains(QStringLiteral("relay.php"))&&path.contains(QStringLiteral("number")))
|
||||
{
|
||||
// number=0&state=1
|
||||
int startNumberStr= path.indexOf(QStringLiteral("number="));
|
||||
int startNumber = startNumberStr+7;
|
||||
QString numberStr = path.mid(startNumber,1);
|
||||
int number = numberStr.toInt();
|
||||
|
||||
int startStateStr= path.indexOf(QStringLiteral("state="));
|
||||
int startState = startStateStr+6;
|
||||
QString stateStr = path.mid(startState,1);
|
||||
int state = stateStr.toInt();
|
||||
|
||||
relayStates[number] = state;
|
||||
|
||||
qDebug() << "Relais wird geschaltet"
|
||||
<< " number=" << number
|
||||
<< " state=" << state << " \n";
|
||||
|
||||
return createHttpResponse(200, QStringLiteral("text/html"), sendState());
|
||||
}
|
||||
else if (path == QStringLiteral("/about"))
|
||||
return createHttpResponse(200, QStringLiteral("text/html"),QStringLiteral( "</h1>über uns: Qt Webserver</h1>"));
|
||||
else
|
||||
return createHttpResponse(404, QStringLiteral("text/html"), QStringLiteral("<h1>404 - Seite nicht gefunden</h1>"));
|
||||
}
|
||||
|
||||
// POST-Anfrage verarbeiten if (method == "POST") {
|
||||
QString postData = lines.last(); // POST-Daten befinden sich in der letzten Zeile qDebug() << "POST-Daten:" << postData;
|
||||
|
||||
QString responseStr = QStringLiteral("{\"status\": \"success\", \"data\": \"");
|
||||
responseStr += postData;
|
||||
responseStr += QStringLiteral("\"}");
|
||||
|
||||
// return createHttpResponse(200, QStringLiteral("application/json"),
|
||||
// QString("{\"status\": \"success\", \"data\": \"%1\"}").arg(postData));
|
||||
return createHttpResponse(200, QStringLiteral("application/json"),responseStr);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void SimpleWebServer::incomingConnection(qintptr socketDescriptor)
|
||||
{
|
||||
// Bei neuer Verbindung
|
||||
QTcpSocket *clientSocket = new QTcpSocket(this);
|
||||
clientSocket->setSocketDescriptor(socketDescriptor);
|
||||
|
||||
connect(clientSocket, &QTcpSocket::readyRead, [clientSocket]()
|
||||
{
|
||||
// HTTP-Anfrage lesen
|
||||
QByteArray request = clientSocket->readAll();
|
||||
qDebug() << "Anfrage erhalten:\n" << request;
|
||||
|
||||
// Verarbeite die Anfrage
|
||||
QByteArray response = processRequest(request);
|
||||
|
||||
// Antwort senden
|
||||
clientSocket->write(response);
|
||||
clientSocket->disconnectFromHost();
|
||||
|
||||
});
|
||||
|
||||
connect(clientSocket, &QTcpSocket::disconnected, clientSocket, &QTcpSocket::deleteLater);
|
||||
|
||||
qDebug() << "Neue Verbindung von:" << clientSocket->peerAddress().toString();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user