From d3b7bbe7102cd089680a828f5d8f6402c8cf6342 Mon Sep 17 00:00:00 2001 From: pansen <pansen626@sina.com> Date: 星期四, 07 三月 2019 14:43:28 +0800 Subject: [PATCH] GB28181集成完成,集成推流模块 --- QiaoJiaSystem/StructureApp/HiredisTool.cpp | 426 +++++++++++++++++++++++----------------------------- 1 files changed, 190 insertions(+), 236 deletions(-) diff --git a/QiaoJiaSystem/StructureApp/HiredisTool.cpp b/QiaoJiaSystem/StructureApp/HiredisTool.cpp index e329b1d..c2b2422 100644 --- a/QiaoJiaSystem/StructureApp/HiredisTool.cpp +++ b/QiaoJiaSystem/StructureApp/HiredisTool.cpp @@ -4,42 +4,38 @@ #include <jsoncpp/json/json.h> #include <basic/debug/Debug.h> #include <basic/util/app/AppConfig.h> -HiredisTool::HiredisTool(): -m_redis(nullptr), -m_nImgListLen(0) -{ + +HiredisTool::HiredisTool() : + m_redis(nullptr), + m_nImgListLen(0) { init(); } -HiredisTool::~HiredisTool() -{ - if(m_redis != nullptr) - { + +HiredisTool::~HiredisTool() { + if (m_redis != nullptr) { redisFree(m_redis);//鏋愭瀯鍑芥暟閲婃斁璧勬簮 - DBG("~RedisTool :: free redis connection ") ; + DBG("~RedisTool :: free redis connection "); } } -void HiredisTool::init() -{ - struct timeval timeout = { 1, 500000 }; // 1.5 seconds 璁剧疆杩炴帴绛夊緟鏃堕棿 +void HiredisTool::init() { + struct timeval timeout = {1, 500000}; // 1.5 seconds 璁剧疆杩炴帴绛夊緟鏃堕棿 - // m_nImgListLen=appConfig.getIntProperty("redis_buf_len"); - std::string ip=appConfig.getStringProperty("redis_ip"); + // m_nImgListLen=appConfig.getIntProperty("redis_buf_len"); + std::string ip = appConfig.getStringProperty("redis_ip"); - int port=appConfig.getIntProperty("redis_port"); - m_redis = redisConnectWithTimeout(ip.c_str(), port, timeout);//寤虹珛杩炴帴 - if (m_redis->err) { + int port = appConfig.getIntProperty("redis_port"); + m_redis = redisConnectWithTimeout(ip.c_str(), port, timeout);//寤虹珛杩炴帴 + if (m_redis->err) { - DBG("RedisTool : Connection error: %s"<< m_redis->errstr); - redisFree(m_redis); - m_redis= nullptr; - } - else - { - DBG( "init redis tool success " ); + DBG("RedisTool : Connection error: %s" << m_redis->errstr); + redisFree(m_redis); + m_redis = nullptr; + } else { + DBG("init redis tool success "); - } + } } @@ -81,42 +77,41 @@ -bool HiredisTool::pushImageBuf(const std::string& file_name,const cv::Mat& img) -{ - - - std::vector<uchar> buf; - Json::Value json; - - CvUtil::cvMat2Buffer(img,buf); -// json["time"]=info.time; - json["img"]=std::string(buf.begin(),buf.end()); - // buf.clear(); - std::string strBuf=std::string(buf.begin(),buf.end()); - if(!listLpush(file_name,json.toStyledString())); - { - - return false; - } - - //DBG(json.toStyledString()); - - return true; -} - -bool HiredisTool::setKeyImage(const std::string &file_name, const cv::Mat &img) -{ +bool HiredisTool::pushImageBuf(const std::string &file_name, const cv::Mat &img) { std::vector<uchar> buf; Json::Value json; - CvUtil::cvMat2Buffer(img,buf); + CvUtil::cvMat2Buffer(img, buf); // json["time"]=info.time; - json["img"]=std::string(buf.begin(),buf.end()); + json["img"] = std::string(buf.begin(), buf.end()); + // buf.clear(); + std::string strBuf = std::string(buf.begin(), buf.end()); + if (!listLpush(file_name, json.toStyledString())); + { + + return false; + } + + //DBG(json.toStyledString()); + + return true; +} + +bool HiredisTool::setKeyImage(const std::string &file_name, const cv::Mat &img) { + + + std::vector<uchar> buf; + Json::Value json; + + CvUtil::cvMat2Buffer(img, buf); +// json["time"]=info.time; + json["img"] = std::move(m_base64.Encode(buf.data(), buf.size())); +// json["img"] = std::string(buf.begin(), buf.end()); // buf.clear(); // std::string strBuf=std::string(buf.begin(),buf.end()); - if(!setKeyValue(file_name,json.toStyledString())); + if (!setKeyValue(file_name, json.toStyledString())); { return false; @@ -128,62 +123,59 @@ } -void HiredisTool::getImage(const std::string& file_name, cv::Mat& img) -{ +void HiredisTool::getImage(const std::string &file_name, cv::Mat &img) { - - std::string content; - cv::Mat imgTep; - - listRpop(file_name,content); - - - Json::Reader reader; - Json::Value value; - - if(!reader.parse(content,value)) - { - return ; - } - - std::string str=value["img"].asString(); - std::vector<uchar> data; - - data.resize(str.size()); - - data.assign(str.begin(),str.end()); - - CvUtil::buffer2CvMat(data,imgTep); - - imgTep.copyTo(img); - // return img; -} - - -void HiredisTool::getKeyImage(const std::string &imageName, cv::Mat &img) -{ std::string content; cv::Mat imgTep; - getKeyValue(imageName,content); + listRpop(file_name, content); + Json::Reader reader; Json::Value value; - if(!reader.parse(content,value)) - { - return ; + if (!reader.parse(content, value)) { + return; } - std::string str=value["img"].asString(); + std::string str = value["img"].asString(); std::vector<uchar> data; data.resize(str.size()); - data.assign(str.begin(),str.end()); + data.assign(str.begin(), str.end()); - CvUtil::buffer2CvMat(data,imgTep); + CvUtil::buffer2CvMat(data, imgTep); + + imgTep.copyTo(img); + // return img; +} + + +void HiredisTool::getKeyImage(const std::string &imageName, cv::Mat &img) { + + std::string content; + cv::Mat imgTep; + + getKeyValue(imageName, content); + + Json::Reader reader; + Json::Value value; + + if (!reader.parse(content, value)) { + return; + } + + std::string str = value["img"].asString(); + str = std::move(m_base64.Decode(str.c_str(), str.size())); + std::vector<uchar> data; + + data.resize(str.size()); + + data.assign(str.begin(), str.end()); + + CvUtil::buffer2CvMat(data, imgTep); imgTep.copyTo(img); // return img; @@ -195,25 +187,22 @@ // DBG("type="<<type); // return hashSet(file_list,cam_id,type); //} -bool HiredisTool::clearAllImageBuf() -{ +bool HiredisTool::clearAllImageBuf() { // auto cam_map=findAllCamera(); // for(auto cam:cam_map) // { // delKey(cam.first); // } } -bool HiredisTool::listLpush(const std::string& key,const std::string& value) -{ - if(!checkParam()) - { + +bool HiredisTool::listLpush(const std::string &key, const std::string &value) { + if (!checkParam()) { return false; } redisReply *reply; - reply = (redisReply*)redisCommand(m_redis,"lpush %s %s", key.c_str(),value.c_str()); - if(!checkResult(reply)) - { + reply = (redisReply *) redisCommand(m_redis, "lpush %s %s", key.c_str(), value.c_str()); + if (!checkResult(reply)) { return false; } @@ -222,17 +211,14 @@ return true; } -bool HiredisTool::setKeyValue(const std::string &key, const std::string &value) -{ - if(!checkParam()) - { +bool HiredisTool::setKeyValue(const std::string &key, const std::string &value) { + if (!checkParam()) { return false; } redisReply *reply; - reply = (redisReply*)redisCommand(m_redis,"set %s %s", key.c_str(),value.c_str()); - if(!checkResult(reply)) - { + reply = (redisReply *) redisCommand(m_redis, "set %s %s", key.c_str(), value.c_str()); + if (!checkResult(reply)) { return false; } @@ -241,64 +227,54 @@ return true; } -bool HiredisTool::getKeyValue(const std::string &key, std::string &value) -{ - if(!checkParam()) - { +bool HiredisTool::getKeyValue(const std::string &key, std::string &value) { + if (!checkParam()) { return false; } redisReply *reply; //1.find all filename - reply = (redisReply*)redisCommand(m_redis,"get %s ", key.c_str()); + reply = (redisReply *) redisCommand(m_redis, "get %s ", key.c_str()); - if(!checkResult(reply)) - { + if (!checkResult(reply)) { return false; } - if(reply->type == REDIS_REPLY_STRING) - { - value=reply->str; + if (reply->type == REDIS_REPLY_STRING) { + value = reply->str; } freeReplyObject(reply); return true; } -bool HiredisTool::listRpop(const std::string& key,std::string& value) -{ - if(!checkParam()) - { +bool HiredisTool::listRpop(const std::string &key, std::string &value) { + if (!checkParam()) { return false; } redisReply *reply; //1.find all filename - reply = (redisReply*)redisCommand(m_redis,"rpop %s ", key.c_str()); + reply = (redisReply *) redisCommand(m_redis, "rpop %s ", key.c_str()); - if(!checkResult(reply)) - { + if (!checkResult(reply)) { return false; } - if(reply->type == REDIS_REPLY_STRING) - { - value=reply->str; + if (reply->type == REDIS_REPLY_STRING) { + value = reply->str; } freeReplyObject(reply); return true; } -bool HiredisTool::hashSet(const std::string& tab,const std::string& key,const int& value) -{ - if(!checkParam()) - { + +bool HiredisTool::hashSet(const std::string &tab, const std::string &key, const int &value) { + if (!checkParam()) { return false; } redisReply *reply; - reply = (redisReply*)redisCommand(m_redis,"hset %s %s %d", tab.c_str(),key.c_str(),value); + reply = (redisReply *) redisCommand(m_redis, "hset %s %s %d", tab.c_str(), key.c_str(), value); - if(!checkResult(reply)) - { + if (!checkResult(reply)) { return false; } freeReplyObject(reply); @@ -307,176 +283,154 @@ return true; } -int HiredisTool::getSize(const std::string& key) -{ - int size=0; - if(!checkParam()) - { +int HiredisTool::getSize(const std::string &key) { + int size = 0; + if (!checkParam()) { return size; } redisReply *reply; - reply = (redisReply*)redisCommand(m_redis,"llen %s", key.c_str()); + reply = (redisReply *) redisCommand(m_redis, "llen %s", key.c_str()); - if(!checkResult(reply)) - { + if (!checkResult(reply)) { return size; } - size=reply->integer; + size = reply->integer; freeReplyObject(reply); return size; } -bool HiredisTool::listLindex(const std::string& key,std::string& value) -{ - int size=0; - if(!checkParam()) - { + +bool HiredisTool::listLindex(const std::string &key, std::string &value) { + int size = 0; + if (!checkParam()) { return size; } redisReply *reply; - reply = (redisReply*)redisCommand(m_redis,"lindex %s -1", key.c_str()); + reply = (redisReply *) redisCommand(m_redis, "lindex %s -1", key.c_str()); - if(!checkResult(reply)) - { + if (!checkResult(reply)) { return false; } - if(reply->type == REDIS_REPLY_STRING) - { - value=reply->str; + if (reply->type == REDIS_REPLY_STRING) { + value = reply->str; } freeReplyObject(reply); } -bool HiredisTool::delKey(const std::string& key) -{ - if(!checkParam()) - { + +bool HiredisTool::delKey(const std::string &key) { + if (!checkParam()) { return false; } redisReply *reply; - reply = (redisReply*)redisCommand(m_redis,"del %s", key.c_str()); + reply = (redisReply *) redisCommand(m_redis, "del %s", key.c_str()); - if(!checkResult(reply)) - { + if (!checkResult(reply)) { return false; } freeReplyObject(reply); return true; } -bool HiredisTool::checkParam() -{ - if(m_redis == nullptr || m_redis->err) - { - if(m_redis) - { - redisFree(m_redis); - m_redis= nullptr; - } - DBG("Redis init Error !!!"); - init(); - return false; - } - return true; + +bool HiredisTool::checkParam() { + if (m_redis == nullptr || m_redis->err) { + if (m_redis) { + redisFree(m_redis); + m_redis = nullptr; + } + DBG("Redis init Error !!!"); + init(); + return false; + } + return true; } -bool HiredisTool::checkResult(redisReply *reply) -{ - if (reply==nullptr) - { - DBG("reply nullptr !!!"); - return false; + +bool HiredisTool::checkResult(redisReply *reply) { + if (reply == nullptr) { + DBG("reply nullptr !!!"); + return false; } - if (reply->type == REDIS_REPLY_ERROR) - { - ERR("redisCommand Error: "<<reply->str); + if (reply->type == REDIS_REPLY_ERROR) { + ERR("redisCommand Error: " << reply->str); freeReplyObject(reply); return false; } return true; } -void HiredisTool::addFileInfo(const std::string& filename,const int& file_status) -{ - hashSet(file_list,filename,file_status); - } -std::queue<cv::Mat> HiredisTool::getImgQue(const std::string& filename) -{ +void HiredisTool::addFileInfo(const std::string &filename, const int &file_status) { + + hashSet(file_list, filename, file_status); +} + +std::queue<cv::Mat> HiredisTool::getImgQue(const std::string &filename) { std::queue<cv::Mat> que; - if(!checkParam()) - { + if (!checkParam()) { return que; } redisReply *reply; - reply = (redisReply*)redisCommand(m_redis,"lrange %s 0 -1", filename.c_str()); + reply = (redisReply *) redisCommand(m_redis, "lrange %s 0 -1", filename.c_str()); - if(!checkResult(reply)) - { + if (!checkResult(reply)) { return que; } - if(reply->type == REDIS_REPLY_ARRAY) - { - for(int i=0;i<reply->elements;++i) - { - std::string str=reply->element[i]->str; + if (reply->type == REDIS_REPLY_ARRAY) { + for (int i = 0; i < reply->elements; ++i) { + std::string str = reply->element[i]->str; std::vector<uchar> data; cv::Mat img; data.resize(str.size()); - data.assign(str.begin(),str.end()); - CvUtil::buffer2CvMat(data,img); + data.assign(str.begin(), str.end()); + CvUtil::buffer2CvMat(data, img); que.push(img); } } freeReplyObject(reply); } -std::map<std::string,int> HiredisTool::findAllFileStatus() -{ - std::map<std::string,int> fileMap; - if(!checkParam()) - { - return fileMap; - } - redisReply *reply; +std::map<std::string, int> HiredisTool::findAllFileStatus() { + std::map<std::string, int> fileMap; + if (!checkParam()) { + return fileMap; + } - reply = (redisReply*)redisCommand(m_redis,"hgetall %s", file_list); + redisReply *reply; - if(!checkResult(reply)) - { - return fileMap; - } + reply = (redisReply *) redisCommand(m_redis, "hgetall %s", file_list); - if(reply->type == REDIS_REPLY_ARRAY) - { + if (!checkResult(reply)) { + return fileMap; + } - for (int i= 0; i < reply->elements; i+=2) - { - std::string key=reply->element[i]->str; - std::string value=reply->element[i+1]->str; + if (reply->type == REDIS_REPLY_ARRAY) { - if(key.empty()||value.empty()) continue; + for (int i = 0; i < reply->elements; i += 2) { + std::string key = reply->element[i]->str; + std::string value = reply->element[i + 1]->str; - fileMap.insert(std::pair<std::string,int>(key,atoi(value.c_str()))); - } - } - freeReplyObject(reply); - return fileMap; + if (key.empty() || value.empty()) continue; + + fileMap.insert(std::pair<std::string, int>(key, atoi(value.c_str()))); + } + } + freeReplyObject(reply); + return fileMap; } -bool HiredisTool::hashDel(const std::string& tab,const std::string &key) -{ - if(!checkParam()) - { + +bool HiredisTool::hashDel(const std::string &tab, const std::string &key) { + if (!checkParam()) { return false; } redisReply *reply; - reply = (redisReply*)redisCommand(m_redis,"hdel %s %s", tab.c_str(),key.c_str()); + reply = (redisReply *) redisCommand(m_redis, "hdel %s %s", tab.c_str(), key.c_str()); - if(!checkResult(reply)) - { + if (!checkResult(reply)) { return false; } freeReplyObject(reply); -- Gitblit v1.8.0