This commit is contained in:
Fancy code 2024-06-07 09:37:05 +08:00
commit a2159d9951
27 changed files with 378 additions and 60 deletions

View File

@ -12,6 +12,12 @@ set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTOR
message("platform = ${TARGET_PLATFORM}")
message("platform PATH = ${PLATFORM_PATH}")
add_custom_target(
sdk_clean
COMMAND echo "sdk clean finished."
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
# Gdb debug
include(build/sdk_config.cmake)
@ -166,22 +172,6 @@ set(TEST_LINUX_MOCK "" CACHE STRING INTERNAL)
# if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX})
set(TEST_LINK_LIB "testUtils" CACHE STRING INTERNAL FORCE)
# endif()
if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX})
# set(TEST_LINUX_MOCK "-Wl,--wrap=fopen,--wrap=fprintf_gpio,--wrap=fprintf_dir" CACHE STRING INTERNAL FORCE)
# set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=tcgetattr" CACHE STRING INTERNAL FORCE)
# set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=tcsetattr" CACHE STRING INTERNAL FORCE)
# set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=gethostbyname" CACHE STRING INTERNAL FORCE)
# set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=connect" CACHE STRING INTERNAL FORCE)
# set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=socket" CACHE STRING INTERNAL FORCE)
# set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=select" CACHE STRING INTERNAL FORCE)
# set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=linux_open" CACHE STRING INTERNAL FORCE)
# set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=linux_read" CACHE STRING INTERNAL FORCE)
# set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=linux_write" CACHE STRING INTERNAL FORCE)
# set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=linux_close" CACHE STRING INTERNAL FORCE)
# set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=linux_fclose" CACHE STRING INTERNAL FORCE)
# set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=linux_fread" CACHE STRING INTERNAL FORCE)
# set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=linux_fcntl" CACHE STRING INTERNAL FORCE)
endif()
#
add_subdirectory(external)

View File

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

View File

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

View File

@ -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<SetLedState> mDeviceStatus;
};
#endif

View File

@ -30,6 +30,7 @@ void MissionState::GoInState()
void MissionState::GoOutState()
{
LogInfo(" ========== MissionState::GoOutState.\n");
LedsHandle::DeleteAllLeds();
}
bool MissionState::ExecuteStateMsg(VStateMachineData *msg)
{

View File

@ -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<MissionState>
#include "LedsHandle.h"
class MissionState : public State,
public DataProcessing,
public LedsHandle,
public std::enable_shared_from_this<MissionState>
{
public:
MissionState(const std::string &name);

View File

@ -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> SetLedState::ControlLed(const std::string &ledName, const LedState &state,
const long int &keepAliveTime, const unsigned int &blinkPeriod)
{
std::shared_ptr<SetLedState> ledState = std::make_shared<SetLedState>(state, keepAliveTime, blinkPeriod);
std::shared_ptr<LedControlContext> ledState2 = ledState;
IDeviceManager::GetInstance()->ControlLed(ledName, ledState2);
return ledState;
}

View File

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

View File

@ -34,6 +34,7 @@ void TestMissionState::GoInState()
std::shared_ptr<VAppMonitor> monitor =
std::dynamic_pointer_cast<TestMissionState>(MissionState::shared_from_this());
IAppManager::GetInstance()->SetAppMonitor(monitor);
ControlDeviceStatusLed(DeviceStatus::NORMAL);
}
void TestMissionState::GoOutState()
{

3
external/.gitignore vendored
View File

@ -1,2 +1,3 @@
goahead-5.2.0/GoAhead
goahead-5.2.0/GoAhead
ffmpeg/ffmpeg-6.1.1

View File

@ -19,4 +19,5 @@ add_subdirectory(httpserver.h-master/src)
# ================= httpserver end ================= #
add_subdirectory(cJSON-1.7.17)
add_subdirectory(libhv/libhv-1.3.2)
add_subdirectory(libhv/libhv-1.3.2)
add_subdirectory(ffmpeg)

40
external/ffmpeg/CMakeLists.txt vendored Normal file
View File

@ -0,0 +1,40 @@
add_custom_target(
ffmpeg
# DEPENDS ${EXTERNAL_LIBS_OUTPUT_PATH}/libgo.a
# COMMAND mkdir ${GOAHEAD_UPLOAD_TMP_PATH}
# COMMAND cp ${EXTERNAL_SOURCE_PATH}/goahead-5.2.0/modify/http.c ${EXTERNAL_SOURCE_PATH}/goahead-5.2.0/GoAhead/src
# COMMAND touch ${EXTERNAL_SOURCE_PATH}/goahead-5.2.0/GoAhead/src/http.c
COMMAND test -f ${EXTERNAL_SOURCE_PATH}/ffmpeg/Makefile || tar -xf ffmpeg_6.1.1.orig.tar.xz
COMMAND cd ffmpeg-6.1.1 && ./configure --enable-cross-compile --target-os=linux --arch=arm64
--cc=${CMAKE_C_COMPILER}
--cxx=${CMAKE_CXX_COMPILER}
--prefix=${EXTERNAL_LIBS_OUTPUT_PATH}/ffmpeg
--disable-asm --enable-parsers --disable-decoders --enable-decoder=h264
--disable-debug --enable-ffmpeg --enable-shared --enable-static --disable-stripping --disable-doc
--enable-gpl --enable-nonfree --enable-version3 --enable-small
--disable-mipsdsp --disable-mipsdspr2
--disable-encoders
--disable-muxers --enable-muxer=mov --enable-muxer=mp4
--disable-decoders --enable-decoder=aac
--disable-filters
--disable-demuxers --enable-demuxer=mov
--disable-parsers
--disable-protocols --enable-protocol=file
--disable-bsfs --enable-bsf=aac_adtstoasc --enable-bsf=h264_mp4toannexb --enable-bsf=hevc_mp4toannexb
--disable-indevs
--disable-outdevs --disable-ffprobe --disable-ffmpeg --disable-ffplay --disable-debug
COMMAND cd ffmpeg-6.1.1 && make
COMMAND cd ffmpeg-6.1.1 && make install
WORKING_DIRECTORY ${EXTERNAL_SOURCE_PATH}/ffmpeg/
)
add_custom_target(
remove_ffmpeg_source_files
COMMAND rm -rf ffmpeg-6.1.1
WORKING_DIRECTORY ${EXTERNAL_SOURCE_PATH}/ffmpeg/
)
# cleanclean_script
add_dependencies(sdk_clean remove_ffmpeg_source_files)

10
external/ffmpeg/build_ffmpeg.sh vendored Executable file
View File

@ -0,0 +1,10 @@
#!/bin/bash
# 编译ffmpeg
echo "Compile ffmpeg."
cd ffmpeg-6.1.1
./configure --enable-cross-compile --target-os=linux --arch=arm64 \
--cc=gcc \
--cxx=g++ \
--prefix=/home/xiaojiazhu/project/tmp \
--disable-asm --enable-parsers --disable-decoders --enable-decoder=h264 --enable-decoder=aac \
--disable-debug --enable-ffmpeg --enable-shared --enable-static --disable-stripping --disable-doc

BIN
external/ffmpeg/ffmpeg_6.1.1.orig.tar.xz vendored Executable file

Binary file not shown.

View File

@ -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<IDeviceManager> &GetInstance(std::shared_ptr<IDeviceManager> *impl = nullptr);
virtual const StatusCode Init(void);
virtual const StatusCode UnInit(void);
virtual const StatusCode ControlLed(const std::string &ledName, std::shared_ptr<VirtualLedControl> &control);
virtual const StatusCode ControlLed(const std::string &ledName, std::shared_ptr<LedControlContext> &control);
virtual const StatusCode SetAllKeysMonitor(std::shared_ptr<VKeyMonitor> &monitor);
};
#endif

View File

@ -36,7 +36,7 @@ const StatusCode DeviceManager::SetAllKeysMonitor(std::shared_ptr<VKeyMonitor> &
LogInfo("DeviceManager::SetAllKeysMonitor\n");
return KeyManager::GetInstance()->SetKeyMonitor(monitor);
}
const StatusCode DeviceManager::ControlLed(const std::string &ledName, std::shared_ptr<VirtualLedControl> &control)
const StatusCode DeviceManager::ControlLed(const std::string &ledName, std::shared_ptr<LedControlContext> &control)
{
LedManager::GetInstance()->ControlLed(ledName, control);
return CreateStatusCode(STATUS_CODE_OK);

View File

@ -26,7 +26,7 @@ public:
const StatusCode Init(void) override;
const StatusCode UnInit(void) override;
const StatusCode SetAllKeysMonitor(std::shared_ptr<VKeyMonitor> &monitor) override;
const StatusCode ControlLed(const std::string &ledName, std::shared_ptr<VirtualLedControl> &control) override;
const StatusCode ControlLed(const std::string &ledName, std::shared_ptr<LedControlContext> &control) override;
private:
// std::vector<std::shared_ptr<LedManager>> mLedManagers;

View File

@ -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<VirtualLedControl> &control)
const StatusCode IDeviceManager::ControlLed(const std::string &ledName, std::shared_ptr<LedControlContext> &control)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}

View File

@ -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<VirtualLedControl> &control)
void LedManager::ControlLed(const std::string &ledName, std::shared_ptr<LedControlContext> &control)
{
std::lock_guard<std::mutex> locker(mMutex);
std::shared_ptr<VSingleControl> singleControl = std::dynamic_pointer_cast<VSingleControl>(control);
@ -61,6 +66,11 @@ void LedManager::ControlLed(const std::string &ledName, std::shared_ptr<VirtualL
LogError("led can't be controled.\n");
return;
}
auto iter = mAllLedHal.find(ledName);
if (iter == mAllLedHal.end()) {
LogError("Can't find led [%s].\n", ledName.c_str());
return;
}
mAllLedHal[ledName]->AddLedState(singleControl);
}
void LedManager::StartTimer(void)

View File

@ -31,7 +31,7 @@ public:
void UnInit(void);
void StartTimer(void);
void StopTimer(void);
void ControlLed(const std::string &ledName, std::shared_ptr<VirtualLedControl> &control);
void ControlLed(const std::string &ledName, std::shared_ptr<LedControlContext> &control);
private:
void Timer(void);

View File

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

View File

@ -18,6 +18,7 @@
#include "MainThread.h"
#include <thread>
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<VKeyHal> key = HalTestTool::MakeKeyHalTest(KEY_RESET);
mAllKeysMock[KEY_RESET] = key;
// std::shared_ptr<VLedHal> 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<VLedHal> led = HalTestTool::MakeLedHalTest(LED_DEVICE_STATUS);
mAllLedsMock[LED_DEVICE_STATUS] = led;
}
void HuntingCameraTest::DestroyAllLedsMock(void)
{
mAllLedsMock.clear();
}

View File

@ -52,11 +52,14 @@ private:
void DestroyAllCamerasMock(void);
void CreateAllKeysMcok(void);
void DestroyAllKeysMock(void);
void CreateAllLedsMcok(void);
void DestroyAllLedsMock(void);
protected:
std::shared_ptr<LinuxTest> mLinuxTest;
std::map<CameraType, std::shared_ptr<VCameraHal>> mAllCamerasMock;
std::map<std::string, std::shared_ptr<VKeyHal>> mAllKeysMock;
std::map<std::string, std::shared_ptr<VLedHal>> mAllLedsMock;
};
#endif

View File

@ -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<VKeyMonitor> monitor = std::make_shared<KeyMonitorMock>();
IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor);
std::shared_ptr<LedControlContext> 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

View File

@ -54,8 +54,9 @@ public:
void UnInit(void);
public:
std::shared_ptr<VirtualLedControl> ControlLed(const std::string &ledName, const LedState &state,
std::shared_ptr<LedControlContext> ControlLed(const std::string &ledName, const LedState &state,
const unsigned int &aliveTimeMs, const unsigned int &blinkTimeMs);
void DestoryLedControl(std::shared_ptr<LedControlContext> &ledControl);
protected:
void DeviceManagerNotice(const std::string &keyName, const unsigned int &pressingTimeMs) override;

View File

@ -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<VSingleControl> &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<VSingleControl> &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<VSingleControl> &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<VSingleControl> &state);

View File

@ -14,9 +14,45 @@
*/
#include "LedControl.h"
#include "ILog.h"
#include <algorithm>
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<VSingleControl> &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<VSingleControl> &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<VSingleControl> &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<VSingleControl> &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");