<template>
  <p align="center"><textarea rows="20" :value="this.cardId" cols="111"></textarea></p>
</template>

<script>
export default {
  name : 'test',
  data() {
    return {
      websock: null,
      paramStr: '',
      isComOpen: false,
      cardId: ''
    }
  },
  created() {
    this.initWebSocket();
  },
  destroyed() {
    console.log("离开路由之后断开websocket连接")
    this.websock.close() //离开路由之后断开websocket连接
  },
  methods: {
    initWebSocket(){ //初始化websocket
      const wsuri = "ws://localhost:81/webReaderServer";
      if ("WebSocket" in window){
        this.websock = new WebSocket(wsuri);
      }
      else if("MozWebSocket" in window){
        this.websock = new MozWebSocket(wsuri);
      }
      this.websock.onmessage = this.websocketonmessage;
      this.websock.onopen = this.websocketonopen;
      this.websock.onerror = this.websocketonerror;
      this.websock.onclose = this.websocketclose;
    },
    websocketonopen(){ //连接建立之后执行send方法发送数据
      console.log("连接建立之后执行send方法发送数据")
      this.websocketsend("0webReader");
      this.websocketsend("10|adaptReader|1");
    },
    websocketonerror(){//连接建立失败重连
      this.cardId = "服务未运行"
      this.initWebSocket();
    },
    websocketonmessage(e){ //数据接收
      console.log(e.data);
      var str = "";
      str = e.data;
      var id = str.substr(0, 1);
      var separator = str.indexOf("|");
      var funcid = "";
      var arg1 = "";
      if (separator != -1) {
        funcid = str.substr(1, separator - 1);
        arg1 = str.substr(separator + 1);
      } else
        funcid = str.substr(1);
      //alert("id:" + id + ",funcid:" + funcid +",arg1:" + arg1);
      var resultData = {
        type: "Result",
        FunctionID: parseInt(funcid),
        RePara_Int: parseInt(this._getCmdResult(arg1)),
        RePara_Str: this._getResultPara(arg1)
      };
      if(resultData.FunctionID == "0"){
        //连接上websocket，连接读卡器
        this.cardConnect()
      }else if(resultData.FunctionID == "1"){
        //配置参数
        this.paramStr = resultData.RePara_Str
        this.websocketsend("1399|config_card|" + this.paramStr +"65");
      }else if (resultData.FunctionID == "399") {
        //寻找卡片
        this.findCard()

      }else if (resultData.FunctionID == "32") {
        if (resultData.RePara_Str!= "") {
          this.cardId = resultData.RePara_Str
          console.log("卡号：" + this.cardId)
          this.websocketsend("145|halt|" + this.paramStr +"");
        }else {
          this.cardId = this.cardId + "等待卡片,"
          this.findCard()
        }
      }else if (resultData.FunctionID == "45") {
        this.websocketsend("13|beep|" + this.paramStr +"10");
      }else if (resultData.FunctionID == "3") {
        this.disconnect()
        //寻找卡片
        // let _this = this
        // setTimeout(function (){
        //   _this.findCard()
        // }, 1000);
      }
      // console.log(resultData);
    },
    websocketsend(Data){//数据发送
      this.websock.send(Data);
    },
    websocketclose(e){  //关闭
      console.log('断开连接',e);
    },
    cardConnect() {
      try{
        if(this.isComOpen==false)          //if reader link failed
        {
          // alert("initialcom");
          this.websocketsend("11|initialcom|100,115200");
        }
      }catch(e){alert(e.message);}
      return;
    },
    disconnect() {
       this.websocketsend("12|exit|"+ this.paramStr);
    },
    findCard() {
      this.websocketsend("132|findcardStr|" + this.paramStr +"0");
    },
     _getCmdResult(relPara) {
      var iRel;
      var separator = relPara.indexOf(",");
      if (separator != -1) {
        iRel = relPara.substr(0, separator);
      } else
        iRel = relPara.substr(0);

      return iRel;
    },
    _getResultPara(relPara) {
      var szPara = "";
      var separator = relPara.indexOf(",");
      if (separator != -1) {
        szPara = relPara.substr(separator + 1);
      }
      return szPara;
    }
  },

}
</script>

<style scoped>

</style>
