Improve LG smarttv plugin
This commit is contained in:
parent
364f7e5499
commit
7027b9f9d1
@ -57,13 +57,12 @@ DevicePluginLgSmartTv::DevicePluginLgSmartTv()
|
||||
|
||||
DevicePluginLgSmartTv::~DevicePluginLgSmartTv()
|
||||
{
|
||||
hardwareManager()->pluginTimerManager()->unregisterTimer(m_pluginTimer);
|
||||
|
||||
}
|
||||
|
||||
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 ¶ms)
|
||||
@ -79,52 +78,61 @@ DeviceManager::DeviceError DevicePluginLgSmartTv::discoverDevices(const DeviceCl
|
||||
|
||||
DeviceManager::DeviceSetupStatus DevicePluginLgSmartTv::setupDevice(Device *device)
|
||||
{
|
||||
if (device->deviceClassId() != lgSmartTvDeviceClassId) {
|
||||
return DeviceManager::DeviceSetupStatusFailure;
|
||||
}
|
||||
|
||||
TvDevice *tvDevice = new TvDevice(QHostAddress(device->paramValue(lgSmartTvDeviceHostAddressParamTypeId).toString()),
|
||||
device->paramValue(lgSmartTvDevicePortParamTypeId).toInt(), this);
|
||||
qCDebug(dcLgSmartTv()) << "Setup LG smart TV" << device->name() << device->params();
|
||||
QHostAddress address = QHostAddress(device->paramValue(lgSmartTvDeviceHostAddressParamTypeId).toString());
|
||||
TvDevice *tvDevice = new TvDevice(address, device->paramValue(lgSmartTvDevicePortParamTypeId).toInt(), this);
|
||||
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()) {
|
||||
// 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())) {
|
||||
qCWarning(dcLgSmartTv) << "could not find any pairing key";
|
||||
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());
|
||||
|
||||
tvDevice->setKey(key);
|
||||
device->setParamValue(lgSmartTvDeviceKeyParamTypeId, key);
|
||||
} else {
|
||||
// add the key for editing
|
||||
//Add the key for editing
|
||||
if (!m_tvKeys.contains(device->paramValue(lgSmartTvDeviceUuidParamTypeId).toString())) {
|
||||
m_tvKeys.insert(tvDevice->uuid(), tvDevice->key());
|
||||
}
|
||||
}
|
||||
|
||||
connect(tvDevice, &TvDevice::stateChanged, this, &DevicePluginLgSmartTv::stateChanged);
|
||||
|
||||
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)
|
||||
{
|
||||
if (!m_tvList.values().contains(device)) {
|
||||
if (!m_tvList.values().contains(device))
|
||||
return;
|
||||
}
|
||||
|
||||
TvDevice *tvDevice= m_tvList.key(device);
|
||||
qCDebug(dcLgSmartTv) << "Remove device" << device->name();
|
||||
unpairTvDevice(device);
|
||||
m_tvList.remove(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)
|
||||
@ -262,8 +270,9 @@ DeviceManager::DeviceSetupStatus DevicePluginLgSmartTv::confirmPairing(const Pai
|
||||
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());
|
||||
int port = device->paramValue(lgSmartTvDevicePortParamTypeId).toInt();
|
||||
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);
|
||||
connect(reply, &QNetworkReply::finished, this, &DevicePluginLgSmartTv::onNetworkManagerReplyFinished);
|
||||
|
||||
if (setup) {
|
||||
m_asyncSetup.insert(reply, device);
|
||||
} else {
|
||||
m_pairRequests.insert(reply, device);
|
||||
}
|
||||
}
|
||||
|
||||
void DevicePluginLgSmartTv::unpairTvDevice(Device *device)
|
||||
@ -297,10 +302,6 @@ void DevicePluginLgSmartTv::refreshTv(Device *device)
|
||||
connect(volumeReply, &QNetworkReply::finished, this, &DevicePluginLgSmartTv::onNetworkManagerReplyFinished);
|
||||
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()
|
||||
@ -348,9 +349,11 @@ void DevicePluginLgSmartTv::onUpnpDiscoveryFinished()
|
||||
void DevicePluginLgSmartTv::onNetworkManagerReplyFinished()
|
||||
{
|
||||
QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
|
||||
|
||||
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
|
||||
reply->deleteLater();
|
||||
|
||||
|
||||
if (m_showPinReply.contains(reply)) {
|
||||
m_showPinReply.removeAll(reply);
|
||||
if (status != 200) {
|
||||
@ -382,17 +385,15 @@ void DevicePluginLgSmartTv::onNetworkManagerReplyFinished()
|
||||
if(status != 200) {
|
||||
qCWarning(dcLgSmartTv) << "Pair TV request error:" << status << reply->errorString();
|
||||
tv->setPaired(false);
|
||||
emit deviceSetupFinished(device, DeviceManager::DeviceSetupStatusFailure);
|
||||
} else {
|
||||
qCDebug(dcLgSmartTv) << "Paired TV successfully.";
|
||||
tv->setPaired(true);
|
||||
refreshTv(device);
|
||||
emit deviceSetupFinished(device, DeviceManager::DeviceSetupStatusSuccess);
|
||||
}
|
||||
} else if (m_deleteTv.contains(reply)) {
|
||||
m_deleteTv.removeAll(reply);
|
||||
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 {
|
||||
qCDebug(dcLgSmartTv) << "End pairing TV (device deleted) successfully.";
|
||||
}
|
||||
@ -402,9 +403,18 @@ void DevicePluginLgSmartTv::onNetworkManagerReplyFinished()
|
||||
if(status != 200) {
|
||||
tv->setReachable(false);
|
||||
qCWarning(dcLgSmartTv) << "Volume information request error:" << status << reply->errorString();
|
||||
if (status == 401) {
|
||||
qCDebug(dcLgSmartTv()) << status << reply->errorString();
|
||||
pairTvDevice(device);
|
||||
}
|
||||
} else {
|
||||
tv->setReachable(true);
|
||||
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)) {
|
||||
Device *device = m_channelInfoRequests.take(reply);
|
||||
@ -412,6 +422,10 @@ void DevicePluginLgSmartTv::onNetworkManagerReplyFinished()
|
||||
if(status != 200) {
|
||||
tv->setReachable(false);
|
||||
qCWarning(dcLgSmartTv) << "Channel information request error:" << status << reply->errorString();
|
||||
if (status == 401) {
|
||||
qCDebug(dcLgSmartTv()) << status << reply->errorString();
|
||||
pairTvDevice(device);
|
||||
}
|
||||
} else {
|
||||
tv->setReachable(true);
|
||||
tv->onChannelInformationUpdate(reply->readAll());
|
||||
@ -425,7 +439,6 @@ void DevicePluginLgSmartTv::onNetworkManagerReplyFinished()
|
||||
emit actionExecutionFinished(actionId, DeviceManager::DeviceErrorNoError);
|
||||
}
|
||||
}
|
||||
reply->deleteLater();
|
||||
}
|
||||
|
||||
void DevicePluginLgSmartTv::stateChanged()
|
||||
@ -433,7 +446,7 @@ void DevicePluginLgSmartTv::stateChanged()
|
||||
TvDevice *tvDevice = static_cast<TvDevice*>(sender());
|
||||
Device *device = m_tvList.value(tvDevice);
|
||||
|
||||
device->setStateValue(lgSmartTvReachableStateTypeId, tvDevice->reachable());
|
||||
device->setStateValue(lgSmartTvConnectedStateTypeId, tvDevice->reachable());
|
||||
device->setStateValue(lgSmartTvTv3DModeStateTypeId, tvDevice->is3DMode());
|
||||
device->setStateValue(lgSmartTvTvVolumeLevelStateTypeId, tvDevice->volumeLevel());
|
||||
device->setStateValue(lgSmartTvTvMuteStateTypeId, tvDevice->mute());
|
||||
|
||||
@ -43,6 +43,7 @@ public:
|
||||
DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) override;
|
||||
DeviceManager::DeviceSetupStatus setupDevice(Device *device) override;
|
||||
void deviceRemoved(Device *device) override;
|
||||
void postSetupDevice(Device *device) override;
|
||||
|
||||
DeviceManager::DeviceError executeAction(Device *device, const Action &action) override;
|
||||
DeviceManager::DeviceError displayPin(const PairingTransactionId &pairingTransactionId, const DeviceDescriptor &deviceDescriptor) override;
|
||||
@ -60,7 +61,6 @@ private:
|
||||
|
||||
// async setup
|
||||
QHash<QNetworkReply *, Device *> m_asyncSetup;
|
||||
QHash<QNetworkReply *, Device *> m_pairRequests;
|
||||
QList<QNetworkReply *> m_deleteTv;
|
||||
|
||||
// action requests
|
||||
@ -70,7 +70,7 @@ private:
|
||||
QHash<QNetworkReply *, Device *> m_volumeInfoRequests;
|
||||
QHash<QNetworkReply *, Device *> m_channelInfoRequests;
|
||||
|
||||
void pairTvDevice(Device *device, const bool &setup = false);
|
||||
void pairTvDevice(Device *device);
|
||||
void unpairTvDevice(Device *device);
|
||||
void refreshTv(Device *device);
|
||||
|
||||
|
||||
@ -13,14 +13,9 @@
|
||||
"name": "lgSmartTv",
|
||||
"displayName": "LG Smart Tv",
|
||||
"deviceIcon": "Tv",
|
||||
"basicTags": [
|
||||
"Device",
|
||||
"Multimedia",
|
||||
"Actuator"
|
||||
],
|
||||
"createMethods": [ "discovery" ],
|
||||
"setupMethod": "displayPin",
|
||||
"criticalStateTypeId": "b056c36b-df87-4177-8d5d-1e7c1e8cdc7a",
|
||||
"interfaces": ["connectable"],
|
||||
"pairingInfo": "Please enter the pairing key displayed on the Tv.",
|
||||
"paramTypes": [
|
||||
{
|
||||
@ -68,9 +63,9 @@
|
||||
"stateTypes": [
|
||||
{
|
||||
"id": "b056c36b-df87-4177-8d5d-1e7c1e8cdc7a",
|
||||
"name": "reachable",
|
||||
"displayName": "reachable",
|
||||
"displayNameEvent": "reachable changed",
|
||||
"name": "connected",
|
||||
"displayName": "Reachable",
|
||||
"displayNameEvent": "Reachable changed",
|
||||
"type": "bool",
|
||||
"defaultValue": false
|
||||
},
|
||||
@ -86,7 +81,7 @@
|
||||
"id": "07d39a6e-7eab-42d0-851d-9f3bcd3bbb57",
|
||||
"name": "tvVolumeLevel",
|
||||
"displayName": "volume",
|
||||
"displayNameEvent": "volume changed",
|
||||
"displayNameEvent": "Volume changed",
|
||||
"type": "int",
|
||||
"ruleRelevant": false,
|
||||
"eventRuleRelevant": false,
|
||||
|
||||
@ -36,7 +36,6 @@ TvDevice::TvDevice(const QHostAddress &hostAddress, const int &port, QObject *pa
|
||||
m_channelNumber(-1)
|
||||
{
|
||||
m_eventHandler = new TvEventHandler(hostAddress, port, this);
|
||||
|
||||
connect(m_eventHandler, &TvEventHandler::eventOccured, this, &TvDevice::eventOccured);
|
||||
}
|
||||
|
||||
@ -96,6 +95,7 @@ bool TvDevice::paired() const
|
||||
void TvDevice::setReachable(const bool &reachable)
|
||||
{
|
||||
if (m_reachable != reachable) {
|
||||
qCDebug(dcLgSmartTv()) << "TV Event handler" << (reachable ? "reachable" : "not reachable any more");
|
||||
m_reachable = reachable;
|
||||
emit stateChanged();
|
||||
}
|
||||
@ -200,6 +200,19 @@ QPair<QNetworkRequest, QByteArray> TvDevice::createEndPairingRequest(const QHost
|
||||
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()
|
||||
{
|
||||
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)
|
||||
{
|
||||
//qCDebug(dcLgSmartTv) << printXmlData(data);
|
||||
//qCDebug(dcLgSmartTv()) << printXmlData(data);
|
||||
QXmlStreamReader xml(data);
|
||||
|
||||
while(!xml.atEnd() && !xml.hasError()) {
|
||||
xml.readNext();
|
||||
|
||||
@ -257,7 +269,7 @@ void TvDevice::onVolumeInformationUpdate(const QByteArray &data)
|
||||
|
||||
void TvDevice::onChannelInformationUpdate(const QByteArray &data)
|
||||
{
|
||||
//qCDebug(dcLgSmartTv) << printXmlData(data);
|
||||
//qCDebug(dcLgSmartTv()) << printXmlData(data);
|
||||
QXmlStreamReader xml(data);
|
||||
|
||||
while(!xml.atEnd() && !xml.hasError()) {
|
||||
@ -299,14 +311,14 @@ QString TvDevice::printXmlData(const QByteArray &data)
|
||||
}
|
||||
}
|
||||
if(reader.hasError()) {
|
||||
qCWarning(dcLgSmartTv) << "error reading XML device information:" << reader.errorString();
|
||||
qCWarning(dcLgSmartTv()) << "error reading XML device information:" << reader.errorString();
|
||||
}
|
||||
return xmlOut;
|
||||
}
|
||||
|
||||
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(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
|
||||
// the pairing will be closed.
|
||||
if(data.contains("api type=\"pairing\"") && data.contains("byebye")) {
|
||||
qCDebug(dcLgSmartTv) << "Ended pairing (host)";
|
||||
qCDebug(dcLgSmartTv()) << "Ended pairing (host)";
|
||||
setPaired(false);
|
||||
setReachable(false);
|
||||
return;
|
||||
|
||||
@ -148,6 +148,8 @@ public:
|
||||
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 QHostAddress &host, const int &port);
|
||||
static QPair<QNetworkRequest, QByteArray> createEventRequest(const QHostAddress &host, const int &port);
|
||||
|
||||
QPair<QNetworkRequest, QByteArray> createPressButtonRequest(const TvDevice::RemoteKey &key);
|
||||
|
||||
QNetworkRequest createVolumeInformationRequest();
|
||||
|
||||
@ -36,8 +36,7 @@ void TvEventHandler::incomingConnection(qintptr socket)
|
||||
{
|
||||
QTcpSocket* tcpSocket = new QTcpSocket(this);
|
||||
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::disconnected, this, &TvEventHandler::onDisconnected);
|
||||
}
|
||||
@ -50,7 +49,7 @@ void TvEventHandler::readClient()
|
||||
if(socket->peerAddress() != m_host){
|
||||
socket->close();
|
||||
socket->deleteLater();
|
||||
qCWarning(dcLgSmartTv) << "Event handler -> rejecting connection from " << socket->peerAddress().toString();
|
||||
qCWarning(dcLgSmartTv()) << "Event handler -> rejecting connection from " << socket->peerAddress().toString();
|
||||
return;
|
||||
}
|
||||
|
||||
@ -79,7 +78,7 @@ void TvEventHandler::readClient()
|
||||
if (data.startsWith("POST") && !m_expectingData) {
|
||||
m_expectingData = true;
|
||||
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()
|
||||
{
|
||||
QTcpSocket* socket = (QTcpSocket*)sender();
|
||||
qCDebug(dcLgSmartTv) << "event handler -> client disconnected" << socket->peerAddress();
|
||||
qCDebug(dcLgSmartTv()) << "event handler -> client disconnected" << socket->peerAddress();
|
||||
socket->deleteLater();
|
||||
}
|
||||
|
||||
|
||||
@ -38,6 +38,8 @@ class TvEventHandler : public QTcpServer
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit TvEventHandler(const QHostAddress &host, const int &port = 8080, QObject *parent = 0);
|
||||
|
||||
protected:
|
||||
void incomingConnection(qintptr socket) override;
|
||||
|
||||
private:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user