zhangbo
2017-03-07 2a2f6b1229a0eaf18a0eee67fd3d17d1f9e8420b
ProxyConsole/proxyconsole-hc.cpp
@@ -29,17 +29,19 @@
#include <unistd.h>
#include <jsoncpp/json/json.h>
#include <json/json.h>
#include "MD5Linux.h"
#include "HCNetSDK/incCn/HCNetSDK.h"
#include "HCNetSDK64/incCn/HCNetSDK.h"
#define  HPR_OK 0
#define  HPR_ERROR -1
#define MAX_NUM 16
#define MAX 1024
#define fileread_buf 128
volatile time_t last_alarmout;
typedef std::map<std::string, int> hcnetsdk_user_t; // decoder_ip, userid
hcnetsdk_user_t g_hcnetsdk_user;
@@ -49,12 +51,22 @@
   ET__FIRST,
   ET_PC_SERVER,
   ET_HCNET_DECODER__FIRST,
   ET_HCNET_DECODER_POPWIN,
   ET_HCNET_DECODER_LOGO,
   ET_HCNET_DECODER_ALARMOUT,
   ET_HCNET_DECODER_POPWIN, // 3
   ET_HCNET_DECODER_LOGO, // 4
   ET_HCNET_DECODER_ALARMOUT, // 5
   ET_HCNET_DECODER__LAST,
   ET__LAST
};
struct HCNetSDKConfig
{
   std::string serverIP;
   int serverport;
   std::string username;
   std::string passwd;
};
HCNetSDKConfig g_hcnetsdkconfig;
struct HCNetCameraConfig
{
@@ -219,7 +231,7 @@
            }
            SupperConnectedflag[i]=1;
            SupperConnectedIP[i] = equipments[i].serverIP;
            printf("server connect Success=:%s:%s\n",equipments[i].serverIP,equipments[i].serverport);
            //printf("server connect Success=:%s:%s\n",equipments[i].serverIP,equipments[i].serverport);
            pthread_create(&thread_no[i],NULL,&createServerthread,(void*)i);//super
         }
      }
@@ -311,11 +323,15 @@
   if(NET_DVR_SetAlarmOut(lUserID,camConfig.lAlarmOutPort,1) == FALSE){
      printf("NET_DVR_SetAlarmOut failed, err: %d\n", NET_DVR_GetLastError());
   }
   last_alarmout = time(NULL);
   
   sleep(3);
   
   //解除警报
   NET_DVR_SetAlarmOut(lUserID,camConfig.lAlarmOutPort,0);
   if (time(NULL) - last_alarmout >= 3)
   {
      //解除警报
      NET_DVR_SetAlarmOut(lUserID,camConfig.lAlarmOutPort,0);
   }
}
int hcnetsdk_alarm_popwin(LONG lUserID, const HCNetCameraConfig& camConfig)
@@ -406,14 +422,51 @@
   std::cout << "hcnetsdk_alarm_popwin" << std::endl;
}
int hcnetsdk_alarm_logo()
{
}
void hcnetsdk_finial()
{
   //NET_DVR_Logout(lUserID); //#todo
   NET_DVR_Cleanup();
}
int hcnetsdk_init_logo(LONG lUserID)
{
   //logo信息
   NET_DVR_MATRIX_LOGO_INFO net_dvr_matrix_logocfg1;
   net_dvr_matrix_logocfg1.byRes[32]=0;
   net_dvr_matrix_logocfg1.dwSize=0;
   net_dvr_matrix_logocfg1.dwLogoSize=0;
   char logobuf[5000];
   {
      FILE * pFile;
      pFile = fopen("./ico.bmp","rb");
      if (pFile!=NULL)
      {
         net_dvr_matrix_logocfg1.dwLogoSize = fread(logobuf, 1, sizeof(logobuf), pFile);
         fclose (pFile);
      }
      else
      {
         perror ("Error opening file");
         return -1;
      }
   }
   net_dvr_matrix_logocfg1.dwSize= sizeof(net_dvr_matrix_logocfg1);
   //上传logo
   if (TRUE != NET_DVR_UploadLogo_NEW(lUserID, 1, &net_dvr_matrix_logocfg1 , logobuf))
   {
      printf("NET_DVR_UploadLogo_NEW error, err: %d\n", NET_DVR_GetLastError());
      return -1;
   }
   else
   {
      printf("NET_DVR_UploadLogo_NEW done.\n");
      return 0;
   }
   return -1;
}
int hcnetsdk_init()
@@ -426,14 +479,17 @@
   LONG lUserID;
   NET_DVR_DEVICEINFO_V30 struDeviceInfo;
   lUserID = NET_DVR_Login_V30("192.168.1.101", 8000, "admin", "a1234567", &struDeviceInfo);
   lUserID = NET_DVR_Login_V30((char*)g_hcnetsdkconfig.serverIP.c_str(), g_hcnetsdkconfig.serverport,
      (char*)g_hcnetsdkconfig.username.c_str(), (char*)g_hcnetsdkconfig.passwd.c_str(), &struDeviceInfo);
   if (lUserID < 0) {
      printf("NET_DVR_Login_V30 failed, err: %d\n", NET_DVR_GetLastError());
      NET_DVR_Cleanup();
      return -1;
   }
   
   g_hcnetsdk_user.insert(std::make_pair("192.168.1.101", int(lUserID)));
   g_hcnetsdk_user.insert(std::make_pair(g_hcnetsdkconfig.serverIP, int(lUserID)));
   hcnetsdk_init_logo(lUserID);
   
   return lUserID;
}
@@ -452,6 +508,84 @@
   }
   
   return NULL;
}
sockinfo* hcnetsdk_find_equip(const std::string& decoder_ip)
{
   for (size_t i = 0; i < NUM; i++)
   {
      sockinfo* equip = &(equipments[i]);
      if (equip->m_type > ET_HCNET_DECODER__FIRST || equip->m_type < ET_HCNET_DECODER__LAST)
      {
         if (equip->serverIP == decoder_ip)
            return equip;
      }
   }
   return NULL;
}
sockinfo* hcnetsdk_find_equip(const std::string& decoder_ip, int channel)
{
   for (size_t i = 0; i < NUM; i++)
   {
      sockinfo* equip = &(equipments[i]);
      if (equip->m_type > ET_HCNET_DECODER__FIRST || equip->m_type < ET_HCNET_DECODER__LAST)
      {
         HCNetCameraConfig* camConfig = (HCNetCameraConfig*)equip->user_config;
         if (camConfig != NULL && camConfig->channel == channel && equip->serverIP == decoder_ip)
            return equip;
      }
   }
   return NULL;
}
int hcnetsdk_alarm_logo(LONG lUserID, const HCNetCameraConfig& _camConfig, const std::string& ip, int channel, bool disalarm)
{
   HCNetCameraConfig camConfig(_camConfig);
   if (camConfig.channel != channel)
   {
      camConfig.channel = -1;
      sockinfo* equipConfig = hcnetsdk_find_equip(ip, channel);
      if (equipConfig != NULL)
         camConfig = *(HCNetCameraConfig*)(equipConfig->user_config);
   }
   printf("hcnetsdk_alarm_logo, channel=%d, matchc=%d\n", channel, camConfig.channel);
   if (camConfig.channel < 0)
      return -1;
   //窗口配置信息
   NET_DVR_WIN_LOGO_CFG net_dvr_win_logo_cfg;
   net_dvr_win_logo_cfg.dwLogoNo = 1;
   net_dvr_win_logo_cfg.byEnable = (disalarm ? 0 : 1);//0:hidden,1:visible
   net_dvr_win_logo_cfg.byRes1[3] =0;
   net_dvr_win_logo_cfg.dwCoordinateX = 5;
   net_dvr_win_logo_cfg.dwCoordinateY =5;
   net_dvr_win_logo_cfg.byFlash = 1;
   net_dvr_win_logo_cfg.byTranslucent = 0;
   net_dvr_win_logo_cfg.byRes2[34] = 0;
   net_dvr_win_logo_cfg.dwSize = sizeof(net_dvr_win_logo_cfg);
   //设置窗口输出logo
   int byWallNo=1,dwSubWinNo=2,dwWinNo=1;
   DWORD dwDecChanNum=(camConfig.byWallNo << 24) | (camConfig.dwSubWinNo << 16) | camConfig.dwWinNo;
   if(!NET_DVR_SetDVRConfig(lUserID,NET_DVR_SET_WIN_LOGO_CFG,dwDecChanNum,&net_dvr_win_logo_cfg,net_dvr_win_logo_cfg.dwSize))
   {
      printf("NET_DVR_SetDVRConfig2 error, err: %d\n", NET_DVR_GetLastError());
      return -1;
   }
   else
   {
      printf("NET_DVR_SetDVRConfig2 done.\n");
      //return 0;
   }
   //sleep(3);
   //net_dvr_win_logo_cfg.byEnable = 0;
   //NET_DVR_SetDVRConfig(lUserID,NET_DVR_SET_WIN_LOGO_CFG,dwDecChanNum,&net_dvr_win_logo_cfg,net_dvr_win_logo_cfg.dwSize);
}
int main(int argc,char **argv)
@@ -496,13 +630,16 @@
      timer.it_interval.tv_usec=250000;
      /*Start a virtual timer.It counts down whenever this process is executing.*/
      setitimer(ITIMER_VIRTUAL,&timer,NULL);
      //setitimer(ITIMER_VIRTUAL,&timer,NULL);
      
      
      
      
      while(1)
         sleep(1);
      while ( 1 )
      {
         zclock_sleep(250);
         timer_handler(0);
      }
}
void* createServerthread_hcnetsdk(void* arg)
@@ -516,7 +653,7 @@
   int length=0;
   char buff[102400];
  
   puts("Step.1 ========>Server Thread is strarted......");
   //puts("Step.1 ========>Server Thread is strarted......");
   unsigned char data[4096];  
  //=======================================equipment data================================
  int idx=0;  
@@ -535,16 +672,16 @@
  }
  data[idx++]=0x16;
  write(sock_upper[sidx],&data,idx);//send to equipment;
  printf("Send equipment's info to Server OK! len=:%d\n",idx);
  //printf("Send equipment's info to Server OK! len=:%d\n",idx);
  //=======================================data================================================
  while(1)
   {
      int j;
      length=read(sock_upper[sidx],&buff,sizeof(buff)-1);
      printf("from Server received data===============:%d\n",length);
      //printf("from Server received data===============:%d\n",length);
       if(length<=0)
      {
         printf(" Upper Socket closed \n");
         //printf(" Upper Socket closed \n");
         break;
      }
      else {            
@@ -616,7 +753,7 @@
         continue;
      }
  }
  puts("Server Thread is stoped..");
  //puts("Server Thread is stoped..");
  close(sock_upper[sidx]);
  sock_upper[sidx] = -1;
  SupperConnectedflag[sidx]=0;//?//#todo
@@ -672,7 +809,7 @@
void* MyEquipmentthread(void* arg)
 {
   int eidx = (int)arg;   
   printf("Step.3 ===%d=====>RealTimeEvent Thread is strarted......\n",eidx);
   //printf("Step.3 ===%d=====>RealTimeEvent Thread is strarted......\n",eidx);
   int length=0;
   char buff[102400];
@@ -687,11 +824,22 @@
      }
      else if(length>0)
      {
         bool isDisalarm = false;
         if (strstr(buff, "ViqEventNotificationAlert") == NULL) // <urn
            continue;
         if (strstr(buff, "startTime") == NULL) // <urn
            continue;
            isDisalarm = true;
         //std::cout << buff<<std::endl;
         int channel = -1;
         {
            const char* strChannel = strstr(buff, "<urn:channelId>");
            if (strChannel != NULL)
            {
               channel = strtol(strChannel + strlen("<urn:channelId>"), NULL, 10);
            }
         }
            if(gCapturing[eidx]==1)
         {
            int k;
@@ -733,12 +881,16 @@
                  if(SupperConnectedIP.size() > k)
                  {
                     const std::string& ip(SupperConnectedIP[k]);
                     sockinfo* equipConfig = hcnetsdk_find_equip(ip);
                     HCNetCameraConfig* camConfig =  hcnetsdk_find_config(ip);
                     int lUserData = hcnetsdk_get_userid(ip);
                     if (camConfig != NULL && lUserData >= 0)
                     if (camConfig != NULL && equipConfig != NULL && lUserData >= 0)
                     {
                        std::cout << "alarm begin k=" << k << std::endl;
                        hcnetsdk_alarm_alarmout(lUserData, *camConfig);
                        if (equipConfig->m_type == ET_HCNET_DECODER_ALARMOUT && !isDisalarm)
                           hcnetsdk_alarm_alarmout(lUserData, *camConfig);
                        else if (equipConfig->m_type == ET_HCNET_DECODER_LOGO)
                           hcnetsdk_alarm_logo(lUserData, *camConfig, ip, channel, isDisalarm);
                        std::cout << "alarm end k=" << k << std::endl;
                     }
                     else
@@ -776,6 +928,13 @@
      std::cout << "open config.json error" << std::endl;
      exit(EXIT_FAILURE);
   }
   //HCNetSDKConfig
   Json::Value jsonHcsdk = root["hcsdk"];
   g_hcnetsdkconfig.serverIP = jsonHcsdk["serverIP"].asString();
   g_hcnetsdkconfig.serverport = jsonHcsdk["serverport"].asInt();
   g_hcnetsdkconfig.username = jsonHcsdk["username"].asString();
   g_hcnetsdkconfig.passwd = jsonHcsdk["passwd"].asString();
   Json::Value jsonEquipments = root["equipments"];
   for (int i = 0; i < jsonEquipments.size() && i < sizeof(equipments) / sizeof(sockinfo); i++)
@@ -1033,16 +1192,16 @@
   int i;
   int retVal;
   int result;
   printf("Step.3 ========>GPIO manupulation===> AnalyEquipmentNum=%d \n",equip_num);
   //printf("Step.3 ========>GPIO manupulation===> AnalyEquipmentNum=%d \n",equip_num);
   for(i=0;i<equip_num;i++)
   {
     retVal = -1;
     result=-1;
      printf("=============================:%d \n",equipments[i].reboot);
      //printf("=============================:%d \n",equipments[i].reboot);
      result=net_test(equipments[i].ip,equipments[i].port);
      if(result<0)
      {
              printf("Equipment(%d) check=%s:%s\n",i+1,equipments[i].ip,equipments[i].port);
              //printf("Equipment(%d) check=%s:%s\n",i+1,equipments[i].ip,equipments[i].port);
                     retVal = GPIO_OPEN(PortNum);                   
                     if(retVal == 0)
                      {
@@ -1052,7 +1211,10 @@
                      }
                  else printf("GPIO OPEN FAIL! \n");
      }
      else printf("Equipment(%d) Check Success!=%s:%s\n",i+1,equipments[i].ip,equipments[i].port);
      else
      {
         //printf("Equipment(%d) Check Success!=%s:%s\n",i+1,equipments[i].ip,equipments[i].port);
         }
   }  
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -1300,7 +1462,7 @@
      length=read(rsock_down[Idx],&buff,sizeof(buff));
      alarm(0);
      sigrelse(SIGALRM);   
      printf("Myequipment connect ==================length =%d:\n",length);
      //printf("Myequipment connect ==================length =%d:\n",length);
      if(length<=0)
      {
         close(rsock_down[Idx]);