#include "AppPaController.h" #include #include #include #include #include #include #include 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_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 &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; }