<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          size="mini"-->
<!--          @click="testAgingCabinetAndPowerCheck"-->
<!--        >测试老化柜状态获取-->
<!--        </el-button>-->
<!--      </el-col>-->
      <!-- 新增的批量操作按钮 -->
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-reading"
          size="mini"
          @click="handleReading"
        >批量读取设备状态</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="info"-->
<!--          plain-->
<!--          icon="el-icon-reading"-->
<!--          size="mini"-->
<!--          @click="handleHoldingRegister"-->
<!--        >测试501端口</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-circle-close"
          size="mini"
          @click="handleWriteHour"
        >写时间测试</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-circle-check"
          size="mini"
          @click="handleScanAndStartAgingAllCabinetLayers"
        >扫描并老化</el-button>
      </el-col>
    </el-row>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-circle-plus"
          size="mini"
          @click="handleAllPowerOn"
        >所有设备上电</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-circle-plus"
          size="mini"
          @click="handleDesignatedDevicePowerOn"
        >指定设备上电</el-button>
      </el-col>
    </el-row>
    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-remove"
          size="mini"
          @click="handleAllPowerOff"
        >所有设备断电</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-remove"
          size="mini"
          @click="handleDesignatedDevicePowerOff"
        >指定设备断电</el-button>
      </el-col>
    </el-row>
    <el-input
      type="textarea"
      :rows="15"
      placeholder="返回内容信息"
      v-model="textarea">
    </el-input>
  </div>
</template>

<script>
import {getAgingCabinetAndPowerCheck} from "@/api/testScheduledTasks/testTasks";
import {
  batchReadingCabinetStatus, readingHoldingRegister, powerOnAllStore, powerOffAllStore,
  scanAndStartAgingAllCabinetLayers, designatedDevicePowerOn, designatedDevicePowerOff
} from "@/api/storey/storey";
import {sendWriteHour} from "@/api/robotArm/robotArmCommand";

export default {
  name: "TestScheduledTasks",
  data() {
    return {
      textarea: null
    };
  },
  created() {
  },
  methods: {
    testAgingCabinetAndPowerCheck() {
      getAgingCabinetAndPowerCheck().then(response => {
          this.textarea = "";
          if (response.length > 0) {
            for (let i = 0; i < response.length; i++) {
              let item = response[i];
              let deviceCode = item.deviceCode;
              let registerValues = item.registerValues;
              let deviceStatus = item.deviceStatus;
              for (let i = 0; i < 10; i++) {
                let registerValue = registerValues[i];
                this.textarea += "设备编号：" + deviceCode + "状态是：" + deviceStatus + "  寄存器编号：" + i + "  寄存器值：" + registerValue + "\n";
              }
            }
          }
        }
      );
    },

    // 从原组件提取的功能方法
    handleReading() {
      this.$prompt('请输入老化柜编号', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({ value }) => {
        batchReadingCabinetStatus(value).then(response => {
          if(response.code === 200) {
            this.textarea = "批量读取设备状态成功：" + JSON.stringify(response.data);
            this.$message.success("读取成功");
          }
        })
      }).catch(() => {});
    },

    handleHoldingRegister() {
      this.$prompt('请输入ip加端口号 逗号分隔', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({ value }) => {
        readingHoldingRegister(value).then(response => {
          if(response.code === 200) {
            this.textarea = "测试501端口成功：" + JSON.stringify(response.data);
            this.$message.success("测试成功");
          }
        })
      }).catch(() => {});
    },

    handleAllPowerOn() {
      this.$confirm('是否确认给所有设备上电?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return powerOnAllStore();
      }).then(() => {
        this.textarea = "所有设备上电操作执行成功";
        this.$message.success("上电成功");
      }).catch(() => {});
    },

    handleDesignatedDevicePowerOff() {
      this.$prompt('请输入需要断电的柜子设备编号北1-15，南16-30,英文逗号分隔', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({ value }) => {
        designatedDevicePowerOff(value).then(response => {
          if(response.code === 200) {
            this.textarea = "指定设备已断电成功：" + JSON.stringify(response.data);
            this.$message.success("指定设备已断电成功");
          }
        })
      }).catch(() => {});
    },
    handleDesignatedDevicePowerOn() {
      this.$prompt('请输入需要上电的柜子设备编号北1-15，南16-30,英文逗号分隔', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({ value }) => {
        designatedDevicePowerOn(value).then(response => {
          if(response.code === 200) {
            this.textarea = "指定设备已上电成功：" + JSON.stringify(response.data);
            this.$message.success("指定设备已上电成功");
          }
        })
      }).catch(() => {});
    },
    handleAllPowerOff() {
      this.$confirm('是否确认给所有设备断电?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return powerOffAllStore();
      }).then(() => {
        this.textarea = "所有设备断电操作执行成功";
        this.$message.success("断电成功");
      }).catch(() => {});
    },
    // 扫描并老化
    handleScanAndStartAgingAllCabinetLayers() {
      this.$confirm('警示：此操作将清理所有已存在的任务并重新扫描所有老化柜的层信息去生成任务，确认操作请点击确定！！！', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return scanAndStartAgingAllCabinetLayers();
      }).then(() => {
        this.$message.success("我想好了");
      }).catch(() => {});
    },

    handleWriteHour() {
      this.$prompt('请输入ip加端口号 逗号分隔', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({ value }) => {
        sendWriteHour(value).then(response => {
          if(response.code === 200) {
            this.textarea = "写时间测试成功：" + JSON.stringify(response.data);
            this.$message.success("写时间成功");
          }
        })
      }).catch(() => {});
    }
  }
};
</script>
