From ffa3485e9d36911f3b6aeac7ddbcca61c3e3dde6 Mon Sep 17 00:00:00 2001
From: pansen <pansen626@sina.com>
Date: 星期四, 10 一月 2019 20:12:15 +0800
Subject: [PATCH] Merge svn 3352

---
 QiaoJiaSystem/DataManagerServer/http_configserver.cpp |  279 ++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 243 insertions(+), 36 deletions(-)

diff --git a/QiaoJiaSystem/DataManagerServer/http_configserver.cpp b/QiaoJiaSystem/DataManagerServer/http_configserver.cpp
index 65fdc9e..c857f11 100644
--- a/QiaoJiaSystem/DataManagerServer/http_configserver.cpp
+++ b/QiaoJiaSystem/DataManagerServer/http_configserver.cpp
@@ -226,6 +226,20 @@
                                std::bind(&devHttpServer_c::editDevId, this,
                                          std::placeholders::_1, std::placeholders::_2,
                                          std::placeholders::_3, std::placeholders::_4));
+    _HttpSrvRetRecieve.setInfo("^/findSdkRule$", "POST",
+                               std::bind(&devHttpServer_c::findSdkRule, this,
+                                         std::placeholders::_1, std::placeholders::_2,
+                                         std::placeholders::_3, std::placeholders::_4));
+    _HttpSrvRetRecieve.setInfo("^/editSdkRule$", "POST",
+                               std::bind(&devHttpServer_c::editSdkRule, this,
+                                         std::placeholders::_1, std::placeholders::_2,
+                                         std::placeholders::_3, std::placeholders::_4));
+    _HttpSrvRetRecieve.setInfo("^/getSnapshot$", "POST",
+                               std::bind(&devHttpServer_c::getSnapshot, this,
+                                         std::placeholders::_1, std::placeholders::_2,
+                                         std::placeholders::_3, std::placeholders::_4));
+
+
     _HttpSrvRetRecieve.start();
     _HttpSrvRetRecieve.waitForShutDown();
 }
@@ -969,10 +983,10 @@
         elem["str_brand"] = iter->str_brand.toStdString();
         elem["str_reserved"] = iter->str_reserved.toStdString();
 
-        Record_Cam_Sdk rec_sdk = db_c.searchCamSdkTableByCamId(iter->str_cam_dev_id);
-        elem["str_sdks"] = rec_sdk.str_sdks.toStdString();
-        elem["str_det_thr"] = rec_sdk.str_det_thr.toStdString();
-        elem["str_cmp_thr"] = rec_sdk.str_cmp_thr.toStdString();
+//        Record_Cam_Sdk rec_sdk = db_c.searchCamSdkTableByCamId(iter->str_cam_dev_id);
+//        elem["str_sdks"] = rec_sdk.str_sdks.toStdString();
+//        elem["str_det_thr"] = rec_sdk.str_det_thr.toStdString();
+//        elem["str_cmp_thr"] = rec_sdk.str_cmp_thr.toStdString();
 
         cout << elem.toStyledString() << endl;
 
@@ -1021,12 +1035,12 @@
         rec.str_password = QString::fromStdString(value["str_password"].asString());
         rec.str_brand = QString::fromStdString(value["str_brand"].asString());
 
-        rec_sdk_old = db_c.searchCamSdkTableByCamId(rec.str_cam_dev_id);
+//        rec_sdk_old = db_c.searchCamSdkTableByCamId(rec.str_cam_dev_id);
 
-        rec_sdk.str_cam_dev_id = rec.str_cam_dev_id;
-        rec_sdk.str_sdks = QString::fromStdString(value["str_sdks"].asString());
-        rec_sdk.str_det_thr = QString::fromStdString(value["str_det_thr"].asString());
-        rec_sdk.str_cmp_thr = QString::fromStdString(value["str_cmp_thr"].asString());
+//        rec_sdk.str_cam_dev_id = rec.str_cam_dev_id;
+//        rec_sdk.str_sdks = QString::fromStdString(value["str_sdks"].asString());
+//        rec_sdk.str_det_thr = QString::fromStdString(value["str_det_thr"].asString());
+//        rec_sdk.str_cmp_thr = QString::fromStdString(value["str_cmp_thr"].asString());
 
         ret = db_c.updateCamAndSdkTable(rec, rec_sdk);
         if (!ret) {
@@ -1040,14 +1054,14 @@
         db_c.updateConfigTableByDevType(DEV_CAMERA);
         runAllApp();
     }
-        //edit camera's sdks
-    else/* if ((rec_sdk_old.str_sdks != rec_sdk.str_sdks) ||
-             (rec_sdk_old.str_det_thr != rec_sdk.str_det_thr) ||
-             (rec_sdk_old.str_cmp_thr != rec_sdk.str_cmp_thr)) */{
-        killVideoAnalysFromHCApp();
-        sleep(1);
-        runAllApp();
-    }
+    //edit camera's sdks
+//    else/* if ((rec_sdk_old.str_sdks != rec_sdk.str_sdks) ||
+//             (rec_sdk_old.str_det_thr != rec_sdk.str_det_thr) ||
+//             (rec_sdk_old.str_cmp_thr != rec_sdk.str_cmp_thr)) */{
+//        killVideoAnalysFromHCApp();
+//        sleep(1);
+//        runAllApp();
+//    }
 
     Json::Value value_out;
     value_out["str_cam_dev_id"] = rec.str_cam_dev_id.toStdString();
@@ -1435,8 +1449,8 @@
 
     if (reader.parse(content, value)) {
         int cut_max_duration = value["cut_max_duration"].asInt();
-
-        ret = db_c.updateConfigTableByCutDuration(cut_max_duration);
+        int cut_min_duration = value["cut_min_duration"].asInt();
+        ret = db_c.updateConfigTableByCutDuration(cut_max_duration, cut_min_duration);
 
     } else {
         return "{\"ret_status\": \"浼犺緭閿欒锛岃妫�鏌ワ紒\"}";
@@ -1460,10 +1474,11 @@
 
     Json::Value value;
 
-    int cut_max_duration = db_c.searchConfigTableWithinCutDuration();
 
-    value["cut_max_duration"] = cut_max_duration;
+    Record_Config rec = db_c.searchConfigTableWithinServerInfo();
 
+    value["cut_max_duration"] = rec.n_cut_max_duration;
+    value["cut_min_duration"] = rec.n_cut_min_duration;
     std::string out = value.toStyledString();
 
     return out;
@@ -2371,9 +2386,27 @@
 
             std::string str_tmpTime = AppUtil::getCmdResult(cmd_getVideoFileTime);
 
+
+
             if (str_time > str_tmpTime) {
-                ERR("{\"error\":\"Time error\"}" << str_time << " str_tmpTime " << str_tmpTime);
-                str_time = str_tmpTime.size() > 0 ? str_tmpTime : str_time;
+
+                //str_time = str_tmpTime.size() > 0 ? str_tmpTime : str_time;
+                 qint64 v_time=getVideoTime(str_tmpTime)-1000;
+                 int haomiao = (v_time % 1000) > 50 ? ((v_time % 1000) - 50) : 0;
+                 v_time /= 1000;
+                 int m = v_time;
+                 int shi, fen, miao;
+                 char ch_time[128];
+                 if (m / 3600 < 24) {
+                     shi = m / 3600;
+                     fen = m / 60 % 60;
+                     miao = m % 60;
+                     sprintf(ch_time, "%02d:%02d:%02d.%02d", shi, fen, miao, haomiao / 10);
+
+                 } else printf("杈撳叆鏁板�煎お澶�");
+
+                str_time=ch_time;
+                  ERR("{\"error\":\"Time error\"}" << str_time << " str_tmpTime " << str_tmpTime<<" ch_time="<<ch_time);
 //            return "{\"error\":\"Time error\"}";
             }
 
@@ -2524,7 +2557,7 @@
 std::string devHttpServer_c::getVideoPathByTime(const std::string &time, const std::string &camId, qint64 &sub) {
     std::string t_FilePath = appConfig.getStringProperty("cutPath");
 
-    bool find = false;;
+    bool find = false;
     if (t_FilePath.back() != '/') {
         t_FilePath.push_back('/');
     }
@@ -2541,8 +2574,8 @@
     qint64 t = dt.toMSecsSinceEpoch();
     std::vector<std::string> vec = forEachFile(t_FilePath);
     std::sort(vec.begin(), vec.end());
-    DBG("t_FilePath" << t_FilePath);
-    DBG("vec.size()" << vec.size());
+//    DBG("t_FilePath" << t_FilePath);
+//    DBG("vec.size()" << vec.size());
     int size = vec.size();
     for (int i = 0; i < size; ++i) {
         qint64 t1 = QDateTime::fromString(QString::fromStdString(vec[i]),
@@ -2554,7 +2587,7 @@
                 DBG("path=" << vec[i]);
                 t_FilePath += vec[i] + ".mp4";
                 sub = t - t1;
-                find = sub < getVideoTime(t_FilePath);
+//                find = sub < getVideoTime(t_FilePath);
                 break;
             }
 
@@ -2564,7 +2597,7 @@
                 DBG("path=" << vec[i]);
                 t_FilePath += vec[i] + ".mp4";
 
-                find = sub < getVideoTime(t_FilePath);
+//                find = sub < getVideoTime(t_FilePath);
 
                 DBG("find:" << find);
                 break;
@@ -2574,18 +2607,18 @@
 
     }
     DBG("sub=" << sub);
-    if (!find) {
-        t_FilePath = "";
-    }
+//    if (!find) {
+//        t_FilePath = "";
+//    }
     return t_FilePath;
 
 }
 
-qint64 devHttpServer_c::getVideoTime(const std::string &videoPath) {
-    std::string cmd_getVideoFileTime(
-        " ffmpeg -i '" + videoPath + "' 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//");
+qint64 devHttpServer_c::getVideoTime(/*const std::string &videoPath,*/std::string& str_tmpTime) {
+//    std::string cmd_getVideoFileTime(
+//        " ffmpeg -i '" + videoPath + "' 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//");
 
-    std::string str_tmpTime = AppUtil::getCmdResult(cmd_getVideoFileTime);
+//     str_tmpTime = AppUtil::getCmdResult(cmd_getVideoFileTime);
     DBG("str_tmpTime:" << str_tmpTime);//00:00:06.89
     qint64 len_ms = 0;
 
@@ -2814,4 +2847,178 @@
         return "";
 //        return "{\"error\":\"浼犺緭閿欒锛岃妫�鏌ワ紒\"}";
     }
-}
\ No newline at end of file
+}
+
+std::string devHttpServer_c::editSdkRule(std::string ip, unsigned int port, std::string content, PResponse &response) {
+    Json::Reader reader;
+    Json::Value value;
+    Json::FastWriter writer;
+    if (reader.parse(content, value)) {
+        SdkRule rule;
+        rule.nSdkType = value["nSdkType"].asInt();
+        rule.nAlarmNumLowerLimit = value["nAlarmNumLowerLimit"].asInt();
+        rule.nAlarmNumUpperLimit = value["nAlarmNumUpperLimit"].asInt();
+        rule.nTriggerDelay = value["nTriggerDelay"].asInt();
+        rule.nTriggerTolerance = value["nTriggerTolerance"].asInt();
+        rule.nThreshold = value["nThreshold"].asInt();
+        rule.nQuality = value["nQuality"].asInt();
+        rule.nIsRun = value["nIsRun"].asInt();
+        rule.strBroadcast = value["nIsBroadcast"].asCString();
+        rule.strAreas = value["strAreas"].asCString();
+        rule.strCamId = value["strCamId"].asCString();
+        rule.strExAreas = value["strExAreas"].asCString();
+        rule.strLine = value["strLine"].asCString();
+        rule.strExLine = value["strExLine"].asCString();
+        if (rule.strCamId.isEmpty()) {
+            return "{\"ret_status\":\"鍐呭鏈夎锛岃妫�鏌ワ紒\"}";
+        }
+        if (rule.nSdkType <= SdkTypeStart || rule.nSdkType >= SdkTypeEnd) {
+            return "{\"ret_status\":\"鍐呭鏈夎锛岃妫�鏌ワ紒\"}";
+        }
+
+        for (int i = 0; i < value["weekRuleArray"].size(); ++i) {
+            Json::Value item = value["weekRuleArray"][i];
+            LActRuleWeekRecord weekRule;
+            weekRule.m_nSdkType = rule.nSdkType;
+            weekRule.m_nType = item["nType"].asInt();
+            weekRule.m_strCamId = rule.strCamId;
+            weekRule.m_strBegin = item["strBegin"].asCString();
+            weekRule.m_strEnd = item["strEnd"].asCString();
+            //rule.weekRuleVec.push_back(weekRule);
+            db_c.updateCameraWeekRule(weekRule);
+        }
+
+
+        if (db_c.updateSdkRule(rule)) {
+//            if(rule.nIsRun==1)
+
+            killVideoAnalysFromHCApp();
+            sleep(1);
+            runAllApp();
+
+
+            return "{\"ret_status\": \"ok_ack\"}";
+        } else {
+            return "{\"ret_status\":\"鍐呭鏈夎锛岃妫�鏌ワ紒\"}";
+        }
+    } else {
+        return "{\"error\":\"浼犺緭閿欒锛岃妫�鏌ワ紒\"}";
+    }
+}
+
+std::string devHttpServer_c::findSdkRule(std::string ip, unsigned int port, std::string content, PResponse &response) {
+    Json::Reader reader;
+    Json::Value value;
+
+    if (reader.parse(content, value)) {
+
+        QString strCamId = value["strCamId"].asCString();
+
+        SdkRuleMap ruleMap = db_c.searchSdkRuleByCamId(strCamId);
+
+        int count = ruleMap.size();
+
+        if (count > 0) {
+            Json::Value objs;
+            Json::Value obj;
+
+            for (auto rule:ruleMap) {
+                obj["nSdkType"] = rule.second.nSdkType;
+                obj["nAlarmNumLowerLimit"] = rule.second.nAlarmNumLowerLimit;
+                obj["nAlarmNumUpperLimit"] = rule.second.nAlarmNumUpperLimit;
+                obj["nTriggerDelay"] = rule.second.nTriggerDelay;
+                obj["nTriggerTolerance"] = rule.second.nTriggerTolerance;
+                obj["nThreshold"] = rule.second.nThreshold;
+                obj["nQuality"] = rule.second.nQuality;
+                obj["nIsRun"] = rule.second.nIsRun;
+                obj["nIsBroadcast"] = rule.second.strBroadcast.toStdString();
+                obj["strAreas"] = rule.second.strAreas.toStdString();
+                obj["strCamId"] = rule.second.strCamId.toStdString();
+                obj["strExAreas"] = rule.second.strExAreas.toStdString();
+                obj["strLine"] = rule.second.strLine.toStdString();
+                obj["strExLine"] = rule.second.strExLine.toStdString();
+                Json::Value weekRuleArray;
+
+
+                Json::Value weekRule;
+                std::vector<LActRuleWeekRecord> weekRuleVec = db_c.searchCameraWeekRuleByCamId(strCamId, rule.first);
+                for (int k = 0; k < weekRuleVec.size(); ++k) {
+                    Json::Value weekRuleObj;
+                    weekRuleObj["nSdkType"] = weekRuleVec[k].m_nSdkType;
+                    weekRuleObj["strCamId"] = weekRuleVec[k].m_strCamId.toStdString();
+                    weekRuleObj["strBegin"] = weekRuleVec[k].m_strBegin.toStdString();
+                    weekRuleObj["strEnd"] = weekRuleVec[k].m_strEnd.toStdString();
+                    weekRuleObj["nType"] = weekRuleVec[k].m_nType;
+                    weekRule.append(weekRuleObj);
+                }
+                //weekRuleArray[j]=weekRule;
+
+
+                obj["weekRuleArray"] = weekRule;
+                objs.append(obj);
+            }
+            return objs.toStyledString();
+        } else {
+//            return "{\"ret_status\":\"鍐呭鏈夎锛岃妫�鏌ワ紒\"}";
+            return "[]";
+        }
+    } else {
+        return "{\"error\":\"浼犺緭閿欒锛岃妫�鏌ワ紒\"}";
+    }
+}
+
+std::string devHttpServer_c::getSnapshot(std::string ip, unsigned int port, std::string content, PResponse &response) {
+    Json::Reader reader;
+    Json::Value value;
+
+    if (reader.parse(content, value)) {
+
+        std::string ip = value["str_ip"].asString();
+//        int port= value["n_port"].asInt();
+        int port = 554;
+        std::string username = value["str_username"].asString();
+        std::string pass = value["str_password"].asString();
+        std::string brand = value["str_brand"].asString();
+        std::string rtsp_url = rtspAddrBuild(ip, port, username, pass, brand);
+        if (rtsp_url.empty()) {
+            return "{\"ret_status\":\"鍐呭鏈夎锛岃妫�鏌ワ紒\"}";
+        }
+        std::string str_imgName = appConfig.getStringProperty("cutPath");
+        if (str_imgName.back() != '/') {
+            str_imgName.push_back('/');
+        }
+        str_imgName += "snapshot.jpg";
+        //admin:a1234567@192.168.1.201:554/h264/ch1/main/av_stream -r 1/25 -f image2 -s 1920*1080 /home/basic/work_src/a.jpg
+        std::string cmd("ffmpeg -i " + rtsp_url + " -r 1/25  -f image2 -s 1920*1080 -y " + str_imgName);
+        DBG(cmd);
+        system(cmd.c_str());
+
+        cv::Mat img = cv::imread(str_imgName);
+
+        if (img.empty()) {
+
+            return "{\"ret_status\":\"鍐呭鏈夎锛岃妫�鏌ワ紒\"}";
+        }
+        fdfsClient.rwLock.rdlock();
+        std::string strImgUrl = "http://";
+        if (fdfsClient.fastFds != nullptr) {
+            std::vector<unsigned char> buffer;
+            CvUtil::cvMat2Buffer(img, buffer);
+            std::string strImgUrlTmp = "";
+            fdfsClient.fastFds->uploadFile(buffer, strImgUrlTmp, "jpg");
+            strImgUrl.append(fdfsClient.fastFds->getIp() + "/" + strImgUrlTmp);
+            strImgUrl.clear();
+            strImgUrl = strImgUrlTmp;
+        }
+        fdfsClient.rwLock.unlock();
+        Json::Value result;
+        result["imgUrl"] = strImgUrl;
+
+        return result.toStyledString();
+
+
+    } else {
+        return "{\"error\":\"浼犺緭閿欒锛岃妫�鏌ワ紒\"}";
+    }
+}
+

--
Gitblit v1.8.0