diff --git a/plugins/deviceplugins/awattar/devicepluginawattar.cpp b/plugins/deviceplugins/awattar/devicepluginawattar.cpp index 1039d769..adde2998 100644 --- a/plugins/deviceplugins/awattar/devicepluginawattar.cpp +++ b/plugins/deviceplugins/awattar/devicepluginawattar.cpp @@ -84,6 +84,10 @@ DeviceManager::HardwareResources DevicePluginAwattar::requiredHardware() const DeviceManager::DeviceSetupStatus DevicePluginAwattar::setupDevice(Device *device) { + if (!myDevices().isEmpty()) { + qCWarning(dcAwattar) << "Only one aWATTar device can be configured."; + } + QString token = device->paramValue("token").toString(); qCDebug(dcAwattar) << "Setup device" << device->params(); @@ -95,24 +99,17 @@ DeviceManager::DeviceSetupStatus DevicePluginAwattar::setupDevice(Device *device void DevicePluginAwattar::startMonitoringAutoDevices() { - QHostAddress rplAddress = QHostAddress(configuration().paramValue("RPL address").toString()); - - if (rplAddress.isNull()) { - qCWarning(dcAwattar) << "Invalid RPL address" << configuration().paramValue("RPL address").toString(); - return; - } - - qCDebug(dcAwattar) << "Search heat pump" << rplAddress.toString(); - - QNetworkRequest request(QUrl(QString("http://[%1]").arg(rplAddress.toString()))); - QNetworkReply *reply = networkManagerGet(request); - - m_searchPumpReplies.append(reply); + searchHeatPumps(); } void DevicePluginAwattar::deviceRemoved(Device *device) { Q_UNUSED(device) + + foreach (HeatPump *pump, m_heatPumps) { + qCDebug(dcAwattar) << "Delete pump" << pump->address().toString(); + pump->deleteLater(); + } } void DevicePluginAwattar::networkManagerReplyReady(QNetworkReply *reply) @@ -143,7 +140,7 @@ void DevicePluginAwattar::networkManagerReplyReady(QNetworkReply *reply) processPriceData(device, jsonDoc.toVariant().toMap(), true); - QNetworkReply *userReply = requestUserData(device->paramValue("token").toString(), device->paramValue("user id").toString()); + QNetworkReply *userReply = requestUserData(device->paramValue("token").toString(), device->paramValue("user uuid").toString()); m_updateUserData.insert(userReply, device); } else if (m_updatePrice.keys().contains(reply)) { @@ -167,7 +164,7 @@ void DevicePluginAwattar::networkManagerReplyReady(QNetworkReply *reply) processPriceData(device, jsonDoc.toVariant().toMap()); - QNetworkReply *userReply = requestUserData(device->paramValue("token").toString(), device->paramValue("user id").toString()); + QNetworkReply *userReply = requestUserData(device->paramValue("token").toString(), device->paramValue("user uuid").toString()); m_updateUserData.insert(userReply, device); } else if (m_updateUserData.keys().contains(reply)) { @@ -190,6 +187,7 @@ void DevicePluginAwattar::networkManagerReplyReady(QNetworkReply *reply) } processUserData(device, jsonDoc.toVariant().toMap()); + } else if (m_searchPumpReplies.contains(reply)) { m_searchPumpReplies.removeAll(reply); @@ -211,6 +209,7 @@ void DevicePluginAwattar::guhTimer() { foreach (Device *device, myDevices()) { //qCDebug(dcAwattar) << "Update device" << device->id().toString(); + searchHeatPumps(); updateDevice(device); } } @@ -226,7 +225,16 @@ DeviceManager::DeviceError DevicePluginAwattar::executeAction(Device *device, co pump->setLed(action.param("led power").value().toBool()); } + } else if (action.actionTypeId() == sgModeActionTypeId) { + foreach (HeatPump *pump, m_heatPumps) { + if (!pump->reachable()) + return DeviceManager::DeviceErrorHardwareNotAvailable; + + pump->setSgMode(action.param("sg-mode").value().toInt()); + } } + + return DeviceManager::DeviceErrorNoError; } @@ -346,7 +354,8 @@ void DevicePluginAwattar::processUserData(Device *device, const QVariantMap &dat device->setStateValue(sgModeStateTypeId, "4 - On"); break; default: - break; + device->setStateValue(sgModeStateTypeId, "0 - Invalid"); + continue; } foreach (HeatPump *pump, m_heatPumps) { @@ -368,12 +377,13 @@ void DevicePluginAwattar::processPumpSearchData(const QByteArray &data) // remove the '/128' from the address QHostAddress pumpAddress(QString(data.left(line.length() - 4))); if (!pumpAddress.isNull()) { - qCDebug(dcAwattar) << "Found heat pump at" << pumpAddress.toString(); // check if we already created this heat pump if (heatPumpExists(pumpAddress)) continue; + qCDebug(dcAwattar) << "Found heat pump at" << pumpAddress.toString(); + HeatPump *pump = new HeatPump(pumpAddress, this); connect(pump, SIGNAL(reachableChanged()), this, SLOT(onHeatPumpReachableChanged())); @@ -412,6 +422,23 @@ void DevicePluginAwattar::updateDevice(Device *device) m_updatePrice.insert(priceReply, device); } +void DevicePluginAwattar::searchHeatPumps() +{ + QHostAddress rplAddress = QHostAddress(configuration().paramValue("RPL address").toString()); + + if (rplAddress.isNull()) { + qCWarning(dcAwattar) << "Invalid RPL address" << configuration().paramValue("RPL address").toString(); + return; + } + + //qCDebug(dcAwattar) << "Search heat pump" << rplAddress.toString(); + + QNetworkRequest request(QUrl(QString("http://[%1]").arg(rplAddress.toString()))); + QNetworkReply *reply = networkManagerGet(request); + + m_searchPumpReplies.append(reply); +} + bool DevicePluginAwattar::heatPumpExists(const QHostAddress &pumpAddress) { foreach (HeatPump *pump, m_heatPumps) { diff --git a/plugins/deviceplugins/awattar/devicepluginawattar.h b/plugins/deviceplugins/awattar/devicepluginawattar.h index 384c2ad2..c508e056 100644 --- a/plugins/deviceplugins/awattar/devicepluginawattar.h +++ b/plugins/deviceplugins/awattar/devicepluginawattar.h @@ -65,8 +65,10 @@ private: QNetworkReply *requestUserData(const QString& token, const QString &userId); void updateDevice(Device *device); + void searchHeatPumps(); bool heatPumpExists(const QHostAddress &pumpAddress); + private slots: void onHeatPumpReachableChanged(); diff --git a/plugins/deviceplugins/awattar/devicepluginawattar.json b/plugins/deviceplugins/awattar/devicepluginawattar.json index caeb0563..e2646c18 100644 --- a/plugins/deviceplugins/awattar/devicepluginawattar.json +++ b/plugins/deviceplugins/awattar/devicepluginawattar.json @@ -34,7 +34,7 @@ "defaultValue": "Heating system" }, { - "name": "user id", + "name": "user uuid", "type": "QString", "inputType": "TextLine" }, @@ -103,28 +103,46 @@ { "id": "b83d3533-aeae-4a9b-95d8-28466bf6c0cf", "idName": "sgMode", - "name": "sg mode", + "name": "sg-mode", "type": "QString", "possibleValues": [ + "0 - Invalid", "1 - Off", "2 - Normal", "3 - High Temperature", "4 - On" ], - "defaultValue": "1 - Off" + "defaultValue": "0 - Invalid" } ], "actionTypes": [ { "id": "5be2f57f-a22d-4766-856a-a31481bcf6d6", "idName": "ledPower", - "name": "led power", + "name": "set led power", "paramTypes": [ { "name": "power", "type": "bool" } ] + }, + { + "id": "03af6639-a815-4291-abbb-26fc81cdf740", + "idName": "sgMode", + "name": "set sg-mode", + "paramTypes": [ + { + "name": "sg-mode", + "type": "QString", + "allowedValues": [ + "1", + "2", + "3", + "4" + ] + } + ] } ] } diff --git a/plugins/deviceplugins/awattar/heatpump.cpp b/plugins/deviceplugins/awattar/heatpump.cpp index 056171a9..c9b78cad 100644 --- a/plugins/deviceplugins/awattar/heatpump.cpp +++ b/plugins/deviceplugins/awattar/heatpump.cpp @@ -27,7 +27,7 @@ HeatPump::HeatPump(QHostAddress address, QObject *parent) : QObject(parent), m_address(address), m_reachable(false), - m_sgMode(1) + m_sgMode(0) { m_coap = new Coap(this); @@ -54,12 +54,18 @@ bool HeatPump::reachable() const void HeatPump::setSgMode(const int &sgMode) { + if (m_sgMode != sgMode) { + m_sgMode = sgMode; + qCDebug(dcAwattar) << "Setting sg-mode to" << sgMode; + } + QUrl url; url.setScheme("coap"); url.setHost(m_address.toString()); url.setPath("/a/sg_mode"); - m_sgModeReplies.append(m_coap->post(CoapRequest(url), QByteArray::number(sgMode))); + QByteArray payload = QString("mode=%1").arg(QString::number(sgMode)).toUtf8(); + m_sgModeReplies.append(m_coap->post(CoapRequest(url), payload)); } void HeatPump::setLed(const bool &power) @@ -70,9 +76,9 @@ void HeatPump::setLed(const bool &power) url.setPath("/a/led"); if (power) { - m_ledReplies.append(m_coap->post(CoapRequest(url), "mode=on")); + m_ledReplies.append(m_coap->post(CoapRequest(url), "mode=1")); } else { - m_ledReplies.append(m_coap->post(CoapRequest(url), "mode=off")); + m_ledReplies.append(m_coap->post(CoapRequest(url), "mode=0")); } } @@ -94,6 +100,11 @@ void HeatPump::onReplyFinished(CoapReply *reply) return; } + if (reply->statusCode() != CoapPdu::Content) { + qCWarning(dcAwattar()) << "Resource discovery:" << reply; + return; + } + qCDebug(dcAwattar) << "Discovered successfully the resources"; CoreLinkParser parser(reply->payload()); foreach (const CoreLink &link, parser.links()) { @@ -112,6 +123,11 @@ void HeatPump::onReplyFinished(CoapReply *reply) return; } + if (reply->statusCode() != CoapPdu::Content) { + qCWarning(dcAwattar()) << "Set sg-mode:" << reply; + return; + } + if (!reachable()) qCDebug(dcAwattar) << "Set sg-mode successfully."; @@ -127,6 +143,11 @@ void HeatPump::onReplyFinished(CoapReply *reply) return; } + if (reply->statusCode() != CoapPdu::Content) { + qCWarning(dcAwattar()) << "Set LED:" << reply; + return; + } + qCDebug(dcAwattar) << "Set led power successfully."; } else { @@ -138,6 +159,11 @@ void HeatPump::onReplyFinished(CoapReply *reply) return; } + if (reply->statusCode() != CoapPdu::Content) { + qCWarning(dcAwattar()) << reply; + return; + } + qCDebug(dcAwattar) << reply; } diff --git a/server/jsonrpc/devicehandler.cpp b/server/jsonrpc/devicehandler.cpp index 1f4143ce..783a0841 100644 --- a/server/jsonrpc/devicehandler.cpp +++ b/server/jsonrpc/devicehandler.cpp @@ -622,7 +622,7 @@ void DeviceHandler::devicesDiscovered(const DeviceClassId &deviceClassId, const void DeviceHandler::deviceSetupFinished(Device *device, DeviceManager::DeviceError status) { - qCDebug(dcJsonRpc) << "got a device setup finished"; + qCDebug(dcJsonRpc) << "Got a device setup finished" << device->name() << device->id(); if (!m_asynDeviceAdditions.contains(device->id())) { return; // Not the device we're waiting for... }