派生自 development/c++

pansen
2019-03-07 979bc003bce710bf300bc2bd87a8278585678763
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include "AppPaController.h"
#include <basic/util/app/AppPreference.hpp>
#include <basic/util/app/AppUtil.h>
#include <algorithm>
#include <basic/util/app/AppConfig.h>
#include <QtCore/QDateTime>
#include <QtCore/QtGlobal>
#include <net_config.h>
 
 
AppPaController:: AppPaController(int index, const ControllerConfig& cfg ):
 videoCaptureElement(cfg.camInfo.strRtsp, 25, 3000, appPref.getLongData("gpu.index"), true),
 m_index(index),
 fdfsClient(nullptr),
 yoloRpcElement(to_string(index) + "yoloRpc"),
 triggerElement(cfg.paAlarmInfo.nTriggerDelay, cfg.paAlarmInfo.nAlarmDelay),
 recordVideoElement(index, cfg.camInfo.strRtsp),
 m_cfg(cfg)
 {
 
      init();
 }
 
AppPaController::~AppPaController() {
 
}
 
 
 
bool AppPaController::getRunning() {
    return videoCaptureElement.isRunning();
}
 
 
 
void AppPaController::init() {
 
    unsigned char ip_old[15] = {0};
    std::string net_ifname = appConfig.getStringProperty("netIfName");
//    if (!(GetIpAddress(net_ifname.c_str(), ip_old))) {
//        ERR("couldn't get ip");
//    }
 
    bUp = false;
 
    yoloRpcElement.setProperty("str_addr", m_cfg.camInfo.strAdrr);
    yoloRpcElement.setProperty("local_ip", std::string((char *) ip_old));
   // yoloRpcElement.setMask(QString::fromStdString(m_cfg.paAlarmInfo.strMaskPath));
    yoloRpcElement.SetRuleCfg(&m_cfg);
    yoloRpcElement.setProperty("dev_id", m_cfg.camInfo.strCamId);
    m_json_Record["rtsp"] = m_cfg.camInfo.strRtsp;
 
 
 
    videoCaptureElement.registerConnector([&] {
 
            auto i_t = videoCaptureElement.getChangeLevel();
            if (i_t > 80)
            {
//                cout << __FUNCTION__ << " -> " << __LINE__ << " -> " << i_t << endl;
                triggerElement.setState(true);
            }
            else if (i_t < 50)
            {
//                cout << __FUNCTION__ << " -> " << __LINE__ << " -> " << i_t << endl;
                triggerElement.setState(false);
            }
 
            triggerElement.triggerOnce();
 
            int test_sub = triggerElement.getState() ? 1 : 5;//
 
            videoCaptureElement.setOutPutInterval(test_sub);
 
            if (triggerElement.getTriggerState()) {
                if (!bUp) {
 
                    string t_FilePath = "";
                    m_json_Record["path"] = t_FilePath;
                    std::string t_recJsonStr = m_fastWriter.write(m_json_Record);
                    t_FilePath = recordVideoElement.startRecord(t_recJsonStr);
                    INFO(m_cfg.camInfo.strCamId << "file keey is ::" << t_FilePath);
                    yoloRpcElement.setProperty("path", t_FilePath);
                    bUp = true;
 
                    std::thread timerFunc([&] {
                        //录制时间超过用户设定的时间后截断,重新录制
                        long startTime = AppUtil::getCurrentMs();
                        int cut_max_duration = appPref.getIntData("n_cut_max_duration");
                        long triggerTime = cut_max_duration == -1 ? 2 * 60 * 1000 : cut_max_duration * 1000;
                        while (bUp) {
                            if (AppUtil::getCurrentMs() - startTime > triggerTime) {
//                                if (bUp) {
                                bUp = false;
//                                }
                                break;
                            } else {
                                usleep(5000);
                            }
                        }
                    });
                    timerFunc.detach();
                } else {
                    recordVideoElement.feedDog();
                }
            } else {
                if (bUp) {
                    bUp = false;
                    //DBG(m_camId << "endRecord");
                    recordVideoElement.endRecord();
                }
//                return;
            }
 
 
 
        std::string strNewTime;
        if (m_cfg.camInfo.strCamId.size() > 0) {
            strNewTime = AppUtil::getTimeSecString();
        } else {
            //#todo 计算当前文件时间
            //#TODO 输出间隔用于计算时间
            // 现为每三帧计算一次,导致计算的时间长于文件的实际时间
            auto opit = 25;// videoCaptureElement.getOutPutInterval();
            auto opidx = videoCaptureElement.getoutPutIndex();
            int second = opidx / opit;
            qint64 newTime = m_dt.toMSecsSinceEpoch() + second * 1000;
            QDateTime newDt = QDateTime::fromMSecsSinceEpoch(newTime);
            strNewTime = newDt.toString("yyyy-MM-dd hh:mm:ss").toStdString();
 
        }
//        DBG(strNewTime);
 
 
 
        if (!yoloRpcElement.isBusy()) {
//            yoloRpcElement.setProperty("uuid", uuid);
            //#todo
            yoloRpcElement.setProperty("time", strNewTime);
            yoloRpcElement.setImage(videoCaptureElement.getImage());
            yoloRpcElement.submit();
        }
 
        if (!imageDrawElement.isBusy()) {
            imageDrawElement.setImage(videoCaptureElement.getImage());
            imageDrawElement.submit();
        }
 
 
    });
 
 
    triggerElement.registerConnector([&] {
 
//        std::string strImgPath,strVideoPath,strCurTime;
//        makePath(strImgPath,strVideoPath,strCurTime);
        if(yoloRpcElement.getRealNum()<m_cfg.paAlarmInfo.nAssembleCount)
            return;
        switch (triggerElement.getTriggerType()) {
        case UP:
            INFO("UP:sendVideoAlarm(true) num="<<yoloRpcElement.getRealNum());
          //  this->sendVideoAlarm(true);
            //this->openSoundAlarm();
            //if(isSaveResult()) alarmResultSaveToDB(strImgPath,strVideoPath,strCurTime);
            //if(isSaveVideo())  recordVideoElement.startRecord()strVideoPath.c_str());
            break;
        case DOWN:
 
            INFO("UP:sendVideoAlarm(false) num="<<yoloRpcElement.getRealNum());
           // this->sendVideoAlarm(false);
           // this->closeSoundAlarm();
            //if(isSaveVideo()) recordVideoElement.endRecord();
            break;
        default:
            break;
        };
//        if(isSaveImage() &&triggerElement.getTriggerType()==UP)
//        {
 
//             std::vector<cv::Rect> rects ;
//             std::vector<ScoredRect> scoredRects=yoloRpcElement.getLastScoreRects();
//             for(auto scoreRect:scoredRects)
//             {
//                 rects.push_back(scoreRect.rect);
//             }
 
//            cv::Mat img = this->videoCaptureElement.getImage();
//            AppPaController::saveRectsImage(rects,img,strImgPath.c_str());
//        }
 
    });
    yoloRpcElement.registerConnector([&] {
        imageDrawElement.setYoloObjects(yoloRpcElement.getLastScoreRects());
        triggerElement.setState(yoloRpcElement.getRealNum()>=m_cfg.paAlarmInfo.nAssembleCount);
 
    });
 
 
    imageDrawElement.registerConnector([&] {
        if (appPref.getIntData("show.image") == 1) {
            ImageShowElement::showImage(to_string(this->m_index), *imageDrawElement.getImage());
        }
    });
 
    registerElement(videoCaptureElement);
 
    registerElement(yoloRpcElement);
 
 
    registerElement(imageDrawElement);
 
    registerElement(triggerElement);
 
    videoCaptureElement.setOutPutInterval(3);
 
}
 
 
 
void AppPaController::setfdfsClient(FastFdsWithLock *p_fdfsClient) {
    fdfsClient = p_fdfsClient;
    yoloRpcElement.setFdfs(fdfsClient);
}
 
std::string AppPaController::getRtmp() {
    std::string ret = "";// = videoPublishElement.getPath();
    size_t pos = ret.find(".flv");
    ret = ret.substr(0, pos);
    INFO(ret);
    return ret;
}
//void AppPaController::sendVideoAlarm(bool value)
//{
//    ::Alarm::AlarmInfo alarmInfo;
//    alarmInfo.bAlarm = value;
//    alarmInfo.strRtsp = m_cfg.camInfo.strRtsp;
//    alarmInfo.wallNo = m_cfg.paAlarmInfo.nWallNum;
//    alarmInfo.subWallNo = m_cfg.paAlarmInfo.nWallSubNum;
//    alarmInfo.windowNo = m_cfg.paAlarmInfo.nWindowNum;
//    try {
//        auto server = alarmRpc.getServer();
//        if(server){
//            server->AlarmSingle(alarmInfo);
//        }else{
//            ERR("sendVideoAlarm falid; server is null");
//        }
//    } catch (std::exception& e) {
//        ERR(e.what());
//    }
//}
 
void AppPaController::openSoundAlarm()
{
    try {
//        auto server = alarmRpc.getServer();
//        if(server){
//            server->AlarmSound(appPref.getStringData("pa.identity"),true);
//        }else{
//            ERR("openSoundAlarm falid; server is null");
//        }
    } catch (std::exception& e) {
        ERR(e.what());
    }
}
 
void AppPaController::closeSoundAlarm()
{
    try {
//        auto server = alarmRpc.getServer();
//        if(server){
//            server->AlarmSound(appPref.getStringData("pa.identity"),false);
//        }else{
//            ERR("closeSoundAlarm falid; server is null");
//        }
    } catch (std::exception& e) {
        ERR(e.what());
    }
}
 
void AppPaController::playSound()
{
    try {
//        auto server = soundRpc.getServer();
//        if(server){
 
//            server->SoundPlay(m_index,soundInfos);
//        }else{
//            ERR("playSound falid; server is null");
//        }
    } catch (std::exception& e) {
        ERR(e.what());
    }
}
 
void AppPaController::stopSound()
{
    try {
//        auto server = soundRpc.getServer();
//        if(server){
//            server->SoundStop(m_index);
//        }else{
//            ERR("stopSound falid; server is null");
//        }
    } catch (std::exception& e) {
        ERR(e.what());
    }
}
 
 
 
 
//void AppPaController::alarmResultSaveToDB(std::string& imgPath,std::string& videoPath,std::string& curTime)
//{
//    try {
//        auto server = m_SaveAlarmRpc.getServer();
//        if(server){
//            ::SaveModule::AlarmInfo iceAlarmInfo;
//            iceAlarmInfo.nCamID = index;
//            iceAlarmInfo.nStatus = 0;
//            iceAlarmInfo.strAlarmDescribe = "invade";
//            size_t pos = imgPath.find("/static");
//            iceAlarmInfo.strAlarmImgPath = imgPath.substr(pos,imgPath.length()-pos);
//            pos = videoPath.find("/static");
//            iceAlarmInfo.strAlarmVideoPath = videoPath.substr(pos,videoPath.length()-pos);
//            iceAlarmInfo.strCreateTime = curTime;
//            server->saveAlarmInfo(iceAlarmInfo);
//        }else{
//            ERR("ice alarmSaveToDB falid; server is null");
//        }
//    }catch (std::exception& e) {
//        ERR(e.what());
//    }
//}
 
static int AppPaController::saveRectsImage(const vector<cv::Rect2f> &rects, const cv::Mat &img, const char *path)
{
    if(img.rows==0||img.cols==0)return -1;
    cv::Mat image = img.clone();
    for(auto& rectf:rects){
        cv::Rect rect(rectf.x*image.cols,rectf.y*image.rows,rectf.width*image.cols,rectf.height*image.rows);
        cv::rectangle(image, rect,cv::Scalar(0,0,255),4);
    }
    return cv::imwrite(path,image);
 
}
 
bool AppPaController::isSaveVideo()
{
    return appPref.getIntData("video.save") != 0;
}
 
bool AppPaController::isSaveResult()
{
    return appPref.getIntData("alarmresult.save") != 0;
}
 
bool AppPaController::isSaveImage()
{
    return appPref.getIntData("image.save") != 0;
}