From 53578065f1240a8f921d8f2b439787d4d9427b68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=9B=A6?= Date: Wed, 12 Mar 2025 21:44:00 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=A6=86=E5=86=B0=E6=B0=94=E8=B1=A1?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E9=98=9F=E5=88=97=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/cpp/CMakeLists.txt | 1 + app/src/main/cpp/DataController.cpp | 112 ++++++++++++++++++++++++++++ app/src/main/cpp/DataController.h | 77 +++++++++++++++++++ 3 files changed, 190 insertions(+) create mode 100644 app/src/main/cpp/DataController.cpp create mode 100644 app/src/main/cpp/DataController.h diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 9ecc18f2..cb634f48 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -392,6 +392,7 @@ add_library( # Sets the name of the library. #WeatherComm.cpp SensorsProtocol.cpp SerialComm.cpp + DataController.cpp ncnn/yolov5ncnn.cpp diff --git a/app/src/main/cpp/DataController.cpp b/app/src/main/cpp/DataController.cpp new file mode 100644 index 00000000..8afa9593 --- /dev/null +++ b/app/src/main/cpp/DataController.cpp @@ -0,0 +1,112 @@ +// +// Created by shxy on 2025/3/12. +// + +#include "DataController.h" + +DataController::DataController(CTerminal* pTerminal) :m_pTerminal(pTerminal), m_exit(false){} + +void DataController::Startup() +{ + m_thread = std::thread(DataThreadProc, this); +} + +void DataController::DataThreadProc(DataController* pThis) +{ + pThis->DataProc(); +} +void DataController::AddSendData(unsigned char frameType, unsigned char packetType, time_t timestamp,const vector &data) +{ + DATA_INFO predata = {0}; + predata.frameType = frameType; + predata.packetType = packetType; + predata.data = data; + + m_locker.lock(); + m_datas.push_back(predata); + m_locker.unlock(); + + m_sem.release(); +} + +bool DataController::WaitForResponse(unsigned char frameNo, unsigned char packetType, int sec) +{ + auto startTime = std::chrono::steady_clock::now(); + while (true) + { + { + std::lock_guard lock(m_responseLocker); + if (m_receivedresp.frameNo == frameNo && m_receivedresp.packetType == packetType) + { + m_receivedresp.frameNo = 0; + m_receivedresp.packetType = 0; + return true; + } + } + + auto currentTime = std::chrono::steady_clock::now(); + auto elapsed = std::chrono::duration_cast(currentTime - startTime).count(); + if (elapsed >= sec) + { + return false; + } + + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + } + +} + +void DataController::OnResponseReceived(unsigned char frameNo, unsigned char packetType) +{ + std::lock_guard lock(m_responseLocker); + m_receivedresp.frameNo = frameNo; + m_receivedresp.packetType = packetType; +} + +void DataController::DataProc() +{ + bool hasData; + DATA_INFO datasend; + string hispath = m_pTerminal->m_appPath + APP_DATA_DIR + "/" + APP_FILE_NAME_HIS_DB; + while(true) { + m_sem.acquire(); + + if (m_exit) { + break; + } + + hasData = false; + + m_locker.lock(); + if (!m_datas.empty()) + { + datasend = m_datas.front(); + m_datas.pop_front(); + hasData = true; + } + m_locker.unlock(); + + if (hasData) { + bool success = false; + for (int retry = 0; retry < 3; retry++) + { + unsigned char frameNo = 0; + m_pTerminal->DataSendTo(datasend.frameType, datasend.packetType, datasend.data, &frameNo); + + if (WaitForResponse(frameNo, datasend.packetType, 8)) + { + success = true; + UpdateHistoryStatus(hispath, datasend.packetType, datasend.timestamp); + break; + } + } + + if (!success) + { + std::lock_guard lock(m_locker); + m_datas.push_back(datasend); + } + } + + } +} \ No newline at end of file diff --git a/app/src/main/cpp/DataController.h b/app/src/main/cpp/DataController.h new file mode 100644 index 00000000..f6c23010 --- /dev/null +++ b/app/src/main/cpp/DataController.h @@ -0,0 +1,77 @@ +// +// Created by shxy on 2025/3/12. +// + +#ifndef MICROPHOTO_DATACONTROLLER_H +#define MICROPHOTO_DATACONTROLLER_H + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct DATA_INFO +{ + unsigned char frameNo; + unsigned char frameType; + unsigned char packetType; + time_t timestamp; + vector data; +}; + +struct DATA_RESP +{ + unsigned char frameNo; + unsigned char packetType; +}; + +class CTerminal; +class DataController { +public: + DataController(CTerminal *pTerminal); + ~DataController() + { + m_exit = true; + m_sem.release(); + if (m_thread.joinable()) { + m_thread.join(); + } + } + + void Startup(); + void AddSendData(unsigned char frameType, unsigned char packetType, time_t timestamp, const vector &data); + void OnResponseReceived(unsigned char frameNo, unsigned char packetType); + + + +protected: + static void DataThreadProc(DataController* pThis); + void DataProc(); + bool WaitForResponse(unsigned char frameNo, unsigned char packetType, int sec); + + + +protected: + + +protected: + std::mutex m_locker; + std::mutex m_responseLocker; + CSemaphore m_sem; + std::deque m_datas; + bool m_exit; + DATA_RESP m_receivedresp = {0,0}; + std::thread m_thread; + + CTerminal* m_pTerminal; +}; + + +#endif //MICROPHOTO_DATACONTROLLER_H From ee93791683169b029761c7ce6fa3c85214e8cf13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=9B=A6?= Date: Wed, 12 Mar 2025 21:44:18 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=A6=86=E5=86=B0=E6=B0=94=E8=B1=A1?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/cpp/CMakeLists.txt | 2 +- app/src/main/cpp/DataController.cpp | 112 ---------------------------- app/src/main/cpp/DataController.h | 77 ------------------- 3 files changed, 1 insertion(+), 190 deletions(-) delete mode 100644 app/src/main/cpp/DataController.cpp delete mode 100644 app/src/main/cpp/DataController.h diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index cb634f48..6ffc05ee 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -392,7 +392,6 @@ add_library( # Sets the name of the library. #WeatherComm.cpp SensorsProtocol.cpp SerialComm.cpp - DataController.cpp ncnn/yolov5ncnn.cpp @@ -449,6 +448,7 @@ add_library( # Sets the name of the library. ${TERM_CORE_ROOT}/Client/UpgradeReceiver.cpp ${TERM_CORE_ROOT}/Client/Database.cpp ${TERM_CORE_ROOT}/Client/SimulatorDevice.cpp + ${TERM_CORE_ROOT}/Client/DataController.cpp ) diff --git a/app/src/main/cpp/DataController.cpp b/app/src/main/cpp/DataController.cpp deleted file mode 100644 index 8afa9593..00000000 --- a/app/src/main/cpp/DataController.cpp +++ /dev/null @@ -1,112 +0,0 @@ -// -// Created by shxy on 2025/3/12. -// - -#include "DataController.h" - -DataController::DataController(CTerminal* pTerminal) :m_pTerminal(pTerminal), m_exit(false){} - -void DataController::Startup() -{ - m_thread = std::thread(DataThreadProc, this); -} - -void DataController::DataThreadProc(DataController* pThis) -{ - pThis->DataProc(); -} -void DataController::AddSendData(unsigned char frameType, unsigned char packetType, time_t timestamp,const vector &data) -{ - DATA_INFO predata = {0}; - predata.frameType = frameType; - predata.packetType = packetType; - predata.data = data; - - m_locker.lock(); - m_datas.push_back(predata); - m_locker.unlock(); - - m_sem.release(); -} - -bool DataController::WaitForResponse(unsigned char frameNo, unsigned char packetType, int sec) -{ - auto startTime = std::chrono::steady_clock::now(); - while (true) - { - { - std::lock_guard lock(m_responseLocker); - if (m_receivedresp.frameNo == frameNo && m_receivedresp.packetType == packetType) - { - m_receivedresp.frameNo = 0; - m_receivedresp.packetType = 0; - return true; - } - } - - auto currentTime = std::chrono::steady_clock::now(); - auto elapsed = std::chrono::duration_cast(currentTime - startTime).count(); - if (elapsed >= sec) - { - return false; - } - - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - } - -} - -void DataController::OnResponseReceived(unsigned char frameNo, unsigned char packetType) -{ - std::lock_guard lock(m_responseLocker); - m_receivedresp.frameNo = frameNo; - m_receivedresp.packetType = packetType; -} - -void DataController::DataProc() -{ - bool hasData; - DATA_INFO datasend; - string hispath = m_pTerminal->m_appPath + APP_DATA_DIR + "/" + APP_FILE_NAME_HIS_DB; - while(true) { - m_sem.acquire(); - - if (m_exit) { - break; - } - - hasData = false; - - m_locker.lock(); - if (!m_datas.empty()) - { - datasend = m_datas.front(); - m_datas.pop_front(); - hasData = true; - } - m_locker.unlock(); - - if (hasData) { - bool success = false; - for (int retry = 0; retry < 3; retry++) - { - unsigned char frameNo = 0; - m_pTerminal->DataSendTo(datasend.frameType, datasend.packetType, datasend.data, &frameNo); - - if (WaitForResponse(frameNo, datasend.packetType, 8)) - { - success = true; - UpdateHistoryStatus(hispath, datasend.packetType, datasend.timestamp); - break; - } - } - - if (!success) - { - std::lock_guard lock(m_locker); - m_datas.push_back(datasend); - } - } - - } -} \ No newline at end of file diff --git a/app/src/main/cpp/DataController.h b/app/src/main/cpp/DataController.h deleted file mode 100644 index f6c23010..00000000 --- a/app/src/main/cpp/DataController.h +++ /dev/null @@ -1,77 +0,0 @@ -// -// Created by shxy on 2025/3/12. -// - -#ifndef MICROPHOTO_DATACONTROLLER_H -#define MICROPHOTO_DATACONTROLLER_H - - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -struct DATA_INFO -{ - unsigned char frameNo; - unsigned char frameType; - unsigned char packetType; - time_t timestamp; - vector data; -}; - -struct DATA_RESP -{ - unsigned char frameNo; - unsigned char packetType; -}; - -class CTerminal; -class DataController { -public: - DataController(CTerminal *pTerminal); - ~DataController() - { - m_exit = true; - m_sem.release(); - if (m_thread.joinable()) { - m_thread.join(); - } - } - - void Startup(); - void AddSendData(unsigned char frameType, unsigned char packetType, time_t timestamp, const vector &data); - void OnResponseReceived(unsigned char frameNo, unsigned char packetType); - - - -protected: - static void DataThreadProc(DataController* pThis); - void DataProc(); - bool WaitForResponse(unsigned char frameNo, unsigned char packetType, int sec); - - - -protected: - - -protected: - std::mutex m_locker; - std::mutex m_responseLocker; - CSemaphore m_sem; - std::deque m_datas; - bool m_exit; - DATA_RESP m_receivedresp = {0,0}; - std::thread m_thread; - - CTerminal* m_pTerminal; -}; - - -#endif //MICROPHOTO_DATACONTROLLER_H