From a9b4eee4ea4c5d0444e69f9a9f168ebd219a7919 Mon Sep 17 00:00:00 2001 From: Fancy code <258828110.@qq.com> Date: Thu, 6 Jun 2024 21:35:36 +0800 Subject: [PATCH] Add:led control function. --- application/MissionManager/CMakeLists.txt | 3 +- application/MissionManager/src/LedsHandle.cpp | 37 +++++++++++++ application/MissionManager/src/LedsHandle.h | 46 ++++++++++++++++ .../MissionManager/src/MissionState.cpp | 1 + application/MissionManager/src/MissionState.h | 6 +- .../MissionManager/src/SetLedState.cpp | 44 +++++++++++++++ application/MissionManager/src/SetLedState.h | 42 ++++++++++++++ .../MissionManager/src/TestMissionState.cpp | 1 + .../DeviceManager/include/IDeviceManager.h | 8 +-- .../DeviceManager/src/DeviceManager.cpp | 2 +- middleware/DeviceManager/src/DeviceManager.h | 2 +- .../DeviceManager/src/IDeviceManager.cpp | 2 +- middleware/DeviceManager/src/LedManager.cpp | 12 +++- middleware/DeviceManager/src/LedManager.h | 2 +- .../src_mock/DeviceManager_Mock_Test.cpp | 14 +++++ .../src_mock/HuntingCameraTest.cpp | 15 ++++- .../src_mock/HuntingCameraTest.h | 3 + .../MissionManager/tool/CMakeLists.txt | 3 +- test/hal/tool/src/HalTestTool.cpp | 6 +- test/hal/tool/src/LedControlMock.cpp | 2 - .../DeviceManager/src/DeviceManager_Test.cpp | 16 ++++++ .../tool/include/DeviceManagerTestTool.h | 3 +- .../tool/src/DeviceManagerTestTool.cpp | 14 ++++- .../tool/src/SingleControlMock.cpp | 4 ++ .../tool/src/SingleControlMock.h | 7 ++- utils/LedControl/include/LedControl.h | 46 +++++++--------- utils/LedControl/src/LedControl.cpp | 55 +++++++++++++++++++ 27 files changed, 344 insertions(+), 52 deletions(-) create mode 100644 application/MissionManager/src/LedsHandle.cpp create mode 100644 application/MissionManager/src/LedsHandle.h create mode 100644 application/MissionManager/src/SetLedState.cpp create mode 100644 application/MissionManager/src/SetLedState.h diff --git a/application/MissionManager/CMakeLists.txt b/application/MissionManager/CMakeLists.txt index b566318..10d17bb 100644 --- a/application/MissionManager/CMakeLists.txt +++ b/application/MissionManager/CMakeLists.txt @@ -10,6 +10,7 @@ include_directories( ${UTILS_SOURCE_PATH}/StatusCode/include ${UTILS_SOURCE_PATH}/Log/include ${UTILS_SOURCE_PATH}/KeyControl/include + ${UTILS_SOURCE_PATH}/LedControl/include ${MIDDLEWARE_SOURCE_PATH}/StateMachine/include ${MIDDLEWARE_SOURCE_PATH}/AppManager/include ${MIDDLEWARE_SOURCE_PATH}/MediaManager/include @@ -29,7 +30,7 @@ aux_source_directory(./src SRC_FILES) set(TARGET_NAME MissionManager) add_library(${TARGET_NAME} STATIC ${SRC_FILES}) -target_link_libraries(${TARGET_NAME} McuAskBase StateMachine MediaManager StorageManager DeviceManager HuntingUpgrade KeyControl StatusCode Log) +target_link_libraries(${TARGET_NAME} McuAskBase StateMachine MediaManager StorageManager DeviceManager HuntingUpgrade KeyControl LedControl StatusCode Log) if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") add_custom_target( diff --git a/application/MissionManager/src/LedsHandle.cpp b/application/MissionManager/src/LedsHandle.cpp new file mode 100644 index 0000000..57fab91 --- /dev/null +++ b/application/MissionManager/src/LedsHandle.cpp @@ -0,0 +1,37 @@ +/* + * 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 "LedsHandle.h" +#include "ILog.h" +void LedsHandle::ControlDeviceStatusLed(const DeviceStatus &status, const long int &keepAliveTime, + const unsigned int &blinkPeriod) +{ + switch (status) { + case DeviceStatus::NORMAL: + mDeviceStatus = SetLedState::ControlLed("device_status", LedState::GREEN, keepAliveTime, blinkPeriod); + break; + + default: + LogWarning("unknow device status.\n"); + break; + } +} +void inline LedsHandle::DeleteDeviceStatusLed(void) +{ + mDeviceStatus->DeleteState(); +} +void LedsHandle::DeleteAllLeds(void) +{ + DeleteDeviceStatusLed(); +} \ No newline at end of file diff --git a/application/MissionManager/src/LedsHandle.h b/application/MissionManager/src/LedsHandle.h new file mode 100644 index 0000000..e394d5b --- /dev/null +++ b/application/MissionManager/src/LedsHandle.h @@ -0,0 +1,46 @@ +/* + * 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. + */ +#ifndef LEDS_HANDLE_H +#define LEDS_HANDLE_H +#include "SetLedState.h" +enum class DeviceStatus +{ + NORMAL = 0, + TAKING_PICTURE_OR_VIDEO, + END +}; +class LedsHandle +{ +public: + LedsHandle() = default; + virtual ~LedsHandle() = default; + +protected: + /** + * @brief This function is designed as a virtual function so that when the board uses different LED circuits, this + * function can be overloaded to achieve polymorphic control of the light. + * @param status + * @param keepAliveTime + * @param blinkPeriod + */ + virtual void ControlDeviceStatusLed(const DeviceStatus &status, const long int &keepAliveTime = KEEP_ALIVE_FOREVER, + const unsigned int &blinkPeriod = LED_NOT_BLINK); + void DeleteDeviceStatusLed(void); + void DeleteAllLeds(void); + +private: + std::shared_ptr mDeviceStatus; +}; +#endif \ No newline at end of file diff --git a/application/MissionManager/src/MissionState.cpp b/application/MissionManager/src/MissionState.cpp index a872ff9..04b3608 100644 --- a/application/MissionManager/src/MissionState.cpp +++ b/application/MissionManager/src/MissionState.cpp @@ -30,6 +30,7 @@ void MissionState::GoInState() void MissionState::GoOutState() { LogInfo(" ========== MissionState::GoOutState.\n"); + LedsHandle::DeleteAllLeds(); } bool MissionState::ExecuteStateMsg(VStateMachineData *msg) { diff --git a/application/MissionManager/src/MissionState.h b/application/MissionManager/src/MissionState.h index 5b11988..2f5fcca 100644 --- a/application/MissionManager/src/MissionState.h +++ b/application/MissionManager/src/MissionState.h @@ -16,7 +16,11 @@ #define MISSION_STATE_H #include "DataProcessing.h" #include "IStateMachine.h" -class MissionState : public State, public DataProcessing, public std::enable_shared_from_this +#include "LedsHandle.h" +class MissionState : public State, + public DataProcessing, + public LedsHandle, + public std::enable_shared_from_this { public: MissionState(const std::string &name); diff --git a/application/MissionManager/src/SetLedState.cpp b/application/MissionManager/src/SetLedState.cpp new file mode 100644 index 0000000..d0f9b81 --- /dev/null +++ b/application/MissionManager/src/SetLedState.cpp @@ -0,0 +1,44 @@ +/* + * 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 "SetLedState.h" +SetLedState::SetLedState(const LedState &state, const unsigned int &keepAliveTime, const unsigned int &blinkPeriod) + : mState(state), mKeepAliveTime(keepAliveTime), mBlinkPeriod(blinkPeriod) +{ +} +StatusCode SetLedState::GetLedState(LedState &state) +{ + state = mState; + return CreateStatusCode(STATUS_CODE_OK); +} +unsigned int SetLedState::GetKeepAliveTimeMs(void) +{ + return mKeepAliveTime; +} +unsigned int SetLedState::GetBlinkTimeMs(void) +{ + return mBlinkPeriod; +} +void SetLedState::DeleteState(void) +{ + mKeepAliveTime = DELETED_LED_STATE; +} +std::shared_ptr SetLedState::ControlLed(const std::string &ledName, const LedState &state, + const long int &keepAliveTime, const unsigned int &blinkPeriod) +{ + std::shared_ptr ledState = std::make_shared(state, keepAliveTime, blinkPeriod); + std::shared_ptr ledState2 = ledState; + IDeviceManager::GetInstance()->ControlLed(ledName, ledState2); + return ledState; +} \ No newline at end of file diff --git a/application/MissionManager/src/SetLedState.h b/application/MissionManager/src/SetLedState.h new file mode 100644 index 0000000..e18ba58 --- /dev/null +++ b/application/MissionManager/src/SetLedState.h @@ -0,0 +1,42 @@ +/* + * 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. + */ +#ifndef SET_LED_STATE_H +#define SET_LED_STATE_H +#include "IDeviceManager.h" +#include "LedControl.h" +constexpr int LED_BLINKING_FAST_PERIOD = 500; +constexpr int LED_BLINKING_SLOW_PERIOD = 1000; +class SetLedState : public LedControlContext, public VSingleControl +{ +public: + SetLedState(const LedState &state, const unsigned int &keepAliveTime = KEEP_ALIVE_FOREVER, + const unsigned int &blinkPeriod = LED_NOT_BLINK); + virtual ~SetLedState() = default; + StatusCode GetLedState(LedState &state) override; + unsigned int GetKeepAliveTimeMs(void) override; + unsigned int GetBlinkTimeMs(void) override; + void DeleteState(void); + +public: + static std::shared_ptr ControlLed(const std::string &ledName, const LedState &state, + const long int &keepAliveTime = KEEP_ALIVE_FOREVER, + const unsigned int &blinkPeriod = LED_NOT_BLINK); + +private: + const LedState mState; + unsigned int mKeepAliveTime; + const unsigned int mBlinkPeriod; +}; +#endif \ No newline at end of file diff --git a/application/MissionManager/src/TestMissionState.cpp b/application/MissionManager/src/TestMissionState.cpp index 2dd95a9..7af850d 100644 --- a/application/MissionManager/src/TestMissionState.cpp +++ b/application/MissionManager/src/TestMissionState.cpp @@ -34,6 +34,7 @@ void TestMissionState::GoInState() std::shared_ptr monitor = std::dynamic_pointer_cast(MissionState::shared_from_this()); IAppManager::GetInstance()->SetAppMonitor(monitor); + ControlDeviceStatusLed(DeviceStatus::NORMAL); } void TestMissionState::GoOutState() { diff --git a/middleware/DeviceManager/include/IDeviceManager.h b/middleware/DeviceManager/include/IDeviceManager.h index a479a4a..a6a5389 100644 --- a/middleware/DeviceManager/include/IDeviceManager.h +++ b/middleware/DeviceManager/include/IDeviceManager.h @@ -38,11 +38,11 @@ public: virtual ~VKeyMonitor() = default; virtual void KeyEventReport(const std::string &keyName, const VirtualKeyEvent &event, const unsigned int &timeMs); }; -class VirtualLedControl +class LedControlContext { public: - VirtualLedControl() = default; - virtual ~VirtualLedControl() = default; + LedControlContext() = default; + virtual ~LedControlContext() = default; }; class IDeviceManager { @@ -52,7 +52,7 @@ public: static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); virtual const StatusCode Init(void); virtual const StatusCode UnInit(void); - virtual const StatusCode ControlLed(const std::string &ledName, std::shared_ptr &control); + virtual const StatusCode ControlLed(const std::string &ledName, std::shared_ptr &control); virtual const StatusCode SetAllKeysMonitor(std::shared_ptr &monitor); }; #endif \ No newline at end of file diff --git a/middleware/DeviceManager/src/DeviceManager.cpp b/middleware/DeviceManager/src/DeviceManager.cpp index c97eea3..a23b191 100644 --- a/middleware/DeviceManager/src/DeviceManager.cpp +++ b/middleware/DeviceManager/src/DeviceManager.cpp @@ -36,7 +36,7 @@ const StatusCode DeviceManager::SetAllKeysMonitor(std::shared_ptr & LogInfo("DeviceManager::SetAllKeysMonitor\n"); return KeyManager::GetInstance()->SetKeyMonitor(monitor); } -const StatusCode DeviceManager::ControlLed(const std::string &ledName, std::shared_ptr &control) +const StatusCode DeviceManager::ControlLed(const std::string &ledName, std::shared_ptr &control) { LedManager::GetInstance()->ControlLed(ledName, control); return CreateStatusCode(STATUS_CODE_OK); diff --git a/middleware/DeviceManager/src/DeviceManager.h b/middleware/DeviceManager/src/DeviceManager.h index faf003c..682ce65 100644 --- a/middleware/DeviceManager/src/DeviceManager.h +++ b/middleware/DeviceManager/src/DeviceManager.h @@ -26,7 +26,7 @@ public: const StatusCode Init(void) override; const StatusCode UnInit(void) override; const StatusCode SetAllKeysMonitor(std::shared_ptr &monitor) override; - const StatusCode ControlLed(const std::string &ledName, std::shared_ptr &control) override; + const StatusCode ControlLed(const std::string &ledName, std::shared_ptr &control) override; private: // std::vector> mLedManagers; diff --git a/middleware/DeviceManager/src/IDeviceManager.cpp b/middleware/DeviceManager/src/IDeviceManager.cpp index 3c3ff15..4ad0e7d 100644 --- a/middleware/DeviceManager/src/IDeviceManager.cpp +++ b/middleware/DeviceManager/src/IDeviceManager.cpp @@ -39,7 +39,7 @@ const StatusCode IDeviceManager::UnInit(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } -const StatusCode IDeviceManager::ControlLed(const std::string &ledName, std::shared_ptr &control) +const StatusCode IDeviceManager::ControlLed(const std::string &ledName, std::shared_ptr &control) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } diff --git a/middleware/DeviceManager/src/LedManager.cpp b/middleware/DeviceManager/src/LedManager.cpp index a147722..5f3a33a 100644 --- a/middleware/DeviceManager/src/LedManager.cpp +++ b/middleware/DeviceManager/src/LedManager.cpp @@ -37,6 +37,11 @@ void LedManager::Init(void) void LedManager::UnInit(void) { StopTimer(); + mMutex.lock(); + for (auto &iter : mAllLedHal) { + iter.second->DeleteAllState(); + } + mMutex.unlock(); mAllLedHal.clear(); } void LedManager::Timer(void) @@ -53,7 +58,7 @@ void LedManager::Timer(void) std::this_thread::sleep_for(std::chrono::milliseconds(LED_STATE_CHECK_PERIOD_MS)); } } -void LedManager::ControlLed(const std::string &ledName, std::shared_ptr &control) +void LedManager::ControlLed(const std::string &ledName, std::shared_ptr &control) { std::lock_guard locker(mMutex); std::shared_ptr singleControl = std::dynamic_pointer_cast(control); @@ -61,6 +66,11 @@ void LedManager::ControlLed(const std::string &ledName, std::shared_ptrAddLedState(singleControl); } void LedManager::StartTimer(void) diff --git a/middleware/DeviceManager/src/LedManager.h b/middleware/DeviceManager/src/LedManager.h index 07d1ca8..2ebc6ea 100644 --- a/middleware/DeviceManager/src/LedManager.h +++ b/middleware/DeviceManager/src/LedManager.h @@ -31,7 +31,7 @@ public: void UnInit(void); void StartTimer(void); void StopTimer(void); - void ControlLed(const std::string &ledName, std::shared_ptr &control); + void ControlLed(const std::string &ledName, std::shared_ptr &control); private: void Timer(void); diff --git a/test/application/HuntingCamera/src_mock/DeviceManager_Mock_Test.cpp b/test/application/HuntingCamera/src_mock/DeviceManager_Mock_Test.cpp index e38c843..1a0dc5d 100644 --- a/test/application/HuntingCamera/src_mock/DeviceManager_Mock_Test.cpp +++ b/test/application/HuntingCamera/src_mock/DeviceManager_Mock_Test.cpp @@ -65,4 +65,18 @@ TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_KeyControlClick) std::this_thread::sleep_for(std::chrono::milliseconds(100)); MainThread::GetInstance()->Runing(); } +/** + * @brief Construct a new test f object + * ../output_files/test/bin/HuntingCameraTest + * --gtest_filter=HuntingCameraTest.HS_INTEGRATION_HunttingCamera_EXAMPLE_LedControl + */ +TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_LedControl) +{ + SetAllLedsResult(mAllLedsMock); + MockOtherSideIpcMissionReply(IpcMission::TEST); + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MainThread::GetInstance()->Runing(); +} } // namespace DeviceManager_Mock_Test \ No newline at end of file diff --git a/test/application/HuntingCamera/src_mock/HuntingCameraTest.cpp b/test/application/HuntingCamera/src_mock/HuntingCameraTest.cpp index 9759b88..579e0ad 100644 --- a/test/application/HuntingCamera/src_mock/HuntingCameraTest.cpp +++ b/test/application/HuntingCamera/src_mock/HuntingCameraTest.cpp @@ -18,6 +18,7 @@ #include "MainThread.h" #include const char *KEY_RESET = "reset"; +const char *LED_DEVICE_STATUS = "device_status"; void MainThreadTest::CustomizationInit(void) { // Do nothing here to make sure test tool work. @@ -44,6 +45,7 @@ void HuntingCameraTest::SetUp() MainThread::GetInstance(&mainThread); HalTestTool::Init(); CreateAllKeysMcok(); + CreateAllLedsMcok(); AppManagerTestTool::Init(); MissionManagerTestTool::Init(); mLinuxTest = LinuxTest::CreateLinuxTest(); @@ -72,6 +74,7 @@ void HuntingCameraTest::TearDown() DeviceManagerTestTool::UnInit(); DestroyAllCamerasMock(); DestroyAllKeysMock(); + DestroyAllLedsMock(); } void HuntingCameraTest::CreateAllCamerasMcok(void) { @@ -86,13 +89,19 @@ void HuntingCameraTest::CreateAllKeysMcok(void) { std::shared_ptr key = HalTestTool::MakeKeyHalTest(KEY_RESET); mAllKeysMock[KEY_RESET] = key; - // std::shared_ptr led = HalTestTool::MakeLedHalTest(LED_TEST); - // mAllLedsMock[LED_TEST] = led; HalTestTool::SetAllKeysResult(mAllKeysMock); } void HuntingCameraTest::DestroyAllKeysMock(void) { mAllKeysMock.clear(); - // mAllLedsMock.clear(); +} +void HuntingCameraTest::CreateAllLedsMcok(void) +{ + std::shared_ptr led = HalTestTool::MakeLedHalTest(LED_DEVICE_STATUS); + mAllLedsMock[LED_DEVICE_STATUS] = led; +} +void HuntingCameraTest::DestroyAllLedsMock(void) +{ + mAllLedsMock.clear(); } \ No newline at end of file diff --git a/test/application/HuntingCamera/src_mock/HuntingCameraTest.h b/test/application/HuntingCamera/src_mock/HuntingCameraTest.h index 3cae61d..2921507 100644 --- a/test/application/HuntingCamera/src_mock/HuntingCameraTest.h +++ b/test/application/HuntingCamera/src_mock/HuntingCameraTest.h @@ -52,11 +52,14 @@ private: void DestroyAllCamerasMock(void); void CreateAllKeysMcok(void); void DestroyAllKeysMock(void); + void CreateAllLedsMcok(void); + void DestroyAllLedsMock(void); protected: std::shared_ptr mLinuxTest; std::map> mAllCamerasMock; std::map> mAllKeysMock; + std::map> mAllLedsMock; }; #endif \ No newline at end of file diff --git a/test/application/MissionManager/tool/CMakeLists.txt b/test/application/MissionManager/tool/CMakeLists.txt index c488dfa..bf73dae 100644 --- a/test/application/MissionManager/tool/CMakeLists.txt +++ b/test/application/MissionManager/tool/CMakeLists.txt @@ -22,6 +22,7 @@ include_directories( ${UTILS_SOURCE_PATH}/LinuxApi/include ${UTILS_SOURCE_PATH}/UpgradeTool/include ${UTILS_SOURCE_PATH}/KeyControl/include + ${UTILS_SOURCE_PATH}/LedControl/include ${TEST_SOURCE_PATH} ${TEST_SOURCE_PATH}/middleware/AppManager/tool/include ${TEST_SOURCE_PATH}/middleware/AppManager/tool/src @@ -37,7 +38,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 KeyControl UpgradeTool StatusCode Log) +target_link_libraries(${TEST_TOOL_TARGET} MissionManager AppManagerTestTool MediaManagerTestTool KeyControl LedControl UpgradeTool StatusCode Log) if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") add_custom_target( diff --git a/test/hal/tool/src/HalTestTool.cpp b/test/hal/tool/src/HalTestTool.cpp index c1d9b6d..305a174 100644 --- a/test/hal/tool/src/HalTestTool.cpp +++ b/test/hal/tool/src/HalTestTool.cpp @@ -272,8 +272,10 @@ void HalTestTool::InitLedsMock(std::shared_ptr &vMock) LogError("vMock error.\n"); return; } - constexpr int LED_SHOULD_NOT_BE_CONTROLED_WHEN_NOBODY_CONTROL_IT = 0; - EXPECT_CALL(*mock.get(), SetLedStateTrace(_)).Times(LED_SHOULD_NOT_BE_CONTROLED_WHEN_NOBODY_CONTROL_IT); + // constexpr int LED_SHOULD_NOT_BE_CONTROLED_WHEN_NOBODY_CONTROL_IT = 0; + EXPECT_CALL(*mock.get(), SetLedStateTrace(_)) + .Times(AnyNumber()) + .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_OK)))); } void HalTestTool::SetAllCamerasResult(std::shared_ptr &vMock, std::map> &allCameras) diff --git a/test/hal/tool/src/LedControlMock.cpp b/test/hal/tool/src/LedControlMock.cpp index 13417e0..87fb0de 100644 --- a/test/hal/tool/src/LedControlMock.cpp +++ b/test/hal/tool/src/LedControlMock.cpp @@ -16,11 +16,9 @@ #include "ILog.h" LedControlTest::LedControlTest(const std::string &ledName) : mLedName(ledName) { - // } LedControlMock::LedControlMock(const std::string &ledName) : LedControlTest(ledName) { - // } StatusCode LedControlTest::SetLedState(const LedState &state) { diff --git a/test/middleware/DeviceManager/src/DeviceManager_Test.cpp b/test/middleware/DeviceManager/src/DeviceManager_Test.cpp index cd56856..ebe37f0 100644 --- a/test/middleware/DeviceManager/src/DeviceManager_Test.cpp +++ b/test/middleware/DeviceManager/src/DeviceManager_Test.cpp @@ -196,4 +196,20 @@ TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed4) std::this_thread::sleep_for(std::chrono::milliseconds(2000)); IDeviceManager::GetInstance()->UnInit(); } +// ../output_files/test/bin/DeviceManagerTest +// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed5 +TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed5) +{ + SetAllLedsResult(mAllLedsMock); + IDeviceManager::GetInstance()->Init(); + std::shared_ptr monitor = std::make_shared(); + IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor); + std::shared_ptr ledControl = + DeviceManagerTestTool::ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE_FOREVER, LED_NOT_BLINK); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + DeviceManagerTestTool::DestoryLedControl(ledControl); + DeviceManagerTestTool::ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE_FOREVER, LED_NOT_BLINK); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IDeviceManager::GetInstance()->UnInit(); +} } // namespace DeviceManagerTest \ No newline at end of file diff --git a/test/middleware/DeviceManager/tool/include/DeviceManagerTestTool.h b/test/middleware/DeviceManager/tool/include/DeviceManagerTestTool.h index 9ee6027..56f0a52 100644 --- a/test/middleware/DeviceManager/tool/include/DeviceManagerTestTool.h +++ b/test/middleware/DeviceManager/tool/include/DeviceManagerTestTool.h @@ -54,8 +54,9 @@ public: void UnInit(void); public: - std::shared_ptr ControlLed(const std::string &ledName, const LedState &state, + std::shared_ptr ControlLed(const std::string &ledName, const LedState &state, const unsigned int &aliveTimeMs, const unsigned int &blinkTimeMs); + void DestoryLedControl(std::shared_ptr &ledControl); protected: void DeviceManagerNotice(const std::string &keyName, const unsigned int &pressingTimeMs) override; diff --git a/test/middleware/DeviceManager/tool/src/DeviceManagerTestTool.cpp b/test/middleware/DeviceManager/tool/src/DeviceManagerTestTool.cpp index 2895e1f..5a1072d 100644 --- a/test/middleware/DeviceManager/tool/src/DeviceManagerTestTool.cpp +++ b/test/middleware/DeviceManager/tool/src/DeviceManagerTestTool.cpp @@ -38,16 +38,26 @@ void DeviceManagerTestTool::UnInit(void) mKeyMonitorMock.reset(); CancelOverrideDeviceMakePtrObject(); } -std::shared_ptr DeviceManagerTestTool::ControlLed(const std::string &ledName, const LedState &state, +std::shared_ptr DeviceManagerTestTool::ControlLed(const std::string &ledName, const LedState &state, const unsigned int &aliveTimeMs, const unsigned int &blinkTimeMs) { HalTestTool::SetLedStateExpectations(ledName, state, aliveTimeMs, blinkTimeMs); - std::shared_ptr ledControl = + std::shared_ptr ledControl = std::make_shared(state, aliveTimeMs, blinkTimeMs); IDeviceManager::GetInstance()->ControlLed(ledName, ledControl); return ledControl; } +void DeviceManagerTestTool::DestoryLedControl(std::shared_ptr &ledControl) +{ + std::shared_ptr ledControlMock = std::dynamic_pointer_cast(ledControl); + if (ledControlMock) { + ledControlMock->DeleteState(); + } + else { + LogWarning("DestoryLedCtx failed.\n"); + } +} void DeviceManagerTestTool::DeviceManagerNotice(const std::string &keyName, const unsigned int &pressingTimeMs) { LogInfo("DeviceManagerTestTool::DeviceManagerNotice\n"); diff --git a/test/middleware/DeviceManager/tool/src/SingleControlMock.cpp b/test/middleware/DeviceManager/tool/src/SingleControlMock.cpp index 0ca4cb6..fd51171 100644 --- a/test/middleware/DeviceManager/tool/src/SingleControlMock.cpp +++ b/test/middleware/DeviceManager/tool/src/SingleControlMock.cpp @@ -24,6 +24,10 @@ StatusCode SingleControlTool::GetLedState(LedState &state) state = mState; return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } +void SingleControlTool::DeleteState(void) +{ + mAliveTimeMs = DELETED_LED_STATE; +} SingleControlMock::SingleControlMock(const LedState &state, const unsigned int aliveTimeMs, const unsigned int &blinkTimeMs) : SingleControlTool(state, aliveTimeMs, blinkTimeMs) diff --git a/test/middleware/DeviceManager/tool/src/SingleControlMock.h b/test/middleware/DeviceManager/tool/src/SingleControlMock.h index 91fd65e..762da50 100644 --- a/test/middleware/DeviceManager/tool/src/SingleControlMock.h +++ b/test/middleware/DeviceManager/tool/src/SingleControlMock.h @@ -16,7 +16,7 @@ #define LED_CONTROL_MOCK_H #include "IDeviceManager.h" #include "LedControl.h" -class SingleControlTool : public VSingleControl, public VirtualLedControl +class SingleControlTool : public VSingleControl, public LedControlContext { public: SingleControlTool(const LedState &state, const unsigned int aliveTimeMs, const unsigned int &blinkTimeMs); @@ -30,11 +30,12 @@ public: { return mBlinkTimeMs; } + void DeleteState(void); private: const LedState mState; - const unsigned int mAliveTimeMs; - const unsigned int mBlinkTimeMs; + unsigned int mAliveTimeMs; + unsigned int mBlinkTimeMs; }; class SingleControlMock : public SingleControlTool { diff --git a/utils/LedControl/include/LedControl.h b/utils/LedControl/include/LedControl.h index c0c1a1a..af95786 100644 --- a/utils/LedControl/include/LedControl.h +++ b/utils/LedControl/include/LedControl.h @@ -30,7 +30,12 @@ enum class LedState ON, GREEN, RED, + BLUE, YELLOW, + GREEN_RED_MEANS_YELLOW, + GREEN_BLUE_MEANS_CYAN, + RED_BLUE_MEANS_PURPLE, + GREEN_RED_BLUE_MEANS_WHITE, LEVEL_0, LEVEL_1, LEVEL_2, @@ -44,39 +49,20 @@ class VSingleControl public: VSingleControl() = default; virtual ~VSingleControl() = default; - virtual StatusCode GetLedState(LedState &state) - { - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); - } - virtual unsigned int GetKeepAliveTimeMs(void) - { - return KEEP_ALIVE_FOREVER; - } - virtual unsigned int GetBlinkTimeMs(void) - { - return LED_NOT_BLINK; - } + virtual StatusCode GetLedState(LedState &state); + virtual unsigned int GetKeepAliveTimeMs(void); + virtual unsigned int GetBlinkTimeMs(void); }; class VLedControl { public: VLedControl() = default; virtual ~VLedControl() = default; - virtual StatusCode SetLedState(const LedState &state) - { - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); - } - virtual void AddLedState(std::shared_ptr &control) - { - } - virtual void CheckState(const unsigned int &period) - { - } - // virtual void SetHalLedState(const VirtualLedState &state) {} - virtual std::string GetLedName(void) - { - return "undefine"; - } + virtual StatusCode SetLedState(const LedState &state); + virtual void AddLedState(std::shared_ptr &control); + virtual void CheckState(const unsigned int &period); + virtual std::string GetLedName(void); + virtual void DeleteAllState(void); }; class LedControl : virtual public VLedControl { @@ -87,6 +73,12 @@ public: void AddLedState(std::shared_ptr &state) override; private: + void DeleteAllState(void) override; + /** + * @brief Each time you control a light, check for invalid data to avoid wasting memory resources. + * + */ + void DeleteUselessState(void); void NewLedStateStart(void); void DeleteTopLedState(void); void BlinkOff(std::shared_ptr &state); diff --git a/utils/LedControl/src/LedControl.cpp b/utils/LedControl/src/LedControl.cpp index 62fea64..3191ae7 100644 --- a/utils/LedControl/src/LedControl.cpp +++ b/utils/LedControl/src/LedControl.cpp @@ -14,9 +14,45 @@ */ #include "LedControl.h" #include "ILog.h" +#include +StatusCode VSingleControl::GetLedState(LedState &state) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +unsigned int VSingleControl::GetKeepAliveTimeMs(void) +{ + return KEEP_ALIVE_FOREVER; +} +unsigned int VSingleControl::GetBlinkTimeMs(void) +{ + return LED_NOT_BLINK; +} +StatusCode VLedControl::SetLedState(const LedState &state) +{ + LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n"); + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +void VLedControl::AddLedState(std::shared_ptr &control) +{ + LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n"); +} +void VLedControl::CheckState(const unsigned int &period) +{ + LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n"); +} +std::string VLedControl::GetLedName(void) +{ + LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n"); + return "undefine"; +} +void VLedControl::DeleteAllState(void) +{ + LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n"); +} void LedControl::AddLedState(std::shared_ptr &state) { LogInfo("Add led state.\n"); + DeleteUselessState(); NewLedStateStart(); LedState ledState = LedState::END; state->GetLedState(ledState); @@ -27,6 +63,24 @@ void LedControl::AddLedState(std::shared_ptr &state) } mStates.push_back(state); } +void LedControl::DeleteAllState(void) +{ + mStates.clear(); + SetLedState(LedState::OFF); +} +void LedControl::DeleteUselessState(void) +{ + constexpr bool DELETE_STATE = true; + constexpr bool KEEP_STATE = false; + auto is_to_remove = [](const std::shared_ptr &state) { + if (DELETED_LED_STATE == state->GetKeepAliveTimeMs()) { + LogInfo(" Delete useless led state.\n"); + return DELETE_STATE; + } + return KEEP_STATE; + }; + mStates.erase(std::remove_if(mStates.begin(), mStates.end(), is_to_remove), mStates.end()); +} void LedControl::CheckState(const unsigned int &period) { const int TOP_STATE_SHOW = mStates.size() - 1; @@ -71,6 +125,7 @@ void LedControl::DeleteTopLedState(void) mStates.erase(mStates.begin() + TOP_STATE_SHOW); const int NEXT_LED_STATE = mStates.size() - 1; if (NEXT_LED_STATE < 0) { + SetLedState(LedState::OFF); return; } LogInfo("Top next led state.\n");