#include "TcpRequestHandler.h" #include "Channel.h" #include "Constant.h" #include "Json.h" #include "Log.h" #include "Tool.h" #include #include extern const QList 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("修改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(); 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); // 如果最后一个文件名是当前正在录制的文件名,则去除 if (videoList.last() == Channel::curRecordFilename) { videoList.pop_back(); } 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("时间同步成功"); } }