// +build ignore

#include "./include/MvCameraControl.h"
#include "../swapi/include_swapiLib.h"

const char *MODULE_NAME = "HKCamReset";

static const char *manufacturer = "Hikrobot"; // 制造厂商

int main(int argc,char *argv[])
{
  HANDLE hCam = NULL; MV_CC_DEVICE_INFO_LIST devList = { 0 };
  MV_CC_DEVICE_INFO *pDevInfo; int ret, index = 0; char name[MAX_LINE_CHARS];

  // 1, 查找相机
  ret = MV_CC_Initialize();
  if(MV_OK != ret)
  {
    sw_log_error("[%s] 相机SDK初始化失败, errCode=0x%x!!", MODULE_NAME, ret);
    goto end_p;
  }

  ret = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &devList); // 目前只枚举G口和U口相机
  if(MV_OK != ret)
  {
    sw_log_error("[%s] 枚举相机,执行错误, errCode=0x%x!!", MODULE_NAME, ret);
    goto end_p;
  }
  if(devList.nDeviceNum < 1)
  {
    ret = -1;
    sw_log_error("[%s] 没有相机,数量=%u!!", MODULE_NAME, devList.nDeviceNum);
    goto end_p;
  }

findp:
  if(index < devList.nDeviceNum) pDevInfo = devList.pDeviceInfo[index++];
  else goto end_p;
  if(!pDevInfo)
  {
    ret = -2;
    sw_log_error("[%s] unexpected internal error, unable to obtain detailed information about the industrial camera!!", MODULE_NAME);
    goto end_p;
  }

  name[0] = '\0'; if(0) ;
  else if(pDevInfo && pDevInfo->nTLayerType == MV_USB_DEVICE /*U口相机*/) strcpy(name, (const char *)pDevInfo->SpecialInfo.stUsb3VInfo.chManufacturerName);
  else if(pDevInfo && pDevInfo->nTLayerType == MV_GIGE_DEVICE/*G口相机*/) strcpy(name, (const char *)pDevInfo->SpecialInfo.stGigEInfo.chManufacturerName);
  if(xstrcasecmp(name, manufacturer) != 0) goto findp;

  // 2, 打开相机
  ret = MV_CC_CreateHandleWithoutLog(&hCam, pDevInfo);
  if(MV_OK == ret) ret = MV_CC_OpenDevice(hCam, MV_ACCESS_Exclusive, 0);
  if(MV_OK != ret)
  {
    sw_log_error("[%s] 打开相机时发生错误, errCode=0x%x!!", MODULE_NAME, ret);
    goto end_p;
  }

  // 3, 重置相机
  ret = MV_CC_SetCommandValue(hCam, "DeviceReset");
  if(MV_OK != ret)
  {
    sw_log_error("[%s] 重置相机时发生错误, errCode=0x%x!!", MODULE_NAME, ret);
    goto end_p;
  }

  if(index < devList.nDeviceNum) goto findp;

  // 4, 关闭相机
end_p:
  if(hCam)
  {
    MV_CC_CloseDevice(hCam);
    MV_CC_DestroyHandle(hCam);
  }
  MV_CC_Finalize();

  return ret;
}
