302 lines
11 KiB
C++
Executable File
302 lines
11 KiB
C++
Executable File
#include "TcpController.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;
|
||
|
||
TcpController::TcpController(QObject* parent)
|
||
: QObject(parent)
|
||
{
|
||
// 映射请求路径与处理函数
|
||
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));
|
||
routes.insert("get_video_enc", std::bind(&TcpController::getVideoEnc, this, std::placeholders::_1));
|
||
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));
|
||
routes.insert("set_name", std::bind(&TcpController::setName, this, std::placeholders::_1));
|
||
routes.insert("get_name", std::bind(&TcpController::getName, this, std::placeholders::_1));
|
||
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));
|
||
routes.insert("reoot", std::bind(&TcpController::reboot, this, std::placeholders::_1));
|
||
}
|
||
|
||
Routes TcpController::getRoutes()
|
||
{
|
||
return this->routes;
|
||
}
|
||
|
||
/**
|
||
* @brief 设置盒子的ip
|
||
* @param socket
|
||
*/
|
||
void TcpController::setIpaddr(QTcpSocket* socket)
|
||
{
|
||
QVariantMap params = this->parseParams(socket);
|
||
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);
|
||
socket->write("url=set_ipaddr\r\nstatus=success");
|
||
Tool::writeCom(Constant::NetScriptPath);
|
||
}
|
||
|
||
/**
|
||
* @brief 设置盒子的名称
|
||
* @param socket
|
||
*/
|
||
void TcpController::setName(QTcpSocket* socket)
|
||
{
|
||
QVariantMap params = this->parseParams(socket);
|
||
if (params.isEmpty() || !params.contains("name")) {
|
||
Log::error("setName params error, missing param \"name\"");
|
||
socket->write("url=set_name\r\nstatus=error");
|
||
return;
|
||
}
|
||
QVariantMap cfg = Json::loadFile(Constant::ConfigurationPath).toMap();
|
||
cfg["name"] = params.value("name");
|
||
Json::saveFile(cfg, Constant::ConfigurationPath);
|
||
socket->write("url=set_name\r\nstatus=success");
|
||
}
|
||
|
||
/**
|
||
* @brief 获取盒子名称
|
||
*/
|
||
void TcpController::getName(QTcpSocket* socket)
|
||
{
|
||
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);
|
||
socket->write(data.toStdString().data());
|
||
}
|
||
|
||
/**
|
||
* @brief 设置视频录制参数
|
||
* @param socket
|
||
*/
|
||
void TcpController::setVideoEnc(QTcpSocket* socket)
|
||
{
|
||
QVariantMap params = this->parseParams(socket);
|
||
if (params.isEmpty() || !params.contains("interface")) {
|
||
Log::error("setVideoEncode params error, missing param \"interface\"");
|
||
socket->write("url=set_video_enc\r\nstatus=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);
|
||
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");
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @brief 获取视频录制参数
|
||
* @param socket
|
||
*/
|
||
void TcpController::getVideoEnc(QTcpSocket* socket)
|
||
{
|
||
QVariantMap params = this->parseParams(socket);
|
||
|
||
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;
|
||
}
|
||
}
|
||
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 {
|
||
Log::error("getVideoEnc params error, missing param \"interface\"");
|
||
socket->write("url=get_video_enc\r\nstatus=error");
|
||
return;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @brief 获取文件列表
|
||
* @param socket
|
||
*/
|
||
void TcpController::getFileList(QTcpSocket* socket)
|
||
{
|
||
QVariantMap params = this->parseParams(socket);
|
||
if (params.isEmpty() || !params.contains("interface")) {
|
||
Log::error("getFileList params error, missing param \"interface\"");
|
||
socket->write("url=get_file_list\r\nstatus=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.isEmpty()) {
|
||
Log::error("getFileList error, video dir is empty ");
|
||
socket->write("url=get_file_list\r\nstatus=error");
|
||
return;
|
||
}
|
||
QString data;
|
||
data = "url=get_file_list\r\nstatus=success\r\n" + videoList.join("\r\n");
|
||
socket->write(data.toStdString().data());
|
||
}
|
||
|
||
/**
|
||
* @brief 删除文件
|
||
* @param socket
|
||
*/
|
||
void TcpController::deleteFile(QTcpSocket* socket)
|
||
{
|
||
QVariantMap params = this->parseParams(socket);
|
||
if (!params.contains("interface") || !params.contains("filename")) {
|
||
Log::error("deleteFile params error, missing params \"interface\" or \"filename\"");
|
||
socket->write("url=delete_file\r\nstatus=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());
|
||
socket->write("url=delete_file\r\nstatus=success");
|
||
} else {
|
||
Log::error("error, file: {} dont exist", filePath.toStdString());
|
||
socket->write("url=delete_file\r\nstatus=error");
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @brief 设置录制模式,一路录制/两路录制,只修改配置文件,重启之后生效
|
||
* @param socket
|
||
*/
|
||
void TcpController::setRecordMode(QTcpSocket* socket)
|
||
{
|
||
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");
|
||
}
|
||
|
||
/**
|
||
* @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);
|
||
} |