From a0070c5747d79876ecb080403b5c32724c5bb3cf Mon Sep 17 00:00:00 2001 From: Fancy code <258828110.@qq.com> Date: Wed, 29 May 2024 19:58:27 +0800 Subject: [PATCH] Improve:key event handle. --- .../MissionManager/src/DataProcessing.cpp | 34 ++++++++++++++++ .../MissionManager/src/DataProcessing.h | 17 +++++++- .../MissionManager/src/TestMissionState.cpp | 5 +++ .../MissionManager/src/TestMissionState.h | 1 + application/MissionManager/src/TopState.cpp | 4 ++ middleware/McuManager/include/IMcuManager.h | 4 +- .../src_mock/TestMissionState_Mock_Test.cpp | 40 +++++++++++++++++++ .../MissionManager/tool/CMakeLists.txt | 3 +- 8 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 test/application/HuntingCamera/src_mock/TestMissionState_Mock_Test.cpp diff --git a/application/MissionManager/src/DataProcessing.cpp b/application/MissionManager/src/DataProcessing.cpp index 7c8cd7c3..5bc2c0a9 100644 --- a/application/MissionManager/src/DataProcessing.cpp +++ b/application/MissionManager/src/DataProcessing.cpp @@ -16,12 +16,20 @@ #include "ILog.h" const bool NOT_EXECUTED = false; const bool EXECUTED = true; +key_event_data::key_event_data(const std::string &keyName, const KeyEvent &keyEvent, const unsigned int &holdTime) + : mKeyName(keyName), mKeyEvent(keyEvent), mHoldTime(holdTime) +{ +} MissionData::MissionData(const std::shared_ptr &data) : mMissionData(data) { } MissionMessage::MissionMessage(const std::shared_ptr &message) : mMissionData(message) { } +DataProcessing::DataProcessing() +{ + mEventHandle[InternalStateEvent::KEY_EVENT_HANDLE] = std::bind(&DataProcessing::KeyEventHandle, this, _1); +} bool DataProcessing::EventHandle(VStateMachineData *msg) { if (nullptr == msg) { @@ -36,4 +44,30 @@ bool DataProcessing::EventHandle(VStateMachineData *msg) return mEventHandle[event](msg); } return NOT_EXECUTED; +} +bool DataProcessing::KeyEventHandle(VStateMachineData *msg) +{ + if (nullptr == msg) { + LogError("nullptr pointer.\n"); + return NOT_EXECUTED; + } + std::map::iterator iter; + std::shared_ptr message = std::dynamic_pointer_cast(msg->GetMessageObj()); + std::shared_ptr> data = + std::dynamic_pointer_cast>(message->mMissionData); + if (!data) { + LogError("nullptr pointer.\n"); + return NOT_EXECUTED; + } + iter = mKeyClickHandle.find(data->mData.mKeyName); + if (iter != mKeyClickHandle.end() && KeyEvent::SHORT_CLICK == data->mData.mKeyEvent) { + return mKeyClickHandle[data->mData.mKeyName](data->mData); + } + if (iter != mKeyHoldDownHandle.end() && KeyEvent::HOLD_DOWN == data->mData.mKeyEvent) { + return mKeyHoldDownHandle[data->mData.mKeyName](data->mData); + } + if (iter != mKeyHoldUpHandle.end() && KeyEvent::HOLD_UP == data->mData.mKeyEvent) { + return mKeyHoldUpHandle[data->mData.mKeyName](data->mData); + } + return NOT_EXECUTED; } \ No newline at end of file diff --git a/application/MissionManager/src/DataProcessing.h b/application/MissionManager/src/DataProcessing.h index f3675523..1a6b33be 100644 --- a/application/MissionManager/src/DataProcessing.h +++ b/application/MissionManager/src/DataProcessing.h @@ -16,6 +16,7 @@ #define DATA_PROCESSING_H #include "IMissionManager.h" #include "IStateMachine.h" +#include "KeyControl.h" #include #include using std::placeholders::_1; @@ -27,8 +28,17 @@ enum class InternalStateEvent ANY_STATE_SD_STATUS_PERORIED, CHECK_UPGRADE_FILE, MEDIA_REPORT_EVENT, + KEY_EVENT_HANDLE, END }; +typedef struct key_event_data +{ + key_event_data(const std::string &keyName, const KeyEvent &keyEvent, const unsigned int &holdTime); + const std::string mKeyName; + const KeyEvent mKeyEvent; + const unsigned int mHoldTime; +} KeyEventData; +using KeyHandleFunc = std::function; class MissionData : public VStateMachineData { public: @@ -51,11 +61,16 @@ public: class DataProcessing { public: - DataProcessing() = default; + DataProcessing(); virtual ~DataProcessing() = default; bool EventHandle(VStateMachineData *msg); + virtual bool KeyEventHandle(VStateMachineData *msg); protected: std::map mEventHandle; + std::map mKeyEventHandle; + std::map mKeyClickHandle; + std::map mKeyHoldDownHandle; + std::map mKeyHoldUpHandle; }; #endif \ No newline at end of file diff --git a/application/MissionManager/src/TestMissionState.cpp b/application/MissionManager/src/TestMissionState.cpp index e0dd1dd0..0de49f3a 100644 --- a/application/MissionManager/src/TestMissionState.cpp +++ b/application/MissionManager/src/TestMissionState.cpp @@ -21,6 +21,7 @@ TestMissionState::TestMissionState() : MissionState("TestMissionState") { mEventHandle[InternalStateEvent::ANY_STATE_SD_STATUS_PERORIED] = std::bind(&TestMissionState::SdCardEventReportSendToApp, this, _1); + mKeyClickHandle["reset"] = std::bind(&TestMissionState::ClickResetKey, this, _1); } void TestMissionState::GoInState() { @@ -47,6 +48,10 @@ bool TestMissionState::SdCardEventReportSendToApp(VStateMachineData *msg) IAppManager::GetInstance()->SetSdCardStatus(status); return EXECUTED; } +bool TestMissionState::ClickResetKey(const KeyEventData &data) +{ + return EXECUTED; +} SdCardStatus TestMissionState::SdCardStatusConvert(const StorageEvent &event) { switch (event) { diff --git a/application/MissionManager/src/TestMissionState.h b/application/MissionManager/src/TestMissionState.h index 62322aef..1b472ffb 100644 --- a/application/MissionManager/src/TestMissionState.h +++ b/application/MissionManager/src/TestMissionState.h @@ -28,6 +28,7 @@ public: private: bool SdCardEventReportSendToApp(VStateMachineData *msg); + bool ClickResetKey(const KeyEventData &data); private: SdCardStatus SdCardStatusConvert(const StorageEvent &event); diff --git a/application/MissionManager/src/TopState.cpp b/application/MissionManager/src/TopState.cpp index ab1e7bcd..83a6fa5e 100644 --- a/application/MissionManager/src/TopState.cpp +++ b/application/MissionManager/src/TopState.cpp @@ -55,6 +55,10 @@ void TopState::KeyEventReport(const std::string &keyName, const VirtualKeyEvent keyName.c_str(), PrintKeyEvent(static_cast(event)), timeMs); + KeyEventData data(keyName, static_cast(event), timeMs); + std::shared_ptr message = std::make_shared>( + static_cast(InternalStateEvent::KEY_EVENT_HANDLE), data); + MissionStateMachine::GetInstance()->SendStateMessage(message); } bool TopState::StorageStartInitHandle(VStateMachineData *msg) { diff --git a/middleware/McuManager/include/IMcuManager.h b/middleware/McuManager/include/IMcuManager.h index eefcd836..6fc4ae46 100644 --- a/middleware/McuManager/include/IMcuManager.h +++ b/middleware/McuManager/include/IMcuManager.h @@ -20,9 +20,9 @@ bool CreateMcuManager(void); bool DestroyMcuManager(void); enum class IpcMission { - PIR_TRIGGERED = 0, - ON, + PIR_TRIGGERED = 1, TEST, + ON, CONTINUOUS_SHOOTING, PIR_TRIGGERED_DELAY, REGULAR_START, diff --git a/test/application/HuntingCamera/src_mock/TestMissionState_Mock_Test.cpp b/test/application/HuntingCamera/src_mock/TestMissionState_Mock_Test.cpp new file mode 100644 index 00000000..f2e02403 --- /dev/null +++ b/test/application/HuntingCamera/src_mock/TestMissionState_Mock_Test.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "AppManagerTestTool.h" +#include "GtestUsing.h" +#include "HalTestTool.h" +#include "HuntingCameraTest.h" +#include "ILog.h" +#include "MainThread.h" +#include "McuManagerTestTool.h" +#include "MissionManagerTestTool.h" +#include "TestManager.h" +#include +namespace TestMissionState_Mock_Test +{ +/** + * @brief Construct a new test f object + * ../output_files/test/bin/HuntingCameraTest + * --gtest_filter=HuntingCameraTest.HS_INTEGRATION_HunttingCamera_EXAMPLE_TestMissionResetKeyCapture + */ +TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_TestMissionResetKeyCapture) +{ + McuManagerTestTool::MockOtherSideIpcMissionReply(IpcMission::TEST); + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + HalTestTool::MockKeyClick("reset", 200); // Simulate pressing a button. + MainThread::GetInstance()->Runing(); +} +} \ No newline at end of file diff --git a/test/application/MissionManager/tool/CMakeLists.txt b/test/application/MissionManager/tool/CMakeLists.txt index 22ad385f..8cf71139 100644 --- a/test/application/MissionManager/tool/CMakeLists.txt +++ b/test/application/MissionManager/tool/CMakeLists.txt @@ -21,6 +21,7 @@ include_directories( ${UTILS_SOURCE_PATH}/Log/include ${UTILS_SOURCE_PATH}/LinuxApi/include ${UTILS_SOURCE_PATH}/UpgradeTool/include + ${UTILS_SOURCE_PATH}/KeyControl/include ${TEST_SOURCE_PATH} ${TEST_SOURCE_PATH}/middleware/AppManager/tool/include ${TEST_SOURCE_PATH}/middleware/AppManager/tool/src @@ -36,7 +37,7 @@ include_directories( aux_source_directory(./src TEST_TOOL_SRC_FILES) set(TEST_TOOL_TARGET MissionManagerTestTool) add_library(${TEST_TOOL_TARGET} STATIC ${TEST_TOOL_SRC_FILES}) -target_link_libraries(${TEST_TOOL_TARGET} MissionManager AppManagerTestTool MediaManagerTestTool UpgradeTool StatusCode Log) +target_link_libraries(${TEST_TOOL_TARGET} MissionManager AppManagerTestTool MediaManagerTestTool KeyControl UpgradeTool StatusCode Log) if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") add_custom_target(