wu
2023-09-06 3eced65b14a858329fd2b7f4c8bc6c5d66710c78
CanadaMes-ui/src/views/Electrical/Parameter.vue
@@ -1,177 +1,155 @@
<template>
  <div class="app">
    <!--面包屑导航区域-->
    <el-breadcrumb separator-class="el-icon-arrow-right" class="el-breadcrumb">
      <router-link to="/home" tag="el-button" type="text">{{ $t('langHome') }}</router-link>
      <el-button type="text">设备管理</el-button>
      <router-link to="/Electrical/Parameter" tag="el-button" type="text">Parameter</router-link>
      <router-link to="/Electrical/Action" tag="el-button" type="text">Action</router-link>
      <router-link to="/Electrical/Sign" tag="el-button" type="text">Sign</router-link>
      <router-link to="/Electrical/State" tag="el-button" type="text">State</router-link>
      <router-link to="/Electrical/alarm" tag="el-button" type="text">Alarm</router-link>
    </el-breadcrumb>
    <div>Parameter</div>
    <el-form label-width="100px" style="display: flex;flex-wrap: wrap;" :model="messagepack.data">
      <div id="btn_div">
        <el-button type="primary" @click="send()">Distribute</el-button>
        <!-- <button @click="send()">测试发送</button> -->
      </div>
      <div class="kuai_div" v-for="item in this.record.xyData" :key="item.name">
        <el-input style="width: 280px;" class="in_mc" v-model="item.name"></el-input>
        <el-input v-model="item.value" style="width: 80px;"></el-input>
      </div>
    </el-form>
  <!--面包屑导航区域-->
  <el-breadcrumb separator-class="el-icon-arrow-right" class="el-breadcrumb">
  <router-link to="/home" tag="el-button" type="text">{{ $t('langHome') }}</router-link>
  <el-button type="text">设备管理</el-button>
  <router-link to="/Electrical/Parameter" tag="el-button" type="text">Parameter</router-link>
  <router-link to="/Electrical/Action" tag="el-button" type="text">Action</router-link>
  <router-link to="/Electrical/Sign" tag="el-button" type="text">Sign</router-link>
  <router-link to="/Electrical/State" tag="el-button" type="text">State</router-link>
  <router-link to="/Electrical/alarm" tag="el-button" type="text">Alarm</router-link>
  </el-breadcrumb>
  <div>Parameter</div>
  <el-form label-width="100px" style="display: flex;flex-wrap: wrap;" :model="messagepack.data">
    <div id="btn_div">
      <el-button type="primary" @click="send()">下发参数</el-button>
    </div>
    <div class="kuai_div" v-for="(item, index) in record.xyData" :key="index">
      <el-input style="width: 280px;" class="in_mc" v-model="item.name" readonly></el-input>
      <el-input v-model.number="item.value" style="width: 80px;" @input="handleChange(index, $event.target.value)"></el-input>
    </div>
  </el-form>
  </div>
</template>
<script >
let socket;
export default {
  </template>
  <script>
  let socket;
  export default {
  name: "Parameter",
  data () {
    return {
      record: {
        params: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        xyData: [
          { name: 'conveyor Velocity(Auto FAST)', value: 0 },
          { name: 'conveyor Velocity(Manual)', value: 0 },
          { name: 'A01 A02 TURN JOG Velocity', value: 0 },
          { name: 'A01 A02 TRAVEL JOG Velocity', value: 0 },
          { name: 'A01 A02 TURN POS Velocity AUTO', value: 0 },
          { name: 'A01 A02 TURN POS Velocity manual', value: 0 },
          { name: '01 A02 TRAVEL POS Velocity AUTO', value: 0 },
          { name: 'A01 A02 TRAVEL POS Velocity manual', value: 0 },
          { name: 'B01 B02 TRAVEL POS Velocity AUTO', value: 0 },
          { name: 'B01 B02 TRAVEL POS Velocity manual', value: 0 },
        ],
        canshu: [
          'conveyor Velocity(Auto FAST)',
          'B01 B02 TRAVEL JOG Velocity',
          'A01 A02 TURN POS Velocity AUTO',
          'A01 A02 TURN POS Velocity manual',
          'A01 A02 TRAVEL POS Velocity AUTO',
          'A01 A02 TRAVEL POS Velocity manual',
          'B01 B02 TRAVEL POS Velocity AUTO',
          'B01 B02 TRAVEL POS Velocity manual',
        ],
      },
      messagepack: {
        data: { taskname: "" }
      },
      queryInfo: {
        data: "1",
        pageSize: 10
      },
    }
  data() {
  return {
  record: {
  params: [100, 200, 10, 10, 10, 10],
  xyData: [
  { name: "conveyor Velocity(Auto FAST)", value: 0 },
  { name: "conveyor Velocity(Auto SLOW)", value: 0 },
  { name: "conveyor Velocity(Manual)", value: 0 },
  { name: "A01 A02 TURN JOG Velocity", value: 0 },
  { name: "A01 A02 TRAVEL JOG Velocity", value: 0 },
  { name: "B01 B02 TRAVEL JOG Velocity", value: 0 },
  { name: "A01 A02 TURN POS Velocity AUTO", value: 0 },
  { name: "A01 A02 TURN POS Velocity manual", value: 0 },
  { name: "A01 A02 TRAVEL POS Velocity AUTO", value: 0 },
  { name: "A01 A02 TRAVEL POS Velocity manual", value: 0 },
  { name: "B01 B02 TRAVEL POS Velocity AUTO", value: 0 },
  { name: "B01 B02 TRAVEL POS Velocity manual", value: 0 }
  ],
  canshu: []
  },
  created () {
    this.init();
  messagepack: {
  data: { taskname: "" }
  },
  queryInfo: {
  data: "1",
  pageSize: 10
  }
  };
  },
  created() {
  this.initWebSocket();
  },
  methods: {
    init () {
      let viewname = "Parameter";
      if (typeof (WebSocket) == "undefined") {
        console.log("您的浏览器不支持WebSocket");
      } else {
        //console.log("您的浏览器支持WebSocket");
        let socketUrl = "ws://" + "localhost:8888" + "/springboot-vue3/api/talk/" + viewname;
        if (socket != null) {
          socket.close();
          socket = null;
  initWebSocket() {
  let viewname = "Parameter";
    if (typeof WebSocket === "undefined") {
      console.log("您的浏览器不支持WebSocket");
    } else {
      let socketUrl = "ws://" + "localhost:8888" + "/springboot-vue3/api/talk/" + viewname;
      if (socket != null) {
        socket.close();
        socket = null;
      }
      // 开启一个websocket服务
      socket = new WebSocket(socketUrl);
      // 打开事件
      socket.onopen = function () {
        console.log("websocket已打开");
      };
      // 收到消息
      socket.onmessage = (msg) => {
        if (!msg.data) {
          return; // 如果收到空数据,则直接返回,不执行后续逻辑
        }
        // 开启一个websocket服务
        socket = new WebSocket(socketUrl);
        //打开事件
        socket.onopen = function () {
          console.log("websocket已打开");
        };
        //  浏览器端收消息,获得从服务端发送过来的文本消息
        socket.onmessage = function (msg) {
          //console.log("收到数据====" + msg.data);
          let obj = JSON.parse(msg.data);
          //this.$set(this.record.params, 0, obj.params[0]);
          this.record.params[0] = obj.params[0];
          for (let a = 0; a <= this.record.params.length - 1; a++) {
            // this.record.xyData[a] = [this.record.canshu[a], this.record.params[0][a]];
            // this.record.xyData[a] = { name: this.record.canshu[a], value: this.record.params[0][a] };
        let obj = JSON.parse(msg.data);
        this.record.params[0] = obj.params[0];
        for (let a = 0; a <= this.record.params[0].length - 1; a++) {
          if (!this.record.xyData[a]) {
            this.record.xyData[a] = { name: this.record.canshu[a], value: this.record.params[0][a] };
          } else {
            this.record.xyData[a].value = this.record.params[0][a];
          }
          this.$forceUpdate();
        }.bind(this);
        //关闭事件
        socket.onclose = function () {
          console.log("websocket已关闭");
        };
        //发生了错误事件
        socket.onerror = function () {
          console.log("websocket发生了错误");
        }
      }
    },
    send () {
      this.messagepack.data = { taskname: "前端到后台" };
      socket?.send(JSON.stringify(this.messagepack));  // 将组装好的json发送给服务端,由服务端进行转发
        console.log(this.record.xyData);
        this.$forceUpdate();
      };
      // 关闭事件
      socket.onclose = function () {
        console.log("websocket已关闭");
      };
      // 发生错误事件
      socket.onerror = function () {
        console.log("websocket发生了错误");
      };
    }
  },
  send() {
    this.messagepack.data = this.record.xyData.map((item) => parseInt(item.value)); // 转换为整数数组
    console.log(this.messagepack);
    socket?.send(JSON.stringify(this.messagepack));
  },
  handleChange(index, value) {
    this.record.xyData[index].value = value;
  }
}
</script>
<style>
.kuai_div {
  }
  };
  </script>
  <style>
  .kuai_div {
  width: 30%;
  margin-bottom: 15px;
}
.el-input {
  }
  .el-input {
  border: none;
  .el-input__inner {
    border: 1 solid black;
  }
}
.in_mc {
  .el-input__inner {
    border: none;
  border: 1px solid black;
  }
}
#btn_div {
  .in_mc .el-input__inner {
  border: none;
  }
  #btn_div {
  width: 100%;
}
#btn_div .el-button {
  }
  #btn_div .el-button {
  float: right;
}
</style>
  }
  </style>