From 2fe56a23ae2fba8f49344b2dbd9a212e8b86c67f Mon Sep 17 00:00:00 2001 From: Fancy code <258828110.@qq.com> Date: Fri, 26 Apr 2024 16:45:47 +0800 Subject: [PATCH] Backup:MissionManger module. --- .../MissionManager/src/DataProcessing.cpp | 8 +++- .../MissionManager/src/DataProcessing.h | 18 +++++-- .../src/MissionManagerMakePtr.cpp | 12 +++++ .../src/MissionManagerMakePtr.h | 2 + .../MissionManager/src/MissionState.cpp | 10 +++- application/MissionManager/src/MissionState.h | 3 ++ .../src/MissionStateMachine.cpp | 28 ++++++++--- .../MissionManager/src/MissionStateMachine.h | 13 +++-- .../MissionManager/src/SdCardHandleState.cpp | 47 +++++++++++++++++++ .../MissionManager/src/SdCardHandleState.h | 32 +++++++++++++ .../MissionManager/src/StorageHandleState.cpp | 34 ++++++++++++++ .../MissionManager/src/StorageHandleState.h | 29 ++++++++++++ .../MissionManager/src/TestMissionState.cpp | 8 ++-- .../MissionManager/src/TestMissionState.h | 2 +- application/MissionManager/src/TopState.cpp | 6 ++- doc/design.md | 28 ++++++++++- 16 files changed, 257 insertions(+), 23 deletions(-) create mode 100644 application/MissionManager/src/SdCardHandleState.cpp create mode 100644 application/MissionManager/src/SdCardHandleState.h create mode 100644 application/MissionManager/src/StorageHandleState.cpp create mode 100644 application/MissionManager/src/StorageHandleState.h diff --git a/application/MissionManager/src/DataProcessing.cpp b/application/MissionManager/src/DataProcessing.cpp index 099988d..7c8cd7c 100644 --- a/application/MissionManager/src/DataProcessing.cpp +++ b/application/MissionManager/src/DataProcessing.cpp @@ -16,6 +16,12 @@ #include "ILog.h" const bool NOT_EXECUTED = false; const bool EXECUTED = true; +MissionData::MissionData(const std::shared_ptr &data) : mMissionData(data) +{ +} +MissionMessage::MissionMessage(const std::shared_ptr &message) : mMissionData(message) +{ +} bool DataProcessing::EventHandle(VStateMachineData *msg) { if (nullptr == msg) { @@ -23,7 +29,7 @@ bool DataProcessing::EventHandle(VStateMachineData *msg) return NOT_EXECUTED; } std::map::iterator iter; - std::shared_ptr message = std::dynamic_pointer_cast(msg->GetMessageObj()); + std::shared_ptr message = std::dynamic_pointer_cast(msg->GetMessageObj()); InternalStateEvent event = static_cast(message->mMissionData->mEvent); iter = mEventHandle.find(event); if (iter != mEventHandle.end()) { diff --git a/application/MissionManager/src/DataProcessing.h b/application/MissionManager/src/DataProcessing.h index 5127af3..95610b9 100644 --- a/application/MissionManager/src/DataProcessing.h +++ b/application/MissionManager/src/DataProcessing.h @@ -18,21 +18,33 @@ #include "IStateMachine.h" #include #include +using std::placeholders::_1; using DataProcessingFunc = std::function; enum class InternalStateEvent { TEST = static_cast(MissionEvent::END), + MEDIA_REPORT_EVENT, END }; class MissionData : public VStateMachineData { public: - MissionData(const std::shared_ptr &data) : mMissionData(data) - { - } + MissionData(const std::shared_ptr &data); virtual ~MissionData() = default; const std::shared_ptr mMissionData; }; +/** + * @brief + * MissionMessage abstracts user data into state machine data, enabling the transfer of arbitrary data within the state + * machine. + */ +class MissionMessage : public VStateMessage +{ +public: + MissionMessage(const std::shared_ptr &message); + virtual ~MissionMessage() = default; + const std::shared_ptr mMissionData; // The message from users of state manager module. +}; class DataProcessing { public: diff --git a/application/MissionManager/src/MissionManagerMakePtr.cpp b/application/MissionManager/src/MissionManagerMakePtr.cpp index 157e15c..9b3bfc0 100644 --- a/application/MissionManager/src/MissionManagerMakePtr.cpp +++ b/application/MissionManager/src/MissionManagerMakePtr.cpp @@ -15,6 +15,8 @@ #include "MissionManagerMakePtr.h" #include "ILog.h" #include "MissionManager.h" +#include "SdCardHandleState.h" +#include "StorageHandleState.h" #include "TestMissionState.h" #include "TopState.h" bool CreateMissionManagerModule(void) @@ -62,4 +64,14 @@ std::shared_ptr MissionManagerMakePtr::CreateMissionState(const IpcMissio { std::shared_ptr state = std::make_shared(); return state; +} +std::shared_ptr MissionManagerMakePtr::CreateStorageHandleState(void) +{ + std::shared_ptr state = std::make_shared(); + return state; +} +std::shared_ptr MissionManagerMakePtr::CreateSdCardHandleState(void) +{ + std::shared_ptr state = std::make_shared(); + return state; } \ No newline at end of file diff --git a/application/MissionManager/src/MissionManagerMakePtr.h b/application/MissionManager/src/MissionManagerMakePtr.h index aac79f2..ff4f855 100644 --- a/application/MissionManager/src/MissionManagerMakePtr.h +++ b/application/MissionManager/src/MissionManagerMakePtr.h @@ -28,5 +28,7 @@ public: virtual const StatusCode CreateMissionManagerInstance(std::shared_ptr &instance); virtual std::shared_ptr CreateTopState(void); virtual std::shared_ptr CreateMissionState(const IpcMission &mission); + virtual std::shared_ptr CreateStorageHandleState(void); + virtual std::shared_ptr CreateSdCardHandleState(void); }; #endif \ No newline at end of file diff --git a/application/MissionManager/src/MissionState.cpp b/application/MissionManager/src/MissionState.cpp index e210d09..554a102 100644 --- a/application/MissionManager/src/MissionState.cpp +++ b/application/MissionManager/src/MissionState.cpp @@ -15,20 +15,26 @@ #include "MissionState.h" #include "IAppManager.h" #include "ILog.h" +#include "MissionStateMachine.h" MissionState::MissionState(const std::string &name) : State(name) { + mEventHandle[InternalStateEvent::MEDIA_REPORT_EVENT] = std::bind(&MissionState::MediaReportHandle, this, _1); } void MissionState::GoInState() { - // LogInfo(" ========== MissionState::GoInState.\n"); } void MissionState::GoOutState() { - // LogInfo(" ========== MissionState::GoOutState.\n"); } bool MissionState::ExecuteStateMsg(VStateMachineData *msg) { return DataProcessing::EventHandle(msg); +} +bool MissionState::MediaReportHandle(VStateMachineData *msg) +{ + MissionStateMachine::GetInstance()->DelayMessage(msg); + MissionStateMachine::GetInstance()->SwitchState(SystemState::STORAGE_HANDLE_STATE); + return EXECUTED; } \ No newline at end of file diff --git a/application/MissionManager/src/MissionState.h b/application/MissionManager/src/MissionState.h index 883a512..880df8d 100644 --- a/application/MissionManager/src/MissionState.h +++ b/application/MissionManager/src/MissionState.h @@ -24,5 +24,8 @@ public: void GoInState() override; void GoOutState() override; bool ExecuteStateMsg(VStateMachineData *msg) override; + +private: + bool MediaReportHandle(VStateMachineData *msg); }; #endif \ No newline at end of file diff --git a/application/MissionManager/src/MissionStateMachine.cpp b/application/MissionManager/src/MissionStateMachine.cpp index c1dd0dd..36b6636 100644 --- a/application/MissionManager/src/MissionStateMachine.cpp +++ b/application/MissionManager/src/MissionStateMachine.cpp @@ -54,7 +54,17 @@ void MissionStateMachine::UnInit(void) mStateMachine->StopHandlerThread(); mStateTree.clear(); } -void MissionStateMachine::SwitchState(const MissionState &state) +StatusCode MissionStateMachine::SendStateMessage(const std::shared_ptr &message) +{ + std::shared_ptr msg = std::make_shared(message); + mStateMachine->SendMessage(static_cast(message->mEvent), msg); + return CreateStatusCode(STATUS_CODE_OK); +} +void MissionStateMachine::DelayMessage(VStateMachineData *msg) +{ + mStateMachine->DelayMessage(msg); +} +void MissionStateMachine::SwitchState(const SystemState &state) { mStateMachine->SwitchState(mStateTree[state].get()); } @@ -76,10 +86,16 @@ IpcMission MissionStateMachine::GetStartMission(void) void MissionStateMachine::RunStateMachine(const IpcMission &mission) { LogInfo("Make all states and start the state machine.\n"); - mStateTree[MissionState::TOP_STATE] = MissionManagerMakePtr::GetInstance()->CreateTopState(); - mStateTree[MissionState::MISSION_STATE] = MissionManagerMakePtr::GetInstance()->CreateMissionState(mission); - mStateMachine->StatePlus(mStateTree[MissionState::TOP_STATE].get(), nullptr); - mStateMachine->StatePlus(mStateTree[MissionState::MISSION_STATE].get(), mStateTree[MissionState::TOP_STATE].get()); - mStateMachine->SetCurrentState(mStateTree[MissionState::TOP_STATE].get()); + mStateTree[SystemState::TOP_STATE] = MissionManagerMakePtr::GetInstance()->CreateTopState(); + mStateTree[SystemState::MISSION_STATE] = MissionManagerMakePtr::GetInstance()->CreateMissionState(mission); + mStateTree[SystemState::STORAGE_HANDLE_STATE] = MissionManagerMakePtr::GetInstance()->CreateStorageHandleState(); + mStateTree[SystemState::SD_CARD_HANDLE_STATE] = MissionManagerMakePtr::GetInstance()->CreateSdCardHandleState(); + mStateMachine->StatePlus(mStateTree[SystemState::TOP_STATE].get(), nullptr); + mStateMachine->StatePlus(mStateTree[SystemState::MISSION_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(), + mStateTree[SystemState::STORAGE_HANDLE_STATE].get()); + mStateMachine->SetCurrentState(mStateTree[SystemState::TOP_STATE].get()); mStateMachine->StartStateMachine(); } diff --git a/application/MissionManager/src/MissionStateMachine.h b/application/MissionManager/src/MissionStateMachine.h index c7273b3..995133d 100644 --- a/application/MissionManager/src/MissionStateMachine.h +++ b/application/MissionManager/src/MissionStateMachine.h @@ -18,11 +18,16 @@ #include "IMcuManager.h" #include "IMissionManager.h" #include "IStateMachine.h" +#include "StatusCode.h" #include -enum class MissionState +const bool NOT_EXECUTED = false; +const bool EXECUTED = true; +enum class SystemState { TOP_STATE = 0, MISSION_STATE, + STORAGE_HANDLE_STATE, + SD_CARD_HANDLE_STATE, END }; class MissionStateMachine @@ -33,7 +38,9 @@ public: static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); void Init(void); void UnInit(void); - void SwitchState(const MissionState &state); + StatusCode SendStateMessage(const std::shared_ptr &message); + void DelayMessage(VStateMachineData *msg); + void SwitchState(const SystemState &state); private: IpcMission GetStartMission(void); @@ -41,7 +48,7 @@ private: private: std::shared_ptr mStateMachine; - std::map> mStateTree; + std::map> mStateTree; IpcMission mStartMission; }; #endif \ No newline at end of file diff --git a/application/MissionManager/src/SdCardHandleState.cpp b/application/MissionManager/src/SdCardHandleState.cpp new file mode 100644 index 0000000..00f7a9a --- /dev/null +++ b/application/MissionManager/src/SdCardHandleState.cpp @@ -0,0 +1,47 @@ +/* + * 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 "SdCardHandleState.h" +#include "ILog.h" +#include "IMediaManager.h" +#include "MissionStateMachine.h" +SdCardHandleState::SdCardHandleState() : State("SdCardHandleState") +{ + mEventHandle[InternalStateEvent::MEDIA_REPORT_EVENT] = std::bind(&SdCardHandleState::MediaReportHandle, this, _1); +} +void SdCardHandleState::GoInState() +{ + LogInfo(" ========== opState::GoInState.\n"); +} +void SdCardHandleState::GoOutState() +{ + LogInfo(" ========== opState::GoOutState.\n"); +} +bool SdCardHandleState::ExecuteStateMsg(VStateMachineData *msg) +{ + return DataProcessing::EventHandle(msg); +} +bool SdCardHandleState::MediaReportHandle(VStateMachineData *msg) +{ + LogInfo(" MediaReportHandle.\n"); + 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; + } + LogInfo(" MediaReportHandle, file = %s, path = %s.\n", data->mData.mFileName.c_str(), data->mData.mFilePath.c_str()); + return EXECUTED; +} \ No newline at end of file diff --git a/application/MissionManager/src/SdCardHandleState.h b/application/MissionManager/src/SdCardHandleState.h new file mode 100644 index 0000000..b0798b3 --- /dev/null +++ b/application/MissionManager/src/SdCardHandleState.h @@ -0,0 +1,32 @@ +/* + * 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 SD_CARD_HANDLE_STATE_H +#define SD_CARD_HANDLE_STATE_H +#include "DataProcessing.h" +#include "IMediaManager.h" +#include "IStateMachine.h" +class SdCardHandleState : public State, public DataProcessing, public std::enable_shared_from_this +{ +public: + SdCardHandleState(); + virtual ~SdCardHandleState() = default; + void GoInState() override; + void GoOutState() override; + bool ExecuteStateMsg(VStateMachineData *msg) override; + +private: + bool MediaReportHandle(VStateMachineData *msg); +}; +#endif \ No newline at end of file diff --git a/application/MissionManager/src/StorageHandleState.cpp b/application/MissionManager/src/StorageHandleState.cpp new file mode 100644 index 0000000..6e243fd --- /dev/null +++ b/application/MissionManager/src/StorageHandleState.cpp @@ -0,0 +1,34 @@ +/* + * 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 "StorageHandleState.h" +#include "ILog.h" +#include "IMediaManager.h" +#include "MissionStateMachine.h" +StorageHandleState::StorageHandleState() : State("StorageHandleState") +{ +} +void StorageHandleState::GoInState() +{ + LogInfo(" ========== opState::GoInState.\n"); + MissionStateMachine::GetInstance()->SwitchState(SystemState::SD_CARD_HANDLE_STATE); +} +void StorageHandleState::GoOutState() +{ + LogInfo(" ========== opState::GoOutState.\n"); +} +bool StorageHandleState::ExecuteStateMsg(VStateMachineData *msg) +{ + return DataProcessing::EventHandle(msg); +} \ No newline at end of file diff --git a/application/MissionManager/src/StorageHandleState.h b/application/MissionManager/src/StorageHandleState.h new file mode 100644 index 0000000..9eb509e --- /dev/null +++ b/application/MissionManager/src/StorageHandleState.h @@ -0,0 +1,29 @@ +/* + * 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 STORAGE_HANDLE_STATE_H +#define STORAGE_HANDLE_STATE_H +#include "DataProcessing.h" +#include "IMediaManager.h" +#include "IStateMachine.h" +class StorageHandleState : public State, public DataProcessing, public std::enable_shared_from_this +{ +public: + StorageHandleState(); + virtual ~StorageHandleState() = default; + void GoInState() override; + void GoOutState() override; + bool ExecuteStateMsg(VStateMachineData *msg) override; +}; +#endif \ No newline at end of file diff --git a/application/MissionManager/src/TestMissionState.cpp b/application/MissionManager/src/TestMissionState.cpp index cfb1f80..9b6beff 100644 --- a/application/MissionManager/src/TestMissionState.cpp +++ b/application/MissionManager/src/TestMissionState.cpp @@ -33,7 +33,7 @@ void TestMissionState::GoOutState() MissionState::GoOutState(); LogInfo(" ========== TestMissionState::GoOutState.\n"); } -bool TestMissionState::ExecuteStateMsg(VStateMachineData *msg) -{ - return MissionState::EventHandle(msg); -} \ No newline at end of file +// bool TestMissionState::ExecuteStateMsg(VStateMachineData *msg) +// { +// return MissionState::EventHandle(msg); +// } \ No newline at end of file diff --git a/application/MissionManager/src/TestMissionState.h b/application/MissionManager/src/TestMissionState.h index 56b96f0..0d266e6 100644 --- a/application/MissionManager/src/TestMissionState.h +++ b/application/MissionManager/src/TestMissionState.h @@ -24,6 +24,6 @@ public: virtual ~TestMissionState() = default; void GoInState() override; void GoOutState() override; - bool ExecuteStateMsg(VStateMachineData *msg) override; + // bool ExecuteStateMsg(VStateMachineData *msg) override; }; #endif \ No newline at end of file diff --git a/application/MissionManager/src/TopState.cpp b/application/MissionManager/src/TopState.cpp index 41b49df..8c2848b 100644 --- a/application/MissionManager/src/TopState.cpp +++ b/application/MissionManager/src/TopState.cpp @@ -24,7 +24,7 @@ void TopState::GoInState() LogInfo(" ========== opState::GoInState.\n"); std::shared_ptr monitor = std::dynamic_pointer_cast(shared_from_this()); IMediaManager::GetInstance()->SetMediaMonitor(monitor); - MissionStateMachine::GetInstance()->SwitchState(MissionState::MISSION_STATE); + MissionStateMachine::GetInstance()->SwitchState(SystemState::MISSION_STATE); } void TopState::GoOutState() { @@ -36,6 +36,8 @@ bool TopState::ExecuteStateMsg(VStateMachineData *msg) } StatusCode TopState::ReportEvent(const MediaReportEvent &event) { - LogInfo("sssssssssssssssssssssssssssssssssssssssssssssssssssssssss\n"); + std::shared_ptr message = std::make_shared>( + static_cast(InternalStateEvent::MEDIA_REPORT_EVENT), event); + MissionStateMachine::GetInstance()->SendStateMessage(message); return CreateStatusCode(STATUS_CODE_OK); } \ No newline at end of file diff --git a/doc/design.md b/doc/design.md index 679b8b7..b9e60e3 100644 --- a/doc/design.md +++ b/doc/design.md @@ -484,7 +484,33 @@ deactivate 大核 2. 定时启动拍照 / 录像; 3. 测试启动; -###### 1.4.3.1.3.2. 任务状态获取启动 +###### 1.4.3.1.3.2. 状态机设计 + +```mermaid +stateDiagram-v2 +[*] --> TopState +TopState --> PowerOff +TopState --> MSDCState +TopState --> DeviceAbnormal +TopState --> MissionState +MissionState --> 存储管理 +存储管理 --> EMMC +存储管理 --> SD卡 +SD卡 --> 插卡 +SD卡 --> 拔卡 +SD卡 --> 卡异常 +MissionState --> 网络管理 +网络管理 --> 联网 +联网 --> 上传文件 +网络管理 --> 未联网 +MissionState --> 直播 +MissionState --> 4G管理 +4G管理 --> Sim卡初始化 +4G管理 --> 注网状态 +MissionState --> Upgrade +``` + +###### 1.4.3.1.3.3. 任务状态获取启动   应用程序运行后,首先需要知道主控是由于何种任务被唤醒,然后根据任务来执行相应的功能代码;