Fixed:fast start media file save ok.
This commit is contained in:
parent
0c139866d2
commit
dae04243be
|
@ -51,21 +51,10 @@ std::string MediaTask::GetTargetNameForSaving(void)
|
|||
return mTargetName;
|
||||
}
|
||||
auto now = std::chrono::system_clock::now();
|
||||
time_t t_now = std::chrono::system_clock::to_time_t(now);
|
||||
struct tm tm_now = *std::localtime(&t_now);
|
||||
|
||||
mCreateTime_s = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count();
|
||||
int hour = tm_now.tm_hour;
|
||||
int minute = tm_now.tm_min;
|
||||
int second = tm_now.tm_sec;
|
||||
const std::string fileType = MediaTaskType::TAKE_VIDEO == mType ? ".mp4" : ".jpeg";
|
||||
std::ostringstream pathStream;
|
||||
pathStream << mSavePath << "xak47-" << mSerialNumber << "-" << std::setw(2) << std::setfill('0') << hour
|
||||
<< std::setw(2) << std::setfill('0') << minute << std::setw(2) << std::setfill('0') << second
|
||||
<< fileType;
|
||||
mTargetName = pathStream.str();
|
||||
LogInfo("GetTargetNameForSaving: %s\n", pathStream.str().c_str());
|
||||
return pathStream.str();
|
||||
return CreateFileName(mSerialNumber, mSavePath, fileType);
|
||||
}
|
||||
std::string MediaTask::GetThumbnailNameForSaving(const std::string &targetName)
|
||||
{
|
||||
|
@ -148,15 +137,24 @@ std::string MediaTask::GetFileNameBySeriaNumber(const unsigned long &serialNumbe
|
|||
std::string extension = sourceFile.substr(dot_pos);
|
||||
fileType = extension;
|
||||
}
|
||||
return CreateFileName(serialNumber, savePaht, fileType);
|
||||
}
|
||||
std::string MediaTask::CreateFileName(const unsigned long &serialNumber, const std::string &savePaht,
|
||||
const std::string &fileType)
|
||||
{
|
||||
auto now = std::chrono::system_clock::now();
|
||||
time_t t_now = std::chrono::system_clock::to_time_t(now);
|
||||
struct tm tm_now = *std::localtime(&t_now);
|
||||
|
||||
/**
|
||||
* @brief When the sequence number is 0, it indicates an invalid sequence number and is replaced by "???".
|
||||
*
|
||||
*/
|
||||
const std::string serialNumberString = 0 == serialNumber ? "???" : std::to_string(serialNumber);
|
||||
int hour = tm_now.tm_hour;
|
||||
int minute = tm_now.tm_min;
|
||||
int second = tm_now.tm_sec;
|
||||
std::ostringstream pathStream;
|
||||
pathStream << savePaht << "xak47-" << serialNumber << "-" << std::setw(2) << std::setfill('0') << hour
|
||||
pathStream << savePaht << "xak47-" << serialNumberString << "-" << std::setw(2) << std::setfill('0') << hour
|
||||
<< std::setw(2) << std::setfill('0') << minute << std::setw(2) << std::setfill('0') << second
|
||||
<< fileType;
|
||||
return pathStream.str();
|
||||
|
|
|
@ -56,6 +56,8 @@ public:
|
|||
static MediaTaskType WorkModeConvert(const WorkMode &mode);
|
||||
static std::string GetFileNameBySeriaNumber(const unsigned long &serialNumber, const std::string &sourceFile,
|
||||
const std::string &savePaht);
|
||||
static std::string CreateFileName(const unsigned long &serialNumber, const std::string &savePaht,
|
||||
const std::string &fileType);
|
||||
|
||||
private:
|
||||
const MediaTaskType mType;
|
||||
|
|
|
@ -109,16 +109,16 @@ bool SdCardHandleState::SdCardEventHandle(VStateMachineData *msg)
|
|||
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 (!mFastStartMediaFiles.empty() && StorageEvent::SD_CARD_INSERT == data->mData) {
|
||||
LogInfo("Sd card is inserted for the first time.\n");
|
||||
// StatusCode code = IFilesManager::GetInstance()->SaveFile(*(mFileNeedToSave.get()));
|
||||
StatusCode code = IFilesManager::GetInstance()->SaveFiles(mFastStartMediaFiles);
|
||||
if (IsCodeOK(code) == false) {
|
||||
LogError("SaveFile failed.\n");
|
||||
}
|
||||
// mFileNeedToSave.reset();
|
||||
mFastStartMediaFiles.clear();
|
||||
}
|
||||
// if (!mFastStartMediaFiles.empty() && StorageEvent::SD_CARD_INSERT == data->mData) {
|
||||
// LogInfo("Sd card is inserted for the first time.\n");
|
||||
// // StatusCode code = IFilesManager::GetInstance()->SaveFile(*(mFileNeedToSave.get()));
|
||||
// StatusCode code = IFilesManager::GetInstance()->SaveFiles(mFastStartMediaFiles);
|
||||
// if (IsCodeOK(code) == false) {
|
||||
// LogError("SaveFile failed.\n");
|
||||
// }
|
||||
// // mFileNeedToSave.reset();
|
||||
// mFastStartMediaFiles.clear();
|
||||
// }
|
||||
mSdCardStatus = data->mData;
|
||||
/**
|
||||
* @brief The file management uses a database. When the SD card is inserted or removed, the card's file data needs
|
||||
|
@ -129,6 +129,16 @@ bool SdCardHandleState::SdCardEventHandle(VStateMachineData *msg)
|
|||
std::shared_ptr<VMissionData> message =
|
||||
std::make_shared<VMissionData>(static_cast<MissionEvent>(InternalStateEvent::CHECK_UPGRADE_FILE));
|
||||
MissionStateMachine::GetInstance()->SendStateMessage(message);
|
||||
if (!mFastStartMediaFiles.empty()) {
|
||||
LogInfo("Sd card is inserted for the first time.\n");
|
||||
// StatusCode code = IFilesManager::GetInstance()->SaveFile(*(mFileNeedToSave.get()));
|
||||
StatusCode code = IFilesManager::GetInstance()->SaveFiles(mFastStartMediaFiles);
|
||||
if (IsCodeOK(code) == false) {
|
||||
LogError("SaveFile failed.\n");
|
||||
}
|
||||
// mFileNeedToSave.reset();
|
||||
mFastStartMediaFiles.clear();
|
||||
}
|
||||
}
|
||||
else {
|
||||
IFilesManager::GetInstance()->UnInit();
|
||||
|
|
|
@ -57,17 +57,19 @@ InfoToBeSaved FilesDatabase::CreateInfoForSavingFiles(const unsigned int &count)
|
|||
StatusCode FilesDatabase::DbSaveFiles(const std::vector<SyncFileInfo> &info)
|
||||
{
|
||||
for (auto &each : info) {
|
||||
unsigned long key = UNDEFINE_SERIAL_NUMBER;
|
||||
bool result = SqliteHandle::GetInstance()->SyncFile(each, key);
|
||||
if (!result) {
|
||||
LogError("Save file failed.\n");
|
||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||
}
|
||||
if (!each.mSourceFile.empty()) {
|
||||
/**
|
||||
* @brief If the source file parameter is passed in, the source file needs to be copied to overwrite the
|
||||
* target file.
|
||||
*/
|
||||
IStorageManager::GetInstance()->SaveFile(each.mSourceFile, each.mFileName);
|
||||
}
|
||||
bool result = SqliteHandle::GetInstance()->SyncFile(each);
|
||||
if (!result) {
|
||||
LogError("Save file failed.\n");
|
||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||
std::string saveFile = SqliteHandle::RenameFileToSync(each.mFileName, key);
|
||||
IStorageManager::GetInstance()->SaveFile(each.mSourceFile, saveFile);
|
||||
}
|
||||
}
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
|
|
|
@ -94,7 +94,7 @@ unsigned long int SqliteHandle::CreateFiles(const unsigned int &count)
|
|||
if (SQLITE_OK != rc) {
|
||||
LogError("SQL error: %s\n", err_msg);
|
||||
sqlite3_free(err_msg);
|
||||
return 0;
|
||||
return UNDEFINE_SERIAL_NUMBER;
|
||||
}
|
||||
}
|
||||
auto last_rowid_lambda = [](void *last_rowid, int argc, char **argv, char **azColName) -> int {
|
||||
|
@ -102,7 +102,7 @@ unsigned long int SqliteHandle::CreateFiles(const unsigned int &count)
|
|||
LogInfo("last_rowid key = %s\n", argv[0]);
|
||||
*(unsigned long int *)last_rowid = strtoul(argv[0], nullptr, 10);
|
||||
}
|
||||
return 0;
|
||||
return UNDEFINE_SERIAL_NUMBER;
|
||||
};
|
||||
unsigned long int last_rowid = 0;
|
||||
const char *sql2 = "SELECT last_insert_rowid();";
|
||||
|
@ -110,16 +110,20 @@ unsigned long int SqliteHandle::CreateFiles(const unsigned int &count)
|
|||
if (SQLITE_OK != rc) {
|
||||
LogError("SQL error: %s\n", err_msg);
|
||||
sqlite3_free(err_msg);
|
||||
return 0;
|
||||
return UNDEFINE_SERIAL_NUMBER;
|
||||
}
|
||||
constexpr int MINIMUM_AUTO_INCREMENT_PRIMARY_KEY = 1;
|
||||
return last_rowid - count + MINIMUM_AUTO_INCREMENT_PRIMARY_KEY;
|
||||
}
|
||||
bool SqliteHandle::SyncFile(const SyncFileInfo &info)
|
||||
bool SqliteHandle::SyncFile(const SyncFileInfo &info, unsigned long &key)
|
||||
{
|
||||
if (UNDEFINE_SERIAL_NUMBER == info.mSerialNumber) {
|
||||
LogError("Serial number is undefine.\n");
|
||||
return false;
|
||||
unsigned long serialNumber = info.mSerialNumber;
|
||||
std::string fileName = info.mFileName;
|
||||
if (UNDEFINE_SERIAL_NUMBER == serialNumber) {
|
||||
LogWarning("Serial number is undefine, create new one.\n");
|
||||
serialNumber = CreateFiles(1);
|
||||
key = serialNumber;
|
||||
fileName = SqliteHandle::RenameFileToSync(fileName, key);
|
||||
}
|
||||
std::stringstream sqlStream;
|
||||
std::string comma = " ";
|
||||
|
@ -129,7 +133,7 @@ bool SqliteHandle::SyncFile(const SyncFileInfo &info)
|
|||
comma = ", ";
|
||||
}
|
||||
if (info.mFileName.empty() == false) {
|
||||
sqlStream << comma << FILE_PATH " = '" << info.mFileName << "'";
|
||||
sqlStream << comma << FILE_PATH " = '" << fileName << "'";
|
||||
comma = ", ";
|
||||
}
|
||||
if (UNDEFINE_FILE_SIZE != info.mFileSize) {
|
||||
|
@ -148,7 +152,7 @@ bool SqliteHandle::SyncFile(const SyncFileInfo &info)
|
|||
sqlStream << comma << FILE_DURATION " = '" << info.mFileDuration << "'";
|
||||
comma = ", ";
|
||||
}
|
||||
sqlStream << " WHERE " TABLE_KEY " = " << info.mSerialNumber << ";";
|
||||
sqlStream << " WHERE " TABLE_KEY " = " << serialNumber << ";";
|
||||
LogInfo("Sql: %s\n", sqlStream.str().c_str());
|
||||
char *errMsg = nullptr;
|
||||
int rc = SQLITE_UNDEFINE;
|
||||
|
@ -446,4 +450,16 @@ FileStatus SqliteHandle::ConvertStringToFileStatus(const std::string &status)
|
|||
return it->second;
|
||||
}
|
||||
return FileStatus::END;
|
||||
}
|
||||
std::string SqliteHandle::RenameFileToSync(const std::string &sourceFile, unsigned long &key)
|
||||
{
|
||||
std::string original = sourceFile;
|
||||
const std::string pattern = "???";
|
||||
std::string replacement = std::to_string(key);
|
||||
size_t pos = 0;
|
||||
while ((pos = original.find(pattern, pos)) != std::string::npos) {
|
||||
original.replace(pos, pattern.length(), replacement);
|
||||
pos += replacement.length();
|
||||
}
|
||||
return original;
|
||||
}
|
|
@ -30,8 +30,22 @@ public:
|
|||
static std::shared_ptr<SqliteHandle> &GetInstance(std::shared_ptr<SqliteHandle> *impl = nullptr);
|
||||
void Init(const std::string &dbFileName);
|
||||
void UnInit(void);
|
||||
/**
|
||||
* @brief Request to create a file and return the minimum value of the file's unique serial number.
|
||||
*
|
||||
* @param count
|
||||
* @return unsigned long int If invalid, returns UNDEFINE_SERIAL_NUMBER.
|
||||
*/
|
||||
unsigned long int CreateFiles(const unsigned int &count);
|
||||
bool SyncFile(const SyncFileInfo &info);
|
||||
/**
|
||||
* @brief Synchronize file information. When the synchronized file number is invalid, a new number will be created
|
||||
* and assigned to the given parameter "key".
|
||||
* @param info
|
||||
* @param key [out] If the key value does not exist, it is created and output as a parameter.
|
||||
* @return true
|
||||
* @return false
|
||||
*/
|
||||
bool SyncFile(const SyncFileInfo &info, unsigned long &key);
|
||||
bool SearchFiles(const std::vector<FileCreateType> &types, std::vector<SyncFileInfo> &info);
|
||||
bool Remove();
|
||||
bool Modified();
|
||||
|
@ -51,6 +65,9 @@ private:
|
|||
static std::string ConvertFileStatusToString(const FileStatus &status);
|
||||
static FileStatus ConvertStringToFileStatus(const std::string &status);
|
||||
|
||||
public:
|
||||
static std::string RenameFileToSync(const std::string &sourceFile, unsigned long &key);
|
||||
|
||||
private:
|
||||
sqlite3 *mDb;
|
||||
};
|
||||
|
|
|
@ -54,6 +54,21 @@ TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_EXAMPLE_TakePicture)
|
|||
MainThread::GetInstance()->Runing();
|
||||
}
|
||||
// ../output_files/test/bin/HuntingCameraTest
|
||||
// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_EXAMPLE_FastStartMediaHandle
|
||||
TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_EXAMPLE_FastStartMediaHandle)
|
||||
{
|
||||
McuManagerTestTool::MockOtherSideIpcMissionReply(IpcMission::TEST);
|
||||
SetAllCamerasResult(mAllCamerasMock);
|
||||
HalTestTool::MockSdCardRemove(mLinuxTest);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
MainThread::GetInstance()->Init();
|
||||
TestManager::ResetTimeOut(1000 * 5);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||
HalTestTool::MockSdCardInsert(mLinuxTest);
|
||||
HalTestTool::MockKeyClick("reset", 200); // Simulate pressing a button.
|
||||
MainThread::GetInstance()->Runing();
|
||||
}
|
||||
// ../output_files/test/bin/HuntingCameraTest
|
||||
// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_EXAMPLE_MediaTask
|
||||
TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_EXAMPLE_MediaTask)
|
||||
{
|
||||
|
|
|
@ -62,17 +62,6 @@ void CameraHalTest::SetCameraMonitorTrace(std::shared_ptr<VCameraHalMonitor> &mo
|
|||
StatusCode CameraHalTest::StartSingleTask(const CameraTaskParam ¶m)
|
||||
{
|
||||
LogInfo("CameraHalTest::StartSingleTask\n");
|
||||
// if (mTaskTimerThread.joinable()) {
|
||||
// mTaskTimerThread.join();
|
||||
// }
|
||||
// ReadFileInit();
|
||||
// auto taskTimerThread = [=](std::shared_ptr<CameraHalTest> cameraHal) {
|
||||
// LogInfo("MockReportMediaStream start.\n");
|
||||
// cameraHal->MockReportMediaStream();
|
||||
// };
|
||||
// std::shared_ptr<CameraHalTest> cameraHal =
|
||||
// std::dynamic_pointer_cast<CameraHalTest>(CameraHal::shared_from_this()); mTaskTimerThread =
|
||||
// std::thread(taskTimerThread, cameraHal);
|
||||
StatusCode code = StartSingleTaskTrace(param);
|
||||
if (StatusCodeEqual(code, "STATUS_CODE_VIRTUAL_FUNCTION")) {
|
||||
return CameraHal::StartSingleTask(param);
|
||||
|
|
Loading…
Reference in New Issue
Block a user