修改数据库表结构

This commit is contained in:
zc 2024-08-22 22:34:58 -07:00
parent 97f22cf302
commit 2782c6867c
7 changed files with 99 additions and 72 deletions

View File

@ -45,7 +45,7 @@ Channel::Channel(QObject* parent)
if (lineIn == nullptr) {
lineIn = Link::create("InputAlsa");
QVariantMap dataIn;
dataIn["path"] = "hw:0,0";
dataIn["path"] = "hw:0";
dataIn["channels"] = 2;
lineIn->start(dataIn);
}
@ -194,53 +194,62 @@ void Channel::init()
void Channel::startRecord()
{
// 记录本次录制开始时间以及当前视频录制的开始时间
QString curTime = QDateTime::currentDateTime().toString("yyyyMMddhhmmss");
startTime = curTime;
currentTime = curTime;
QString time = QDateTime::currentDateTime().toString("yyyyMMddhhmmss");
startTime = time;
curTime = time;
QVariantMap dataRecord;
QString path = QString("%1/%2/%3_%d.mp4").arg(Constant::VideoPath).arg(channelName).arg(curTime);
dataRecord["path"] = path;
record->start(dataRecord);
isRecord = true;
Log::info("{} start recording...", channelName.toStdString());
Log::info("open done {}", path.toStdString());
// 显示录制状态水印
overlay->setData(recordOverlay);
}
/**
* @brief
* @brief
* @param msg
* @param data
*/
void Channel::onNewEvent(QString msg, QVariant data)
{
if (msg == "newSegment") {
QString datetime = curTime;
// 重新设置视频的录制起始时间
curTime = QDateTime::currentDateTime().toString("yyyyMMddhhmmss");
segmentId = data.toInt();
// 将上一次的文件信息存入数据库中
DatabaseManager::File file;
file.channel = channelName == Constant::MainChannel
// 修改文件名本次录制起始时间_%d ==> 当前视频录制起始时间
QString filename = QString("%1_%2.mp4").arg(startTime).arg(segmentId - 1);
QString newFilename = QString("%1/%2/%3.mp4").arg(Constant::VideoPath).arg(channelName).arg(datetime);
QString path = QString("%1/%2/%3").arg(Constant::VideoPath).arg(channelName).arg(filename);
QFile file(path);
if (!file.rename(newFilename)) {
Log::error("rename file name failed in function onNewEvent, old filename: {}, target filename: {} , channel name: {},reason: {}",
path.toStdString(),
newFilename.toStdString(),
channelName.toStdString(),
file.errorString().toStdString());
return;
}
// 将录制完的文件信息保存到数据库
DatabaseManager::File fileInfo;
fileInfo.channel = channelName == Constant::MainChannel
? DatabaseManager::MainChannel
: DatabaseManager::SecondaryChannel;
// 设置当前视频的录制时间信息
file.year = currentTime.mid(0, 4);
file.month = currentTime.mid(4, 2);
file.day = currentTime.mid(6, 2);
file.time = currentTime.mid(8, 6);
// 设置当前视频的文件名格式本次录制开始时间_第几次分片
file.filename = QString("%1_%2.mp4").arg(startTime).arg(segmentId - 1);
if (db->insert(file)) {
fileInfo.datetime = QDateTime::fromString(datetime, "yyyyMMddhhmmss").toString("yyyy-MM-dd hh:mm:ss");
fileInfo.filename = QString("%1.mp4").arg(datetime);
if (db->insert(fileInfo)) {
Log::info("insert one record into database success, name: {}, channel: {}",
file.filename.toStdString(),
fileInfo.filename.toStdString(),
channelName.toStdString());
} else {
Log::error("insert one record into database failed, name: {}, channel: {}",
file.filename.toStdString(),
fileInfo.filename.toStdString(),
channelName.toStdString());
}
// 更新当前录制录制视频的时间
currentTime = QDateTime::currentDateTime().toString("yyyyMMddhhmmss");
emit appendOneVideo(channelName);
}
}
@ -252,17 +261,41 @@ void Channel::stopRecord()
{
Log::info("{} stop recording...", channelName.toStdString());
record->stop(true);
// 修改文件名本次录制起始时间_%d ==> 当前视频录制起始时间
QString filename = QString("%1_%2.mp4").arg(startTime).arg(segmentId);
QString newFilename = QString("%1/%2/%3.mp4").arg(Constant::VideoPath).arg(channelName).arg(curTime);
QString path = QString("%1/%2/%3").arg(Constant::VideoPath).arg(channelName).arg(filename);
QFile file(path);
if (!file.rename(newFilename)) {
Log::error("rename file name failed in function onNewEvent, old filename: {}, target filename: {} , channel name: {},reason: {}",
path.toStdString(),
newFilename.toStdString(),
channelName.toStdString(),
file.errorString().toStdString());
return;
}
// 将录制文件的信息存入数据库
DatabaseManager::File file;
file.channel = channelName == Constant::MainChannel
DatabaseManager::File fileInfo;
fileInfo.channel = channelName == Constant::MainChannel
? DatabaseManager::MainChannel
: DatabaseManager::SecondaryChannel;
file.year = currentTime.mid(0, 4);
file.month = currentTime.mid(4, 2);
file.day = currentTime.mid(6, 2);
file.time = currentTime.mid(8, 6);
file.filename = QString("%1_%2.mp4").arg(startTime).arg(segmentId);
fileInfo.datetime = QDateTime::fromString(curTime, "yyyyMMddhhmmss").toString("yyyy-MM-dd hh:mm:ss");
fileInfo.filename = QString("%1.mp4").arg(curTime);
if (db->insert(fileInfo)) {
Log::info("insert one record into database success, name: {}, channel: {}",
fileInfo.filename.toStdString(),
channelName.toStdString());
} else {
Log::error("insert one record into database failed, name: {}, channel: {}",
fileInfo.filename.toStdString(),
channelName.toStdString());
}
// 更新当前录制录制视频的时间
emit appendOneVideo(channelName);
overlay->setData(norecordOverlay);
// 重置时间
startTime = "";
curTime = "";
}
/**

View File

@ -63,7 +63,7 @@ public:
int duration = 1 * 60 * 1000; // 单个视频时长
bool isRecord = false;
QString startTime; // 本次录制文件的开始时间
QString currentTime; // 当前录制文件的开始时间
QString curTime; // 当前录制文件的开始时间
int segmentId = 0;
LinkObject* overlay; // 水印,提示是否在录制视频
QVariantMap recordOverlay; // 录制状态下的水印参数

View File

@ -34,7 +34,7 @@ void CheckStorageThread::run()
// 从数据库中取出前两条数据,找到相关的文件删除
QList<DatabaseManager::File> fileList = db->getTopTwo();
for (auto& file : fileList) {
QString filename = file.time;
QString filename = file.filename;
QString channel = file.channel == DatabaseManager::MainChannel
? Constant::MainChannel
: Constant::SecondaryChannel;
@ -55,7 +55,7 @@ void CheckStorageThread::run()
Log::info("remove file {} success", path.toStdString());
}
// 从数据库清除这条记录
db->remove(file.channel, file.time);
db->remove(file.channel, file.filename);
}
} else {
emit diskNotFull();

View File

@ -66,18 +66,15 @@ void DatabaseManager::close()
bool DatabaseManager::insert(File file)
{
QSqlQuery query;
query.prepare("insert into file (channel, year, month, day, time, filename) values (?, ?, ?, ?, ?, ?)");
query.bindValue(0, file.channel);
query.bindValue(1, file.year);
query.bindValue(2, file.month);
query.bindValue(3, file.day);
query.bindValue(4, file.time);
query.bindValue(5, file.filename);
query.prepare("insert into file (channel, datetime, filename) values (?, ?, ?)");
query.addBindValue(file.channel);
query.addBindValue(file.datetime);
query.addBindValue(file.filename);
if (query.exec()) {
return true;
} else {
Log::error("insert one record into database failed, reason: {}",
query.lastError().databaseText().toStdString());
query.lastError().databaseText().toStdString() + "," + query.lastError().driverText().toStdString());
return false;
}
}
@ -91,29 +88,24 @@ QList<DatabaseManager::File> DatabaseManager::get(QVariantMap params)
{
QList<DatabaseManager::File> result;
QSqlQuery query;
QString sql = "select * from file where channel=? and year = ? and month = ? and day = ?";
QString sql = "select * from file where channel=? and datetime like \'%1\'";
QString year = params.value("year").toString();
QString month = params.value("month").toString();
QString day = params.value("day").toString();
Channel chn = static_cast<Channel>(params.value("channel").toInt());
query.prepare(sql);
query.bindValue(0, chn);
query.bindValue(1, year);
query.bindValue(2, month);
query.bindValue(3, day);
if (year.isEmpty() || month.isEmpty() || day.isEmpty() || (chn != MainChannel && chn != SecondaryChannel)) {
Log::error("select from database error, params error");
return result;
}
query.prepare(sql);
query.bindValue(0, chn);
query.bindValue(1, QString("%1-%2-%3%"));
if (query.exec()) {
while (query.next()) {
DatabaseManager::File file;
file.id = query.value("id").toInt();
file.year = query.value("year").toString();
file.month = query.value("month").toString();
file.day = query.value("day").toString();
file.channel = static_cast<DatabaseManager::Channel>(query.value("channel").toInt());
file.time = query.value("time").toString();
file.datetime = query.value("datetime").toString();
file.filename = query.value("filename").toString();
result.push_back(file);
}
@ -141,11 +133,8 @@ QList<DatabaseManager::File> DatabaseManager::get(DatabaseManager::Channel chn)
while (query.next()) {
DatabaseManager::File file;
file.id = query.value("id").toInt();
file.year = query.value("year").toString();
file.month = query.value("month").toString();
file.day = query.value("day").toString();
file.channel = static_cast<DatabaseManager::Channel>(query.value("channel").toInt());
file.time = query.value("time").toString();
file.datetime = query.value("datetime").toString();
file.filename = query.value("filename").toString();
result.push_back(file);
}
@ -155,7 +144,6 @@ QList<DatabaseManager::File> DatabaseManager::get(DatabaseManager::Channel chn)
}
return result;
}
/**
* @brief
*/
@ -168,11 +156,8 @@ QList<DatabaseManager::File> DatabaseManager::getTopTwo()
while (query.next()) {
DatabaseManager::File file;
file.id = query.value("id").toInt();
file.year = query.value("year").toString();
file.month = query.value("month").toString();
file.day = query.value("day").toString();
file.channel = static_cast<DatabaseManager::Channel>(query.value("channel").toInt());
file.time = query.value("time").toString();
file.datetime = query.value("datetime").toString();
file.filename = query.value("filename").toString();
result.push_back(file);
}
@ -191,7 +176,11 @@ QStringList DatabaseManager::getAllYears(Channel chn)
{
QStringList result;
QSqlQuery query;
query.prepare("select distinct year from file");
query.prepare(R"(
select distinct strftime('%Y', datetime) as year
from file
order by year
)");
if (query.exec()) {
while (query.next()) {
QString year = query.value(0).toString();
@ -214,7 +203,12 @@ QStringList DatabaseManager::getAllMonths(Channel chn, QString year)
{
QStringList result;
QSqlQuery query;
query.prepare("select distinct month from file where channel = ? and year=?");
query.prepare(R"(
select distinct strftime('%m', datetime) as month
from file
where channel = ? and strftime('%Y', datetime) = '?'
order by month
)");
query.bindValue(0, chn);
query.bindValue(1, year);
if (query.exec()) {
@ -240,7 +234,14 @@ QStringList DatabaseManager::getAllDays(Channel chn, QString year, QString month
{
QStringList result;
QSqlQuery query;
query.prepare("select distinct day from file where channel = ? and year=? and month=?");
query.prepare(R"(
select distinct strftime('%d', datetime) as day
from file
where channel = ?
and strftime('%Y', datetime) = '?'
and strftime('%m', datetime) = '?'
order by day
)");
query.bindValue(0, chn);
query.bindValue(1, year);
query.bindValue(2, month);

View File

@ -13,11 +13,8 @@ public:
struct File {
int id; // id
Channel channel; // 通道
QString year; // 年
QString month; // 月
QString day; // 日
QString time; // 时分秒hh:mm:ss
QString filename; // 真实路径
QString datetime; // 时间 yyyy-MM-dd hh:mm:ss
QString filename; // 文件名
};
static DatabaseManager* getInstace();
~DatabaseManager();
@ -30,7 +27,6 @@ public:
bool remove(DatabaseManager::Channel chn, QString name);
QList<DatabaseManager::File> getTopTwo();
QList<DatabaseManager::File> get(QVariantMap params);
QList<DatabaseManager::File> get(Channel chn);
QStringList getAllYears(Channel chn);

View File

@ -155,7 +155,7 @@ void Menu::renderContents()
for (int i = 0; i < contentList.length(); i++) {
// 文件名格式: hhmmss只将时分秒显示到界面上
DatabaseManager::File file = contentList.at(i);
QString text = QString("%1:%2:%3").arg(file.time.mid(0, 2)).arg(file.time.mid(2, 2)).arg(file.time.mid(4, 2));
QString text = QDateTime::fromString(file.datetime, "yyyy-MM-dd hh:mm:ss").toString("hh:mm:ss");
QPushButton* btn = new QPushButton(text, ui->scrollArea);
btn->setProperty("name", file.filename);
btn->setMinimumHeight(CONTENT_CELL_HEIGHT);

View File

@ -193,13 +193,10 @@ void TcpRequestHandler::getFileList(TcpRequest* request, TcpResponse* reponse)
: DatabaseManager::SecondaryChannel;
QString interface = params.value("interface").toString();
QList<DatabaseManager::File> fileList = db->get(channel);
QVariantList result;
QStringList result;
for (const DatabaseManager::File& file : fileList) {
QString time = file.year + file.month + file.day + file.time;
QVariantMap fileInfo;
fileInfo["time"] = time;
fileInfo["filename"] = file.filename;
result.push_back(fileInfo);
result.push_back(file.filename);
}
reponse->success("获取文件成功", QVariant::fromValue(result));
}