海康 ISC

返回:第三方系统对接

初始化插件时容器的长宽必须为数字不能是百分比

/**
 * 获取rsa公钥
 * @param callback
 */
const findPubKey = (callback = (a) => a) => {
  if (webControlObj.current && hasInstalledObj.current) {
    webControlObj.current
      .JS_RequestInterface({
        funcName: funcNameObj.getRSAPubKey,
        argument: JSON.stringify({ keyLength: 1024 }),
      })
      .then((res) => {
        // console.log(res);
        if (res.responseMsg?.data) {
          // console.info(res.responseMsg, '-=-=-=-=-=-=-=-')
          setPubKey(res.responseMsg.data);
          callback(res.responseMsg.data);
        }
      });
  }
};

// RSA加密
const setEncrypt = (paramValue, paramKey) => {
  const encrypt = new JSEncrypt();
  encrypt.setPublicKey(paramKey);
  return encrypt.encrypt(paramValue);
};

// 推送消息
const cbIntegrationCallBack = (oData) => {
  console.info(
    JSON.stringify(oData.responseMsg),
    "OoO_OoO_OoO_OoO_OoO_OoO_OoO"
  );
};

// 初始化数据
const initData = (paramKey) => {
  if (webControlObj.current && paramKey && hasInstalledObj.current) {
    const { host, appKey, appSecret } = generateVideoParam(videoDevice);
    console.info(
      `\n%c设备扩展信息中appKey:%c${appKey}`,
      "color: #00CFFF; background: #000000; padding: 8px 4px",
      "color: #000000; background: #00CFFF; padding: 8px 4px"
    );
    webControlObj.current
      .JS_RequestInterface({
        funcName: funcNameObj.init,
        argument: JSON.stringify({
          ...initDataObj,
          appkey: appKey || initDataObj.appkey,
          ip: host || initDataObj.ip,
          secret: setEncrypt(appSecret || initDataObj.secret, paramKey),
        }),
      })
      .then((res) =>
        webControlObj.current.JS_Resize(videoStyle.width, videoStyle.height)
      );
    // 初始化后resize一次,规避firefox下首次显示窗口后插件窗口未与DIV窗口重合问题
  }
};

/**
 * 初始化插件
 */
const initPlugin = () => {
  // console.info('开始初始化插件>>>>>>')
  webControlObj.current = new WebControl({
    // 指定容器id
    szPluginContainer: videoContainerId,
    // 指定起止端口号,建议使用该值
    iServicePortStart: 15900,
    iServicePortEnd: 15909,
    // 用于IE10使用ActiveX的clsid
    szClassId: "23BF3B0A-2C56-4D97-9C03-0CB103AA8F11",
    // 创建WebControl实例成功
    cbConnectSuccess() {
      console.log("web控件实例创建成功>>>>>");
      hasInstalledObj.current = true;
      // WebControl实例创建成功后需要启动服务
      webControlObj.current
        .JS_StartService("window", {
          // 值"./VideoPluginConnect.dll"写死
          dllPath: "./VideoPluginConnect.dll",
        })
        .then(
          () => {
            // 启动插件服务成功
            webControlObj.current.JS_SetWindowControlCallback({
              // 设置消息回调
              cbIntegrationCallBack,
            });
            // JS_CreateWnd创建视频播放窗口,宽高可设定
            webControlObj.current
              .JS_CreateWnd(
                videoContainerId,
                videoStyle.width,
                videoStyle.height
              )
              .then(() => {
                // console.info(webControlObj.current, '------------------')
                // 创建播放实例成功后初始化
                findPubKey((paramKey) => initData(paramKey));
              });
          },
          () => {
            // 启动插件服务失败
          }
        );
    },
    cbConnectError() {
      // 创建WebControl实例失败
      webControlObj.current = null;
      $(`#${videoContainerId}`).html("插件未启动,正在尝试启动,请稍候...");
      WebControl.JS_WakeUp("VideoWebPlugin://"); // 程序未启动时执行error函数,采用wakeup来启动程序
      setInitCount((prevState) => {
        const newCount = prevState + 1;
        // console.info(newCount, '-=-=-=-=-=-=-=-=-=-')
        if (newCount < 3) {
          setTimeout(() => {
            initPlugin();
          }, 3000);
        } else {
          hasInstalledObj.current = false;
          $(`#${videoContainerId}`).html("");
        }
        return newCount;
      });
    },
    cbConnectClose(bNormalClose) {
      // 异常断开:bNormalClose = false
      // JS_Disconnect正常断开:bNormalClose = true
      console.log("连接断开<<<<<");
      webControlObj.current = null;
    },
  });
};