RecordControlApplication/TcpRequestHandler.cpp

356 lines
12 KiB
C++
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "TcpRequestHandler.h"
#include "Channel.h"
#include "Constant.h"
#include "DatabaseManager.h"
#include "Json.h"
#include "Log.h"
#include "Tool.h"
#include <QCoreApplication>
#include <QDir>
#include <QProcess>
extern const QList<Channel*> channelList;
extern DatabaseManager* db;
TcpRequestHandler::TcpRequestHandler(QObject* parent)
: QObject(parent)
{
}
void TcpRequestHandler::service(TcpRequest* request, TcpResponse* response)
{
QString url = request->getUrl();
response->setUrl(url);
if (url == "/set_ip_addr") {
this->setIpaddr(request, response);
} else if (url == "/set_name") {
this->setName(request, response);
} else if (url == "/get_name") {
this->getName(request, response);
} else if (url == "/set_video_enc") {
this->setVideoEnc(request, response);
} else if (url == "/get_video_enc") {
this->getVideoEnc(request, response);
} else if (url == "/get_file_list") {
this->getFileList(request, response);
} else if (url == "/delete_file") {
this->deleteFile(request, response);
} else if (url == "/set_record_mode") {
this->setRecordMode(request, response);
} else if (url == "/set_playback_mode") {
this->setPlaybackMode(request, response);
} else if (url == "/reboot") {
this->reboot(request, response);
} else if (url == "/set_current_time") {
this->setCurrentTime(request, response);
} else {
response->error("访问路径未找到");
}
}
/**
* @brief 设置盒子的ip
* @param socket
*/
void TcpRequestHandler::setIpaddr(TcpRequest* request, TcpResponse* reponse)
{
QVariantMap params = request->getBodyParams();
QVariantMap netMap = Json::loadFile(Constant::NetConfigPath).toMap();
if (params.contains("ip"))
netMap["ip"] = params.value("ip");
if (params.contains("mask"))
netMap["mask"] = params.value("mask");
if (params.contains("gateway"))
netMap["gateway"] = params.value("gateway");
Json::saveFile(netMap, Constant::NetConfigPath);
reponse->success("修改ip地址成功请重新连接");
Tool::writeCom(Constant::NetScriptPath);
}
/**
* @brief 设置盒子的名称
* @param socket
*/
void TcpRequestHandler::setName(TcpRequest* request, TcpResponse* reponse)
{
QVariantMap params = request->getBodyParams();
if (params.isEmpty() || !params.contains("name")) {
Log::error("setName params error, missing param \"name\"");
reponse->error("名称不能为空");
return;
}
QVariantMap cfg = Json::loadFile(Constant::ConfigurationPath).toMap();
cfg["name"] = params.value("name");
Json::saveFile(cfg, Constant::ConfigurationPath);
reponse->success("设置名称成功");
}
/**
* @brief 获取盒子名称
*/
void TcpRequestHandler::getName(TcpRequest* request, TcpResponse* reponse)
{
QVariantMap config = Json::loadFile(Constant::ConfigurationPath).toMap();
QString name = config["name"].toString();
QString data = QString("url=get_name\r\nstatus=success\r\nname=%1").arg(name);
reponse->success("获取盒子名称成功", name);
}
/**
* @brief 设置视频录制参数
* @param socket
*/
void TcpRequestHandler::setVideoEnc(TcpRequest* request, TcpResponse* reponse)
{
QVariantMap params = request->getBodyParams();
if (params.isEmpty() || !params.contains("interface")) {
Log::error("setVideoEncode params error, missing param \"interface\"");
reponse->error("缺少接口参数");
return;
}
QString interface = params.value("interface").toString();
Channel* chn = nullptr;
for (Channel* c : channelList) {
if (c->channelName == interface)
chn = c;
}
if (chn) {
// 修改分辨率和帧率
// chn->videoEncoder->setData(params);
// 更新配置文件
QVariantMap cfg = Json::loadFile(Constant::ConfigurationPath).toMap();
QVariantList interfaces = cfg.value("interface").toList();
for (int i = 0; i < interfaces.length(); i++) {
QVariantMap item = interfaces.at(i).toMap();
if (item.value("name").toString() == interface) {
QVariantMap encV = item.value("encV").toMap();
if (params.contains("width"))
encV["width"] = params.value("width");
if (params.contains("height"))
encV["height"] = params.value("height");
if (params.contains("framerate"))
encV["framerate"] = params.value("framerate");
item["encV"] = encV;
interfaces.replace(i, item);
}
}
cfg["interface"] = interfaces;
Json::saveFile(cfg, Constant::ConfigurationPath);
reponse->success("修改录制参数成功,重启后生效");
} else {
Log::error("set video encode params error, no such interface {}", interface.toStdString());
reponse->error(QString("无对应接口: %1").arg(interface));
}
}
/**
* @brief 获取视频录制参数
* @param socket
*/
void TcpRequestHandler::getVideoEnc(TcpRequest* request, TcpResponse* reponse)
{
QVariantMap params = request->getBodyParams();
if (params.contains("interface")) {
QString interface = params.value("interface").toString();
QVariantMap cfg = Json::loadFile(Constant::ConfigurationPath).toMap();
QVariantList interfaces = cfg.value("interface").toList();
QVariantMap targetInterfaceCfg;
for (int i = 0; i < interfaces.length(); i++) {
QVariantMap item = interfaces.at(i).toMap();
if (item.value("name").toString() == interface) {
targetInterfaceCfg = item;
}
}
QVariantMap encodeV = targetInterfaceCfg.value("encV").toMap();
reponse->success("", encodeV);
} else {
Log::error("getVideoEnc params error, missing param \"interface\"");
reponse->error("缺少接口参数");
return;
}
}
/**
* @brief 获取文件列表
* @param socket
*/
void TcpRequestHandler::getFileList(TcpRequest* request, TcpResponse* reponse)
{
QVariantMap params = request->getBodyParams();
QString chn = params.value("interface").toString();
if (chn != Constant::MainChannel && chn != Constant::SecondaryChannel) {
Log::error("getFileList params error, error param \"interface\"");
reponse->error("接口参数错误");
return;
}
DatabaseManager::Channel channel = chn == Constant::MainChannel ? DatabaseManager::MainChannel
: DatabaseManager::SecondaryChannel;
QString interface = params.value("interface").toString();
QList<DatabaseManager::File> fileList = db->get(channel);
QVariantList 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);
}
reponse->success("获取文件成功", QVariant::fromValue(result));
}
/**
* @brief 删除文件
* @param socket
*/
void TcpRequestHandler::deleteFile(TcpRequest* request, TcpResponse* reponse)
{
QVariantMap params = request->getBodyParams();
QString chn = params.value("interface").toString();
QString filename = params.value("filename").toString();
if (chn != Constant::MainChannel && chn != Constant::SecondaryChannel) {
Log::error("deleteFile params error, error params \"interface\" or \"filename\"");
reponse->error("通道参数错误");
return;
}
if (filename.isEmpty()) {
Log::error("deleteFile params error, missing params \"filename\"");
reponse->error("缺少文件名参数");
return;
}
// 先查找是否存在物理文件,再查找数据库中是否有对应记,然后一一删除
// 删除对应的视频文件
QString filePath = QString("%1/%2/%3").arg(Constant::VideoPath).arg(chn).arg(filename);
QFile video(filePath);
if (video.exists()) {
// 删除数据库记录
DatabaseManager::Channel channel = chn == Constant::MainChannel
? DatabaseManager::MainChannel
: DatabaseManager::SecondaryChannel;
bool ret = db->remove(channel, filename);
if (!ret) {
reponse->error("文件不存在");
return;
} else {
video.remove();
Log::info("remove video {}", filePath.toStdString());
reponse->success("删除成功");
}
} else {
Log::error("error, file: {} dont exist", filePath.toStdString());
reponse->error("文件不存在");
}
}
/**
* @brief 设置录制模式,一路录制/两路录制,只修改配置文件,重启之后生效
* @param socket
*/
void TcpRequestHandler::setRecordMode(TcpRequest* request, TcpResponse* reponse)
{
QVariantMap params = request->getBodyParams();
if (!params.contains("recordMode")) {
Log::error("setRecordMode params error, missing param \"recordMode\"");
reponse->error("缺少录制模式参数");
return;
}
int recordMode = params.value("recordMode").toInt();
if (recordMode != Constant::OneChannelRecord
&& recordMode != Constant::TwoChannelRecord
&& recordMode != Constant::NoChannelRecord) {
Log::error("setRecordMode params error, unkown param \"recordMode\": {}", recordMode);
reponse->error("录制参数错误");
return;
}
QVariantMap cfg = Json::loadFile(Constant::ConfigurationPath).toMap();
int mode = cfg["recordMode"].toInt();
if (recordMode == mode) {
Log::info("record mode has not changed, there is nothing need to do");
reponse->success("录制参数无需改变");
return;
}
// 更新配置文件
cfg["recordMode"] = recordMode;
Json::saveFile(cfg, Constant::ConfigurationPath);
reponse->success("设置录制参数成功, 重启后生效");
}
/**
* @brief 设置回放模式, 一路回放/两路同时回放,只修改配置文件,重启之后生效
* @param socket
*/
void TcpRequestHandler::setPlaybackMode(TcpRequest* request, TcpResponse* reponse)
{
QVariantMap params = request->getBodyParams();
if (!params.contains("playbackMode")) {
Log::error("setPlaybackMode params error, missing param \"playbackmode\"");
reponse->error("缺少回放模式参数");
return;
}
int playbackMode = params.value("playbackMode").toInt();
if (playbackMode != Constant::OneChannelPlayback
&& playbackMode != Constant::TwoChannelPlayback) {
Log::error("setPlayBack params error, unkown param \"playbackMode\": {}", playbackMode);
reponse->error("回放模式参数错误");
return;
}
QVariantMap cfg = Json::loadFile(Constant::ConfigurationPath).toMap();
int mode = cfg["playbackMode"].toInt();
;
if (playbackMode == mode) {
Log::info("record mode has not changed, there is nothing need to do");
reponse->success("回放参数无需改变");
return;
}
// 跟新配置文件
cfg["playbackMode"] = playbackMode;
Json::saveFile(cfg, Constant::ConfigurationPath);
reponse->success("修改回放模式成功,重启后生效");
}
/**
* @brief 重启程序
* @param socket
*/
void TcpRequestHandler::reboot(TcpRequest* request, TcpResponse* reponse)
{
// 先停止所有的录制
for (Channel* chn : channelList) {
chn->stopRecord();
}
reponse->success("重启成功");
qApp->quit();
}
/**
* @brief 设置当前开发板时间,进行时间同步
* @param socket
*/
void TcpRequestHandler::setCurrentTime(TcpRequest* request, TcpResponse* reponse)
{
QVariantMap params = request->getBodyParams();
if (!params.contains("time")) {
Log::error("setCurrentTime params error, missing params \"time\"");
reponse->error("缺少时间参数");
return;
}
QString time = params.value("time").toString();
QString cmd = QString("%1 -s time \"%2\"").arg(Constant::TimeSciptPath).arg(time);
QString result = Tool::writeCom(cmd);
// 修改成功会返回类似信息, "Tue Mar 5 15:51:18 CST 2024"
if (result.isEmpty()) {
reponse->error("时间同步失败");
return;
} else {
reponse->success("时间同步成功");
}
}