Improve LG smarttv plugin

This commit is contained in:
Simon Stürz 2019-01-06 10:01:02 +01:00 committed by Michael Zanetti
parent 364f7e5499
commit 7027b9f9d1
7 changed files with 83 additions and 60 deletions

View File

@ -57,13 +57,12 @@ DevicePluginLgSmartTv::DevicePluginLgSmartTv()
DevicePluginLgSmartTv::~DevicePluginLgSmartTv() DevicePluginLgSmartTv::~DevicePluginLgSmartTv()
{ {
hardwareManager()->pluginTimerManager()->unregisterTimer(m_pluginTimer);
} }
void DevicePluginLgSmartTv::init() void DevicePluginLgSmartTv::init()
{ {
m_pluginTimer = hardwareManager()->pluginTimerManager()->registerTimer(15);
connect(m_pluginTimer, &PluginTimer::timeout, this, &DevicePluginLgSmartTv::onPluginTimer);
} }
DeviceManager::DeviceError DevicePluginLgSmartTv::discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params) DeviceManager::DeviceError DevicePluginLgSmartTv::discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params)
@ -79,52 +78,61 @@ DeviceManager::DeviceError DevicePluginLgSmartTv::discoverDevices(const DeviceCl
DeviceManager::DeviceSetupStatus DevicePluginLgSmartTv::setupDevice(Device *device) DeviceManager::DeviceSetupStatus DevicePluginLgSmartTv::setupDevice(Device *device)
{ {
if (device->deviceClassId() != lgSmartTvDeviceClassId) { qCDebug(dcLgSmartTv()) << "Setup LG smart TV" << device->name() << device->params();
return DeviceManager::DeviceSetupStatusFailure; QHostAddress address = QHostAddress(device->paramValue(lgSmartTvDeviceHostAddressParamTypeId).toString());
} TvDevice *tvDevice = new TvDevice(address, device->paramValue(lgSmartTvDevicePortParamTypeId).toInt(), this);
TvDevice *tvDevice = new TvDevice(QHostAddress(device->paramValue(lgSmartTvDeviceHostAddressParamTypeId).toString()),
device->paramValue(lgSmartTvDevicePortParamTypeId).toInt(), this);
tvDevice->setUuid(device->paramValue(lgSmartTvDeviceUuidParamTypeId).toString()); tvDevice->setUuid(device->paramValue(lgSmartTvDeviceUuidParamTypeId).toString());
// if the key is missing, this setup call comes from a pairing procedure // If the key is missing, this setup call comes from a pairing procedure
if (device->paramValue(lgSmartTvDeviceKeyParamTypeId) == QString()) { if (device->paramValue(lgSmartTvDeviceKeyParamTypeId) == QString()) {
// check if we know the key from the pairing procedure // Check if we know the key from the pairing procedure
if (!m_tvKeys.contains(device->paramValue(lgSmartTvDeviceUuidParamTypeId).toString())) { if (!m_tvKeys.contains(device->paramValue(lgSmartTvDeviceUuidParamTypeId).toString())) {
qCWarning(dcLgSmartTv) << "could not find any pairing key"; qCWarning(dcLgSmartTv) << "could not find any pairing key";
return DeviceManager::DeviceSetupStatusFailure; return DeviceManager::DeviceSetupStatusFailure;
} }
// use the key from the pairing procedure // Use the key from the pairing procedure
QString key = m_tvKeys.value(device->paramValue(lgSmartTvDeviceUuidParamTypeId).toString()); QString key = m_tvKeys.value(device->paramValue(lgSmartTvDeviceUuidParamTypeId).toString());
tvDevice->setKey(key); tvDevice->setKey(key);
device->setParamValue(lgSmartTvDeviceKeyParamTypeId, key); device->setParamValue(lgSmartTvDeviceKeyParamTypeId, key);
} else { } else {
// add the key for editing //Add the key for editing
if (!m_tvKeys.contains(device->paramValue(lgSmartTvDeviceUuidParamTypeId).toString())) { if (!m_tvKeys.contains(device->paramValue(lgSmartTvDeviceUuidParamTypeId).toString())) {
m_tvKeys.insert(tvDevice->uuid(), tvDevice->key()); m_tvKeys.insert(tvDevice->uuid(), tvDevice->key());
} }
} }
connect(tvDevice, &TvDevice::stateChanged, this, &DevicePluginLgSmartTv::stateChanged); connect(tvDevice, &TvDevice::stateChanged, this, &DevicePluginLgSmartTv::stateChanged);
m_tvList.insert(tvDevice, device); m_tvList.insert(tvDevice, device);
pairTvDevice(device, true);
return DeviceManager::DeviceSetupStatusAsync;
if (!m_pluginTimer) {
m_pluginTimer = hardwareManager()->pluginTimerManager()->registerTimer(5);
connect(m_pluginTimer, &PluginTimer::timeout, this, &DevicePluginLgSmartTv::onPluginTimer);
}
return DeviceManager::DeviceSetupStatusSuccess;
} }
void DevicePluginLgSmartTv::deviceRemoved(Device *device) void DevicePluginLgSmartTv::deviceRemoved(Device *device)
{ {
if (!m_tvList.values().contains(device)) { if (!m_tvList.values().contains(device))
return; return;
}
TvDevice *tvDevice= m_tvList.key(device); TvDevice *tvDevice= m_tvList.key(device);
qCDebug(dcLgSmartTv) << "Remove device" << device->name(); qCDebug(dcLgSmartTv) << "Remove device" << device->name();
unpairTvDevice(device); unpairTvDevice(device);
m_tvList.remove(tvDevice); m_tvList.remove(tvDevice);
delete tvDevice; delete tvDevice;
if (m_tvList.isEmpty() && m_pluginTimer) {
hardwareManager()->pluginTimerManager()->unregisterTimer(m_pluginTimer);
m_pluginTimer = nullptr;
}
}
void DevicePluginLgSmartTv::postSetupDevice(Device *device)
{
pairTvDevice(device);
} }
DeviceManager::DeviceError DevicePluginLgSmartTv::executeAction(Device *device, const Action &action) DeviceManager::DeviceError DevicePluginLgSmartTv::executeAction(Device *device, const Action &action)
@ -262,8 +270,9 @@ DeviceManager::DeviceSetupStatus DevicePluginLgSmartTv::confirmPairing(const Pai
return DeviceManager::DeviceSetupStatusAsync; return DeviceManager::DeviceSetupStatusAsync;
} }
void DevicePluginLgSmartTv::pairTvDevice(Device *device, const bool &setup) void DevicePluginLgSmartTv::pairTvDevice(Device *device)
{ {
qCDebug(dcLgSmartTv()) << "Send pair request TV" << device->name();
QHostAddress host = QHostAddress(device->paramValue(lgSmartTvDeviceHostAddressParamTypeId).toString()); QHostAddress host = QHostAddress(device->paramValue(lgSmartTvDeviceHostAddressParamTypeId).toString());
int port = device->paramValue(lgSmartTvDevicePortParamTypeId).toInt(); int port = device->paramValue(lgSmartTvDevicePortParamTypeId).toInt();
QString key = device->paramValue(lgSmartTvDeviceKeyParamTypeId).toString(); QString key = device->paramValue(lgSmartTvDeviceKeyParamTypeId).toString();
@ -271,11 +280,7 @@ void DevicePluginLgSmartTv::pairTvDevice(Device *device, const bool &setup)
QNetworkReply *reply = hardwareManager()->networkManager()->post(request.first, request.second); QNetworkReply *reply = hardwareManager()->networkManager()->post(request.first, request.second);
connect(reply, &QNetworkReply::finished, this, &DevicePluginLgSmartTv::onNetworkManagerReplyFinished); connect(reply, &QNetworkReply::finished, this, &DevicePluginLgSmartTv::onNetworkManagerReplyFinished);
if (setup) { m_asyncSetup.insert(reply, device);
m_asyncSetup.insert(reply, device);
} else {
m_pairRequests.insert(reply, device);
}
} }
void DevicePluginLgSmartTv::unpairTvDevice(Device *device) void DevicePluginLgSmartTv::unpairTvDevice(Device *device)
@ -297,10 +302,6 @@ void DevicePluginLgSmartTv::refreshTv(Device *device)
connect(volumeReply, &QNetworkReply::finished, this, &DevicePluginLgSmartTv::onNetworkManagerReplyFinished); connect(volumeReply, &QNetworkReply::finished, this, &DevicePluginLgSmartTv::onNetworkManagerReplyFinished);
m_volumeInfoRequests.insert(volumeReply, device); m_volumeInfoRequests.insert(volumeReply, device);
// check channel information
QNetworkReply *channelReply = hardwareManager()->networkManager()->get(tv->createChannelInformationRequest());
connect(channelReply, &QNetworkReply::finished, this, &DevicePluginLgSmartTv::onNetworkManagerReplyFinished);
m_channelInfoRequests.insert(channelReply, device);
} }
void DevicePluginLgSmartTv::onPluginTimer() void DevicePluginLgSmartTv::onPluginTimer()
@ -348,9 +349,11 @@ void DevicePluginLgSmartTv::onUpnpDiscoveryFinished()
void DevicePluginLgSmartTv::onNetworkManagerReplyFinished() void DevicePluginLgSmartTv::onNetworkManagerReplyFinished()
{ {
QNetworkReply *reply = static_cast<QNetworkReply *>(sender()); QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
reply->deleteLater();
if (m_showPinReply.contains(reply)) { if (m_showPinReply.contains(reply)) {
m_showPinReply.removeAll(reply); m_showPinReply.removeAll(reply);
if (status != 200) { if (status != 200) {
@ -382,17 +385,15 @@ void DevicePluginLgSmartTv::onNetworkManagerReplyFinished()
if(status != 200) { if(status != 200) {
qCWarning(dcLgSmartTv) << "Pair TV request error:" << status << reply->errorString(); qCWarning(dcLgSmartTv) << "Pair TV request error:" << status << reply->errorString();
tv->setPaired(false); tv->setPaired(false);
emit deviceSetupFinished(device, DeviceManager::DeviceSetupStatusFailure);
} else { } else {
qCDebug(dcLgSmartTv) << "Paired TV successfully."; qCDebug(dcLgSmartTv) << "Paired TV successfully.";
tv->setPaired(true); tv->setPaired(true);
refreshTv(device); refreshTv(device);
emit deviceSetupFinished(device, DeviceManager::DeviceSetupStatusSuccess);
} }
} else if (m_deleteTv.contains(reply)) { } else if (m_deleteTv.contains(reply)) {
m_deleteTv.removeAll(reply); m_deleteTv.removeAll(reply);
if(status != 200) { if(status != 200) {
qCWarning(dcLgSmartTv) << "Rnd pairing TV (device deleted) request error:" << status << reply->errorString(); qCWarning(dcLgSmartTv) << "End pairing TV (device deleted) request error:" << status << reply->errorString();
} else { } else {
qCDebug(dcLgSmartTv) << "End pairing TV (device deleted) successfully."; qCDebug(dcLgSmartTv) << "End pairing TV (device deleted) successfully.";
} }
@ -402,9 +403,18 @@ void DevicePluginLgSmartTv::onNetworkManagerReplyFinished()
if(status != 200) { if(status != 200) {
tv->setReachable(false); tv->setReachable(false);
qCWarning(dcLgSmartTv) << "Volume information request error:" << status << reply->errorString(); qCWarning(dcLgSmartTv) << "Volume information request error:" << status << reply->errorString();
if (status == 401) {
qCDebug(dcLgSmartTv()) << status << reply->errorString();
pairTvDevice(device);
}
} else { } else {
tv->setReachable(true); tv->setReachable(true);
tv->onVolumeInformationUpdate(reply->readAll()); tv->onVolumeInformationUpdate(reply->readAll());
// Check channel information
QNetworkReply *channelReply = hardwareManager()->networkManager()->get(tv->createChannelInformationRequest());
connect(channelReply, &QNetworkReply::finished, this, &DevicePluginLgSmartTv::onNetworkManagerReplyFinished);
m_channelInfoRequests.insert(channelReply, device);
} }
} else if (m_channelInfoRequests.keys().contains(reply)) { } else if (m_channelInfoRequests.keys().contains(reply)) {
Device *device = m_channelInfoRequests.take(reply); Device *device = m_channelInfoRequests.take(reply);
@ -412,6 +422,10 @@ void DevicePluginLgSmartTv::onNetworkManagerReplyFinished()
if(status != 200) { if(status != 200) {
tv->setReachable(false); tv->setReachable(false);
qCWarning(dcLgSmartTv) << "Channel information request error:" << status << reply->errorString(); qCWarning(dcLgSmartTv) << "Channel information request error:" << status << reply->errorString();
if (status == 401) {
qCDebug(dcLgSmartTv()) << status << reply->errorString();
pairTvDevice(device);
}
} else { } else {
tv->setReachable(true); tv->setReachable(true);
tv->onChannelInformationUpdate(reply->readAll()); tv->onChannelInformationUpdate(reply->readAll());
@ -425,7 +439,6 @@ void DevicePluginLgSmartTv::onNetworkManagerReplyFinished()
emit actionExecutionFinished(actionId, DeviceManager::DeviceErrorNoError); emit actionExecutionFinished(actionId, DeviceManager::DeviceErrorNoError);
} }
} }
reply->deleteLater();
} }
void DevicePluginLgSmartTv::stateChanged() void DevicePluginLgSmartTv::stateChanged()
@ -433,7 +446,7 @@ void DevicePluginLgSmartTv::stateChanged()
TvDevice *tvDevice = static_cast<TvDevice*>(sender()); TvDevice *tvDevice = static_cast<TvDevice*>(sender());
Device *device = m_tvList.value(tvDevice); Device *device = m_tvList.value(tvDevice);
device->setStateValue(lgSmartTvReachableStateTypeId, tvDevice->reachable()); device->setStateValue(lgSmartTvConnectedStateTypeId, tvDevice->reachable());
device->setStateValue(lgSmartTvTv3DModeStateTypeId, tvDevice->is3DMode()); device->setStateValue(lgSmartTvTv3DModeStateTypeId, tvDevice->is3DMode());
device->setStateValue(lgSmartTvTvVolumeLevelStateTypeId, tvDevice->volumeLevel()); device->setStateValue(lgSmartTvTvVolumeLevelStateTypeId, tvDevice->volumeLevel());
device->setStateValue(lgSmartTvTvMuteStateTypeId, tvDevice->mute()); device->setStateValue(lgSmartTvTvMuteStateTypeId, tvDevice->mute());

View File

@ -43,6 +43,7 @@ public:
DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params) override; DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params) override;
DeviceManager::DeviceSetupStatus setupDevice(Device *device) override; DeviceManager::DeviceSetupStatus setupDevice(Device *device) override;
void deviceRemoved(Device *device) override; void deviceRemoved(Device *device) override;
void postSetupDevice(Device *device) override;
DeviceManager::DeviceError executeAction(Device *device, const Action &action) override; DeviceManager::DeviceError executeAction(Device *device, const Action &action) override;
DeviceManager::DeviceError displayPin(const PairingTransactionId &pairingTransactionId, const DeviceDescriptor &deviceDescriptor) override; DeviceManager::DeviceError displayPin(const PairingTransactionId &pairingTransactionId, const DeviceDescriptor &deviceDescriptor) override;
@ -60,7 +61,6 @@ private:
// async setup // async setup
QHash<QNetworkReply *, Device *> m_asyncSetup; QHash<QNetworkReply *, Device *> m_asyncSetup;
QHash<QNetworkReply *, Device *> m_pairRequests;
QList<QNetworkReply *> m_deleteTv; QList<QNetworkReply *> m_deleteTv;
// action requests // action requests
@ -70,7 +70,7 @@ private:
QHash<QNetworkReply *, Device *> m_volumeInfoRequests; QHash<QNetworkReply *, Device *> m_volumeInfoRequests;
QHash<QNetworkReply *, Device *> m_channelInfoRequests; QHash<QNetworkReply *, Device *> m_channelInfoRequests;
void pairTvDevice(Device *device, const bool &setup = false); void pairTvDevice(Device *device);
void unpairTvDevice(Device *device); void unpairTvDevice(Device *device);
void refreshTv(Device *device); void refreshTv(Device *device);

View File

@ -13,14 +13,9 @@
"name": "lgSmartTv", "name": "lgSmartTv",
"displayName": "LG Smart Tv", "displayName": "LG Smart Tv",
"deviceIcon": "Tv", "deviceIcon": "Tv",
"basicTags": [ "createMethods": [ "discovery" ],
"Device",
"Multimedia",
"Actuator"
],
"createMethods": ["discovery"],
"setupMethod": "displayPin", "setupMethod": "displayPin",
"criticalStateTypeId": "b056c36b-df87-4177-8d5d-1e7c1e8cdc7a", "interfaces": ["connectable"],
"pairingInfo": "Please enter the pairing key displayed on the Tv.", "pairingInfo": "Please enter the pairing key displayed on the Tv.",
"paramTypes": [ "paramTypes": [
{ {
@ -68,9 +63,9 @@
"stateTypes": [ "stateTypes": [
{ {
"id": "b056c36b-df87-4177-8d5d-1e7c1e8cdc7a", "id": "b056c36b-df87-4177-8d5d-1e7c1e8cdc7a",
"name": "reachable", "name": "connected",
"displayName": "reachable", "displayName": "Reachable",
"displayNameEvent": "reachable changed", "displayNameEvent": "Reachable changed",
"type": "bool", "type": "bool",
"defaultValue": false "defaultValue": false
}, },
@ -86,7 +81,7 @@
"id": "07d39a6e-7eab-42d0-851d-9f3bcd3bbb57", "id": "07d39a6e-7eab-42d0-851d-9f3bcd3bbb57",
"name": "tvVolumeLevel", "name": "tvVolumeLevel",
"displayName": "volume", "displayName": "volume",
"displayNameEvent": "volume changed", "displayNameEvent": "Volume changed",
"type": "int", "type": "int",
"ruleRelevant": false, "ruleRelevant": false,
"eventRuleRelevant": false, "eventRuleRelevant": false,

View File

@ -36,7 +36,6 @@ TvDevice::TvDevice(const QHostAddress &hostAddress, const int &port, QObject *pa
m_channelNumber(-1) m_channelNumber(-1)
{ {
m_eventHandler = new TvEventHandler(hostAddress, port, this); m_eventHandler = new TvEventHandler(hostAddress, port, this);
connect(m_eventHandler, &TvEventHandler::eventOccured, this, &TvDevice::eventOccured); connect(m_eventHandler, &TvEventHandler::eventOccured, this, &TvDevice::eventOccured);
} }
@ -96,6 +95,7 @@ bool TvDevice::paired() const
void TvDevice::setReachable(const bool &reachable) void TvDevice::setReachable(const bool &reachable)
{ {
if (m_reachable != reachable) { if (m_reachable != reachable) {
qCDebug(dcLgSmartTv()) << "TV Event handler" << (reachable ? "reachable" : "not reachable any more");
m_reachable = reachable; m_reachable = reachable;
emit stateChanged(); emit stateChanged();
} }
@ -200,6 +200,19 @@ QPair<QNetworkRequest, QByteArray> TvDevice::createEndPairingRequest(const QHost
return QPair<QNetworkRequest, QByteArray>(request, data); return QPair<QNetworkRequest, QByteArray>(request, data);
} }
QPair<QNetworkRequest, QByteArray> TvDevice::createEventRequest(const QHostAddress &host, const int &port)
{
QString urlString = "http://" + host.toString() + ":" + QString::number(port) + "/udap/api/event";
QNetworkRequest request;
request.setUrl(QUrl(urlString));
request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("text/xml; charset=utf-8"));
request.setHeader(QNetworkRequest::UserAgentHeader,QVariant("UDAP/2.0 nymea"));
request.setRawHeader("Connection", "Close");
QByteArray data = "<?xml version=\"1.0\" encoding=\"utf-8\"?><envelope><api type=\"event\"><name>byebye</name><port>8080</port></api></envelope>";
return QPair<QNetworkRequest, QByteArray>(request, data);
}
QNetworkRequest TvDevice::createVolumeInformationRequest() QNetworkRequest TvDevice::createVolumeInformationRequest()
{ {
QString urlString = "http://" + hostAddress().toString() + ":" + QString::number(port()) + "/udap/api/data?target=volume_info"; QString urlString = "http://" + hostAddress().toString() + ":" + QString::number(port()) + "/udap/api/data?target=volume_info";
@ -239,9 +252,8 @@ QPair<QNetworkRequest, QByteArray> TvDevice::createPressButtonRequest(const TvDe
void TvDevice::onVolumeInformationUpdate(const QByteArray &data) void TvDevice::onVolumeInformationUpdate(const QByteArray &data)
{ {
//qCDebug(dcLgSmartTv) << printXmlData(data); //qCDebug(dcLgSmartTv()) << printXmlData(data);
QXmlStreamReader xml(data); QXmlStreamReader xml(data);
while(!xml.atEnd() && !xml.hasError()) { while(!xml.atEnd() && !xml.hasError()) {
xml.readNext(); xml.readNext();
@ -257,7 +269,7 @@ void TvDevice::onVolumeInformationUpdate(const QByteArray &data)
void TvDevice::onChannelInformationUpdate(const QByteArray &data) void TvDevice::onChannelInformationUpdate(const QByteArray &data)
{ {
//qCDebug(dcLgSmartTv) << printXmlData(data); //qCDebug(dcLgSmartTv()) << printXmlData(data);
QXmlStreamReader xml(data); QXmlStreamReader xml(data);
while(!xml.atEnd() && !xml.hasError()) { while(!xml.atEnd() && !xml.hasError()) {
@ -299,14 +311,14 @@ QString TvDevice::printXmlData(const QByteArray &data)
} }
} }
if(reader.hasError()) { if(reader.hasError()) {
qCWarning(dcLgSmartTv) << "error reading XML device information:" << reader.errorString(); qCWarning(dcLgSmartTv()) << "error reading XML device information:" << reader.errorString();
} }
return xmlOut; return xmlOut;
} }
void TvDevice::eventOccured(const QByteArray &data) void TvDevice::eventOccured(const QByteArray &data)
{ {
qCDebug(dcLgSmartTv) << "Event handler data received" << printXmlData(data); qCDebug(dcLgSmartTv()) << "Event handler data received" << printXmlData(data);
// if we got a channel changed event... // if we got a channel changed event...
if(data.contains("ChannelChanged")) { if(data.contains("ChannelChanged")) {
@ -319,7 +331,7 @@ void TvDevice::eventOccured(const QByteArray &data)
// if the tv suspends, it will send a byebye message, which means // if the tv suspends, it will send a byebye message, which means
// the pairing will be closed. // the pairing will be closed.
if(data.contains("api type=\"pairing\"") && data.contains("byebye")) { if(data.contains("api type=\"pairing\"") && data.contains("byebye")) {
qCDebug(dcLgSmartTv) << "Ended pairing (host)"; qCDebug(dcLgSmartTv()) << "Ended pairing (host)";
setPaired(false); setPaired(false);
setReachable(false); setReachable(false);
return; return;

View File

@ -148,6 +148,8 @@ public:
static QPair<QNetworkRequest, QByteArray> createPairingRequest(const QHostAddress &host, const int &port, const QString &key); static QPair<QNetworkRequest, QByteArray> createPairingRequest(const QHostAddress &host, const int &port, const QString &key);
static QPair<QNetworkRequest, QByteArray> createEndPairingRequest(const QUrl &url); static QPair<QNetworkRequest, QByteArray> createEndPairingRequest(const QUrl &url);
static QPair<QNetworkRequest, QByteArray> createEndPairingRequest(const QHostAddress &host, const int &port); static QPair<QNetworkRequest, QByteArray> createEndPairingRequest(const QHostAddress &host, const int &port);
static QPair<QNetworkRequest, QByteArray> createEventRequest(const QHostAddress &host, const int &port);
QPair<QNetworkRequest, QByteArray> createPressButtonRequest(const TvDevice::RemoteKey &key); QPair<QNetworkRequest, QByteArray> createPressButtonRequest(const TvDevice::RemoteKey &key);
QNetworkRequest createVolumeInformationRequest(); QNetworkRequest createVolumeInformationRequest();

View File

@ -36,8 +36,7 @@ void TvEventHandler::incomingConnection(qintptr socket)
{ {
QTcpSocket* tcpSocket = new QTcpSocket(this); QTcpSocket* tcpSocket = new QTcpSocket(this);
tcpSocket->setSocketDescriptor(socket); tcpSocket->setSocketDescriptor(socket);
qCDebug(dcLgSmartTv) << "Event handler -> incoming connection" << tcpSocket->peerAddress().toString() << tcpSocket->peerName(); qCDebug(dcLgSmartTv()) << "Event handler -> incoming connection" << tcpSocket->peerAddress().toString() << tcpSocket->peerName();
connect(tcpSocket, &QTcpSocket::readyRead, this, &TvEventHandler::readClient); connect(tcpSocket, &QTcpSocket::readyRead, this, &TvEventHandler::readClient);
connect(tcpSocket, &QTcpSocket::disconnected, this, &TvEventHandler::onDisconnected); connect(tcpSocket, &QTcpSocket::disconnected, this, &TvEventHandler::onDisconnected);
} }
@ -50,7 +49,7 @@ void TvEventHandler::readClient()
if(socket->peerAddress() != m_host){ if(socket->peerAddress() != m_host){
socket->close(); socket->close();
socket->deleteLater(); socket->deleteLater();
qCWarning(dcLgSmartTv) << "Event handler -> rejecting connection from " << socket->peerAddress().toString(); qCWarning(dcLgSmartTv()) << "Event handler -> rejecting connection from " << socket->peerAddress().toString();
return; return;
} }
@ -79,7 +78,7 @@ void TvEventHandler::readClient()
if (data.startsWith("POST") && !m_expectingData) { if (data.startsWith("POST") && !m_expectingData) {
m_expectingData = true; m_expectingData = true;
QStringList tokens = QString(data).split(QRegExp("[ \r\n][ \r\n]*")); QStringList tokens = QString(data).split(QRegExp("[ \r\n][ \r\n]*"));
qCDebug(dcLgSmartTv) << "event handler -> event occured" << "http://" << m_host.toString() << ":" << m_port << tokens[1]; qCDebug(dcLgSmartTv()) << "event handler -> event occured" << "http://" << m_host.toString() << ":" << m_port << tokens[1];
} }
} }
} }
@ -87,7 +86,7 @@ void TvEventHandler::readClient()
void TvEventHandler::onDisconnected() void TvEventHandler::onDisconnected()
{ {
QTcpSocket* socket = (QTcpSocket*)sender(); QTcpSocket* socket = (QTcpSocket*)sender();
qCDebug(dcLgSmartTv) << "event handler -> client disconnected" << socket->peerAddress(); qCDebug(dcLgSmartTv()) << "event handler -> client disconnected" << socket->peerAddress();
socket->deleteLater(); socket->deleteLater();
} }

View File

@ -38,6 +38,8 @@ class TvEventHandler : public QTcpServer
Q_OBJECT Q_OBJECT
public: public:
explicit TvEventHandler(const QHostAddress &host, const int &port = 8080, QObject *parent = 0); explicit TvEventHandler(const QHostAddress &host, const int &port = 8080, QObject *parent = 0);
protected:
void incomingConnection(qintptr socket) override; void incomingConnection(qintptr socket) override;
private: private: