mirror of
https://github.com/intrepidcs/libicsneo.git
synced 2026-08-05 01:18:36 +02:00
Detect device disconnects
When a device is sending any traffic, the device is considered to be connected. If no traffic if being received from the device, a status is requested. If the device fails to report the status back in a timely manner, it is considered to be disconnected. If the device fails to reply to the status request, it is important to confirm that the device is not applying settings. While the device is applying settings, it will not be sending heartbeats or able to process a status request.
This commit is contained in:
@@ -205,6 +205,32 @@ bool Device::open() {
|
||||
handleInternalMessage(message);
|
||||
}));
|
||||
|
||||
std::atomic<bool> receivedMessage{false};
|
||||
messageReceivedCallbackID = com->addMessageCallback(MessageCallback(filter, [&](std::shared_ptr<Message> message) {
|
||||
receivedMessage = true;
|
||||
}));
|
||||
|
||||
heartbeatThread = std::thread([&]() {
|
||||
EventManager::GetInstance().downgradeErrorsOnCurrentThread();
|
||||
while(true) {
|
||||
// Wait for 110ms for a possible heartbeat
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(110));
|
||||
if(!receivedMessage) {
|
||||
// No heartbeat received, request a status
|
||||
com->sendCommand(Command::RequestStatusUpdate);
|
||||
// The response should come back quickly if the com is quiet
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
// Check if we got a message, and if not, if settings are being applied
|
||||
if(!receivedMessage && !settings->applyingSettings) {
|
||||
if(!stopHeartbeatThread)
|
||||
report(APIEvent::Type::DeviceDisconnected, APIEvent::Severity::Error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
receivedMessage = false;
|
||||
}
|
||||
});
|
||||
|
||||
forEachExtension([](const std::shared_ptr<DeviceExtension>& ext) { ext->onDeviceOpen(); return true; });
|
||||
return true;
|
||||
}
|
||||
@@ -215,12 +241,17 @@ bool Device::close() {
|
||||
return false;
|
||||
}
|
||||
|
||||
stopHeartbeatThread = true;
|
||||
|
||||
if(isOnline())
|
||||
goOffline();
|
||||
|
||||
if(internalHandlerCallbackID)
|
||||
com->removeMessageCallback(internalHandlerCallbackID);
|
||||
|
||||
if(messageReceivedCallbackID)
|
||||
com->removeMessageCallback(messageReceivedCallbackID);
|
||||
|
||||
internalHandlerCallbackID = 0;
|
||||
|
||||
forEachExtension([](const std::shared_ptr<DeviceExtension>& ext) { ext->onDeviceClose(); return true; });
|
||||
|
||||
Reference in New Issue
Block a user