diff --git a/middleware/FilesManager/CMakeLists.txt b/middleware/FilesManager/CMakeLists.txt index ad0b3cd2..914d16f9 100644 --- a/middleware/FilesManager/CMakeLists.txt +++ b/middleware/FilesManager/CMakeLists.txt @@ -17,6 +17,7 @@ include_directories( #) aux_source_directory(./src SRC_FILES) +aux_source_directory(./src/sqlite3 SRC_FILES) set(TARGET_NAME FilesManager) add_library(${TARGET_NAME} STATIC ${SRC_FILES}) diff --git a/middleware/FilesManager/src/FilesDatabase.h b/middleware/FilesManager/src/FilesDatabase.h new file mode 100644 index 00000000..16aae5dc --- /dev/null +++ b/middleware/FilesManager/src/FilesDatabase.h @@ -0,0 +1,27 @@ +/* + * 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 FILES_DATABASE_H +#define FILES_DATABASE_H +#include "StatusCode.h" +#include "FilesHandle.h" +#include "IFilesManager.h" +class FilesDatabase : public FilesHandle +{ +public: + FilesDatabase() = default; + virtual ~FilesDatabase() = default; + StatusCode DatabaseSaveFile(const SaveFileInfo &fileInfo); +}; +#endif \ No newline at end of file diff --git a/middleware/FilesManager/src/FilesHandle.cpp b/middleware/FilesManager/src/FilesHandle.cpp new file mode 100644 index 00000000..14f21775 --- /dev/null +++ b/middleware/FilesManager/src/FilesHandle.cpp @@ -0,0 +1,59 @@ +/* + * 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 "FilesHandle.h" +#include "ILog.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +std::string FilesHandle::CreateFilePathName(const std::string &sourceFile) +{ + const std::string ERROR_SUFFIX = ""; + const char *dot = strrchr(sourceFile.c_str(), '.'); + if (!dot || dot == sourceFile.c_str()) { + LogError("File suffix error.\n"); + return ERROR_SUFFIX; + } + std::string fileSuffix = dot + 1; + if (fileSuffix != "jpg" && fileSuffix != "mp4" && fileSuffix != "JPG" && fileSuffix != "MP4") { + LogError("File suffix error.\n"); + return ERROR_SUFFIX; + } + std::string fileType = (fileSuffix == "jpg" || fileSuffix == "JPG") ? "picture" : "video"; + 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); + + int year = tm_now.tm_year + 1900; + int month = tm_now.tm_mon + 1; + int day = tm_now.tm_mday; + int hour = tm_now.tm_hour; + int minute = tm_now.tm_min; + int second = tm_now.tm_sec; + + std::ostringstream pathStream; + pathStream << "/DCIM/" << fileType << "/" << std::setw(4) << std::setfill('0') << year << "/" << std::setw(2) + << std::setfill('0') << month << "/" << std::setw(2) << std::setfill('0') << day << "/" << std::setw(2) + << std::setfill('0') << hour << std::setw(2) << std::setfill('0') << minute << std::setw(2) + << std::setfill('0') << second << "." << fileSuffix; + return pathStream.str(); +} \ No newline at end of file diff --git a/middleware/FilesManager/src/FilesHandle.h b/middleware/FilesManager/src/FilesHandle.h new file mode 100644 index 00000000..7b4a503d --- /dev/null +++ b/middleware/FilesManager/src/FilesHandle.h @@ -0,0 +1,25 @@ +/* + * 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 FILES_HANDLE_H +#define FILES_HANDLE_H +#include +class FilesHandle +{ +public: + FilesHandle() = default; + virtual ~FilesHandle() = default; + std::string CreateFilePathName(const std::string &sourceFile); +}; +#endif \ No newline at end of file diff --git a/middleware/FilesManager/src/FilesManagerImpl.cpp b/middleware/FilesManager/src/FilesManagerImpl.cpp index 6b8d8100..289910fa 100644 --- a/middleware/FilesManager/src/FilesManagerImpl.cpp +++ b/middleware/FilesManager/src/FilesManagerImpl.cpp @@ -24,6 +24,5 @@ StatusCode FilesManagerImpl::UnInit(void) } StatusCode FilesManagerImpl::SaveFile(const SaveFileInfo &fileInfo) { - IStorageManager::GetInstance()->SaveFile(fileInfo.mFileName, "/DCIM/picture/2024/04/28/test.jpg"); - return CreateStatusCode(STATUS_CODE_OK); + return FilesDatabase::DatabaseSaveFile(fileInfo); } \ No newline at end of file diff --git a/middleware/FilesManager/src/FilesManagerImpl.h b/middleware/FilesManager/src/FilesManagerImpl.h index 895490e1..114bee72 100644 --- a/middleware/FilesManager/src/FilesManagerImpl.h +++ b/middleware/FilesManager/src/FilesManagerImpl.h @@ -14,13 +14,16 @@ */ #ifndef FILES_MANAGER_IMPL_H #define FILES_MANAGER_IMPL_H +#include "FilesDatabase.h" #include "IFilesManager.h" #include -class FilesManagerImpl : public IFilesManager +class FilesManagerImpl : public IFilesManager, public FilesDatabase { public: FilesManagerImpl() = default; virtual ~FilesManagerImpl() = default; + +protected: StatusCode Init(void) override; StatusCode UnInit(void) override; StatusCode SaveFile(const SaveFileInfo &fileInfo) override; diff --git a/middleware/FilesManager/src/sqlite3/FilesDatabase.cpp b/middleware/FilesManager/src/sqlite3/FilesDatabase.cpp new file mode 100644 index 00000000..95a0fc24 --- /dev/null +++ b/middleware/FilesManager/src/sqlite3/FilesDatabase.cpp @@ -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. + */ +#include "FilesDatabase.h" +#include "ILog.h" +#include "IStorageManager.h" +StatusCode FilesDatabase::DatabaseSaveFile(const SaveFileInfo &fileInfo) +{ + std::string saveFile = FilesHandle::CreateFilePathName(fileInfo.mFileName); + LogInfo("Save file:%s\n", saveFile.c_str()); + if (0 == saveFile.length()) { + LogError("File suffix error, save file failed.\n"); + return CreateStatusCode(STATUS_CODE_NOT_OK); + } + StatusCode code = IStorageManager::GetInstance()->SaveFile(fileInfo.mFileName, saveFile); + if (IsCodeOK(code)) { + // TODO: handle database here + return CreateStatusCode(STATUS_CODE_OK); + } + return CreateStatusCode(STATUS_CODE_NOT_OK); +} \ No newline at end of file diff --git a/test/application/HunttingCamera/src_mock/MediaManager_Mock_Test.cpp b/test/application/HunttingCamera/src_mock/MediaManager_Mock_Test.cpp index 87d9a89c..19cc15ff 100644 --- a/test/application/HunttingCamera/src_mock/MediaManager_Mock_Test.cpp +++ b/test/application/HunttingCamera/src_mock/MediaManager_Mock_Test.cpp @@ -34,7 +34,7 @@ TEST_F(HunttingCameraTest, INTEGRATION_HunttingCamera_EXAMPLE_MediaReprot) MainThread::GetInstance()->Init(); TestManager::ResetTimeOut(1000 * 3); std::this_thread::sleep_for(std::chrono::milliseconds(100)); - MockReportCameraEvent("test name", "test path", CameraEvent::PICTIRUE, CameraType::MAIN_CAMERA); + MockReportCameraEvent("/tmp/test.MP4", "test path", CameraEvent::PICTIRUE, CameraType::MAIN_CAMERA); // MockAppPlayback(); MainThread::GetInstance()->Runing(); }