Add:FormattingState.

This commit is contained in:
Fancy code 2024-07-16 16:25:31 +08:00
parent 0b49e1a2ec
commit dd0f1b0895
13 changed files with 161 additions and 12 deletions

View File

@ -21,8 +21,9 @@ stateDiagram-v2
[*] --> TopState
TopState --> PowerOff
TopState --> MSDCState
TopState --> DeviceAbnormal
TopState --> MissionState
TopState --> DeviceAbnormal
TopState --> FormattingSDCard
MissionState --> 空闲
MissionState --> 存储管理
存储管理 --> EMMC

View File

@ -0,0 +1,70 @@
/*
* 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 "FormattingState.h"
#include "DataProcessing.h"
#include "IFilesManager.h"
#include "ILog.h"
#include "IStateMachine.h"
#include "IStorageManager.h"
#include "MissionStateMachine.h"
#include <functional>
#include <memory>
using std::placeholders::_1;
FormattingState::FormattingState() : State("FormattingState")
{
mEventHandle[InternalStateEvent::FORMAT_KEY_FORMAT_SD_CARD] =
std::bind(&FormattingState::FormatKeyFormattingSDCardHandle, this, _1);
}
void FormattingState::GoInState()
{
LogInfo(" ========== FormattingState::GoInState.\n");
}
void FormattingState::GoOutState()
{
LogInfo(" ========== FormattingState::GoOutState.\n");
if (mFormattingThread.joinable()) {
mFormattingThread.join();
}
}
bool FormattingState::ExecuteStateMsg(VStateMachineData *msg)
{
return DataProcessing::EventHandle(msg);
}
void FormattingState::StateInit(void)
{
}
void FormattingState::StateUnInit(void)
{
if (mFormattingThread.joinable()) {
mFormattingThread.join();
}
}
bool FormattingState::FormatKeyFormattingSDCardHandle(VStateMachineData *msg)
{
LogInfo("Now formatting SD card.\n");
auto formatting = [](std::shared_ptr<FormattingState> impl) {
impl->FormattingThread();
};
mFormattingThread = std::thread(formatting, shared_from_this());
return EXECUTED;
}
void FormattingState::FormattingThread(void)
{
IFilesManager::GetInstance()->UnInit();
IStorageManager::GetInstance()->FormatSDCardNow();
// IFilesManager::GetInstance()->Init();
MissionStateMachine::GetInstance()->SwitchState(SystemState::STORAGE_HANDLE_STATE);
LogInfo("Formatting SD card done.\n");
}

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 FORMATTING_STATE_H
#define FORMATTING_STATE_H
#include "DataProcessing.h"
#include "IFilesManager.h"
#include "IMediaManager.h"
#include "IStateMachine.h"
#include "IStorageManager.h"
#include "VStateBase.h"
#include <thread>
class FormattingState : public State,
public DataProcessing,
public VStateBase,
public std::enable_shared_from_this<FormattingState>
{
public:
FormattingState();
virtual ~FormattingState() = default;
void GoInState() override;
void GoOutState() override;
bool ExecuteStateMsg(VStateMachineData *msg) override;
protected:
void StateInit(void) override;
void StateUnInit(void) override;
bool FormatKeyFormattingSDCardHandle(VStateMachineData *msg);
void FormattingThread(void);
private:
std::thread mFormattingThread;
};
#endif

View File

@ -13,6 +13,7 @@
* limitations under the License.
*/
#include "MissionManagerMakePtr.h"
#include "FormattingState.h"
#include "ILog.h"
#include "IMcuManager.h"
#include "IMissionManager.h"
@ -120,4 +121,9 @@ std::shared_ptr<State> MissionManagerMakePtr::CreateIdleState(void)
{
std::shared_ptr<State> state = std::make_shared<IdleState>();
return state;
}
std::shared_ptr<State> MissionManagerMakePtr::CreateFormattingState(void)
{
std::shared_ptr<State> state = std::make_shared<FormattingState>();
return state;
}

View File

@ -33,5 +33,6 @@ public:
virtual std::shared_ptr<State> CreateUpgradeState(void);
virtual std::shared_ptr<State> CreateMediaHandleState(void);
virtual std::shared_ptr<State> CreateIdleState(void);
virtual std::shared_ptr<State> CreateFormattingState(void);
};
#endif

View File

@ -108,8 +108,10 @@ void MissionStateMachine::RunStateMachine(const IpcMission &mission)
mStateTree[SystemState::UPGRADE_STATE] = MissionManagerMakePtr::GetInstance()->CreateUpgradeState();
mStateTree[SystemState::MEDIA_HANDLE_STATE] = MissionManagerMakePtr::GetInstance()->CreateMediaHandleState();
mStateTree[SystemState::IDLE_STATE] = MissionManagerMakePtr::GetInstance()->CreateIdleState();
mStateTree[SystemState::FORMATTING_STATE] = MissionManagerMakePtr::GetInstance()->CreateFormattingState();
mStateMachine->StatePlus(mStateTree[SystemState::TOP_STATE].get(), nullptr);
mStateMachine->StatePlus(mStateTree[SystemState::MISSION_STATE].get(), mStateTree[SystemState::TOP_STATE].get());
mStateMachine->StatePlus(mStateTree[SystemState::FORMATTING_STATE].get(), mStateTree[SystemState::TOP_STATE].get());
mStateMachine->StatePlus(mStateTree[SystemState::STORAGE_HANDLE_STATE].get(),
mStateTree[SystemState::MISSION_STATE].get());
mStateMachine->StatePlus(mStateTree[SystemState::SD_CARD_HANDLE_STATE].get(),
@ -121,13 +123,6 @@ void MissionStateMachine::RunStateMachine(const IpcMission &mission)
mStateMachine->StatePlus(mStateTree[SystemState::IDLE_STATE].get(), mStateTree[SystemState::MISSION_STATE].get());
mStateMachine->SetCurrentState(mStateTree[SystemState::TOP_STATE].get());
mStateMachine->StartStateMachine();
// /**
// * @brief The business can only be processed after the state machine is started.
// *
// */
// std::shared_ptr<VMissionData> message =
// std::make_shared<VMissionData>(static_cast<MissionEvent>(InternalStateEvent::STORAGE_HANDLE_STATE_INIT));
// SendStateMessage(message);
InitAllState();
}
void MissionStateMachine::InitAllState(void)

View File

@ -33,6 +33,7 @@ enum class SystemState
UPGRADE_STATE,
MEDIA_HANDLE_STATE,
IDLE_STATE,
FORMATTING_STATE,
END
};
class MissionStateMachine

View File

@ -112,11 +112,20 @@ bool SdCardHandleState::ResetKeyMediaTaskHandle(VStateMachineData *msg)
LogWarning("Sd card is not inserted, ignore reset key media task.\n");
return HANDLE_HELPESS;
}
/**
* @brief Why do we need to switch status and go through the formatting process here?
* This is because the guarantee business requires that the SD card be formatted safely under the condition that the SD
* card is not occupied.
* @param msg
* @return true
* @return false
*/
bool SdCardHandleState::FormatKeyFormattingSDCardHandle(VStateMachineData *msg)
{
if (StorageEvent::SD_CARD_INSERT == mSdCardStatus) {
LogInfo("FormatKeyFormattingSDCardHandle.\n");
IStorageManager::GetInstance()->FormatSDCardNow();
MissionStateMachine::GetInstance()->DelayMessage(msg);
MissionStateMachine::GetInstance()->SwitchState(SystemState::FORMATTING_STATE);
return EXECUTED;
}
LogWarning("Sd card is not inserted, ignore format key.\n");

View File

@ -47,6 +47,7 @@ void TestMissionState::GoInState()
}
void TestMissionState::GoOutState()
{
IAppManager::GetInstance()->UnInit();
MissionState::GoOutState();
LogInfo(" ========== TestMissionState::GoOutState.\n");
}

View File

@ -60,6 +60,7 @@ GOAHEAD:
std::shared_ptr<IAppProtocolHandle> protocolHandle = std::make_shared<IAppProtocolHandle>();
IAppProtocolHandle::GetInstance(&protocolHandle);
mAppMonitor.reset();
LogInfo("AppManager uninit success.\n");
return CreateStatusCode(STATUS_CODE_OK);
}
const StatusCode AppManager::SetAppMonitor(std::shared_ptr<VAppMonitor> &monitor)
@ -198,6 +199,7 @@ void AppManager::TcpServerStop(void)
{
if (nullptr != mTcpServer) {
FreeTcpServer(mTcpServer);
mTcpServer = nullptr;
}
}
void AppManager::WifiApModeInit(const AppParam &param)

View File

@ -249,4 +249,20 @@ TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_AppPlayback)
MockAppPlayback();
MainThread::GetInstance()->Runing();
}
/**
* @brief Construct a new test f object
* ../output_files/test/bin/HuntingCameraTest
* --gtest_filter=HuntingCameraTest.HS_INTEGRATION_HunttingCamera_EXAMPLE_ReInitAppManager
*/
TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_ReInitAppManager)
{
SetAllCamerasResult(mAllCamerasMock);
MockOtherSideIpcMissionReply(IpcMission::TEST);
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 20);
HalTestTool::MockKeyClick("format", 1000 * 18); // Simulate pressing a button.
std::this_thread::sleep_for(std::chrono::milliseconds(1000 * 17));
MockGetSdCardInfo();
MainThread::GetInstance()->Runing();
}
} // namespace AppMonitor_Mock_Test

View File

@ -76,7 +76,7 @@ TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_FormatSDCard)
MockOtherSideIpcMissionReply(IpcMission::TEST);
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 17);
HalTestTool::MockKeyClick("format", 1000 * 16); // Simulate pressing a button.
HalTestTool::MockKeyClick("format", 1000 * 18); // Simulate pressing a button.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MainThread::GetInstance()->Runing();
}

View File

@ -322,8 +322,9 @@ void AppManagerTestTool::AppManagerMockInit(std::shared_ptr<IAppManager> &vMock)
AppManagerTestTool::AppMonitorInit(mAppMonitorMock);
};
EXPECT_CALL(*mock.get(), SetAppMonitorTrace(_))
.Times(Between(0, 1))
.WillOnce(DoAll(WithArgs<0>(Invoke(getAppMonitor)), Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
.Times(AtLeast(1))
.WillRepeatedly(
DoAll(WithArgs<0>(Invoke(getAppMonitor)), Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
}
std::shared_ptr<VAppMonitor> AppManagerTestTool::MakeMonitorMock(void)
{