Improve:key event handle.

This commit is contained in:
Fancy code 2024-05-29 19:58:27 +08:00
parent 2d9e3711b1
commit a0070c5747
8 changed files with 104 additions and 4 deletions

View File

@ -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<VMissionData> &data) : mMissionData(data)
{
}
MissionMessage::MissionMessage(const std::shared_ptr<VMissionData> &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<std::string, KeyHandleFunc>::iterator iter;
std::shared_ptr<MissionMessage> message = std::dynamic_pointer_cast<MissionMessage>(msg->GetMessageObj());
std::shared_ptr<VMissionDataV2<KeyEventData>> data =
std::dynamic_pointer_cast<VMissionDataV2<KeyEventData>>(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;
}

View File

@ -16,6 +16,7 @@
#define DATA_PROCESSING_H
#include "IMissionManager.h"
#include "IStateMachine.h"
#include "KeyControl.h"
#include <functional>
#include <map>
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<bool(const KeyEventData &)>;
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<InternalStateEvent, DataProcessingFunc> mEventHandle;
std::map<std::string, DataProcessingFunc> mKeyEventHandle;
std::map<std::string, KeyHandleFunc> mKeyClickHandle;
std::map<std::string, KeyHandleFunc> mKeyHoldDownHandle;
std::map<std::string, KeyHandleFunc> mKeyHoldUpHandle;
};
#endif

View File

@ -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) {

View File

@ -28,6 +28,7 @@ public:
private:
bool SdCardEventReportSendToApp(VStateMachineData *msg);
bool ClickResetKey(const KeyEventData &data);
private:
SdCardStatus SdCardStatusConvert(const StorageEvent &event);

View File

@ -55,6 +55,10 @@ void TopState::KeyEventReport(const std::string &keyName, const VirtualKeyEvent
keyName.c_str(),
PrintKeyEvent(static_cast<KeyEvent>(event)),
timeMs);
KeyEventData data(keyName, static_cast<KeyEvent>(event), timeMs);
std::shared_ptr<VMissionData> message = std::make_shared<VMissionDataV2<KeyEventData>>(
static_cast<MissionEvent>(InternalStateEvent::KEY_EVENT_HANDLE), data);
MissionStateMachine::GetInstance()->SendStateMessage(message);
}
bool TopState::StorageStartInitHandle(VStateMachineData *msg)
{

View File

@ -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,

View File

@ -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 <thread>
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();
}
}

View File

@ -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(