ytdhcamcore.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "ytdhcamcore.h"
  2. #include "ytdhcam.h"
  3. #include "dhinc/dhnetsdk.h"
  4. #define UN_REFERENCED_PARAMETER(x) {(x) = (x);}
  5. int ServiceCallBack(LLONG lHandle, char *pIp, WORD wPort, LONG lCommand, void *pParam, DWORD dwParamLen, LDWORD dwUserData)
  6. {
  7. int i;
  8. UN_REFERENCED_PARAMETER(lHandle);
  9. UN_REFERENCED_PARAMETER(dwParamLen);
  10. printf("IP:%s\n",pIp);
  11. if(lCommand == DH_DVR_SERIAL_RETURN){
  12. QDateTime t = QDateTime::currentDateTime();
  13. QString deviceId = QString((char *)pParam);
  14. ytDHCamCore *core = (ytDHCamCore *)dwUserData;
  15. if(lCommand==DH_DVR_SERIAL_RETURN){
  16. for(i=0;i<102400;i++){
  17. if(sysConfShm->deviceInfo[i].Enabled==0x01){
  18. if(QString::compare(deviceId,QString::fromUtf8(sysConfShm->deviceInfo[i].Device_Code))==0){
  19. if(sysConfShm->deviceInfo[i].RegistStatus==0x01){
  20. if(sysConfShm->deviceInfo[i].keepThread==0x00){
  21. sprintf(sysConfShm->deviceInfo[i].Ip,"%s",pIp);
  22. sysConfShm->deviceInfo[i].Port = wPort;
  23. sysConfShm->deviceInfo[i].lastCommTime = t.toTime_t();
  24. sysConfShm->deviceInfo[i].LoginStatus = 0x00;
  25. core->appendLOg(QString("[ %1 %2 ] new connect").arg(t.toString("yyyy-MM-dd HH:mm:ss.zzz")).arg(QString(pIp)));
  26. core->getNewDev(i);
  27. }
  28. }else{
  29. sysConfShm->deviceInfo[i].RegistStatus = 0x01;
  30. sprintf(sysConfShm->deviceInfo[i].Ip,"%s",pIp);
  31. sysConfShm->deviceInfo[i].Port = wPort;
  32. sysConfShm->deviceInfo[i].lastCommTime = t.toTime_t();
  33. sysConfShm->deviceInfo[i].LoginStatus = 0x00;
  34. core->appendLOg(QString("[ %1 %2 ] new connect").arg(t.toString("yyyy-MM-dd HH:mm:ss.zzz")).arg(QString(pIp)));
  35. core->getNewDev(i);
  36. }
  37. break;
  38. }
  39. }
  40. }
  41. }else if(lCommand==DH_DVR_DISCONNECT){
  42. for(i=0;i<102400;i++){
  43. if(sysConfShm->deviceInfo[i].Enabled==0x01){
  44. if(QString::compare(deviceId,QString::fromUtf8(sysConfShm->deviceInfo[i].Device_Code))==0){
  45. if(sysConfShm->deviceInfo[i].keepThread!=0x00){
  46. sysConfShm->deviceInfo[i].RegistStatus = 0x00;
  47. sysConfShm->deviceInfo[i].LoginStatus = 0x00;
  48. sysConfShm->deviceInfo[i].Port = 0;
  49. bzero(sysConfShm->deviceInfo[i].Ip,sizeof (sysConfShm->deviceInfo[i].Ip));
  50. sysConfShm->deviceInfo[i].keepThread = 0x00;
  51. }
  52. }
  53. }
  54. }
  55. }
  56. }
  57. return 0;
  58. }
  59. void CALLBACK DisConnectFunc(LLONG lLoginID, char *pchDVRIP, LONG nDVRPort, LDWORD dwUser)
  60. {
  61. UN_REFERENCED_PARAMETER(nDVRPort);
  62. int i;
  63. if(dwUser!=0){
  64. QDateTime t = QDateTime::currentDateTime();
  65. ytDHCamCore *core = (ytDHCamCore *)dwUser;
  66. core->appendLOg(QString::fromUtf8("[ %1 %2 ] Disconnect").arg(t.toString("yyyy-MM-dd HH:mm:ss.zzz")).arg(pchDVRIP));
  67. for(i=0;i<102400;i++){
  68. if(sysConfShm->deviceInfo[i].Enabled==0x01){
  69. if((sysConfShm->deviceInfo[i].RegistStatus==0x01)&&(sysConfShm->deviceInfo[i].LoginHandle==lLoginID)){
  70. if(QString::compare(QString(sysConfShm->deviceInfo[i].Ip),QString(pchDVRIP))==0){
  71. sysConfShm->deviceInfo[i].RegistStatus = 0x00;
  72. sysConfShm->deviceInfo[i].LoginStatus = 0x00;
  73. sysConfShm->deviceInfo[i].Port = 0;
  74. bzero(sysConfShm->deviceInfo[i].Ip,sizeof (sysConfShm->deviceInfo[i].Ip));
  75. sysConfShm->deviceInfo[i].keepThread = 0x00;
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }
  82. ytDHCamCore::ytDHCamCore(QObject *parent) : QObject(parent)
  83. {
  84. lListenHandle = 0;
  85. logThread = new LogThread(this);
  86. logThread->start();
  87. dbThread = new DatabaseThread(this);
  88. // connect(dbThread,SIGNAL(SqlAction(QString)),this,SLOT(appendLOg(QString)));
  89. connect(dbThread,&DatabaseThread::AlarmReport,this,&ytDHCamCore::AlarmReport);
  90. dbThread->start();
  91. timer = new QTimer(this);
  92. connect(timer,SIGNAL(timeout()),this,SLOT(timeout()));
  93. for(quint16 i=0;i<8;i++){
  94. wxThreads[i] = new WechartThreads(this,i,"47.98.201.73","/jdxf/wxapp2.php/Home/GeneralPush/voice");
  95. wssThread[i] = new ttsThreads(this,i,"47.98.201.73","/report",55125);
  96. wxThreads[i]->start();
  97. wssThread[i]->start();
  98. }
  99. CLIENT_Init(DisConnectFunc,(LDWORD)this);
  100. }
  101. ytDHCamCore::~ytDHCamCore()
  102. {
  103. if(lListenHandle!=0){
  104. CLIENT_StopListenServer(lListenHandle);
  105. lListenHandle = 0;
  106. }
  107. CLIENT_Cleanup();
  108. }
  109. void ytDHCamCore::getNewDev(int idx)
  110. {
  111. QDateTime t = QDateTime::currentDateTime();
  112. logThread->appendData(QString("[ %1 %2 ] port: %3, id: %4")
  113. .arg(t.toString("yyyy-MM-dd HH:mm:ss.zzz")).arg(sysConfShm->deviceInfo[idx].Ip)
  114. .arg(sysConfShm->deviceInfo[idx].Port).arg(sysConfShm->deviceInfo[idx].Device_Code));
  115. CamThread *thread = new CamThread(idx,this);
  116. connect(thread,&CamThread::finished,thread,&CamThread::quit);
  117. connect(thread,&CamThread::CamMsg,this,&ytDHCamCore::appendLOg);
  118. connect(thread,&CamThread::CamAlarm,this,&ytDHCamCore::CamAlarm);
  119. thread->start();
  120. }
  121. void ytDHCamCore::start()
  122. {
  123. QDateTime t = QDateTime::currentDateTime();
  124. timer->start(1000);
  125. lListenHandle = CLIENT_ListenServer("172.19.198.166",9500,10000,ServiceCallBack,(LDWORD)this);
  126. if(lListenHandle==0){
  127. logThread->appendData(QString("[ %1 %2 ] start port(9500) open failed").arg(t.toString("yyyy-MM-dd HH:mm:ss.zzz")).arg("172.19.198.166"));
  128. }else{
  129. logThread->appendData(QString("[ %1 %2 ] start port(9500) open success").arg(t.toString("yyyy-MM-dd HH:mm:ss.zzz")).arg("172.19.198.166"));
  130. }
  131. }
  132. void ytDHCamCore::appendLOg(QString log)
  133. {
  134. logThread->appendData(log);
  135. }
  136. void ytDHCamCore::timeout()
  137. {
  138. }
  139. void ytDHCamCore::CamAlarm(QString deviceid, QString alarmtype, QString picpath, QString alarmtime){
  140. dbThread->appendAlarm(AlarmRep(deviceid,QString::fromUtf8("insert into sp_video2017 (id,port,device_code,time,status,address,ncmd,data1,data2,data3,data4,data5,clsj,clr,clnr,clwb,cllxr,cldh,clzt,cllx,video) values (NULL,'%1','%2','%3','%4','','%5','%6','%7','%8','电动车禁入/区域入侵告警','','1970-01-01 08:00:00','','','','','',0,'','')")
  141. .arg(2).arg(deviceid).arg(alarmtime).arg(1)
  142. .arg(301).arg(1).arg(picpath).arg(""),
  143. QString("10%1").arg(alarmtype),alarmtime));
  144. }
  145. void ytDHCamCore::AlarmReport(QString addr, quint64 insertid, QString type, QString time, QString companycode, QString phonelist)
  146. {
  147. QString alarmtype = QString::fromUtf8("电动车禁入/区域入侵告警");
  148. QString rep2 = QString("{\"SubType\":16,\"DeviceId\":\"%1\",\"InsertId\":%2,\"Confirmed\":0,\"CompanyCode\":\"%3\",\"evt_name\":\"%4\",\"evt\":0,\"alarm_time\":\"%5\"}")
  149. .arg(addr).arg(insertid).arg(companycode).arg(alarmtype).arg(time);
  150. logThread->appendData(rep2);
  151. wssThread[repcur]->appendData(rep2);
  152. repcur++;
  153. repcur &= 0x07;
  154. if(phonelist.length()>0){
  155. QString data = QString("evt_name=%1&device_code=%2&phone=%3&insert_id=%4&time=%5&bz=%1").arg(alarmtype).arg(addr).arg(phonelist).arg(insertid).arg(time)
  156. .replace(",","%2C").replace(":","%3A").replace(" ","+");
  157. logThread->appendData(data);
  158. wxThreads[wxcur]->appendData(data);
  159. wxcur++;
  160. wxcur &= 0x07;
  161. }
  162. }