RecordControlApplication/TcpRequestHandler.cpp

338 lines
11 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 "Json.h"
#include "Log.h"
#include "Tool.h"
#include <QDir>
#include <QProcess>
extern const QList<Channel*> channelList;
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();
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();
if (params.isEmpty() || !params.contains("interface")) {
Log::error("getFileList params error, missing param \"interface\"");
reponse->error("缺少接口参数");
return;
}
QString interface = params.value("interface").toString();
QString videoPath = QString("%1/%2").arg(Constant::VideoPath).arg(interface);
QStringList videoList = Tool::getFileList(videoPath);
// 复制两份测试用
QStringList copy = videoList;
QStringList copy1 = videoList;
videoList.append(copy);
videoList.append(copy1);
reponse->success("获取文件成功", videoList);
}
/**
* @brief 删除文件
* @param socket
*/
void TcpRequestHandler::deleteFile(TcpRequest* request, TcpResponse* reponse)
{
QVariantMap params = request->getBodyParams();
if (!params.contains("interface") || !params.contains("filename")) {
Log::error("deleteFile params error, missing params \"interface\" or \"filename\"");
reponse->error("缺少端口或文件名");
return;
}
QString interface = params.value("interface").toString();
QString fileName = params.value("filename").toString();
// xxx.mp4 ==> xxx.jpg
QString snapName = fileName.split(".")[0] + ".jpg";
QString filePath = QString("%1/%2/%3").arg(Constant::VideoPath).arg(interface).arg(fileName);
QFile video(filePath);
if (video.exists()) {
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("重启成功");
// 退出程序等待后台运行的shell脚本重启程序
exit(0);
}
/**
* @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("/link/bin/rtc -s time %1").arg(time);
QString result = Tool::writeCom(cmd);
// 修改成功会返回类似信息, "Tue Mar 5 15:51:18 CST 2024"
if (result.isEmpty()) {
reponse->error("时间同步失败");
return;
} else {
reponse->success("时间同步成功");
}
}