RecordControlApplication/TcpRequestHandler.cpp

353 lines
12 KiB
C++
Raw Permalink Normal View History

#include "TcpRequestHandler.h"
2024-03-04 16:22:40 +08:00
#include "Channel.h"
#include "Constant.h"
2024-08-12 11:26:42 +08:00
#include "DatabaseManager.h"
#include "Json.h"
2024-03-04 16:22:40 +08:00
#include "Log.h"
#include "Tool.h"
#include <QCoreApplication>
2024-01-18 15:41:43 +08:00
#include <QDir>
#include <QProcess>
2024-03-04 16:22:40 +08:00
extern const QList<Channel*> channelList;
2024-08-12 11:26:42 +08:00
extern DatabaseManager* db;
2024-01-18 15:41:43 +08:00
TcpRequestHandler::TcpRequestHandler(QObject* parent)
2024-01-18 15:41:43 +08:00
: QObject(parent)
{
}
void TcpRequestHandler::service(TcpRequest* request, TcpResponse* response)
2024-01-18 15:41:43 +08:00
{
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("访问路径未找到");
}
2024-01-18 15:41:43 +08:00
}
/**
2024-03-04 16:22:40 +08:00
* @brief ip
2024-01-18 15:41:43 +08:00
* @param socket
*/
void TcpRequestHandler::setIpaddr(TcpRequest* request, TcpResponse* reponse)
2024-01-18 15:41:43 +08:00
{
QVariantMap params = request->getBodyParams();
2024-03-04 16:22:40 +08:00
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");
2024-03-04 16:22:40 +08:00
Json::saveFile(netMap, Constant::NetConfigPath);
reponse->success("修改ip地址成功请重新连接");
2024-03-04 16:22:40 +08:00
Tool::writeCom(Constant::NetScriptPath);
2024-01-18 15:41:43 +08:00
}
/**
2024-03-04 16:22:40 +08:00
* @brief
2024-01-18 15:41:43 +08:00
* @param socket
*/
void TcpRequestHandler::setName(TcpRequest* request, TcpResponse* reponse)
{
QVariantMap params = request->getBodyParams();
if (params.isEmpty() || !params.contains("name")) {
2024-03-04 16:22:40 +08:00
Log::error("setName params error, missing param \"name\"");
reponse->error("名称不能为空");
return;
}
2024-03-04 16:22:40 +08:00
QVariantMap cfg = Json::loadFile(Constant::ConfigurationPath).toMap();
cfg["name"] = params.value("name");
2024-03-04 16:22:40 +08:00
Json::saveFile(cfg, Constant::ConfigurationPath);
reponse->success("设置名称成功");
}
2024-03-04 16:22:40 +08:00
/**
* @brief
*/
void TcpRequestHandler::getName(TcpRequest* request, TcpResponse* reponse)
2024-01-18 15:41:43 +08:00
{
2024-03-04 16:22:40 +08:00
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);
2024-01-18 15:41:43 +08:00
}
/**
2024-03-04 16:22:40 +08:00
* @brief
2024-01-18 15:41:43 +08:00
* @param socket
*/
void TcpRequestHandler::setVideoEnc(TcpRequest* request, TcpResponse* reponse)
2024-01-18 15:41:43 +08:00
{
QVariantMap params = request->getBodyParams();
if (params.isEmpty() || !params.contains("interface")) {
2024-03-04 16:22:40 +08:00
Log::error("setVideoEncode params error, missing param \"interface\"");
reponse->error("缺少接口参数");
return;
}
QString interface = params.value("interface").toString();
2024-03-04 16:22:40 +08:00
Channel* chn = nullptr;
for (Channel* c : channelList) {
if (c->channelName == interface)
chn = c;
}
if (chn) {
// 修改分辨率和帧率
// chn->videoEncoder->setData(params);
2024-03-04 16:22:40 +08:00
// 更新配置文件
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);
}
}
2024-03-04 16:22:40 +08:00
cfg["interface"] = interfaces;
Json::saveFile(cfg, Constant::ConfigurationPath);
reponse->success("修改录制参数成功,重启后生效");
2024-03-04 16:22:40 +08:00
} else {
Log::error("set video encode params error, no such interface {}", interface.toStdString());
reponse->error(QString("无对应接口: %1").arg(interface));
}
}
2024-03-04 16:22:40 +08:00
/**
* @brief
* @param socket
*/
void TcpRequestHandler::getVideoEnc(TcpRequest* request, TcpResponse* reponse)
{
QVariantMap params = request->getBodyParams();
if (params.contains("interface")) {
QString interface = params.value("interface").toString();
2024-03-04 16:22:40 +08:00
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 {
2024-03-04 16:22:40 +08:00
Log::error("getVideoEnc params error, missing param \"interface\"");
reponse->error("缺少接口参数");
2024-01-18 15:41:43 +08:00
return;
}
}
/**
2024-03-04 16:22:40 +08:00
* @brief
2024-01-18 15:41:43 +08:00
* @param socket
*/
void TcpRequestHandler::getFileList(TcpRequest* request, TcpResponse* reponse)
2024-01-18 15:41:43 +08:00
{
QVariantMap params = request->getBodyParams();
2024-08-12 11:26:42 +08:00
QString chn = params.value("interface").toString();
if (chn != Constant::MainChannel && chn != Constant::SecondaryChannel) {
2024-08-12 11:26:42 +08:00
Log::error("getFileList params error, error param \"interface\"");
reponse->error("接口参数错误");
2024-01-18 15:41:43 +08:00
return;
}
2024-08-12 11:26:42 +08:00
DatabaseManager::Channel channel = chn == Constant::MainChannel ? DatabaseManager::MainChannel
: DatabaseManager::SecondaryChannel;
QString interface = params.value("interface").toString();
2024-08-12 11:26:42 +08:00
QList<DatabaseManager::File> fileList = db->get(channel);
2024-08-23 13:34:58 +08:00
QStringList result;
2024-08-12 11:26:42 +08:00
for (const DatabaseManager::File& file : fileList) {
QVariantMap fileInfo;
2024-08-23 13:34:58 +08:00
result.push_back(file.filename);
}
reponse->success("获取文件成功", QVariant::fromValue(result));
2024-01-18 15:41:43 +08:00
}
/**
2024-03-04 16:22:40 +08:00
* @brief
2024-01-18 15:41:43 +08:00
* @param socket
*/
void TcpRequestHandler::deleteFile(TcpRequest* request, TcpResponse* reponse)
2024-01-18 15:41:43 +08:00
{
QVariantMap params = request->getBodyParams();
2024-08-12 11:26:42 +08:00
QString chn = params.value("interface").toString();
QString filename = params.value("filename").toString();
if (chn != Constant::MainChannel && chn != Constant::SecondaryChannel) {
2024-08-12 11:26:42 +08:00
Log::error("deleteFile params error, error params \"interface\" or \"filename\"");
reponse->error("通道参数错误");
return;
}
2024-08-12 11:26:42 +08:00
if (filename.isEmpty()) {
Log::error("deleteFile params error, missing params \"filename\"");
reponse->error("缺少文件名参数");
return;
}
// 先查找是否存在物理文件,再查找数据库中是否有对应记,然后一一删除
// 删除对应的视频文件
2024-08-12 11:26:42 +08:00
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 {
2024-03-04 16:22:40 +08:00
Log::error("error, file: {} dont exist", filePath.toStdString());
reponse->error("文件不存在");
}
}
2024-03-04 16:22:40 +08:00
/**
* @brief /
* @param socket
*/
void TcpRequestHandler::setRecordMode(TcpRequest* request, TcpResponse* reponse)
{
QVariantMap params = request->getBodyParams();
2024-03-04 16:22:40 +08:00
if (!params.contains("recordMode")) {
Log::error("setRecordMode params error, missing param \"recordMode\"");
reponse->error("缺少录制模式参数");
2024-03-04 16:22:40 +08:00
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("录制参数错误");
2024-03-04 16:22:40 +08:00
return;
}
2024-03-04 16:22:40 +08:00
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("录制参数无需改变");
2024-03-04 16:22:40 +08:00
return;
}
// 更新配置文件
cfg["recordMode"] = recordMode;
Json::saveFile(cfg, Constant::ConfigurationPath);
reponse->success("设置录制参数成功, 重启后生效");
2024-03-04 16:22:40 +08:00
}
/**
* @brief /
* @param socket
*/
void TcpRequestHandler::setPlaybackMode(TcpRequest* request, TcpResponse* reponse)
2024-03-04 16:22:40 +08:00
{
QVariantMap params = request->getBodyParams();
2024-03-04 16:22:40 +08:00
if (!params.contains("playbackMode")) {
Log::error("setPlaybackMode params error, missing param \"playbackmode\"");
reponse->error("缺少回放模式参数");
2024-03-04 16:22:40 +08:00
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;
2024-03-04 16:22:40 +08:00
}
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;
}
// 跟新配置文件
2024-03-04 16:22:40 +08:00
cfg["playbackMode"] = playbackMode;
Json::saveFile(cfg, Constant::ConfigurationPath);
reponse->success("修改回放模式成功,重启后生效");
2024-01-18 15:41:43 +08:00
}
/**
* @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("时间同步成功");
}
2024-08-12 11:26:42 +08:00
}