From 6859bd2b783c0cefcb25c009927d91f7046f6472 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Tue, 1 Apr 2014 01:24:15 +0200 Subject: [PATCH] add some states and another event to the mockdevice --- .../deviceplugins/mock/devicepluginmock.cpp | 40 ++++++--- plugins/deviceplugins/mock/devicepluginmock.h | 3 +- plugins/deviceplugins/mock/httpdaemon.cpp | 83 ++++++++++++++----- plugins/deviceplugins/mock/httpdaemon.h | 10 ++- 4 files changed, 104 insertions(+), 32 deletions(-) diff --git a/plugins/deviceplugins/mock/devicepluginmock.cpp b/plugins/deviceplugins/mock/devicepluginmock.cpp index 430e7b42..290d92d2 100644 --- a/plugins/deviceplugins/mock/devicepluginmock.cpp +++ b/plugins/deviceplugins/mock/devicepluginmock.cpp @@ -26,6 +26,9 @@ #include 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 DevicePluginMock::supportedDevices() const deviceClassMock.setParams(mockParams); -// QList detectorStates; + QList 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 mockEvents; @@ -69,6 +78,11 @@ QList 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(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); diff --git a/plugins/deviceplugins/mock/devicepluginmock.h b/plugins/deviceplugins/mock/devicepluginmock.h index 31fbf769..8d8f291e 100644 --- a/plugins/deviceplugins/mock/devicepluginmock.h +++ b/plugins/deviceplugins/mock/devicepluginmock.h @@ -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 m_daemons; diff --git a/plugins/deviceplugins/mock/httpdaemon.cpp b/plugins/deviceplugins/mock/httpdaemon.cpp index e890672e..c078c0d2 100644 --- a/plugins/deviceplugins/mock/httpdaemon.cpp +++ b/plugins/deviceplugins/mock/httpdaemon.cpp @@ -1,14 +1,17 @@ #include "httpdaemon.h" #include "device.h" +#include "deviceclass.h" +#include "deviceplugin.h" +#include "statetype.h" #include #include #include #include -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" - "" - "" - "

Mock device

\n" - "Name: %1
" - "ID: %2" - "
" - "" - "" - "
" - "" - "\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( + "" + "" + "

Mock device Controller

\n" + "Name: %1
" + "ID: %2
" + "DeviceClass ID: %3
").arg(m_device->name()).arg(m_device->id().toString()).arg(deviceClass.id().toString()); + body.append(""); + for (int i = 0; i < deviceClass.states().count(); ++i) { + body.append(""); + body.append(""); + const StateType &stateType = deviceClass.states().at(i); + body.append(""); + body.append(QString("").arg(stateType.id().toString()).arg(m_device->states().at(i).value().toString())); + body.append(""); + body.append(""); + body.append(""); + } + body.append("
" + stateType.name() + "
"); + + body.append(""); + for (int i = 0; i < deviceClass.events().count(); ++i) { + const EventType &eventType = deviceClass.events().at(i); + body.append(QString( + "" + "" + "" + "" + "" + "" + ).arg(eventType.name()).arg(eventType.id().toString())); + } + body.append("
Event %1
"); + + body.append("\n"); + + return contentHeader + body; +} diff --git a/plugins/deviceplugins/mock/httpdaemon.h b/plugins/deviceplugins/mock/httpdaemon.h index 4f85c2fb..128475f9 100644 --- a/plugins/deviceplugins/mock/httpdaemon.h +++ b/plugins/deviceplugins/mock/httpdaemon.h @@ -4,12 +4,13 @@ #include 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; };