finish awattar prototype

This commit is contained in:
Simon Stürz 2016-01-16 15:05:21 +01:00 committed by Michael Zanetti
parent dfd8186c19
commit 5faef5b7b5
5 changed files with 99 additions and 26 deletions

View File

@ -84,6 +84,10 @@ DeviceManager::HardwareResources DevicePluginAwattar::requiredHardware() const
DeviceManager::DeviceSetupStatus DevicePluginAwattar::setupDevice(Device *device) DeviceManager::DeviceSetupStatus DevicePluginAwattar::setupDevice(Device *device)
{ {
if (!myDevices().isEmpty()) {
qCWarning(dcAwattar) << "Only one aWATTar device can be configured.";
}
QString token = device->paramValue("token").toString(); QString token = device->paramValue("token").toString();
qCDebug(dcAwattar) << "Setup device" << device->params(); qCDebug(dcAwattar) << "Setup device" << device->params();
@ -95,24 +99,17 @@ DeviceManager::DeviceSetupStatus DevicePluginAwattar::setupDevice(Device *device
void DevicePluginAwattar::startMonitoringAutoDevices() void DevicePluginAwattar::startMonitoringAutoDevices()
{ {
QHostAddress rplAddress = QHostAddress(configuration().paramValue("RPL address").toString()); searchHeatPumps();
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);
} }
void DevicePluginAwattar::deviceRemoved(Device *device) void DevicePluginAwattar::deviceRemoved(Device *device)
{ {
Q_UNUSED(device) Q_UNUSED(device)
foreach (HeatPump *pump, m_heatPumps) {
qCDebug(dcAwattar) << "Delete pump" << pump->address().toString();
pump->deleteLater();
}
} }
void DevicePluginAwattar::networkManagerReplyReady(QNetworkReply *reply) void DevicePluginAwattar::networkManagerReplyReady(QNetworkReply *reply)
@ -143,7 +140,7 @@ void DevicePluginAwattar::networkManagerReplyReady(QNetworkReply *reply)
processPriceData(device, jsonDoc.toVariant().toMap(), true); 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); m_updateUserData.insert(userReply, device);
} else if (m_updatePrice.keys().contains(reply)) { } else if (m_updatePrice.keys().contains(reply)) {
@ -167,7 +164,7 @@ void DevicePluginAwattar::networkManagerReplyReady(QNetworkReply *reply)
processPriceData(device, jsonDoc.toVariant().toMap()); 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); m_updateUserData.insert(userReply, device);
} else if (m_updateUserData.keys().contains(reply)) { } else if (m_updateUserData.keys().contains(reply)) {
@ -190,6 +187,7 @@ void DevicePluginAwattar::networkManagerReplyReady(QNetworkReply *reply)
} }
processUserData(device, jsonDoc.toVariant().toMap()); processUserData(device, jsonDoc.toVariant().toMap());
} else if (m_searchPumpReplies.contains(reply)) { } else if (m_searchPumpReplies.contains(reply)) {
m_searchPumpReplies.removeAll(reply); m_searchPumpReplies.removeAll(reply);
@ -211,6 +209,7 @@ void DevicePluginAwattar::guhTimer()
{ {
foreach (Device *device, myDevices()) { foreach (Device *device, myDevices()) {
//qCDebug(dcAwattar) << "Update device" << device->id().toString(); //qCDebug(dcAwattar) << "Update device" << device->id().toString();
searchHeatPumps();
updateDevice(device); updateDevice(device);
} }
} }
@ -226,7 +225,16 @@ DeviceManager::DeviceError DevicePluginAwattar::executeAction(Device *device, co
pump->setLed(action.param("led power").value().toBool()); 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; return DeviceManager::DeviceErrorNoError;
} }
@ -346,7 +354,8 @@ void DevicePluginAwattar::processUserData(Device *device, const QVariantMap &dat
device->setStateValue(sgModeStateTypeId, "4 - On"); device->setStateValue(sgModeStateTypeId, "4 - On");
break; break;
default: default:
break; device->setStateValue(sgModeStateTypeId, "0 - Invalid");
continue;
} }
foreach (HeatPump *pump, m_heatPumps) { foreach (HeatPump *pump, m_heatPumps) {
@ -368,12 +377,13 @@ void DevicePluginAwattar::processPumpSearchData(const QByteArray &data)
// remove the '/128' from the address // remove the '/128' from the address
QHostAddress pumpAddress(QString(data.left(line.length() - 4))); QHostAddress pumpAddress(QString(data.left(line.length() - 4)));
if (!pumpAddress.isNull()) { if (!pumpAddress.isNull()) {
qCDebug(dcAwattar) << "Found heat pump at" << pumpAddress.toString();
// check if we already created this heat pump // check if we already created this heat pump
if (heatPumpExists(pumpAddress)) if (heatPumpExists(pumpAddress))
continue; continue;
qCDebug(dcAwattar) << "Found heat pump at" << pumpAddress.toString();
HeatPump *pump = new HeatPump(pumpAddress, this); HeatPump *pump = new HeatPump(pumpAddress, this);
connect(pump, SIGNAL(reachableChanged()), this, SLOT(onHeatPumpReachableChanged())); connect(pump, SIGNAL(reachableChanged()), this, SLOT(onHeatPumpReachableChanged()));
@ -412,6 +422,23 @@ void DevicePluginAwattar::updateDevice(Device *device)
m_updatePrice.insert(priceReply, 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) bool DevicePluginAwattar::heatPumpExists(const QHostAddress &pumpAddress)
{ {
foreach (HeatPump *pump, m_heatPumps) { foreach (HeatPump *pump, m_heatPumps) {

View File

@ -65,8 +65,10 @@ private:
QNetworkReply *requestUserData(const QString& token, const QString &userId); QNetworkReply *requestUserData(const QString& token, const QString &userId);
void updateDevice(Device *device); void updateDevice(Device *device);
void searchHeatPumps();
bool heatPumpExists(const QHostAddress &pumpAddress); bool heatPumpExists(const QHostAddress &pumpAddress);
private slots: private slots:
void onHeatPumpReachableChanged(); void onHeatPumpReachableChanged();

View File

@ -34,7 +34,7 @@
"defaultValue": "Heating system" "defaultValue": "Heating system"
}, },
{ {
"name": "user id", "name": "user uuid",
"type": "QString", "type": "QString",
"inputType": "TextLine" "inputType": "TextLine"
}, },
@ -103,28 +103,46 @@
{ {
"id": "b83d3533-aeae-4a9b-95d8-28466bf6c0cf", "id": "b83d3533-aeae-4a9b-95d8-28466bf6c0cf",
"idName": "sgMode", "idName": "sgMode",
"name": "sg mode", "name": "sg-mode",
"type": "QString", "type": "QString",
"possibleValues": [ "possibleValues": [
"0 - Invalid",
"1 - Off", "1 - Off",
"2 - Normal", "2 - Normal",
"3 - High Temperature", "3 - High Temperature",
"4 - On" "4 - On"
], ],
"defaultValue": "1 - Off" "defaultValue": "0 - Invalid"
} }
], ],
"actionTypes": [ "actionTypes": [
{ {
"id": "5be2f57f-a22d-4766-856a-a31481bcf6d6", "id": "5be2f57f-a22d-4766-856a-a31481bcf6d6",
"idName": "ledPower", "idName": "ledPower",
"name": "led power", "name": "set led power",
"paramTypes": [ "paramTypes": [
{ {
"name": "power", "name": "power",
"type": "bool" "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"
]
}
]
} }
] ]
} }

View File

@ -27,7 +27,7 @@ HeatPump::HeatPump(QHostAddress address, QObject *parent) :
QObject(parent), QObject(parent),
m_address(address), m_address(address),
m_reachable(false), m_reachable(false),
m_sgMode(1) m_sgMode(0)
{ {
m_coap = new Coap(this); m_coap = new Coap(this);
@ -54,12 +54,18 @@ bool HeatPump::reachable() const
void HeatPump::setSgMode(const int &sgMode) void HeatPump::setSgMode(const int &sgMode)
{ {
if (m_sgMode != sgMode) {
m_sgMode = sgMode;
qCDebug(dcAwattar) << "Setting sg-mode to" << sgMode;
}
QUrl url; QUrl url;
url.setScheme("coap"); url.setScheme("coap");
url.setHost(m_address.toString()); url.setHost(m_address.toString());
url.setPath("/a/sg_mode"); 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) void HeatPump::setLed(const bool &power)
@ -70,9 +76,9 @@ void HeatPump::setLed(const bool &power)
url.setPath("/a/led"); url.setPath("/a/led");
if (power) { if (power) {
m_ledReplies.append(m_coap->post(CoapRequest(url), "mode=on")); m_ledReplies.append(m_coap->post(CoapRequest(url), "mode=1"));
} else { } 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; return;
} }
if (reply->statusCode() != CoapPdu::Content) {
qCWarning(dcAwattar()) << "Resource discovery:" << reply;
return;
}
qCDebug(dcAwattar) << "Discovered successfully the resources"; qCDebug(dcAwattar) << "Discovered successfully the resources";
CoreLinkParser parser(reply->payload()); CoreLinkParser parser(reply->payload());
foreach (const CoreLink &link, parser.links()) { foreach (const CoreLink &link, parser.links()) {
@ -112,6 +123,11 @@ void HeatPump::onReplyFinished(CoapReply *reply)
return; return;
} }
if (reply->statusCode() != CoapPdu::Content) {
qCWarning(dcAwattar()) << "Set sg-mode:" << reply;
return;
}
if (!reachable()) if (!reachable())
qCDebug(dcAwattar) << "Set sg-mode successfully."; qCDebug(dcAwattar) << "Set sg-mode successfully.";
@ -127,6 +143,11 @@ void HeatPump::onReplyFinished(CoapReply *reply)
return; return;
} }
if (reply->statusCode() != CoapPdu::Content) {
qCWarning(dcAwattar()) << "Set LED:" << reply;
return;
}
qCDebug(dcAwattar) << "Set led power successfully."; qCDebug(dcAwattar) << "Set led power successfully.";
} else { } else {
@ -138,6 +159,11 @@ void HeatPump::onReplyFinished(CoapReply *reply)
return; return;
} }
if (reply->statusCode() != CoapPdu::Content) {
qCWarning(dcAwattar()) << reply;
return;
}
qCDebug(dcAwattar) << reply; qCDebug(dcAwattar) << reply;
} }

View File

@ -622,7 +622,7 @@ void DeviceHandler::devicesDiscovered(const DeviceClassId &deviceClassId, const
void DeviceHandler::deviceSetupFinished(Device *device, DeviceManager::DeviceError status) 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())) { if (!m_asynDeviceAdditions.contains(device->id())) {
return; // Not the device we're waiting for... return; // Not the device we're waiting for...
} }