/* * 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 #include 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 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; } 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(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 message = std::dynamic_pointer_cast(msg->GetMessageObj()); std::shared_ptr> data = std::dynamic_pointer_cast>(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 message = std::make_shared(static_cast(InternalStateEvent::CHECK_UPGRADE_FILE)); MissionStateMachine::GetInstance()->SendStateMessage(message); } return EXECUTED; }