From b8c5eb8048b566d632b25391a79ccbd6ce33c010 Mon Sep 17 00:00:00 2001
From: chenshijun <csj_sky@126.com>
Date: 星期六, 30 三月 2019 16:10:01 +0800
Subject: [PATCH] 1.3 增加nsq消息,解决进程重启

---
 QiaoJiaSystem/VideoToImageMulth/RtspAnalysManager.cpp |  158 ++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 138 insertions(+), 20 deletions(-)

diff --git a/QiaoJiaSystem/VideoToImageMulth/RtspAnalysManager.cpp b/QiaoJiaSystem/VideoToImageMulth/RtspAnalysManager.cpp
index 8eafe6d..d786f47 100644
--- a/QiaoJiaSystem/VideoToImageMulth/RtspAnalysManager.cpp
+++ b/QiaoJiaSystem/VideoToImageMulth/RtspAnalysManager.cpp
@@ -9,12 +9,107 @@
 using std::string;
 
 RtspAnalysManager::RtspAnalysManager(LDBTool *_dbTool) : m_lDBTool(nullptr), m_maxCount(50), m_currentCount(0) {
-    INFO("MYH DEBUG HERE");
+    DBG("MYH DEBUG HERE");
     m_lDBTool = new LDBTool;
     init();
 
+    //nsq   set callback func
+    DBG("NsqMsgConsumer Init");
+    nsqMsgConsumer = new BasicMsg::Nsq::NsqMsgConsumer("127.0.0.1", "4150", "cut_dura_edit", to_string(appPref.getIntData("RpcVTIMPort")));
+    nsqMsgConsumer->setMessageCallback(std::bind(&OnMsgFunc, std::placeholders::_1, this));
+    nsqMsgConsumer->init();
+    DBG("NsqMsgConsumer Init END!!");
+
 }
 
+void RtspAnalysManager::OnMsgFunc(void *msgPtr, void *pThisPtr) {
+    evnsq::Message *msg = (evnsq::Message *) msgPtr;
+    DBG("Received a message, id=" << msg->id << " message=[" << msg->body.ToString() << "]");
+    RtspAnalysManager *pthis = (RtspAnalysManager *) pThisPtr;
+    Json::Reader reader;
+    Json::Value value;
+    if (reader.parse(msg->body.ToString(), value)) {
+        int cut_max_duration = value["cut_max_duration"].asInt();
+        int cut_min_duration = value["cut_min_duration"].asInt();
+        DBG("cut_max_duration: " << cut_max_duration << "   cut_min_duration: " << cut_min_duration);
+        appPref.setIntData("n_cut_max_duration", cut_max_duration);
+        appPref.setIntData("n_cut_min_duration", cut_min_duration);
+
+        DBG("cut_max_duration: " << cut_max_duration << "   cut_min_duration: " << cut_min_duration);
+//        m_GB28181_Enable = appPref.getIntData("GB28181_Enable");
+        //#todo GB28181
+        if (pthis->m_GB28181_Enable) {
+            DBG("searchCamDevTableByType before");
+            auto lst = pthis->m_lDBTool->searchCamDevTableByType(1);
+            DBG("searchCamDevTableByType size:" << lst.size());
+
+            if (lst.size() > 0) {
+                int startCamNO = appPref.getIntData("CamStartNO");
+                int CamCount = appPref.getIntData("CamEndNO") - appPref.getIntData("CamStartNO") + 1;
+                INFO("StartCamNO: " << startCamNO << " CamCount: " << CamCount);
+                auto itor = lst.begin();
+
+                if (startCamNO >= lst.size()) {
+                    ERR("startCamNO > lst.size()");
+                    return;
+                }
+
+                for (int i = 0; i < startCamNO; i++) {
+                    itor++;
+                }
+
+                for (int i = 0; i < CamCount; i++) {
+                    if (itor == lst.end()) {
+                        ERR("itor == lst.end()");
+                        return;
+                    }
+                    std::string t_camIdex = itor->str_cam_dev_id.toStdString();
+                    DBG("-----camID:" << t_camIdex << "min:" << cut_min_duration << "max:" << cut_max_duration);
+                    pthis->m_controllers_videoCapElem[t_camIdex]->SetVideoMinMaxSeconds(cut_min_duration, cut_max_duration);
+                    itor++;
+                }
+            } else {
+                ERR("searchCamDevTableByType size is 0");
+            }
+
+        } else {
+            auto lst = pthis->m_lDBTool->searchCamDevTableByType(0);
+
+            if (lst.size() > 0) {
+                int startCamNO = appPref.getIntData("CamStartNO");
+                int CamCount = appPref.getIntData("CamEndNO") - appPref.getIntData("CamStartNO") + 1;
+                INFO("StartCamNO: " << startCamNO << " CamCount: " << CamCount);
+                auto itor = lst.begin();
+
+                if (startCamNO >= lst.size()) {
+                    ERR("startCamNO > lst.size()");
+                    return;
+                }
+
+                for (int i = 0; i < startCamNO; i++) {
+                    itor++;
+                }
+
+                for (int i = 0; i < CamCount; i++) {
+                    if (itor == lst.end()) {
+                        ERR("itor == lst.end()");
+                        return;
+                    }
+                    std::string t_camIdex = itor->str_cam_dev_id.toStdString();
+                    DBG("-----camID:" << t_camIdex << "min:" << cut_min_duration << "max:" << cut_max_duration);
+                    pthis->m_controllers[t_camIdex]->SetVideoMinMaxSeconds(cut_min_duration, cut_max_duration);
+                    itor++;
+                }
+
+            } else {
+                ERR("searchCamDevTableByType size is 0");
+            }
+
+        }
+    } else {
+        ERR("Error Message!!");
+    }
+}
 
 static std::string rtspAddrBuild(std::string ip, const int port, std::string username,
                                  std::string passwd, std::string brand) {
@@ -51,29 +146,49 @@
         appPref.setIntData("n_cut_max_duration", lst_dev.n_cut_max_duration);
         appPref.setIntData("n_cut_min_duration", lst_dev.n_cut_min_duration);
         if (lst.size() > 0) {
-            for (auto item : lst) {
-                std::string t_camIdex = item.str_cam_dev_id.toStdString();
+            int startCamNO = appPref.getIntData("CamStartNO");
+            int CamCount = appPref.getIntData("CamEndNO") - appPref.getIntData("CamStartNO") + 1;
+            INFO("StartCamNO: " << startCamNO << " CamCount: " << CamCount);
+            auto itor = lst.begin();
+
+            if (startCamNO >= lst.size()) {
+                ERR("startCamNO > lst.size()");
+                return;
+            }
+
+            for (int i = 0; i < startCamNO; i++) {
+                itor++;
+            }
+
+            for (int i = 0; i < CamCount; i++) {
+                if (itor == lst.end()) {
+                    ERR("itor == lst.end()");
+                    return;
+                }
+                std::string t_camIdex = itor->str_cam_dev_id.toStdString();
                 std::string rtsp_url = "GB28181";
-//            rtspAddrBuild(item.str_ip.toStdString(), 554, item.str_username.toStdString(),
-//                                                 item.str_password.toStdString(), item.str_brand.toStdString());
-                INFO("cam add is " << item.str_addr.toStdString());
+
+                INFO("cam add is " << itor->str_addr.toStdString());
+
                 addCamera(t_camIdex, rtsp_url);
+                sleep(1);
+                itor++;
             }
         } else {
-            ERR("searchCamDevTableAll size is 0");
+            ERR("searchCamDevTableByType size is 0");
         }
 
     } else {
-        auto lst = m_lDBTool->searchCamDevTableAll();
+        auto lst = m_lDBTool->searchCamDevTableByType(0);
         Record_Config lst_dev = m_lDBTool->searchConfigTableWithinServerInfo();
 
         //璁剧疆瑙嗛鐨勬渶闀垮拰鏈�鐭椂闂撮棿闅�
         appPref.setIntData("n_cut_max_duration", lst_dev.n_cut_max_duration);
         appPref.setIntData("n_cut_min_duration", lst_dev.n_cut_min_duration);
         if (lst.size() > 0) {
-            int startCamNO = appPref.getIntData("CamStart") * appPref.getIntData("CamStep");
-            int CamCount = appPref.getIntData("CamStep");
-            INFO("StartCamNO: " << startCamNO << " CamStep: " << CamCount);
+            int startCamNO = appPref.getIntData("CamStartNO");
+            int CamCount = appPref.getIntData("CamEndNO") - appPref.getIntData("CamStartNO") + 1;
+            INFO("StartCamNO: " << startCamNO << " CamCount: " << CamCount);
             auto itor = lst.begin();
 
             if (startCamNO >= lst.size()) {
@@ -115,7 +230,7 @@
 //
 //        }
         } else {
-            ERR("searchCamDevTableAll size is 0");
+            ERR("searchCamDevTableByType size is 0");
         }
     }
 }
@@ -165,9 +280,12 @@
             m_imgRedisCRwLock.unlock();
 
             //VideoCaptureElementWithRtp(std::string &chanPubID, int fps, int streamTransType, int gpuIdx = -1)
-            m_controllers_videoCapElem[index] = new BASICGB28181::VideoCaptureElementWithRtp(
-                const_cast<string &>(index),
-                25, 0, 0, this);
+//            m_controllers_videoCapElem[index] = new BASICGB28181::VideoCaptureElementWithRtp(
+//                const_cast<string &>(index),
+//                25, 0, 0, this);
+
+            m_controllers_videoCapElem[index] = new PushStreamAppPipeController(const_cast<string &>(index),
+                                                                                25, 0, appPref.getIntData("gpu.index"), this);
             m_controllers_videoCapElem[index]->SetVideoMinMaxSeconds(lst_dev.n_cut_min_duration,
                                                                      lst_dev.n_cut_max_duration);
             m_controllers_videoCapElem[index]->start();
@@ -194,7 +312,7 @@
             m_imgRedisControllers[index] = new RtspImageRedisElement(index);
             m_imgRedisControllers[index]->start();
 
-            m_controllers[index] = new RtspCaptureElement(rtsp, index, 25, 3000, 0, this);
+            m_controllers[index] = new RtspCaptureElement(rtsp, index, 25, 3000, appPref.getIntData("gpu.index"), this);
             m_controllers[index]->SetVideoMinMaxSeconds(lst_dev.n_cut_min_duration, lst_dev.n_cut_max_duration);
             m_controllers[index]->start();
             m_currentCount++;
@@ -310,7 +428,7 @@
             auto pCaptureElem = m_controllers_videoCapElem.find(nameSt.m_camId);
             if (pCaptureElem != m_controllers_videoCapElem.end()) {
 //#todo ~~~~~~!!!!!!! 杩欓噷鏈塨ug 闇�瑕佹斁寮�璋冭瘯
-//                pCaptureElem->second->SaveVideo(name);
+                pCaptureElem->second->SaveVideo(name);
             } else {
                 ERR("Can not Find CamId " << nameSt.m_camId);
             }
@@ -330,18 +448,18 @@
 
 //淇濆瓨瑙嗛鍒癛tspImageRedis鐨勯槦鍒�,鐢盧tspCaptureElement璋冪敤
 bool RtspAnalysManager::SaveImageToRedis(const std::string &camId, const std::string &imageName, const cv::Mat &img) {
-    INFO("MYH DEBUG HERE  " << camId);
+//    INFO("MYH DEBUG HERE  " << camId);
 
 //    m_imgRedisCRwLock.rdlock();
     auto item = m_imgRedisControllers.find(camId);
     if (item != m_imgRedisControllers.end()) {
-        INFO("Save Succeed Cam: " << camId << " ImageKey: " << imageName);
+//        INFO("Save Succeed Cam: " << camId << " ImageKey: " << imageName);
         item->second->SaveImage(imageName, img);
     } else {
         ERR("Save Failed Cam: " << camId << " ImageKey: " << imageName);
     }
 //    m_imgRedisCRwLock.unlock();
-    INFO("MYH DEBUG HERE");
+//    INFO("MYH DEBUG HERE");
     return true;
 }
 

--
Gitblit v1.8.0