From e4c9b5bfb1d9000c08d84f2f044cd0c3605a2945 Mon Sep 17 00:00:00 2001
From: miyanhui <dennismi1024@gmail.com>
Date: 星期三, 13 二月 2019 14:17:13 +0800
Subject: [PATCH] 修复检测结果ImgKey不对应的问题

---
 QiaoJiaSystem/StructureApp/HiredisTool.cpp |  122 +++++++++++++++++++++++++++++++++++++---
 1 files changed, 111 insertions(+), 11 deletions(-)

diff --git a/QiaoJiaSystem/StructureApp/HiredisTool.cpp b/QiaoJiaSystem/StructureApp/HiredisTool.cpp
index 6a8715b..e329b1d 100644
--- a/QiaoJiaSystem/StructureApp/HiredisTool.cpp
+++ b/QiaoJiaSystem/StructureApp/HiredisTool.cpp
@@ -27,11 +27,13 @@
      //  m_nImgListLen=appConfig.getIntProperty("redis_buf_len");
        std::string ip=appConfig.getStringProperty("redis_ip");
 
-       int port=6379;
+        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);
+            DBG("RedisTool : Connection error: %s"<< m_redis->errstr);
+            redisFree(m_redis);
+            m_redis= nullptr;
         }
         else
         {
@@ -89,22 +91,42 @@
       CvUtil::cvMat2Buffer(img,buf);
 //      json["time"]=info.time;
       json["img"]=std::string(buf.begin(),buf.end());
-      if(!checkParam())
+  //    buf.clear();
+      std::string strBuf=std::string(buf.begin(),buf.end());
+      if(!listLpush(file_name,json.toStyledString()));
       {
-          return false;
-      }
-      redisReply *reply;
 
-      reply = (redisReply*)redisCommand(m_redis,"lpush %s %s", file_name.c_str(),json.toStyledString().c_str());
-      if(!checkResult(reply))
-      {
           return false;
       }
 
-      freeReplyObject(reply);
+      //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::string(buf.begin(),buf.end());
+    //    buf.clear();
+//    std::string strBuf=std::string(buf.begin(),buf.end());
+    if(!setKeyValue(file_name,json.toStyledString()));
+    {
+
+        return false;
+    }
+
+    //DBG(json.toStyledString());
+
+    return true;
+}
+
 
 void  HiredisTool::getImage(const std::string& file_name, cv::Mat& img)
 {
@@ -135,8 +157,39 @@
 
         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();
+    std::vector<uchar> data;
+
+    data.resize(str.size());
+
+    data.assign(str.begin(),str.end());
+
+    CvUtil::buffer2CvMat(data,imgTep);
+
+    imgTep.copyTo(img);
+    // return img;
+}
+
+
 //bool HiredisTool::setCameraState(const std::string& cam_id,const int& type)
 //{
 //    DBG("type="<<type);
@@ -164,6 +217,48 @@
         return false;
     }
 
+    freeReplyObject(reply);
+
+    return true;
+}
+
+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))
+    {
+        return false;
+    }
+
+    freeReplyObject(reply);
+
+    return true;
+}
+
+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());
+
+    if(!checkResult(reply))
+    {
+        return false;
+    }
+    if(reply->type == REDIS_REPLY_STRING)
+    {
+        value=reply->str;
+    }
     freeReplyObject(reply);
 
     return true;
@@ -274,6 +369,11 @@
 {
      if(m_redis == nullptr || m_redis->err)
      {
+         if(m_redis)
+         {
+             redisFree(m_redis);
+             m_redis= nullptr;
+         }
          DBG("Redis init Error !!!");
          init();
          return false;

--
Gitblit v1.8.0