2024-01-18 15:41:43 +08:00
|
|
|
|
#include "TcpController.h"
|
2024-03-04 16:22:40 +08:00
|
|
|
|
#include "Channel.h"
|
|
|
|
|
#include "Constant.h"
|
2024-01-25 09:57:08 +08:00
|
|
|
|
#include "Json.h"
|
2024-03-04 16:22:40 +08:00
|
|
|
|
#include "Log.h"
|
|
|
|
|
#include "Tool.h"
|
2024-01-18 15:41:43 +08:00
|
|
|
|
#include <QDir>
|
2024-01-25 09:57:08 +08:00
|
|
|
|
#include <QProcess>
|
|
|
|
|
|
2024-03-04 16:22:40 +08:00
|
|
|
|
extern const QList<Channel*> channelList;
|
2024-01-18 15:41:43 +08:00
|
|
|
|
|
|
|
|
|
TcpController::TcpController(QObject* parent)
|
|
|
|
|
: QObject(parent)
|
|
|
|
|
{
|
2024-03-04 16:22:40 +08:00
|
|
|
|
// 映射请求路径与处理函数
|
2024-01-18 15:41:43 +08:00
|
|
|
|
routes.insert("set_ipaddr", std::bind(&TcpController::setIpaddr, this, std::placeholders::_1));
|
|
|
|
|
routes.insert("set_video_enc", std::bind(&TcpController::setVideoEnc, this, std::placeholders::_1));
|
2024-01-25 09:57:08 +08:00
|
|
|
|
routes.insert("get_video_enc", std::bind(&TcpController::getVideoEnc, this, std::placeholders::_1));
|
2024-01-18 15:41:43 +08:00
|
|
|
|
routes.insert("get_file_list", std::bind(&TcpController::getFileList, this, std::placeholders::_1));
|
|
|
|
|
routes.insert("delete_file", std::bind(&TcpController::deleteFile, this, std::placeholders::_1));
|
2024-01-25 09:57:08 +08:00
|
|
|
|
routes.insert("set_name", std::bind(&TcpController::setName, this, std::placeholders::_1));
|
|
|
|
|
routes.insert("get_name", std::bind(&TcpController::getName, this, std::placeholders::_1));
|
2024-03-04 16:22:40 +08:00
|
|
|
|
routes.insert("set_record_mode", std::bind(&TcpController::setRecordMode, this, std::placeholders::_1));
|
|
|
|
|
routes.insert("set_playback_mode", std::bind(&TcpController::setPlaybackMode, this, std::placeholders::_1));
|
2024-03-05 13:47:32 +08:00
|
|
|
|
routes.insert("reoot", std::bind(&TcpController::reboot, this, std::placeholders::_1));
|
2024-01-18 15:41:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Routes TcpController::getRoutes()
|
|
|
|
|
{
|
|
|
|
|
return this->routes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-03-04 16:22:40 +08:00
|
|
|
|
* @brief 设置盒子的ip
|
2024-01-18 15:41:43 +08:00
|
|
|
|
* @param socket
|
|
|
|
|
*/
|
|
|
|
|
void TcpController::setIpaddr(QTcpSocket* socket)
|
|
|
|
|
{
|
2024-01-25 09:57:08 +08:00
|
|
|
|
QVariantMap params = this->parseParams(socket);
|
2024-03-04 16:22:40 +08:00
|
|
|
|
QVariantMap netMap = Json::loadFile(Constant::NetConfigPath).toMap();
|
2024-01-25 09:57:08 +08:00
|
|
|
|
|
|
|
|
|
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);
|
2024-01-25 09:57:08 +08:00
|
|
|
|
socket->write("url=set_ipaddr\r\nstatus=success");
|
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
|
|
|
|
|
*/
|
2024-01-25 09:57:08 +08:00
|
|
|
|
void TcpController::setName(QTcpSocket* socket)
|
|
|
|
|
{
|
|
|
|
|
QVariantMap params = this->parseParams(socket);
|
|
|
|
|
if (params.isEmpty() || !params.contains("name")) {
|
2024-03-04 16:22:40 +08:00
|
|
|
|
Log::error("setName params error, missing param \"name\"");
|
2024-01-25 09:57:08 +08:00
|
|
|
|
socket->write("url=set_name\r\nstatus=error");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-03-04 16:22:40 +08:00
|
|
|
|
QVariantMap cfg = Json::loadFile(Constant::ConfigurationPath).toMap();
|
2024-01-25 09:57:08 +08:00
|
|
|
|
cfg["name"] = params.value("name");
|
2024-03-04 16:22:40 +08:00
|
|
|
|
Json::saveFile(cfg, Constant::ConfigurationPath);
|
2024-01-25 09:57:08 +08:00
|
|
|
|
socket->write("url=set_name\r\nstatus=success");
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-04 16:22:40 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief 获取盒子名称
|
|
|
|
|
*/
|
2024-01-25 09:57:08 +08:00
|
|
|
|
void TcpController::getName(QTcpSocket* socket)
|
2024-01-18 15:41:43 +08:00
|
|
|
|
{
|
2024-03-04 16:22:40 +08:00
|
|
|
|
QVariantMap config = Json::loadFile(Constant::ConfigurationPath).toMap();
|
2024-01-25 09:57:08 +08:00
|
|
|
|
QString name = config["name"].toString();
|
|
|
|
|
QString data = QString("url=get_name\r\nstatus=success\r\nname=%1").arg(name);
|
|
|
|
|
socket->write(data.toStdString().data());
|
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
|
|
|
|
|
*/
|
2024-01-25 09:57:08 +08:00
|
|
|
|
void TcpController::setVideoEnc(QTcpSocket* socket)
|
2024-01-18 15:41:43 +08:00
|
|
|
|
{
|
2024-01-25 09:57:08 +08:00
|
|
|
|
QVariantMap params = this->parseParams(socket);
|
|
|
|
|
if (params.isEmpty() || !params.contains("interface")) {
|
2024-03-04 16:22:40 +08:00
|
|
|
|
Log::error("setVideoEncode params error, missing param \"interface\"");
|
2024-01-25 09:57:08 +08:00
|
|
|
|
socket->write("url=set_video_enc\r\nstatus=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) {
|
|
|
|
|
// 修改分辨率和帧率
|
2024-01-25 09:57:08 +08:00
|
|
|
|
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-01-25 09:57:08 +08:00
|
|
|
|
}
|
2024-03-04 16:22:40 +08:00
|
|
|
|
cfg["interface"] = interfaces;
|
|
|
|
|
Json::saveFile(cfg, Constant::ConfigurationPath);
|
|
|
|
|
socket->write("url=set_video_enc\r\nstatus=success");
|
|
|
|
|
} else {
|
|
|
|
|
Log::error("set video encode params error, no such interface {}", interface.toStdString());
|
|
|
|
|
socket->write("url=set_video_enc\r\nstatus=error");
|
2024-01-25 09:57:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-04 16:22:40 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief 获取视频录制参数
|
|
|
|
|
* @param socket
|
|
|
|
|
*/
|
2024-01-25 09:57:08 +08:00
|
|
|
|
void TcpController::getVideoEnc(QTcpSocket* socket)
|
|
|
|
|
{
|
|
|
|
|
QVariantMap params = this->parseParams(socket);
|
|
|
|
|
|
|
|
|
|
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();
|
2024-01-25 09:57:08 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
QString width;
|
|
|
|
|
QString height;
|
|
|
|
|
QString framerate;
|
|
|
|
|
QVariantMap encodeV = targetInterfaceCfg.value("encV").toMap();
|
|
|
|
|
width = encodeV.value("width").toString();
|
|
|
|
|
height = encodeV.value("height").toString();
|
|
|
|
|
framerate = encodeV.value("framerate").toString();
|
|
|
|
|
QString data = QString("url=get_video_enc\r\nstatus=success\r\nwidth=%1\r\nheight=%2\r\nframerate=%3")
|
|
|
|
|
.arg(width)
|
|
|
|
|
.arg(height)
|
|
|
|
|
.arg(framerate);
|
|
|
|
|
socket->write(data.toStdString().data());
|
|
|
|
|
} else {
|
2024-03-04 16:22:40 +08:00
|
|
|
|
Log::error("getVideoEnc params error, missing param \"interface\"");
|
2024-01-25 09:57:08 +08:00
|
|
|
|
socket->write("url=get_video_enc\r\nstatus=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
|
|
|
|
|
*/
|
2024-01-25 09:57:08 +08:00
|
|
|
|
void TcpController::getFileList(QTcpSocket* socket)
|
2024-01-18 15:41:43 +08:00
|
|
|
|
{
|
2024-01-25 09:57:08 +08:00
|
|
|
|
QVariantMap params = this->parseParams(socket);
|
|
|
|
|
if (params.isEmpty() || !params.contains("interface")) {
|
2024-03-04 16:22:40 +08:00
|
|
|
|
Log::error("getFileList params error, missing param \"interface\"");
|
2024-01-25 09:57:08 +08:00
|
|
|
|
socket->write("url=get_file_list\r\nstatus=error");
|
2024-01-18 15:41:43 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-25 09:57:08 +08:00
|
|
|
|
QString interface = params.value("interface").toString();
|
2024-03-04 16:22:40 +08:00
|
|
|
|
QString videoPath = QString("%1/%2").arg(Constant::VideoPath).arg(interface);
|
2024-01-25 09:57:08 +08:00
|
|
|
|
|
2024-03-04 16:22:40 +08:00
|
|
|
|
QStringList videoList = Tool::getFileList(videoPath);
|
2024-01-18 15:41:43 +08:00
|
|
|
|
|
2024-01-25 09:57:08 +08:00
|
|
|
|
if (videoList.isEmpty()) {
|
2024-03-04 16:22:40 +08:00
|
|
|
|
Log::error("getFileList error, video dir is empty ");
|
2024-01-25 09:57:08 +08:00
|
|
|
|
socket->write("url=get_file_list\r\nstatus=error");
|
|
|
|
|
return;
|
2024-01-18 15:41:43 +08:00
|
|
|
|
}
|
2024-01-25 09:57:08 +08:00
|
|
|
|
QString data;
|
|
|
|
|
data = "url=get_file_list\r\nstatus=success\r\n" + videoList.join("\r\n");
|
|
|
|
|
socket->write(data.toStdString().data());
|
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 TcpController::deleteFile(QTcpSocket* socket)
|
|
|
|
|
{
|
2024-01-25 09:57:08 +08:00
|
|
|
|
QVariantMap params = this->parseParams(socket);
|
2024-03-04 16:22:40 +08:00
|
|
|
|
if (!params.contains("interface") || !params.contains("filename")) {
|
|
|
|
|
Log::error("deleteFile params error, missing params \"interface\" or \"filename\"");
|
2024-01-25 09:57:08 +08:00
|
|
|
|
socket->write("url=delete_file\r\nstatus=error");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString interface = params.value("interface").toString();
|
2024-03-04 16:22:40 +08:00
|
|
|
|
QString fileName = params.value("filename").toString();
|
2024-01-25 09:57:08 +08:00
|
|
|
|
// xxx.mp4 ==> xxx.jpg
|
|
|
|
|
QString snapName = fileName.split(".")[0] + ".jpg";
|
|
|
|
|
|
2024-03-04 16:22:40 +08:00
|
|
|
|
QString filePath = QString("%1/%2/%3").arg(Constant::VideoPath).arg(interface).arg(fileName);
|
2024-01-25 09:57:08 +08:00
|
|
|
|
QFile video(filePath);
|
|
|
|
|
if (video.exists()) {
|
|
|
|
|
video.remove();
|
2024-03-04 16:22:40 +08:00
|
|
|
|
Log::info("remove video {}", filePath.toStdString());
|
2024-01-25 09:57:08 +08:00
|
|
|
|
socket->write("url=delete_file\r\nstatus=success");
|
|
|
|
|
} else {
|
2024-03-04 16:22:40 +08:00
|
|
|
|
Log::error("error, file: {} dont exist", filePath.toStdString());
|
2024-01-25 09:57:08 +08:00
|
|
|
|
socket->write("url=delete_file\r\nstatus=error");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-04 16:22:40 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief 设置录制模式,一路录制/两路录制,只修改配置文件,重启之后生效
|
|
|
|
|
* @param socket
|
|
|
|
|
*/
|
|
|
|
|
void TcpController::setRecordMode(QTcpSocket* socket)
|
2024-01-25 09:57:08 +08:00
|
|
|
|
{
|
2024-03-04 16:22:40 +08:00
|
|
|
|
QVariantMap params = this->parseParams(socket);
|
|
|
|
|
if (!params.contains("recordMode")) {
|
|
|
|
|
Log::error("setRecordMode params error, missing param \"recordMode\"");
|
|
|
|
|
socket->write("url=set_record_mode\r\nstatus=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);
|
|
|
|
|
socket->write("url=set_record_mode\r\nstatus=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");
|
|
|
|
|
socket->write("url=set_record_mode\r\nstatus=success");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 更新配置文件
|
|
|
|
|
cfg["recordMode"] = recordMode;
|
|
|
|
|
Json::saveFile(cfg, Constant::ConfigurationPath);
|
|
|
|
|
socket->write("url=set_record_mode\r\nstatus=success");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 设置回放模式, 一路回放/两路同时回放,只修改配置文件,重启之后生效
|
|
|
|
|
* @param socket
|
|
|
|
|
*/
|
|
|
|
|
void TcpController::setPlaybackMode(QTcpSocket* socket)
|
|
|
|
|
{
|
|
|
|
|
QVariantMap params = this->parseParams(socket);
|
|
|
|
|
if (!params.contains("playbackMode")) {
|
|
|
|
|
Log::error("setPlaybackMode params error, missing param \"playbackmode\"");
|
|
|
|
|
socket->write("url=set_playback_mode\r\nstatus=error");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int playbackMode = params.value("playbackMode").toInt();
|
|
|
|
|
if (playbackMode != Constant::OneChannelPlayback
|
|
|
|
|
&& playbackMode != Constant::TwoChannelPlayback) {
|
|
|
|
|
Log::error("setPlayBack params error, unkown param \"playbackMode\": {}", playbackMode);
|
|
|
|
|
socket->write("url=set_playback_mode\r\nstatus=error");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariantMap cfg = Json::loadFile(Constant::ConfigurationPath).toMap();
|
|
|
|
|
cfg["playbackMode"] = playbackMode;
|
|
|
|
|
Json::saveFile(cfg, Constant::ConfigurationPath);
|
|
|
|
|
socket->write("url=set_playback_mode\r\nstatus=success");
|
2024-01-18 15:41:43 +08:00
|
|
|
|
}
|
2024-03-05 13:47:32 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 重启程序
|
|
|
|
|
* @param socket
|
|
|
|
|
*/
|
|
|
|
|
void TcpController::reboot(QTcpSocket* socket)
|
|
|
|
|
{
|
|
|
|
|
// 先停止所有的录制
|
|
|
|
|
for (Channel* chn : channelList) {
|
|
|
|
|
chn->stopRecord();
|
|
|
|
|
}
|
|
|
|
|
socket->write("url=reboot\r\nstatus=success");
|
|
|
|
|
socket->flush();
|
|
|
|
|
|
|
|
|
|
// 退出程序,等待后台运行的shell脚本重启程序
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|