mirror of
https://gitee.com/jiuyilian/embedded-framework.git
synced 2025-01-06 10:16:51 -05:00
94 lines
3.8 KiB
C++
94 lines
3.8 KiB
C++
/*
|
|
* 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 "DataProcessing.h"
|
|
#include "IFilesManager.h"
|
|
#include "ILog.h"
|
|
#include "IMediaManager.h"
|
|
#include "IMissionManager.h"
|
|
#include "IStateMachine.h"
|
|
#include "IStorageManager.h"
|
|
#include "MissionStateMachine.h"
|
|
#include "StatusCode.h"
|
|
#include <functional>
|
|
#include <memory>
|
|
using std::placeholders::_1;
|
|
SdCardHandleState::SdCardHandleState() : State("SdCardHandleState"), mSdCardStatus(StorageEvent::END)
|
|
{
|
|
mEventHandle[InternalStateEvent::MEDIA_REPORT_EVENT] = std::bind(&SdCardHandleState::MediaReportHandle, this, _1);
|
|
mEventHandle[InternalStateEvent::SD_CARD_HANDLE_STATE_SD_STATUS_REPORTED] =
|
|
std::bind(&SdCardHandleState::SdCardEventHandle, this, _1);
|
|
}
|
|
void SdCardHandleState::GoInState()
|
|
{
|
|
LogInfo(" ========== SdCardHandleState::GoInState.\n");
|
|
}
|
|
void SdCardHandleState::GoOutState()
|
|
{
|
|
LogInfo(" ========== SdCardHandleState::GoOutState.\n");
|
|
}
|
|
bool SdCardHandleState::ExecuteStateMsg(VStateMachineData *msg)
|
|
{
|
|
return DataProcessing::EventHandle(msg);
|
|
}
|
|
bool SdCardHandleState::MediaReportHandle(VStateMachineData *msg)
|
|
{
|
|
LogInfo(" MediaReportHandle.\n");
|
|
std::shared_ptr<MissionMessage> message = std::dynamic_pointer_cast<MissionMessage>(msg->GetMessageObj());
|
|
std::shared_ptr<VMissionDataV2<MediaReportEvent>> data =
|
|
std::dynamic_pointer_cast<VMissionDataV2<MediaReportEvent>>(message->mMissionData);
|
|
if (!data) {
|
|
LogError("nullptr pointer.\n");
|
|
return NOT_EXECUTED;
|
|
}
|
|
if (StorageEvent::SD_CARD_INSERT != mSdCardStatus) {
|
|
/**
|
|
* @brief The SD card has not been mounted yet, cache the data for the quick start first.
|
|
*
|
|
*/
|
|
LogWarning("Sd card is not inserted, cache data.\n");
|
|
mFileNeedToSave = std::make_shared<SaveFileInfo>(data->mData.mFileName);
|
|
return EXECUTED;
|
|
}
|
|
LogInfo("file = %s.\n", data->mData.mFileName.c_str());
|
|
SaveFileInfo saveFileInfo(data->mData.mFileName);
|
|
StatusCode code = IFilesManager::GetInstance()->SaveFile(saveFileInfo);
|
|
if (IsCodeOK(code) == false) {
|
|
LogError("SaveFile failed.\n");
|
|
}
|
|
return EXECUTED;
|
|
}
|
|
bool SdCardHandleState::SdCardEventHandle(VStateMachineData *msg)
|
|
{
|
|
std::shared_ptr<MissionMessage> message = std::dynamic_pointer_cast<MissionMessage>(msg->GetMessageObj());
|
|
std::shared_ptr<VMissionDataV2<StorageEvent>> data =
|
|
std::dynamic_pointer_cast<VMissionDataV2<StorageEvent>>(message->mMissionData);
|
|
LogInfo(" SdCardEventHandle event:%s.\n", IStorageManager::GetInstance()->PrintStringStorageEvent(data->mData));
|
|
if (mFileNeedToSave && StorageEvent::SD_CARD_INSERT == data->mData) {
|
|
LogInfo("Sd card is inserted for the first time.\n");
|
|
StatusCode code = IFilesManager::GetInstance()->SaveFile(*(mFileNeedToSave.get()));
|
|
if (IsCodeOK(code) == false) {
|
|
LogError("SaveFile failed.\n");
|
|
}
|
|
mFileNeedToSave.reset();
|
|
}
|
|
mSdCardStatus = data->mData;
|
|
if (StorageEvent::SD_CARD_INSERT == mSdCardStatus) {
|
|
std::shared_ptr<VMissionData> message =
|
|
std::make_shared<VMissionData>(static_cast<MissionEvent>(InternalStateEvent::CHECK_UPGRADE_FILE));
|
|
MissionStateMachine::GetInstance()->SendStateMessage(message);
|
|
}
|
|
return EXECUTED;
|
|
} |