Fix connected state based on client count and clean up code
parent
007dfedc20
commit
31e5b0c8a5
|
|
@ -54,9 +54,9 @@ void IntegrationPluginTcpCommander::setupThing(ThingSetupInfo *info)
|
||||||
// In case of a reconfigure, make sure we reconnect
|
// In case of a reconfigure, make sure we reconnect
|
||||||
tcpSocket->disconnectFromHost();
|
tcpSocket->disconnectFromHost();
|
||||||
}
|
}
|
||||||
|
|
||||||
connect(tcpSocket, &QTcpSocket::stateChanged, thing, [=](QAbstractSocket::SocketState state){
|
connect(tcpSocket, &QTcpSocket::stateChanged, thing, [=](QAbstractSocket::SocketState state){
|
||||||
thing->setStateValue(tcpClientConnectedStateTypeId, state == QAbstractSocket::ConnectedState);
|
thing->setStateValue(tcpClientConnectedStateTypeId, state == QAbstractSocket::ConnectedState);
|
||||||
|
|
||||||
if (state == QAbstractSocket::UnconnectedState) {
|
if (state == QAbstractSocket::UnconnectedState) {
|
||||||
QTimer::singleShot(10000, tcpSocket, [=](){
|
QTimer::singleShot(10000, tcpSocket, [=](){
|
||||||
qCDebug(dcTCPCommander()) << "Reconnecting to server" << address << port;
|
qCDebug(dcTCPCommander()) << "Reconnecting to server" << address << port;
|
||||||
|
|
@ -64,12 +64,12 @@ void IntegrationPluginTcpCommander::setupThing(ThingSetupInfo *info)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
connect(tcpSocket, &QTcpSocket::readyRead, thing, [=](){
|
connect(tcpSocket, &QTcpSocket::readyRead, thing, [=](){
|
||||||
QByteArray data = tcpSocket->readAll();
|
QByteArray data = tcpSocket->readAll();
|
||||||
ParamList params;
|
ParamList params;
|
||||||
params << Param(tcpClientTriggeredEventDataParamTypeId, data);
|
params << Param(tcpClientTriggeredEventDataParamTypeId, data);
|
||||||
Event event(tcpClientTriggeredEventTypeId, thing->id(), params);
|
emit emitEvent(Event(tcpClientTriggeredEventTypeId, thing->id(), params));
|
||||||
emitEvent(event);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
tcpSocket->connectToHost(address, port);
|
tcpSocket->connectToHost(address, port);
|
||||||
|
|
@ -171,15 +171,8 @@ void IntegrationPluginTcpCommander::onTcpServerConnectionCountChanged(int connec
|
||||||
{
|
{
|
||||||
TcpServer *tcpServer = static_cast<TcpServer *>(sender());
|
TcpServer *tcpServer = static_cast<TcpServer *>(sender());
|
||||||
Thing *thing = m_tcpServers.key(tcpServer);
|
Thing *thing = m_tcpServers.key(tcpServer);
|
||||||
if (!thing)
|
if (thing && thing->thingClassId() == tcpServerThingClassId) {
|
||||||
return;
|
qDebug(dcTCPCommander()) << thing->name() << "Tcp Server Client connected";
|
||||||
qDebug(dcTCPCommander()) << thing->name() << "Tcp Server Client connected";
|
|
||||||
if (thing->thingClassId() == tcpServerThingClassId) {
|
|
||||||
if (connections > 0) {
|
|
||||||
thing->setStateValue(tcpServerConnectedStateTypeId, true);
|
|
||||||
} else {
|
|
||||||
thing->setStateValue(tcpServerConnectedStateTypeId, false);
|
|
||||||
}
|
|
||||||
thing->setStateValue(tcpServerConnectionCountStateTypeId, connections);
|
thing->setStateValue(tcpServerConnectionCountStateTypeId, connections);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -190,10 +183,8 @@ void IntegrationPluginTcpCommander::onTcpServerCommandReceived(const QString &cl
|
||||||
Thing *thing = m_tcpServers.key(tcpServer);
|
Thing *thing = m_tcpServers.key(tcpServer);
|
||||||
qDebug(dcTCPCommander()) << thing->name() << "Message received" << data;
|
qDebug(dcTCPCommander()) << thing->name() << "Message received" << data;
|
||||||
|
|
||||||
Event event = Event(tcpServerTriggeredEventTypeId, thing->id());
|
|
||||||
ParamList params;
|
ParamList params;
|
||||||
params.append(Param(tcpServerTriggeredEventDataParamTypeId, data));
|
params.append(Param(tcpServerTriggeredEventDataParamTypeId, data));
|
||||||
params.append(Param(tcpServerTriggeredEventClientIpParamTypeId, clientIp));
|
params.append(Param(tcpServerTriggeredEventClientIpParamTypeId, clientIp));
|
||||||
event.setParams(params);
|
emit emitEvent(Event(tcpServerTriggeredEventTypeId, thing->id(), params));
|
||||||
emitEvent(event);
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue