diff --git a/code/Arduino/peripheral/ConnectionController.cpp b/code/Arduino/peripheral/ConnectionController.cpp index 4a5310f..d9ca3ee 100644 --- a/code/Arduino/peripheral/ConnectionController.cpp +++ b/code/Arduino/peripheral/ConnectionController.cpp @@ -64,7 +64,8 @@ void ConnectionController::waitForConnection() //check first part of command (if that command is connection command) if (readInfo.length() < sizeof (connectCommand) || (readInfo.substring(0, sizeof(connectCommand) - 1) != connectCommand)) { - DEBUG_PRINTLN("Bad info"); + DEBUG_PRINT("Bad info: "); + DEBUG_PRINTLNHEX(readInfo); continue; } @@ -143,6 +144,7 @@ String ConnectionController::getCommand() { isConnected = false; waitForConnection(); + timer.reset(); continue; } @@ -162,6 +164,7 @@ String ConnectionController::getCommand() { isConnected = false; waitForConnection(); + timer.reset(); continue; } diff --git a/code/c++/cxx-api/AutoConnector.cpp b/code/c++/cxx-api/AutoConnector.cpp index 17b8a0e..d7a9834 100644 --- a/code/c++/cxx-api/AutoConnector.cpp +++ b/code/c++/cxx-api/AutoConnector.cpp @@ -33,6 +33,7 @@ void AutoConnector::stop() { isRepeat.store(false); pConnectingThread->join(); delete pConnectingThread; + pConnectingThread = nullptr; } } diff --git a/code/c++/cxx-api/AutoConnector.h b/code/c++/cxx-api/AutoConnector.h index 5c0be5e..38038cd 100644 --- a/code/c++/cxx-api/AutoConnector.h +++ b/code/c++/cxx-api/AutoConnector.h @@ -18,7 +18,7 @@ class AutoConnector { uint32_t frequency; public: - AutoConnector(std::function c, uint32_t f = 500); + AutoConnector(std::function c, uint32_t f); ~AutoConnector(); void start(); void stop(); diff --git a/code/c++/cxx-api/TrackPlatform_BasicConnector.cpp b/code/c++/cxx-api/TrackPlatform_BasicConnector.cpp index 50668e6..61a7880 100644 --- a/code/c++/cxx-api/TrackPlatform_BasicConnector.cpp +++ b/code/c++/cxx-api/TrackPlatform_BasicConnector.cpp @@ -17,6 +17,7 @@ void TrackPlatform_BasicConnector::sendStartCommand() std::string command = std::string() + static_cast(communicationControllerID) + static_cast(startCommunicationCommand) + static_cast(APIWithAutoDiconnect); isConnectedToArduino = true; sendOneCommand(command); + autoConnector->start(); } void TrackPlatform_BasicConnector::sendStopCommand() { @@ -24,12 +25,17 @@ void TrackPlatform_BasicConnector::sendStopCommand() { throw NoConnectionException(); } + autoConnector->stop(); std::string command = std::string() + static_cast(communicationControllerID) + static_cast(stopCommunicationCommand); sendOneCommand(command); isConnectedToArduino = false; } -TrackPlatform_BasicConnector::TrackPlatform_BasicConnector() +TrackPlatform_BasicConnector::TrackPlatform_BasicConnector() : + autoConnector(new AutoConnector([this]() + { + this->sendOneCommand(std::string() + static_cast(communicationControllerID) + static_cast(refreshConnectionCommunicationCommand)); + }, timeoutToAutoreconnectInMs)) { } diff --git a/code/c++/cxx-api/TrackPlatform_BasicConnector.h b/code/c++/cxx-api/TrackPlatform_BasicConnector.h index 12370dd..d9f58cd 100644 --- a/code/c++/cxx-api/TrackPlatform_BasicConnector.h +++ b/code/c++/cxx-api/TrackPlatform_BasicConnector.h @@ -2,15 +2,18 @@ #define _TRACKPLATFORM_BASICCONNECTOR_H_ #include +#include "AutoConnector.h" class TrackPlatform_BasicConnector { bool isConnectedToArduino = false; + AutoConnector* autoConnector = nullptr; protected: static const char stopSymbol = '|'; static const uint8_t timesToAutoreconnect = 3; static const uint32_t timeoutToNextConnectInMs = 500; + static const uint32_t timeoutToAutoreconnectInMs = 1000; static const std::string correctAnswer; virtual void write(const std::string& s) = 0; diff --git a/code/c++/cxx-api/TrackPlatform_BasicManagement.h b/code/c++/cxx-api/TrackPlatform_BasicManagement.h index 6e8d6a5..2e71897 100644 --- a/code/c++/cxx-api/TrackPlatform_BasicManagement.h +++ b/code/c++/cxx-api/TrackPlatform_BasicManagement.h @@ -14,7 +14,6 @@ class TrackPlatform_BasicManagement protected: static const uint8_t minSpeed = 0; static const uint8_t maxSpeed = 255; - static const uint32_t reconnectTime = 500; static const char delimiter = ';'; void sendMove(const std::string& additionalInfo); diff --git a/code/c++/cxx-api/TrackPlatform_Manager.cpp b/code/c++/cxx-api/TrackPlatform_Manager.cpp index ec3b710..4b512e2 100644 --- a/code/c++/cxx-api/TrackPlatform_Manager.cpp +++ b/code/c++/cxx-api/TrackPlatform_Manager.cpp @@ -27,24 +27,13 @@ TrackPlatform_BasicConnector* TrackPlatform_Manager::createConnectorByMode(Conne TrackPlatform_Manager::TrackPlatform_Manager(ConnectionModes mode, const CommunicationInfoStruct& info) : TrackPlatform_BasicManagement(createConnectorByMode(mode, info)) { - std::function callback = [this]() { this->refreshConnection(); }; - pAutoConnector = new AutoConnector(callback, reconnectTime); - pAutoConnector->start(); } TrackPlatform_Manager::~TrackPlatform_Manager() { - if (pAutoConnector) delete pAutoConnector; TrackPlatform_BasicConnector* connector = getConnector(); if (connector) { delete connector; } } - -void TrackPlatform_Manager::startAutoConnection() { - pAutoConnector->start(); -} -void TrackPlatform_Manager::stopAutoConnection() { - pAutoConnector->stop(); -} \ No newline at end of file diff --git a/code/c++/cxx-api/TrackPlatform_Manager.h b/code/c++/cxx-api/TrackPlatform_Manager.h index 8cfae12..bc3f15c 100644 --- a/code/c++/cxx-api/TrackPlatform_Manager.h +++ b/code/c++/cxx-api/TrackPlatform_Manager.h @@ -9,14 +9,10 @@ class TrackPlatform_Manager : public TrackPlatform_BasicManagement { static TrackPlatform_BasicConnector* createConnectorByMode(ConnectionModes mode, const CommunicationInfoStruct& info); - AutoConnector* pAutoConnector = nullptr; public: TrackPlatform_Manager(ConnectionModes mode, const CommunicationInfoStruct& info); ~TrackPlatform_Manager(); - - void startAutoConnection(); - void stopAutoConnection(); }; #endif /* _TRACKPLATFORM_MANAGER_H_ */