
<template>
  <div style="color: #000">{{ip}} ---- {{pos}}</div>
</template>

<script>
export default {
  props:{
    ip:{
      type:String,
    },
    pos:{
      type:Number,
    }
  },
  data() {
    return {}
  },
  mounted() {

    setTimeout(()=>{
      if(this.pos == 1) $("#divPlugin"+this.pos);
      if(this.pos == 2) $("#divPlugin"+this.pos);
      if(this.pos == 3) $("#divPlugin"+this.pos);
      this.I_InitPlugin(this.ip)
    },2000)
  },
  methods: {
    I_InitPlugin(ip){
      console.log('开始初始化 ip' + this.ip + '位置' + this.pos)
      let that = this
      // 初始化插件参数及插入插件
      WebVideoCtrl.I_InitPlugin({
        // bEmbed: true,
        bWndFull: true,     //是否支持单窗口双击全屏，默认支持 true:支持 false:不支持
        iWndowType: 1,
        // aIframe: ['divPlugin'+that.pos],
        cbInitPluginComplete: function () {
          WebVideoCtrl.I_InsertOBJECTPlugin("divPlugin"+that.pos).then(() => {
            // 检查插件是否最新
            WebVideoCtrl.I_CheckPluginVersion().then((bFlag) => {
              if (bFlag) {
                alert("检测到新的插件版本，双击开发包目录里的HCWebSDKPlugin.exe升级！");
              }
            });
            console.log("divPlugin"+that.pos,"插件初始化成功");
            // that.I_Login('10.22.20.'+ip,80,'admin','abcd1234')
          }, (error) => {
            console.log(error);
            alert("插件初始化失败，请确认是否已安装插件；如果未安装，请双击开发包目录里的HCWebSDKPlugin.exe安装！");
          });
        }
      });
    },
    I_Login(I_IP,I_Port,I_UserName,I_Password){
      var that = this;
      var res = WebVideoCtrl.I_Login(
          I_IP,
          1,
          I_Port,
          I_UserName,
          I_Password,
          {
            // async: false,
            success: function (xmlDoc) {
              console.log(I_IP,I_Port,I_UserName,I_Password,"登录成功");
              // var xmlDoc = WebVideoCtrl.I_GetLocalCfg();
              // console.info(xmlDoc);
              // that.loginNvrs.push(obj);
              setTimeout(() => {
                that.getChannelInfo(I_IP, I_Port);
              }, 30);
            },
            error: function (oError) {
              console.log(I_IP,I_Port,I_UserName,I_Password, " 登录失败！", oError.errorCode, oError.errorMsg)
            },
          }
      );
    },
    getChannelInfo(nvrIp, nvrPort) {
      var that = this;
      // var szIP = that.szIP;
      // 模拟通道
      WebVideoCtrl.I_GetAnalogChannelInfo(nvrIp, {
        // async: false,
        success: function (xmlDoc) {
          var oChannels = $(xmlDoc).find("VideoInputChannel");
          var nvrObj = { title: nvrIp, expand: false, children: [] };
          $.each(oChannels, function (i, val) {
            var id = $(this).find("id").eq(0).text(),
                name = $(this).find("name").eq(0).text(),
                online = $(this).find("online").eq(0).text(),
                port = $(this).find("managePortNo").eq(0).text(),
                ipAddress = $(this).find("ipAddress").eq(0).text();
            // console.info("id:" + id + " name:" + name + " online:" + online);
            if ("false" == online) {
              // 过滤禁用的数字通道
              return true;
            }
            nvrObj.children.push({
              title: name,
              id: id,
              channel: id,
              ip: ipAddress,
              nvrIp: nvrIp,
              port: port,
              nvrPort: nvrPort,
            });
            console.log(nvrObj)
          });
          // that.nvrTree.push(nvrObj);
          console.log("获取模拟通道成功！");
          that.I_startRealPlay(nvrIp,nvrPort,nvrObj.children[0].id,1)
        },
        error: function (error) {
          console.log(
              "获取模拟通道失败！" + error + WebVideoCtrl.I_GetLastError()
          );
        },
      });
      // 数字通道
      // WebVideoCtrl.I_GetDigitalChannelInfo(nvrIp, {
      //   // async: false,
      //   success: function (xmlDoc) {
      //     var oChannels = $(xmlDoc).find("InputProxyChannelStatus");
      //     var nvrObj = { title: nvrIp, expand: false, children: [] };
      //     $.each(oChannels, function (i, val) {
      //       var id = $(this).find("id").eq(0).text(),
      //           name = $(this).find("name").eq(0).text(),
      //           online = $(this).find("online").eq(0).text(),
      //           port = $(this).find("managePortNo").eq(0).text(),
      //           ipAddress = $(this).find("ipAddress").eq(0).text();
      //       // console.info("id:" + id + " name:" + name + " online:" + online);
      //       if ("false" == online) {
      //         // 过滤禁用的数字通道
      //         return true;
      //       }
      //       nvrObj.children.push({
      //         title: name,
      //         id: id,
      //         channel: id,
      //         ip: ipAddress,
      //         nvrIp: nvrIp,
      //         port: port,
      //         nvrPort: nvrPort,
      //       });
      //     });
      //     that.nvrTree.push(nvrObj);
      //     console.log("获取数字通道成功！");
      //   },
      //   error: function () {
      //     WebVideoCtrl.I_GetLastError();
      //     console.log("获取数字通道失败！" + WebVideoCtrl.I_GetLastError());
      //   },
      // });
      // 零通道
      // WebVideoCtrl.I_GetZeroChannelInfo(szIP, {
      //     async: false,
      //     success: function(xmlDoc) {
      //         var oChannels = $(xmlDoc).find("ZeroVideoChannel");
      //         that.nZeroChannel = oChannels;
      //         console.log("获取零通道成功！");
      //     },
      //     error: function() {
      //         console.log("获取零通道失败！" + WebVideoCtrl.I_GetLastError());
      //     }
      // });
    },
    // iStreamType = 视频码流 1 2 3 4  主码流 子码流 第三码流 转码码流
    I_startRealPlay(I_IP,I_Port,I_ChannelsID,iStreamType = 1){
      var oWndInfo = WebVideoCtrl.I_GetWindowStatus(),
          szDeviceIdentify = I_IP+'_'+I_Port,
          iRtspPort = 554,
          iChannelID = I_ChannelsID,
          bZeroChannel = false,
          szInfo = "";
      if (null == szDeviceIdentify) {
        return;
      }
      var startRealPlay = function () {
        console.log(szDeviceIdentify,{
          iStreamType: iStreamType,
          iChannelID: iChannelID,
          bZeroChannel: bZeroChannel,
          iPort: iRtspPort
        })
        WebVideoCtrl.I_StartRealPlay(szDeviceIdentify, {
          iStreamType: iStreamType,
          iChannelID: iChannelID,
          bZeroChannel: bZeroChannel,
          iPort: iRtspPort,
          success: function () {
            szInfo = szDeviceIdentify + "开始预览成功！";
            console.log(szInfo)
          },
          error: function (oError) {
            console.log(szDeviceIdentify + " 开始预览失败！", oError.errorCode, oError.errorMsg)
          }
        });
      };
      startRealPlay();
      // console.log(oWndInfo,oWndInfo.length > 0)
      // if (oWndInfo.length > 0 ) {// 已经在播放了，先停止
      //   console.log('重新开始播放',oWndInfo)
      //   WebVideoCtrl.I_Stop({
      //     success: function () {
      //       console.log('重新开始播放')
      //       startRealPlay();
      //     },
      //     error: function (oError) {
      //       console.log(" 停止预览失败！", oError.errorCode, oError.errorMsg)
      //     }
      //   });
      // } else {
      //   console.log('开始播放')
      //   startRealPlay();
      // }
    },
  }
}
</script>

<style scoped>

</style>
