wemo plugin: switch finished.

This commit is contained in:
Simon Stürz 2014-08-05 13:04:56 +02:00 committed by Michael Zanetti
parent b549b013b4
commit 7f26df1662
3 changed files with 57 additions and 11 deletions

View File

@ -140,9 +140,10 @@ QPair<DeviceManager::DeviceSetupStatus, QString> DevicePluginWemo::setupDevice(D
wemoSwitch->setDeviceType(device->paramValue("device type").toString());
connect(wemoSwitch,SIGNAL(stateChanged()),this,SLOT(wemoSwitchStateChanged()));
connect(wemoSwitch,SIGNAL(setPowerFinished(bool,ActionId)),this,SLOT(setPowerFinished(bool,ActionId)));
m_wemoSwitches.insert(wemoSwitch,device);
wemoSwitch->refresh();
return reportDeviceSetup();
}
return reportDeviceSetup(DeviceManager::DeviceSetupStatusSuccess);
@ -155,7 +156,18 @@ DeviceManager::HardwareResources DevicePluginWemo::requiredHardware() const
QPair<DeviceManager::DeviceError, QString> DevicePluginWemo::executeAction(Device *device, const Action &action)
{
return report();
if(device->deviceClassId() == wemoSwitchDeviceClassId){
if(action.actionTypeId() == powerActionTypeId){
WemoSwitch *wemoSwitch = m_wemoSwitches.key(device);
wemoSwitch->setPower(action.param("power").value().toBool(),action.id());
return report(DeviceManager::DeviceErrorAsync);
}else{
return report(DeviceManager::DeviceErrorActionTypeNotFound);
}
}
return report(DeviceManager::DeviceErrorDeviceClassNotFound);
}
void DevicePluginWemo::deviceRemoved(Device *device)
@ -181,8 +193,8 @@ PluginId DevicePluginWemo::pluginId() const
void DevicePluginWemo::guhTimer()
{
foreach (WemoSwitch* device, m_wemoSwitches.keys()) {
device->refresh();
foreach (WemoSwitch* wemoSwitch, m_wemoSwitches.keys()) {
wemoSwitch->refresh();
}
}
@ -210,9 +222,21 @@ void DevicePluginWemo::discoveryDone(QList<WemoSwitch *> deviceList)
void DevicePluginWemo::wemoSwitchStateChanged()
{
WemoSwitch *wemoSwitch = static_cast<WemoSwitch*>(sender());
if(m_wemoSwitches.contains(wemoSwitch)){
Device * device = m_wemoSwitches.value(wemoSwitch);
device->setStateValue(powerStateTypeId, wemoSwitch->powerState());
device->setStateValue(reachableStateTypeId, wemoSwitch->reachabel());
}
}
void DevicePluginWemo::setPowerFinished(const bool &succeeded, const ActionId &actionId)
{
if(succeeded){
emit actionExecutionFinished(actionId,DeviceManager::DeviceErrorNoError,QString());
}else{
emit actionExecutionFinished(actionId,DeviceManager::DeviceErrorDeviceNotFound,QString("Action could not be executed."));
}
}

View File

@ -53,6 +53,9 @@ public:
private slots:
void discoveryDone(QList<WemoSwitch *> deviceList);
void wemoSwitchStateChanged();
void setPowerFinished(const bool &succeeded, const ActionId &actionId);
public slots:

View File

@ -131,6 +131,11 @@ bool WemoSwitch::powerState()
return m_powerState;
}
bool WemoSwitch::reachabel()
{
return m_reachabel;
}
void WemoSwitch::replyFinished(QNetworkReply *reply)
{
if(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200){
@ -141,17 +146,27 @@ void WemoSwitch::replyFinished(QNetworkReply *reply)
m_reachabel = true;
}
// if this is the answerer to a refresh request
if(reply == m_refrashReplay){
QByteArray data = reply->readAll();
if(data.contains("<BinaryState>0</BinaryState>")){
qDebug() << "switch is off";
m_powerState = false;
}
if(data.contains("<BinaryState>1</BinaryState>")){
qDebug() << "switch is on";
m_powerState = true;
}
}
// if this is the answerer to a "set power" request
if(reply == m_setPowerReplay){
QByteArray data = reply->readAll();
if(data.contains("<BinaryState>1</BinaryState>") || data.contains("<BinaryState>0</BinaryState>")){
emit setPowerFinished(true,m_actionId);
}else{
emit setPowerFinished(false,m_actionId);
}
refresh();
}
emit stateChanged();
}
@ -172,13 +187,17 @@ void WemoSwitch::setPower(const bool &power, const ActionId &actionId)
{
m_actionId = actionId;
if(m_powerState == power){
emit setPowerFinished(true,actionId);
}
QByteArray setPowerMessage("<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:SetBinaryState xmlns:u=\"urn:Belkin:service:basicevent:1\"><BinaryState>" + QByteArray::number((int)power) + "</BinaryState></u:SetBinaryState></s:Body></s:Envelope>");
QNetworkRequest request;
request.setUrl(QUrl("http://" + m_hostAddress.toString() + ":" + QString::number(m_port) + "/upnp/control/basicevent1"));
request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("text/xml; charset=\"utf-8\""));
request.setHeader(QNetworkRequest::UserAgentHeader,QVariant("guh"));
request.setRawHeader("SOAPACTION", "\"urn:Belkin:service:basicevent:1#GetBinaryState\"");
request.setRawHeader("SOAPACTION", "\"urn:Belkin:service:basicevent:1#SetBinaryState\"");
m_refrashReplay = m_manager->post(request,setPowerMessage);
m_setPowerReplay = m_manager->post(request,setPowerMessage);
}