Improve:files datebase.
This commit is contained in:
		
							parent
							
								
									7bf75e7754
								
							
						
					
					
						commit
						42e1e62756
					
				| 
						 | 
				
			
			@ -14,6 +14,7 @@
 | 
			
		|||
 */
 | 
			
		||||
#include "AppMonitor.h"
 | 
			
		||||
#include "IAppManager.h"
 | 
			
		||||
#include "IFilesManager.h"
 | 
			
		||||
#include "ILog.h"
 | 
			
		||||
#include "IStorageManager.h"
 | 
			
		||||
#include "StatusCode.h"
 | 
			
		||||
| 
						 | 
				
			
			@ -100,6 +101,11 @@ StatusCode AppMonitor::GetStorageInfo(std::vector<AppGetStorageInfo> ¶m)
 | 
			
		|||
}
 | 
			
		||||
StatusCode AppMonitor::GetStorageFileList(const AppGetFileInfo &fileInfo, std::vector<AppGetFileList> ¶m)
 | 
			
		||||
{
 | 
			
		||||
    std::vector<FileCreateType> types;
 | 
			
		||||
    std::vector<SyncFileInfo> info;
 | 
			
		||||
    types.push_back(FileCreateType::PIR);
 | 
			
		||||
    IFilesManager::GetInstance()->GetAllFiles(info);
 | 
			
		||||
    LogInfo("GetStorageFileList: file size = %d.\n", info.size());
 | 
			
		||||
    if (StorageFileEvent::LOOP == fileInfo.mEvent) {
 | 
			
		||||
        AppGetFileList file;
 | 
			
		||||
        file.mCreateTime_s = 123456789;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -66,7 +66,7 @@ bool MediaHandleState::MediaTaskFinishedHandle(VStateMachineData *msg)
 | 
			
		|||
    LogInfo("response files = %d.\n", data->mData.mResponse.size());
 | 
			
		||||
    std::vector<SyncFileInfo> files;
 | 
			
		||||
    for (auto &response : data->mData.mResponse) {
 | 
			
		||||
        SyncFileInfo file(data->mData.mSerialNumber, response.mFileName, 0, 0, FileCreateType::END, FileStatus::END);
 | 
			
		||||
        SyncFileInfo file(data->mData.mSerialNumber, response.mFileName, 0, 0, 0, FileCreateType::END, FileStatus::END);
 | 
			
		||||
        files.push_back(file);
 | 
			
		||||
    }
 | 
			
		||||
    IFilesManager::GetInstance()->SaveFiles(files);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -52,8 +52,8 @@ std::string MediaTask::GetTargetNameForSaving(void)
 | 
			
		|||
    int second = tm_now.tm_sec;
 | 
			
		||||
 | 
			
		||||
    std::ostringstream pathStream;
 | 
			
		||||
    pathStream << mSavePath << std::setw(2) << std::setfill('0') << hour << std::setw(2) << std::setfill('0') << minute
 | 
			
		||||
               << std::setw(2) << std::setfill('0') << second << ".mp4";
 | 
			
		||||
    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 << ".mp4";
 | 
			
		||||
    mTargetName = pathStream.str();
 | 
			
		||||
    LogInfo("GetTargetNameForSaving: %s\n", pathStream.str().c_str());
 | 
			
		||||
    return pathStream.str();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										2
									
								
								external/CMakeLists.txt
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								external/CMakeLists.txt
									
									
									
									
										vendored
									
									
								
							| 
						 | 
				
			
			@ -27,4 +27,4 @@ add_subdirectory(ffmpeg)
 | 
			
		|||
add_subdirectory(fdk-aac)
 | 
			
		||||
# ================= ffmpeg related end ================= #
 | 
			
		||||
add_subdirectory(libconfig)
 | 
			
		||||
add_subdirectory(libfaac)
 | 
			
		||||
# add_subdirectory(libfaac)
 | 
			
		||||
| 
						 | 
				
			
			@ -74,15 +74,17 @@ enum class StorageFileType
 | 
			
		|||
    VIDEO,
 | 
			
		||||
    END
 | 
			
		||||
};
 | 
			
		||||
/**
 | 
			
		||||
 * @brief A file classification list mapped to hunting camera scenes based on the dash cam protocol.
 | 
			
		||||
 */
 | 
			
		||||
enum class StorageFileEvent
 | 
			
		||||
{
 | 
			
		||||
    LOOP = 0,
 | 
			
		||||
    PIR,
 | 
			
		||||
    CRASH,
 | 
			
		||||
    EMR,
 | 
			
		||||
    EVENT,
 | 
			
		||||
    PARK,
 | 
			
		||||
    END
 | 
			
		||||
    LOOP = 0, ///< Loop event.In the hunting camera scene, all files are represented.
 | 
			
		||||
    EMR,      ///< EMR event.In the hunting camera scenario, it indicates the PIR snapshot file.
 | 
			
		||||
    EVENT,    ///< General event.In the hunting camera scenario, it indicates a manually captured file.
 | 
			
		||||
    PARK,     ///< Park event.In the hunting camera scenario, it indicates a timed snapshot file.
 | 
			
		||||
    ALL,      ///< No distinction between categories, all files.
 | 
			
		||||
    END       ///< End of events.
 | 
			
		||||
};
 | 
			
		||||
enum class SwitchStatus
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -597,6 +597,7 @@ AppGetFileInfo inline SixFrameHandle::RequestGetFileListParse(const std::string
 | 
			
		|||
    };
 | 
			
		||||
    std::shared_ptr<VParseUrl> parse = std::make_shared<ParseUrl<AppGetFileInfo>>();
 | 
			
		||||
    std::shared_ptr<ParseUrl<AppGetFileInfo>> parseImpl = std::dynamic_pointer_cast<ParseUrl<AppGetFileInfo>>(parse);
 | 
			
		||||
    parseImpl->mData.mEvent = StorageFileEvent::ALL;
 | 
			
		||||
    ExtractParamsFromUrl(url, parseFunc, parse);
 | 
			
		||||
    return parseImpl->mData;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -19,13 +19,16 @@
 | 
			
		|||
#include <stdio.h>
 | 
			
		||||
#include <time.h>
 | 
			
		||||
#include <vector>
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Enum defining different types of file creation.
 | 
			
		||||
 */
 | 
			
		||||
enum class FileCreateType
 | 
			
		||||
{
 | 
			
		||||
    PIR = 0,
 | 
			
		||||
    MANUAL_TEST,
 | 
			
		||||
    MANUAL_PHONE,
 | 
			
		||||
    TIMED,
 | 
			
		||||
    END
 | 
			
		||||
    PIR = 0,      ///< Automatically created file (PIR triggered).
 | 
			
		||||
    MANUAL_TEST,  ///< File created for manual testing purposes.
 | 
			
		||||
    MANUAL_PHONE, ///< File created manually by phone.
 | 
			
		||||
    TIMED,        ///< File created on a timed schedule.
 | 
			
		||||
    END           ///< End.
 | 
			
		||||
};
 | 
			
		||||
enum class FileStatus
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			@ -41,11 +44,13 @@ constexpr unsigned long UNDEFINE_FILE_SIZE = 0;
 | 
			
		|||
constexpr time_t UNDEFINE_CREATE_TIME = -1;
 | 
			
		||||
typedef struct sync_file_info
 | 
			
		||||
{
 | 
			
		||||
    sync_file_info(const unsigned long &serialNumber, const std::string &fileName, const unsigned long &fileSize,
 | 
			
		||||
                   const time_t &createTime_s, const FileCreateType &type, const FileStatus &status);
 | 
			
		||||
    sync_file_info(const unsigned long &serialNumber, const std::string &fileName, const unsigned int &fileSize,
 | 
			
		||||
                   const unsigned int &fileDuration, const time_t &createTime_s, const FileCreateType &type,
 | 
			
		||||
                   const FileStatus &status);
 | 
			
		||||
    const unsigned long mSerialNumber;
 | 
			
		||||
    const std::string mFileName;
 | 
			
		||||
    const unsigned long mFileSize;
 | 
			
		||||
    const unsigned int mFileSize;
 | 
			
		||||
    const unsigned int mFileDuration;
 | 
			
		||||
    const time_t mCreateTime_s;
 | 
			
		||||
    const FileCreateType mType;
 | 
			
		||||
    const FileStatus mStatus;
 | 
			
		||||
| 
						 | 
				
			
			@ -74,5 +79,7 @@ public:
 | 
			
		|||
    virtual StatusCode SaveFile(const SaveFileInfo &fileInfo);
 | 
			
		||||
    virtual InfoToBeSaved GetInfoForSavingFiles(const unsigned int &count);
 | 
			
		||||
    virtual StatusCode SaveFiles(const std::vector<SyncFileInfo> &info);
 | 
			
		||||
    virtual StatusCode GetAllFiles(std::vector<SyncFileInfo> &info);
 | 
			
		||||
    virtual StatusCode GetFiles(const std::vector<FileCreateType> &types, std::vector<SyncFileInfo> &info);
 | 
			
		||||
};
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			@ -17,6 +17,7 @@
 | 
			
		|||
#include "FilesHandle.h"
 | 
			
		||||
#include "IFilesManager.h"
 | 
			
		||||
#include "StatusCode.h"
 | 
			
		||||
constexpr FileCreateType END_MEANS_SEARCHING_ALL_FILES = FileCreateType::END;
 | 
			
		||||
class FilesDatabase : public FilesHandle
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
| 
						 | 
				
			
			@ -27,5 +28,6 @@ public:
 | 
			
		|||
    StatusCode DbSaveFile(const SaveFileInfo &fileInfo);
 | 
			
		||||
    InfoToBeSaved CreateInfoForSavingFiles(const unsigned int &count);
 | 
			
		||||
    StatusCode DbSaveFiles(const std::vector<SyncFileInfo> &info);
 | 
			
		||||
    StatusCode DbGetFiles(const std::vector<FileCreateType> &types, std::vector<SyncFileInfo> &info);
 | 
			
		||||
};
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			@ -39,4 +39,14 @@ InfoToBeSaved FilesManagerImpl::GetInfoForSavingFiles(const unsigned int &count)
 | 
			
		|||
StatusCode FilesManagerImpl::SaveFiles(const std::vector<SyncFileInfo> &info)
 | 
			
		||||
{
 | 
			
		||||
    return FilesDatabase::DbSaveFiles(info);
 | 
			
		||||
}
 | 
			
		||||
StatusCode FilesManagerImpl::GetAllFiles(std::vector<SyncFileInfo> &info)
 | 
			
		||||
{
 | 
			
		||||
    std::vector<FileCreateType> types;
 | 
			
		||||
    types.push_back(END_MEANS_SEARCHING_ALL_FILES);
 | 
			
		||||
    return FilesDatabase::DbGetFiles(types, info);
 | 
			
		||||
}
 | 
			
		||||
StatusCode FilesManagerImpl::GetFiles(const std::vector<FileCreateType> &types, std::vector<SyncFileInfo> &info)
 | 
			
		||||
{
 | 
			
		||||
    return FilesDatabase::DbGetFiles(types, info);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -29,5 +29,7 @@ protected:
 | 
			
		|||
    StatusCode SaveFile(const SaveFileInfo &fileInfo) override;
 | 
			
		||||
    InfoToBeSaved GetInfoForSavingFiles(const unsigned int &count) override;
 | 
			
		||||
    StatusCode SaveFiles(const std::vector<SyncFileInfo> &info) override;
 | 
			
		||||
    StatusCode GetAllFiles(std::vector<SyncFileInfo> &info) override;
 | 
			
		||||
    StatusCode GetFiles(const std::vector<FileCreateType> &types, std::vector<SyncFileInfo> &info) override;
 | 
			
		||||
};
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			@ -23,10 +23,10 @@ save_file_info::save_file_info(const std::string &fileName) : mFileName(fileName
 | 
			
		|||
{
 | 
			
		||||
}
 | 
			
		||||
sync_file_info::sync_file_info(const unsigned long &serialNumber, const std::string &fileName,
 | 
			
		||||
                               const unsigned long &fileSize, const time_t &createTime_s, const FileCreateType &type,
 | 
			
		||||
                               const FileStatus &status)
 | 
			
		||||
    : mSerialNumber(serialNumber), mFileName(fileName), mFileSize(fileSize), mCreateTime_s(createTime_s), mType(type),
 | 
			
		||||
      mStatus(status)
 | 
			
		||||
                               const unsigned int &fileSize, const unsigned int &fileDuration,
 | 
			
		||||
                               const time_t &createTime_s, const FileCreateType &type, const FileStatus &status)
 | 
			
		||||
    : mSerialNumber(serialNumber), mFileName(fileName), mFileSize(fileSize), mFileDuration(fileDuration),
 | 
			
		||||
      mCreateTime_s(createTime_s), mType(type), mStatus(status)
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
info_to_be_saved::info_to_be_saved(const unsigned long &serialNumber, const std::string mSavingPath)
 | 
			
		||||
| 
						 | 
				
			
			@ -69,6 +69,16 @@ InfoToBeSaved IFilesManager::GetInfoForSavingFiles(const unsigned int &count)
 | 
			
		|||
    return info;
 | 
			
		||||
}
 | 
			
		||||
StatusCode IFilesManager::SaveFiles(const std::vector<SyncFileInfo> &info)
 | 
			
		||||
{
 | 
			
		||||
    LogWarning("STATUS_CODE_VIRTUAL_FUNCTION\n");
 | 
			
		||||
    return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
 | 
			
		||||
}
 | 
			
		||||
StatusCode IFilesManager::GetAllFiles(std::vector<SyncFileInfo> &info)
 | 
			
		||||
{
 | 
			
		||||
    LogWarning("STATUS_CODE_VIRTUAL_FUNCTION\n");
 | 
			
		||||
    return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
 | 
			
		||||
}
 | 
			
		||||
StatusCode IFilesManager::GetFiles(const std::vector<FileCreateType> &types, std::vector<SyncFileInfo> &info)
 | 
			
		||||
{
 | 
			
		||||
    LogWarning("STATUS_CODE_VIRTUAL_FUNCTION\n");
 | 
			
		||||
    return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -64,4 +64,13 @@ StatusCode FilesDatabase::DbSaveFiles(const std::vector<SyncFileInfo> &info)
 | 
			
		|||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return CreateStatusCode(STATUS_CODE_OK);
 | 
			
		||||
}
 | 
			
		||||
StatusCode FilesDatabase::DbGetFiles(const std::vector<FileCreateType> &types, std::vector<SyncFileInfo> &info)
 | 
			
		||||
{
 | 
			
		||||
    bool result = SqliteHandle::GetInstance()->SearchFiles(types, info);
 | 
			
		||||
    if (!result) {
 | 
			
		||||
        LogError("Search files failed.\n");
 | 
			
		||||
        return CreateStatusCode(STATUS_CODE_NOT_OK);
 | 
			
		||||
    }
 | 
			
		||||
    return CreateStatusCode(STATUS_CODE_OK);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -18,17 +18,20 @@
 | 
			
		|||
#include "sqlite3.h"
 | 
			
		||||
#include <cstdlib>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <map>
 | 
			
		||||
#include <memory>
 | 
			
		||||
#include <sstream>
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <time.h>
 | 
			
		||||
#include <vector>
 | 
			
		||||
#define FILES_TABLE "files"
 | 
			
		||||
#define TABLE_KEY "key"
 | 
			
		||||
#define FILE_PATH "path"
 | 
			
		||||
#define CREATE_TIME "time"
 | 
			
		||||
#define CREATE_TIME "create_time"
 | 
			
		||||
#define FILE_TYPE "type"
 | 
			
		||||
#define FILE_SIZE "size"
 | 
			
		||||
#define FILE_STATUS "status"
 | 
			
		||||
#define FILE_DURATION "duration"
 | 
			
		||||
#define FILE_STATUS_BE_RECORDING "recording"
 | 
			
		||||
#define FILE_STATUS_COMPLETE_RECORD "complete_record"
 | 
			
		||||
#define FILE_STATUS_SHOULD_BE_UPLOADED "should_be_uploaded"
 | 
			
		||||
| 
						 | 
				
			
			@ -84,7 +87,7 @@ unsigned long int SqliteHandle::CreateFiles(const unsigned int &count)
 | 
			
		|||
    int rc = SQLITE_UNDEFINE;
 | 
			
		||||
    const char *sql =
 | 
			
		||||
        "INSERT INTO " FILES_TABLE " (" FILE_PATH ", " CREATE_TIME ", " FILE_TYPE ", " FILE_SIZE ", " FILE_STATUS
 | 
			
		||||
        ") VALUES (\"\", 0, \"" FILE_TYPE_UNDEFINE "\", 0, \"" FILE_STATUS_BE_RECORDING "\");";
 | 
			
		||||
        ", " FILE_DURATION ") VALUES (\"\", 0, \"" FILE_TYPE_UNDEFINE "\", 0, \"" FILE_STATUS_BE_RECORDING "\", 0);";
 | 
			
		||||
    for (unsigned int i = 0; i < count; ++i) {
 | 
			
		||||
        rc = sqlite3_exec(mDb, sql, nullptr, nullptr, &err_msg);
 | 
			
		||||
        if (SQLITE_OK != rc) {
 | 
			
		||||
| 
						 | 
				
			
			@ -95,7 +98,7 @@ unsigned long int SqliteHandle::CreateFiles(const unsigned int &count)
 | 
			
		|||
    }
 | 
			
		||||
    auto last_rowid_lambda = [](void *last_rowid, int argc, char **argv, char **azColName) -> int {
 | 
			
		||||
        if (argc > 0) {
 | 
			
		||||
            LogInfo("last_rowid = %s\n", argv[0]);
 | 
			
		||||
            LogInfo("last_rowid key = %s\n", argv[0]);
 | 
			
		||||
            *(unsigned long int *)last_rowid = strtoul(argv[0], nullptr, 10);
 | 
			
		||||
        }
 | 
			
		||||
        return 0;
 | 
			
		||||
| 
						 | 
				
			
			@ -108,7 +111,8 @@ unsigned long int SqliteHandle::CreateFiles(const unsigned int &count)
 | 
			
		|||
        sqlite3_free(err_msg);
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
    return last_rowid - count;
 | 
			
		||||
    constexpr int MINIMUM_AUTO_INCREMENT_PRIMARY_KEY = 1;
 | 
			
		||||
    return last_rowid - count + MINIMUM_AUTO_INCREMENT_PRIMARY_KEY;
 | 
			
		||||
}
 | 
			
		||||
bool SqliteHandle::SyncFile(const SyncFileInfo &info)
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			@ -131,8 +135,32 @@ bool SqliteHandle::SyncFile(const SyncFileInfo &info)
 | 
			
		|||
    if (UpdateFileType(mDb, info.mSerialNumber, info.mType) == false) {
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    if (UpdateFileDuration(mDb, info.mSerialNumber, info.mFileDuration) == false) {
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
bool SqliteHandle::SearchFiles(const std::vector<FileCreateType> &types, std::vector<SyncFileInfo> &info)
 | 
			
		||||
{
 | 
			
		||||
    std::string sqlAnd = " ";
 | 
			
		||||
    if (types.size() > 1) {
 | 
			
		||||
        sqlAnd = " AND ";
 | 
			
		||||
    }
 | 
			
		||||
    constexpr FileCreateType END_MEANS_SEARCHING_ALL_FILES = FileCreateType::END;
 | 
			
		||||
    std::stringstream sqlStream;
 | 
			
		||||
    if (types.size() == 1 && types[0] == END_MEANS_SEARCHING_ALL_FILES) {
 | 
			
		||||
        sqlStream << "SELECT * from " FILES_TABLE << ";";
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
        sqlStream << "SELECT * from " FILES_TABLE;
 | 
			
		||||
        for (auto &type : types) {
 | 
			
		||||
            sqlStream << sqlAnd << "WHERE " FILE_TYPE " = '" << ConvertFileTypeToString(type) << "'";
 | 
			
		||||
        }
 | 
			
		||||
        sqlStream << ";";
 | 
			
		||||
    }
 | 
			
		||||
    LogInfo("Sql: %s\n", sqlStream.str().c_str());
 | 
			
		||||
    return SearchFiles(mDb, sqlStream.str(), info);
 | 
			
		||||
}
 | 
			
		||||
void SqliteHandle::DbInit(sqlite3 *db)
 | 
			
		||||
{
 | 
			
		||||
    if (nullptr == db) {
 | 
			
		||||
| 
						 | 
				
			
			@ -141,9 +169,9 @@ void SqliteHandle::DbInit(sqlite3 *db)
 | 
			
		|||
    }
 | 
			
		||||
    char *errMsg = nullptr;
 | 
			
		||||
    int rc = SQLITE_UNDEFINE;
 | 
			
		||||
    const char *sql =
 | 
			
		||||
        "CREATE TABLE IF NOT EXISTS " FILES_TABLE " (" TABLE_KEY " INTEGER PRIMARY KEY AUTOINCREMENT, " FILE_PATH
 | 
			
		||||
        " TEXT, " CREATE_TIME " INTEGER," FILE_TYPE " TEXT, " FILE_SIZE " INTEGER, " FILE_STATUS " TEXT);";
 | 
			
		||||
    const char *sql = "CREATE TABLE IF NOT EXISTS " FILES_TABLE " (" TABLE_KEY
 | 
			
		||||
                      " INTEGER PRIMARY KEY AUTOINCREMENT, " FILE_PATH " TEXT, " CREATE_TIME " INTEGER," FILE_TYPE
 | 
			
		||||
                      " TEXT, " FILE_SIZE " INTEGER, " FILE_STATUS " TEXT," FILE_DURATION " INTEGER);";
 | 
			
		||||
    rc = sqlite3_exec(db, sql, nullptr, nullptr, &errMsg);
 | 
			
		||||
    if (SQLITE_OK != rc) {
 | 
			
		||||
        LogError("Sql: %s, errMsg: %s\n", sql, errMsg);
 | 
			
		||||
| 
						 | 
				
			
			@ -229,7 +257,7 @@ bool SqliteHandle::UpdateFileType(sqlite3 *db, const unsigned long &key, const F
 | 
			
		|||
    int rc = SQLITE_UNDEFINE;
 | 
			
		||||
    if (FileCreateType::END != type) {
 | 
			
		||||
        std::stringstream sqlStream;
 | 
			
		||||
        sqlStream << "UPDATE " FILES_TABLE " SET " FILE_TYPE " = '" << ConvertFileType(type)
 | 
			
		||||
        sqlStream << "UPDATE " FILES_TABLE " SET " FILE_TYPE " = '" << ConvertFileTypeToString(type)
 | 
			
		||||
                  << "' WHERE " TABLE_KEY " = " << key << ";";
 | 
			
		||||
        LogInfo("Sql: %s\n", sqlStream.str().c_str());
 | 
			
		||||
        rc = sqlite3_exec(db, sqlStream.str().c_str(), nullptr, nullptr, &errMsg);
 | 
			
		||||
| 
						 | 
				
			
			@ -241,22 +269,107 @@ bool SqliteHandle::UpdateFileType(sqlite3 *db, const unsigned long &key, const F
 | 
			
		|||
    }
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
std::string SqliteHandle::ConvertFileType(const FileCreateType &type)
 | 
			
		||||
bool SqliteHandle::UpdateFileDuration(sqlite3 *db, const unsigned long &key, const unsigned int &duration)
 | 
			
		||||
{
 | 
			
		||||
    if (nullptr == db) {
 | 
			
		||||
        LogError("db is null.\n");
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    char *errMsg = nullptr;
 | 
			
		||||
    int rc = SQLITE_UNDEFINE;
 | 
			
		||||
    std::stringstream sqlStream;
 | 
			
		||||
    sqlStream << "UPDATE " FILES_TABLE " SET " FILE_DURATION " = '" << duration << "' WHERE " TABLE_KEY " = " << key
 | 
			
		||||
              << ";";
 | 
			
		||||
    LogInfo("Sql: %s\n", sqlStream.str().c_str());
 | 
			
		||||
    rc = sqlite3_exec(db, sqlStream.str().c_str(), nullptr, nullptr, &errMsg);
 | 
			
		||||
    if (SQLITE_OK != rc) {
 | 
			
		||||
        LogError("SQL error: %s\n", errMsg);
 | 
			
		||||
        sqlite3_free(errMsg);
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
bool SqliteHandle::SearchFiles(sqlite3 *db, const std::string &sql, std::vector<SyncFileInfo> &info)
 | 
			
		||||
{
 | 
			
		||||
    if (nullptr == db) {
 | 
			
		||||
        LogError("db is null.\n");
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    auto dbCallback = [](void *data, int argc, char **argv, char **azColName) -> int {
 | 
			
		||||
        /********************** Do not delete this code to avoid debugging. *********************/
 | 
			
		||||
        int i = 0;
 | 
			
		||||
        fprintf(stderr, "%s: ", "searchFiles");
 | 
			
		||||
        for (i = 0; i < argc; i++) {
 | 
			
		||||
            printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
 | 
			
		||||
        }
 | 
			
		||||
        printf("\n");
 | 
			
		||||
        /********************** Do not delete this code to avoid debugging. *********************/
 | 
			
		||||
        /**
 | 
			
		||||
         * @brief The magic number in the argv variable refers to the SQL statement that creates the form, which is as
 | 
			
		||||
         * follows:
 | 
			
		||||
         * CREATE TABLE IF NOT EXISTS files (key INTEGER PRIMARY KEY AUTOINCREMENT, path TEXT, create_time INTEGER,type
 | 
			
		||||
         *                                    0                                       1              2               3
 | 
			
		||||
         * TEXT, size INTEGER, status TEXT, duration INTEGER);
 | 
			
		||||
         *        4              5              6
 | 
			
		||||
         */
 | 
			
		||||
        if (6 != argc) {
 | 
			
		||||
            LogError("Something wrong happened.\n");
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
        std::vector<SyncFileInfo> *info = (std::vector<SyncFileInfo> *)data;
 | 
			
		||||
        const unsigned long serialNumber = std::stoul(argv[0]);
 | 
			
		||||
        const std::string fileName = argv[1];
 | 
			
		||||
        const unsigned int fileSize = std::stoi(argv[4]);
 | 
			
		||||
        const unsigned int fileDuration = std::stoi(argv[6]);
 | 
			
		||||
        const time_t createTime_s = std::stol(argv[2]);
 | 
			
		||||
        const std::string typeString(argv[3]);
 | 
			
		||||
        const FileCreateType type = SqliteHandle::ConvertStringToFileType(typeString);
 | 
			
		||||
        const std::string statusString(argv[5]);
 | 
			
		||||
        const FileStatus status = SqliteHandle::ConvertStringToFileStatus(statusString);
 | 
			
		||||
        SyncFileInfo fileInfo(serialNumber, fileName, fileSize, fileDuration, createTime_s, type, status);
 | 
			
		||||
        (*info).push_back(fileInfo);
 | 
			
		||||
        return 0;
 | 
			
		||||
    };
 | 
			
		||||
    char *errMsg = nullptr;
 | 
			
		||||
    int rc = SQLITE_UNDEFINE;
 | 
			
		||||
    rc = sqlite3_exec(db, sql.c_str(), dbCallback, &info, &errMsg);
 | 
			
		||||
    if (SQLITE_OK != rc) {
 | 
			
		||||
        LogError("SQL error: %s\n", errMsg);
 | 
			
		||||
        sqlite3_free(errMsg);
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
std::string SqliteHandle::ConvertFileTypeToString(const FileCreateType &type)
 | 
			
		||||
{
 | 
			
		||||
    switch (type) {
 | 
			
		||||
    case FileCreateType::PIR:
 | 
			
		||||
        return "PIR";
 | 
			
		||||
        return FIEL_TYPE_PIR;
 | 
			
		||||
    case FileCreateType::MANUAL_TEST:
 | 
			
		||||
        return "MANUAL_TEST";
 | 
			
		||||
        return FIEL_TYPE_MANUAL_TEST;
 | 
			
		||||
    case FileCreateType::MANUAL_PHONE:
 | 
			
		||||
        return "MANUAL_PHONE";
 | 
			
		||||
        return FIEL_TYPE_MANUAL_PHONE;
 | 
			
		||||
    case FileCreateType::TIMED:
 | 
			
		||||
        return "TIMED";
 | 
			
		||||
        return FIEL_TYPE_TIMED;
 | 
			
		||||
    case FileCreateType::END:
 | 
			
		||||
    default:
 | 
			
		||||
        return "undefine";
 | 
			
		||||
        return FILE_TYPE_UNDEFINE;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
FileCreateType SqliteHandle::ConvertStringToFileType(const std::string &type)
 | 
			
		||||
{
 | 
			
		||||
    std::map<std::string, FileCreateType> fileTypeMap;
 | 
			
		||||
    fileTypeMap[FIEL_TYPE_PIR] = FileCreateType::PIR;
 | 
			
		||||
    fileTypeMap[FIEL_TYPE_MANUAL_TEST] = FileCreateType::MANUAL_TEST;
 | 
			
		||||
    fileTypeMap[FIEL_TYPE_MANUAL_PHONE] = FileCreateType::MANUAL_PHONE;
 | 
			
		||||
    fileTypeMap[FIEL_TYPE_TIMED] = FileCreateType::TIMED;
 | 
			
		||||
    fileTypeMap[FILE_TYPE_UNDEFINE] = FileCreateType::END;
 | 
			
		||||
    auto it = fileTypeMap.find(type);
 | 
			
		||||
    if (it != fileTypeMap.end()) {
 | 
			
		||||
        return it->second;
 | 
			
		||||
    }
 | 
			
		||||
    return FileCreateType::END;
 | 
			
		||||
}
 | 
			
		||||
bool SqliteHandle::UpdateFileStatus(sqlite3 *db, const unsigned long &key, const FileStatus &status)
 | 
			
		||||
{
 | 
			
		||||
    if (nullptr == db) {
 | 
			
		||||
| 
						 | 
				
			
			@ -267,7 +380,7 @@ bool SqliteHandle::UpdateFileStatus(sqlite3 *db, const unsigned long &key, const
 | 
			
		|||
    int rc = SQLITE_UNDEFINE;
 | 
			
		||||
    if (FileStatus::END != status) {
 | 
			
		||||
        std::stringstream sqlStream;
 | 
			
		||||
        sqlStream << "UPDATE " FILES_TABLE " SET " FILE_STATUS " = '" << ConvertFileStatus(status)
 | 
			
		||||
        sqlStream << "UPDATE " FILES_TABLE " SET " FILE_STATUS " = '" << ConvertFileStatusToString(status)
 | 
			
		||||
                  << "' WHERE " TABLE_KEY " = " << key << ";";
 | 
			
		||||
        LogInfo("Sql: %s\n", sqlStream.str().c_str());
 | 
			
		||||
        rc = sqlite3_exec(db, sqlStream.str().c_str(), nullptr, nullptr, &errMsg);
 | 
			
		||||
| 
						 | 
				
			
			@ -279,7 +392,7 @@ bool SqliteHandle::UpdateFileStatus(sqlite3 *db, const unsigned long &key, const
 | 
			
		|||
    }
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
std::string SqliteHandle::ConvertFileStatus(const FileStatus &status)
 | 
			
		||||
std::string SqliteHandle::ConvertFileStatusToString(const FileStatus &status)
 | 
			
		||||
{
 | 
			
		||||
    switch (status) {
 | 
			
		||||
    case FileStatus::RECORDING:
 | 
			
		||||
| 
						 | 
				
			
			@ -296,4 +409,18 @@ std::string SqliteHandle::ConvertFileStatus(const FileStatus &status)
 | 
			
		|||
    default:
 | 
			
		||||
        return "undefine";
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
FileStatus SqliteHandle::ConvertStringToFileStatus(const std::string &status)
 | 
			
		||||
{
 | 
			
		||||
    std::map<std::string, FileStatus> fileStatusMap;
 | 
			
		||||
    fileStatusMap[FILE_STATUS_BE_RECORDING] = FileStatus::RECORDING;
 | 
			
		||||
    fileStatusMap[FILE_STATUS_COMPLETE_RECORD] = FileStatus::FINISHED_RECORD;
 | 
			
		||||
    fileStatusMap[FILE_STATUS_SHOULD_BE_UPLOADED] = FileStatus::SHOULD_BE_UPLOAD;
 | 
			
		||||
    fileStatusMap[FILE_STATUS_UPLOADING] = FileStatus::UPLOADING;
 | 
			
		||||
    fileStatusMap[FILE_STATUS_UPLOADED] = FileStatus::UPLOADED;
 | 
			
		||||
    auto it = fileStatusMap.find(status);
 | 
			
		||||
    if (it != fileStatusMap.end()) {
 | 
			
		||||
        return it->second;
 | 
			
		||||
    }
 | 
			
		||||
    return FileStatus::END;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -16,9 +16,11 @@
 | 
			
		|||
#define SQLITE_HANDLE_H
 | 
			
		||||
#include "IFilesManager.h"
 | 
			
		||||
#include "sqlite3.h"
 | 
			
		||||
#include <map>
 | 
			
		||||
#include <memory>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <time.h>
 | 
			
		||||
#include <vector>
 | 
			
		||||
constexpr int SQLITE_UNDEFINE = -1;
 | 
			
		||||
class SqliteHandle
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			@ -30,6 +32,7 @@ public:
 | 
			
		|||
    void UnInit(void);
 | 
			
		||||
    unsigned long int CreateFiles(const unsigned int &count);
 | 
			
		||||
    bool SyncFile(const SyncFileInfo &info);
 | 
			
		||||
    bool SearchFiles(const std::vector<FileCreateType> &types, std::vector<SyncFileInfo> &info);
 | 
			
		||||
    bool Remove();
 | 
			
		||||
    bool Modified();
 | 
			
		||||
    bool Serach();
 | 
			
		||||
| 
						 | 
				
			
			@ -40,9 +43,13 @@ private:
 | 
			
		|||
    static bool UpdateFileName(sqlite3 *db, const unsigned long &key, const std::string &fileName);
 | 
			
		||||
    static bool UpdateFileSize(sqlite3 *db, const unsigned long &key, const unsigned long &fileSize);
 | 
			
		||||
    static bool UpdateFileType(sqlite3 *db, const unsigned long &key, const FileCreateType &type);
 | 
			
		||||
    static std::string ConvertFileType(const FileCreateType &type);
 | 
			
		||||
    static bool UpdateFileDuration(sqlite3 *db, const unsigned long &key, const unsigned int &duration);
 | 
			
		||||
    static bool SearchFiles(sqlite3 *db, const std::string &sql, std::vector<SyncFileInfo> &info);
 | 
			
		||||
    static std::string ConvertFileTypeToString(const FileCreateType &type);
 | 
			
		||||
    static FileCreateType ConvertStringToFileType(const std::string &type);
 | 
			
		||||
    static bool UpdateFileStatus(sqlite3 *db, const unsigned long &key, const FileStatus &status);
 | 
			
		||||
    static std::string ConvertFileStatus(const FileStatus &status);
 | 
			
		||||
    static std::string ConvertFileStatusToString(const FileStatus &status);
 | 
			
		||||
    static FileStatus ConvertStringToFileStatus(const std::string &status);
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    sqlite3 *mDb;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -167,7 +167,7 @@ TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_GetStorageFileList)
 | 
			
		|||
    MainThread::GetInstance()->Init();
 | 
			
		||||
    TestManager::ResetTimeOut(1000 * 3);
 | 
			
		||||
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
 | 
			
		||||
    MockGetStorageFileList();
 | 
			
		||||
    MockGetStorageFileList(StorageFileEvent::ALL);
 | 
			
		||||
    MainThread::GetInstance()->Runing();
 | 
			
		||||
}
 | 
			
		||||
// ../output_files/test/bin/HuntingCameraTest
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -110,10 +110,10 @@ void CameraHalTest::MockReportMediaStream(void)
 | 
			
		|||
{
 | 
			
		||||
    mTaskRuning = true;
 | 
			
		||||
    if (nullptr != mReadH264File) {
 | 
			
		||||
        IStartReadFile(mReadH264File, TEST_SOURCE_PATH "/support_test/video.h264");
 | 
			
		||||
        IStartReadFile(mReadH264File, TEST_SOURCE_PATH "/support_test/video_sync.h264");
 | 
			
		||||
    }
 | 
			
		||||
    if (nullptr != mReadG711aFile) {
 | 
			
		||||
        IStartReadFile(mReadG711aFile, TEST_SOURCE_PATH "/support_test/audio.g711a");
 | 
			
		||||
        IStartReadFile(mReadG711aFile, TEST_SOURCE_PATH "/support_test/audio_sync.g711a");
 | 
			
		||||
    }
 | 
			
		||||
    while (mTaskRuning) {
 | 
			
		||||
        std::unique_lock<std::mutex> lock(mMutex);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -257,7 +257,7 @@ TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_GetStorageFileList)
 | 
			
		|||
    IAppManager::GetInstance()->Init(mAppParam);
 | 
			
		||||
    IAppManager::GetInstance()->SetAppMonitor(monitor);
 | 
			
		||||
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
 | 
			
		||||
    AppManagerTestTool::MockGetStorageFileList();
 | 
			
		||||
    AppManagerTestTool::MockGetStorageFileList(StorageFileEvent::ALL);
 | 
			
		||||
    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
 | 
			
		||||
    IAppManager::GetInstance()->UnInit();
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -39,7 +39,7 @@ protected: // About http
 | 
			
		|||
    void MockGetCapability(void);
 | 
			
		||||
    void MockGetLockVideoStatus(void);
 | 
			
		||||
    void MockGetStorageInfo(void);
 | 
			
		||||
    void MockGetStorageFileList(void);
 | 
			
		||||
    void MockGetStorageFileList(const StorageFileEvent &event);
 | 
			
		||||
    void MockSetParamValue(const std::string &item, const std::string &value);
 | 
			
		||||
    void MockEnterRecorder(void);
 | 
			
		||||
    void MockAppPlayback(void);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -180,7 +180,7 @@ void AppManagerTestTool::MockGetStorageInfo(void)
 | 
			
		|||
    }
 | 
			
		||||
    ServersMock::GetInstance()->MockGetStorageInfo();
 | 
			
		||||
}
 | 
			
		||||
void AppManagerTestTool::MockGetStorageFileList(void)
 | 
			
		||||
void AppManagerTestTool::MockGetStorageFileList(const StorageFileEvent &event)
 | 
			
		||||
{
 | 
			
		||||
    std::shared_ptr<AppMonitorMock> mock = std::dynamic_pointer_cast<AppMonitorMock>(mAppMonitorMock);
 | 
			
		||||
    if (mock) {
 | 
			
		||||
| 
						 | 
				
			
			@ -188,7 +188,7 @@ void AppManagerTestTool::MockGetStorageFileList(void)
 | 
			
		|||
            .Times(ONLY_BE_CALLED_ONCE)
 | 
			
		||||
            .WillOnce(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
 | 
			
		||||
    }
 | 
			
		||||
    ServersMock::GetInstance()->MockGetStorageFileList();
 | 
			
		||||
    ServersMock::GetInstance()->MockGetStorageFileList(event);
 | 
			
		||||
}
 | 
			
		||||
void AppManagerTestTool::MockSetParamValue(const std::string &item, const std::string &value)
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -13,6 +13,7 @@
 | 
			
		|||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
#include "ServersMock.h"
 | 
			
		||||
#include "IAppManager.h"
 | 
			
		||||
#include "ILog.h"
 | 
			
		||||
#include "servers.h"
 | 
			
		||||
#include <cstdlib>
 | 
			
		||||
| 
						 | 
				
			
			@ -126,10 +127,30 @@ void ServersMock::MockGetStorageInfo(void)
 | 
			
		|||
    std::string mockRequest = mServerUrl + APP_GET_STORAGE_INFO;
 | 
			
		||||
    MockHttpGet(mockRequest);
 | 
			
		||||
}
 | 
			
		||||
void ServersMock::MockGetStorageFileList(void)
 | 
			
		||||
void ServersMock::MockGetStorageFileList(const StorageFileEvent &event)
 | 
			
		||||
{
 | 
			
		||||
    LogInfo("APP_GET_FILE_LIST test start.\n");
 | 
			
		||||
    std::string mockRequest = mServerUrl + APP_GET_FILE_LIST + "?folder=loop&start=0&end=99";
 | 
			
		||||
    std::string mockRequest;
 | 
			
		||||
    switch (event) {
 | 
			
		||||
    case StorageFileEvent::ALL:
 | 
			
		||||
        mockRequest = mServerUrl + APP_GET_FILE_LIST;
 | 
			
		||||
        break;
 | 
			
		||||
    case StorageFileEvent::LOOP:
 | 
			
		||||
        mockRequest = mServerUrl + APP_GET_FILE_LIST + "?folder=loop&start=0&end=99";
 | 
			
		||||
        break;
 | 
			
		||||
    case StorageFileEvent::EVENT:
 | 
			
		||||
        mockRequest = mServerUrl + APP_GET_FILE_LIST + "?folder=event&start=0&end=99";
 | 
			
		||||
        break;
 | 
			
		||||
    case StorageFileEvent::EMR:
 | 
			
		||||
        mockRequest = mServerUrl + APP_GET_FILE_LIST + "?folder=emr&start=0&end=99";
 | 
			
		||||
        break;
 | 
			
		||||
    case StorageFileEvent::PARK:
 | 
			
		||||
        mockRequest = mServerUrl + APP_GET_FILE_LIST + "?folder=park&start=0&end=99";
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    default:
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
    MockHttpGet(mockRequest);
 | 
			
		||||
}
 | 
			
		||||
void ServersMock::MockSetDateTime(void)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -14,6 +14,7 @@
 | 
			
		|||
 */
 | 
			
		||||
#ifndef SERVERS_MOCK_H
 | 
			
		||||
#define SERVERS_MOCK_H
 | 
			
		||||
#include "IAppManager.h"
 | 
			
		||||
#include "servers.h"
 | 
			
		||||
#include <memory>
 | 
			
		||||
class ServersMock
 | 
			
		||||
| 
						 | 
				
			
			@ -34,7 +35,7 @@ public:
 | 
			
		|||
    virtual void MockGetCapability(void);
 | 
			
		||||
    virtual void MockGetLockVideoStatus(void);
 | 
			
		||||
    virtual void MockGetStorageInfo(void);
 | 
			
		||||
    virtual void MockGetStorageFileList(void);
 | 
			
		||||
    virtual void MockGetStorageFileList(const StorageFileEvent &event);
 | 
			
		||||
    virtual void MockSetDateTime(void);
 | 
			
		||||
    virtual void MockSetTimeZone(void);
 | 
			
		||||
    virtual void MockSetParamValue(const std::string &item, const std::string &value);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user