refactor: add onDeviceConnected

This commit is contained in:
Barry 2022-04-10 11:32:14 +08:00
commit c4fdbffbf4
3 changed files with 23 additions and 8 deletions

View file

@ -257,7 +257,7 @@ void Device::initSignals()
emit m_controller->onSetScreenPowerMode(ControlMsg::SPM_OFF); emit m_controller->onSetScreenPowerMode(ControlMsg::SPM_OFF);
} }
} else { } else {
disconnectDevice(); m_server->stop();
} }
}); });
connect(m_server, &Server::serverStoped, this, [this]() { connect(m_server, &Server::serverStoped, this, [this]() {

View file

@ -40,6 +40,7 @@ bool DeviceManage::connectDevice(Device::DeviceParams params)
} }
*/ */
Device *device = new Device(params); Device *device = new Device(params);
connect(device, &Device::deviceConnected, this, &DeviceManage::onDeviceConnected);
connect(device, &Device::deviceDisconnected, this, &DeviceManage::onDeviceDisconnected); connect(device, &Device::deviceDisconnected, this, &DeviceManage::onDeviceDisconnected);
connect(device, &Device::controlStateChange, this, &DeviceManage::onControlStateChange); connect(device, &Device::controlStateChange, this, &DeviceManage::onControlStateChange);
if (!device->connectDevice()) { if (!device->connectDevice()) {
@ -190,15 +191,16 @@ void DeviceManage::setGroupControlHost(Device *host, bool install)
} }
} }
void DeviceManage::onDeviceConnected(bool success, const QString &serial, const QString &deviceName, const QSize &size)
{
if (!success) {
removeDevice(serial);
}
}
void DeviceManage::onDeviceDisconnected(QString serial) void DeviceManage::onDeviceDisconnected(QString serial)
{ {
if (!serial.isEmpty() && m_devices.contains(serial)) { removeDevice(serial);
if (m_devices[serial]->controlState() == Device::GroupControlState::GCS_HOST) {
setGroupControlHost(nullptr, false);
}
m_devices[serial]->deleteLater();
m_devices.remove(serial);
}
} }
void DeviceManage::onControlStateChange(Device *device, Device::GroupControlState oldState, Device::GroupControlState newState) void DeviceManage::onControlStateChange(Device *device, Device::GroupControlState oldState, Device::GroupControlState newState)
@ -300,3 +302,14 @@ quint16 DeviceManage::getFreePort()
} }
return 0; return 0;
} }
void DeviceManage::removeDevice(const QString &serial)
{
if (!serial.isEmpty() && m_devices.contains(serial)) {
if (m_devices[serial]->controlState() == Device::GroupControlState::GCS_HOST) {
setGroupControlHost(nullptr, false);
}
m_devices[serial]->deleteLater();
m_devices.remove(serial);
}
}

View file

@ -26,6 +26,7 @@ protected:
void setGroupControlHost(Device *host, bool install); void setGroupControlHost(Device *host, bool install);
protected slots: protected slots:
void onDeviceConnected(bool success, const QString& serial, const QString& deviceName, const QSize& size);
void onDeviceDisconnected(QString serial); void onDeviceDisconnected(QString serial);
void onControlStateChange(Device *device, Device::GroupControlState oldState, Device::GroupControlState newState); void onControlStateChange(Device *device, Device::GroupControlState oldState, Device::GroupControlState newState);
@ -36,6 +37,7 @@ protected slots:
private: private:
quint16 getFreePort(); quint16 getFreePort();
void removeDevice(const QString& serial);
private: private:
QMap<QString, QPointer<Device>> m_devices; QMap<QString, QPointer<Device>> m_devices;