add some states and another event to the mockdevice
This commit is contained in:
parent
66890e3161
commit
6859bd2b78
@ -26,6 +26,9 @@
|
|||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
|
|
||||||
QUuid mockEvent1Id = QUuid("45bf3752-0fc6-46b9-89fd-ffd878b5b22b");
|
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()
|
DevicePluginMock::DevicePluginMock()
|
||||||
{
|
{
|
||||||
@ -46,15 +49,21 @@ QList<DeviceClass> DevicePluginMock::supportedDevices() const
|
|||||||
|
|
||||||
deviceClassMock.setParams(mockParams);
|
deviceClassMock.setParams(mockParams);
|
||||||
|
|
||||||
// QList<StateType> detectorStates;
|
QList<StateType> mockStates;
|
||||||
|
|
||||||
// StateType inRangeState(inRangeStateTypeId);
|
StateType intState(mockIntStateId);
|
||||||
// inRangeState.setName("inRange");
|
intState.setName("intState");
|
||||||
// inRangeState.setType(QVariant::Bool);
|
intState.setType(QVariant::Int);
|
||||||
// inRangeState.setDefaultValue(false);
|
intState.setDefaultValue(10);
|
||||||
// detectorStates.append(inRangeState);
|
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;
|
QList<EventType> mockEvents;
|
||||||
|
|
||||||
@ -69,6 +78,11 @@ QList<DeviceClass> DevicePluginMock::supportedDevices() const
|
|||||||
// event1.setParameters(detectorEventParams);
|
// event1.setParameters(detectorEventParams);
|
||||||
mockEvents.append(event1);
|
mockEvents.append(event1);
|
||||||
|
|
||||||
|
EventType event2(mockEvent2Id);
|
||||||
|
event2.setName("event2");
|
||||||
|
// event2.setParameters(detectorEventParams);
|
||||||
|
mockEvents.append(event2);
|
||||||
|
|
||||||
deviceClassMock.setEvents(mockEvents);
|
deviceClassMock.setEvents(mockEvents);
|
||||||
|
|
||||||
ret.append(deviceClassMock);
|
ret.append(deviceClassMock);
|
||||||
@ -103,12 +117,18 @@ bool DevicePluginMock::deviceCreated(Device *device)
|
|||||||
return false;
|
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;
|
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());
|
HttpDaemon *daemon = qobject_cast<HttpDaemon*>(sender());
|
||||||
if (!daemon) {
|
if (!daemon) {
|
||||||
@ -117,7 +137,7 @@ void DevicePluginMock::triggerEvent(int id)
|
|||||||
|
|
||||||
Device *device = m_daemons.key(daemon);
|
Device *device = m_daemons.key(daemon);
|
||||||
|
|
||||||
Event event(mockEvent1Id, device->id(), QVariantMap());
|
Event event(id, device->id(), QVariantMap());
|
||||||
|
|
||||||
qDebug() << "Emitting event " << event.eventTypeId();
|
qDebug() << "Emitting event " << event.eventTypeId();
|
||||||
emit emitEvent(event);
|
emit emitEvent(event);
|
||||||
|
|||||||
@ -44,7 +44,8 @@ public:
|
|||||||
bool deviceCreated(Device *device) override;
|
bool deviceCreated(Device *device) override;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void triggerEvent(int id);
|
void setState(const QUuid &stateTypeId, const QVariant &value);
|
||||||
|
void triggerEvent(const QUuid &id);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QHash<Device*, HttpDaemon*> m_daemons;
|
QHash<Device*, HttpDaemon*> m_daemons;
|
||||||
|
|||||||
@ -1,14 +1,17 @@
|
|||||||
#include "httpdaemon.h"
|
#include "httpdaemon.h"
|
||||||
|
|
||||||
#include "device.h"
|
#include "device.h"
|
||||||
|
#include "deviceclass.h"
|
||||||
|
#include "deviceplugin.h"
|
||||||
|
#include "statetype.h"
|
||||||
|
|
||||||
#include <QTcpSocket>
|
#include <QTcpSocket>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
#include <QUrlQuery>
|
#include <QUrlQuery>
|
||||||
|
|
||||||
HttpDaemon::HttpDaemon(Device *device, QObject *parent):
|
HttpDaemon::HttpDaemon(Device *device, DevicePlugin *parent):
|
||||||
QTcpServer(parent), disabled(false), m_device(device)
|
QTcpServer(parent), disabled(false), m_plugin(parent), m_device(device)
|
||||||
{
|
{
|
||||||
listen(QHostAddress::LocalHost, device->params().value("httpport").toInt());
|
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]*"));
|
QStringList tokens = QString(data).split(QRegExp("[ \r\n][ \r\n]*"));
|
||||||
qDebug() << "incoming data" << tokens[1];
|
qDebug() << "incoming data" << tokens[1];
|
||||||
if (tokens[1].contains('?')) {
|
if (tokens[1].contains('?')) {
|
||||||
QUrlQuery query(QUrl("http://foo.bar" + tokens[1]));
|
QUrl url("http://foo.bar" + tokens[1]);
|
||||||
qDebug() << "query is" << query.queryItemValue("eventid");
|
QUrlQuery query(url);
|
||||||
emit triggerEvent(query.queryItemValue("eventid").toInt());
|
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") {
|
if (tokens[0] == "GET") {
|
||||||
QTextStream os(socket);
|
QTextStream os(socket);
|
||||||
os.setAutoDetectUnicode(true);
|
os.setAutoDetectUnicode(true);
|
||||||
os << QString("HTTP/1.0 200 Ok\r\n"
|
os << generateWebPage();
|
||||||
"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());
|
|
||||||
socket->close();
|
socket->close();
|
||||||
|
|
||||||
qDebug() << "Wrote to client";
|
qDebug() << "Wrote to client";
|
||||||
@ -94,3 +90,52 @@ void HttpDaemon::discardClient()
|
|||||||
|
|
||||||
qDebug() << "Connection closed";
|
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>
|
#include <QTcpServer>
|
||||||
|
|
||||||
class Device;
|
class Device;
|
||||||
|
class DevicePlugin;
|
||||||
|
|
||||||
class HttpDaemon : public QTcpServer
|
class HttpDaemon : public QTcpServer
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
HttpDaemon(Device *device, QObject* parent = 0);
|
HttpDaemon(Device *device, DevicePlugin* parent = 0);
|
||||||
|
|
||||||
void incomingConnection(qintptr socket) override;
|
void incomingConnection(qintptr socket) override;
|
||||||
|
|
||||||
@ -18,15 +19,20 @@ public:
|
|||||||
void resume();
|
void resume();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void triggerEvent(int id);
|
void setState(const QUuid &stateTypeId, const QVariant &value);
|
||||||
|
void triggerEvent(const QUuid &eventTypeId);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void readClient();
|
void readClient();
|
||||||
void discardClient();
|
void discardClient();
|
||||||
|
|
||||||
|
private:
|
||||||
|
QString generateWebPage();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool disabled;
|
bool disabled;
|
||||||
|
|
||||||
|
DevicePlugin *m_plugin;
|
||||||
Device *m_device;
|
Device *m_device;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user