add some states and another event to the mockdevice
This commit is contained in:
parent
66890e3161
commit
6859bd2b78
@ -26,6 +26,9 @@
|
||||
#include <QStringList>
|
||||
|
||||
QUuid mockEvent1Id = QUuid("45bf3752-0fc6-46b9-89fd-ffd878b5b22b");
|
||||
QUuid mockEvent2Id = QUuid("863d5920-b1cf-4eb9-88bd-8f7b8583b1cf");
|
||||
QUuid mockIntStateId = QUuid("80baec19-54de-4948-ac46-31eabfaceb83");
|
||||
QUuid mockBoolStateId = QUuid("9dd6a97c-dfd1-43dc-acbd-367932742310");
|
||||
|
||||
DevicePluginMock::DevicePluginMock()
|
||||
{
|
||||
@ -46,15 +49,21 @@ QList<DeviceClass> DevicePluginMock::supportedDevices() const
|
||||
|
||||
deviceClassMock.setParams(mockParams);
|
||||
|
||||
// QList<StateType> detectorStates;
|
||||
QList<StateType> mockStates;
|
||||
|
||||
// StateType inRangeState(inRangeStateTypeId);
|
||||
// inRangeState.setName("inRange");
|
||||
// inRangeState.setType(QVariant::Bool);
|
||||
// inRangeState.setDefaultValue(false);
|
||||
// detectorStates.append(inRangeState);
|
||||
StateType intState(mockIntStateId);
|
||||
intState.setName("intState");
|
||||
intState.setType(QVariant::Int);
|
||||
intState.setDefaultValue(10);
|
||||
mockStates.append(intState);
|
||||
|
||||
// deviceClassWifiDetector.setStates(detectorStates);
|
||||
StateType boolState(mockBoolStateId);
|
||||
boolState.setName("boolState");
|
||||
boolState.setType(QVariant::Int);
|
||||
boolState.setDefaultValue(false);
|
||||
mockStates.append(boolState);
|
||||
|
||||
deviceClassMock.setStates(mockStates);
|
||||
|
||||
QList<EventType> mockEvents;
|
||||
|
||||
@ -69,6 +78,11 @@ QList<DeviceClass> DevicePluginMock::supportedDevices() const
|
||||
// event1.setParameters(detectorEventParams);
|
||||
mockEvents.append(event1);
|
||||
|
||||
EventType event2(mockEvent2Id);
|
||||
event2.setName("event2");
|
||||
// event2.setParameters(detectorEventParams);
|
||||
mockEvents.append(event2);
|
||||
|
||||
deviceClassMock.setEvents(mockEvents);
|
||||
|
||||
ret.append(deviceClassMock);
|
||||
@ -103,12 +117,18 @@ bool DevicePluginMock::deviceCreated(Device *device)
|
||||
return false;
|
||||
}
|
||||
|
||||
connect(daemon, SIGNAL(triggerEvent(int)), SLOT(triggerEvent(int)));
|
||||
connect(daemon, SIGNAL(triggerEvent(QUuid)), SLOT(triggerEvent(QUuid)));
|
||||
connect(daemon, SIGNAL(setState(QUuid, QVariant)), SLOT(setState(QUuid,QVariant)));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void DevicePluginMock::triggerEvent(int id)
|
||||
void DevicePluginMock::setState(const QUuid &stateTypeId, const QVariant &value)
|
||||
{
|
||||
qDebug() << "should set state" << stateTypeId << value;
|
||||
}
|
||||
|
||||
void DevicePluginMock::triggerEvent(const QUuid &id)
|
||||
{
|
||||
HttpDaemon *daemon = qobject_cast<HttpDaemon*>(sender());
|
||||
if (!daemon) {
|
||||
@ -117,7 +137,7 @@ void DevicePluginMock::triggerEvent(int id)
|
||||
|
||||
Device *device = m_daemons.key(daemon);
|
||||
|
||||
Event event(mockEvent1Id, device->id(), QVariantMap());
|
||||
Event event(id, device->id(), QVariantMap());
|
||||
|
||||
qDebug() << "Emitting event " << event.eventTypeId();
|
||||
emit emitEvent(event);
|
||||
|
||||
@ -44,7 +44,8 @@ public:
|
||||
bool deviceCreated(Device *device) override;
|
||||
|
||||
private slots:
|
||||
void triggerEvent(int id);
|
||||
void setState(const QUuid &stateTypeId, const QVariant &value);
|
||||
void triggerEvent(const QUuid &id);
|
||||
|
||||
private:
|
||||
QHash<Device*, HttpDaemon*> m_daemons;
|
||||
|
||||
@ -1,14 +1,17 @@
|
||||
#include "httpdaemon.h"
|
||||
|
||||
#include "device.h"
|
||||
#include "deviceclass.h"
|
||||
#include "deviceplugin.h"
|
||||
#include "statetype.h"
|
||||
|
||||
#include <QTcpSocket>
|
||||
#include <QDebug>
|
||||
#include <QDateTime>
|
||||
#include <QUrlQuery>
|
||||
|
||||
HttpDaemon::HttpDaemon(Device *device, QObject *parent):
|
||||
QTcpServer(parent), disabled(false), m_device(device)
|
||||
HttpDaemon::HttpDaemon(Device *device, DevicePlugin *parent):
|
||||
QTcpServer(parent), disabled(false), m_plugin(parent), m_device(device)
|
||||
{
|
||||
listen(QHostAddress::LocalHost, device->params().value("httpport").toInt());
|
||||
}
|
||||
@ -54,27 +57,20 @@ void HttpDaemon::readClient()
|
||||
QStringList tokens = QString(data).split(QRegExp("[ \r\n][ \r\n]*"));
|
||||
qDebug() << "incoming data" << tokens[1];
|
||||
if (tokens[1].contains('?')) {
|
||||
QUrlQuery query(QUrl("http://foo.bar" + tokens[1]));
|
||||
qDebug() << "query is" << query.queryItemValue("eventid");
|
||||
emit triggerEvent(query.queryItemValue("eventid").toInt());
|
||||
QUrl url("http://foo.bar" + tokens[1]);
|
||||
QUrlQuery query(url);
|
||||
qDebug() << "query is" << url.path();
|
||||
if (url.path() == "/setstate") {
|
||||
emit setState(QUuid(query.queryItems().first().first), QVariant(query.queryItems().first().second));
|
||||
} else if (url.path() == "/generateevent") {
|
||||
qDebug() << "got generateevent" << query.queryItemValue("eventid");
|
||||
emit triggerEvent(QUuid(query.queryItemValue("eventid")));
|
||||
}
|
||||
}
|
||||
if (tokens[0] == "GET") {
|
||||
QTextStream os(socket);
|
||||
os.setAutoDetectUnicode(true);
|
||||
os << QString("HTTP/1.0 200 Ok\r\n"
|
||||
"Content-Type: text/html; charset=\"utf-8\"\r\n"
|
||||
"\r\n"
|
||||
"<html>"
|
||||
"<body>"
|
||||
"<h1>Mock device</h1>\n"
|
||||
"Name: %1<br>"
|
||||
"ID: %2"
|
||||
"<form action=\"/\" method=\"get\">"
|
||||
"<input type='hidden'' name='eventid' value='1'>"
|
||||
"<input type='submit' value='Generate event1'/>"
|
||||
"</form>"
|
||||
"</body>"
|
||||
"</html>\n").arg(m_device->name()).arg(m_device->id().toString());
|
||||
os << generateWebPage();
|
||||
socket->close();
|
||||
|
||||
qDebug() << "Wrote to client";
|
||||
@ -94,3 +90,52 @@ void HttpDaemon::discardClient()
|
||||
|
||||
qDebug() << "Connection closed";
|
||||
}
|
||||
|
||||
QString HttpDaemon::generateWebPage()
|
||||
{
|
||||
DeviceClass deviceClass = m_plugin->supportedDevices().first();
|
||||
|
||||
QString contentHeader(
|
||||
"HTTP/1.0 200 Ok\r\n"
|
||||
"Content-Type: text/html; charset=\"utf-8\"\r\n"
|
||||
"\r\n"
|
||||
);
|
||||
|
||||
QString body = QString(
|
||||
"<html>"
|
||||
"<body>"
|
||||
"<h1>Mock device Controller</h1>\n"
|
||||
"Name: %1<br>"
|
||||
"ID: %2<br>"
|
||||
"DeviceClass ID: %3<br>").arg(m_device->name()).arg(m_device->id().toString()).arg(deviceClass.id().toString());
|
||||
body.append("<table>");
|
||||
for (int i = 0; i < deviceClass.states().count(); ++i) {
|
||||
body.append("<tr>");
|
||||
body.append("<form action=\"/setstate\" method=\"get\">");
|
||||
const StateType &stateType = deviceClass.states().at(i);
|
||||
body.append("<td>" + stateType.name() + "</td>");
|
||||
body.append(QString("<td><input type='input'' name='%1' value='%2'></td>").arg(stateType.id().toString()).arg(m_device->states().at(i).value().toString()));
|
||||
body.append("<td><input type=submit value='Set State'/></td>");
|
||||
body.append("</form>");
|
||||
body.append("</tr>");
|
||||
}
|
||||
body.append("</table>");
|
||||
|
||||
body.append("<table>");
|
||||
for (int i = 0; i < deviceClass.events().count(); ++i) {
|
||||
const EventType &eventType = deviceClass.events().at(i);
|
||||
body.append(QString(
|
||||
"<tr>"
|
||||
"<form action=\"/generateevent\" method=\"get\">"
|
||||
"<td>Event %1<input type='hidden' name='eventid' value='%2'/></td>"
|
||||
"<td><input type='submit' value='Generate'/></td>"
|
||||
"</form>"
|
||||
"</tr>"
|
||||
).arg(eventType.name()).arg(eventType.id().toString()));
|
||||
}
|
||||
body.append("</table>");
|
||||
|
||||
body.append("</body></html>\n");
|
||||
|
||||
return contentHeader + body;
|
||||
}
|
||||
|
||||
@ -4,12 +4,13 @@
|
||||
#include <QTcpServer>
|
||||
|
||||
class Device;
|
||||
class DevicePlugin;
|
||||
|
||||
class HttpDaemon : public QTcpServer
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
HttpDaemon(Device *device, QObject* parent = 0);
|
||||
HttpDaemon(Device *device, DevicePlugin* parent = 0);
|
||||
|
||||
void incomingConnection(qintptr socket) override;
|
||||
|
||||
@ -18,15 +19,20 @@ public:
|
||||
void resume();
|
||||
|
||||
signals:
|
||||
void triggerEvent(int id);
|
||||
void setState(const QUuid &stateTypeId, const QVariant &value);
|
||||
void triggerEvent(const QUuid &eventTypeId);
|
||||
|
||||
private slots:
|
||||
void readClient();
|
||||
void discardClient();
|
||||
|
||||
private:
|
||||
QString generateWebPage();
|
||||
|
||||
private:
|
||||
bool disabled;
|
||||
|
||||
DevicePlugin *m_plugin;
|
||||
Device *m_device;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user