#include "TcpController.h" #include "Config.h" #include "Json.h" #include #include #include extern const char* VideoPath; extern const char* SnapPath; extern const char* NetConfigPath; extern const char* NetScriptPath; extern const char* ConfigurationPath; TcpController::TcpController(QObject* parent) : QObject(parent) { // map the requesr url and the handler 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 TcpController::getRoutes() { return this->routes; } /** * @brief set box's ip address * @param socket */ void TcpController::setIpaddr(QTcpSocket* socket) { QVariantMap params = this->parseParams(socket); QVariantMap netMap = Json::loadFile(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, NetConfigPath); socket->write("url=set_ipaddr\r\nstatus=success"); writeCom(NetScriptPath); } /** * @brief set box's name * @param socket */ void TcpController::setName(QTcpSocket* socket) { QVariantMap params = this->parseParams(socket); if (params.isEmpty() || !params.contains("name")) { socket->write("url=set_name\r\nstatus=error"); return; } QVariantMap cfg = Json::loadFile(ConfigurationPath).toMap(); cfg["name"] = params.value("name"); Json::saveFile(cfg, ConfigurationPath); socket->write("url=set_name\r\nstatus=success"); } void TcpController::getName(QTcpSocket* socket) { QVariantMap config = Json::loadFile(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 set the video encode params * @param socket */ void TcpController::setVideoEnc(QTcpSocket* socket) { QVariantMap params = this->parseParams(socket); if (params.isEmpty() || !params.contains("interface")) { qDebug() << "set video encode params error"; socket->write("url=set_video_enc\r\nstatus=error"); return; } // change resolution and framerate of channel QString interface = params.value("interface").toString(); Channel* chn = Config::findChannelByName(interface); if (chn) chn->videoEncoder->setData(params); // update config QVariantMap cfg = Json::loadFile(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, ConfigurationPath); socket->write("url=set_video_enc\r\nstatus=success"); } void TcpController::getVideoEnc(QTcpSocket* socket) { QVariantMap params = this->parseParams(socket); if (params.contains("interface")) { QString interface = params.value("interface").toString(); QVariantMap cfg = Json::loadFile(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 { qDebug() << "get video encode params error"; socket->write("url=get_video_enc\r\nstatus=error"); return; } } /** * @brief get the video file list * @param socket */ void TcpController::getFileList(QTcpSocket* socket) { QVariantMap params = this->parseParams(socket); if (params.isEmpty() || !params.contains("interface")) { qDebug() << "getFileList params error"; socket->write("url=get_file_list\r\nstatus=error"); return; } QString interface = params.value("interface").toString(); QString videoPath = QString("%1/%2").arg(VideoPath).arg(interface); QStringList videoList = this->listFile(videoPath); if (videoList.isEmpty()) { qDebug() << "error, video folders 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 delete video file by file name * @param socket */ void TcpController::deleteFile(QTcpSocket* socket) { QVariantMap params = this->parseParams(socket); if (params.isEmpty()) { 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(VideoPath).arg(interface).arg(fileName); QString snapPath = QString("%1/%2/%3").arg(SnapPath).arg(interface).arg(fileName); QFile video(filePath); if (video.exists()) { video.remove(); qDebug() << "remove video:" << filePath; QFile image(snapPath); if (image.exists()) { image.remove(); qDebug() << "remove image:" << snapPath; } socket->write("url=delete_file\r\nstatus=success"); } else { qDebug() << "error, file:" << filePath << "do not exist"; socket->write("url=delete_file\r\nstatus=error"); } } QString TcpController::writeCom(const QString& com) { QProcess proc; QStringList argList; argList << "-c" << com; proc.start("/bin/sh", argList); // wait process start proc.waitForFinished(); proc.waitForReadyRead(); // read data from console QByteArray procOutput = proc.readAll(); proc.close(); return QString(procOutput); }