
<template>
  <a-form-model ref="form" :model="model" :rules="validatorRules">
    <a-form-model-item label="设备参数名" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-input v-model="tagname" style="width: 20%" />
      <a-button @click="get" icon="reload" type="primary" style="margin-left: 10px">获取参数值</a-button>
      <a-button @click="getdirect" icon="reload" type="warning" style="margin-left: 10px">直接获取</a-button>
    </a-form-model-item>
    <a-form-model-item label="设备参数值" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-input v-model="tagvalue" style="width: 20%" />
      <a-button type="info" icon="reload" @click="set" style="margin-left: 10px">设置参数值</a-button>
    </a-form-model-item>
    <a-form-model-item label="获取历史数据" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-button type="info" icon="reload" @click="getlog">获取历史数据</a-button>
      <a-button type="info" icon="reload" style="margin-left: 10px" @click="loginwa">登录WA</a-button>
      <a-button @click="getall" icon="reload" type="warning" style="margin-left: 10px">直接获取所有</a-button>
    </a-form-model-item>
    <a-form-model-item label="返回内容" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-textarea v-model="message" rows="9" />
    </a-form-model-item>
  </a-form-model>
</template>

<script>
import { getWATagNameValues, setWATagNameValues, getWADataLog } from '@/api/datacoll/webaccess.js'
import axios from 'axios'
export default {
  name: 'Index',
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      tagname: '设备状态',
      tagvalue: 2,
      message: '',
      confirmLoading: false,
    }
  },
  methods: {
    // 登录webaccess
    loginwa() {
      const instance = axios.create({
        baseURL: window._CONFIG['webaccessURL'] + '/WaWebService', // 目标服务器的URL
        timeout: 20000, // 请求超时时间
        headers: {
          'Content-Type': 'application/json',
          Authorization: window._CONFIG['webaccessAUTHOR'],
        },
      })
      console.log('登录前')
      instance
        .get('/Json/Logon')
        .then((response) => {
          console.log('登录', response)
          // console.log(response.header);
        })
        .catch((error) => {
          // 处理错误
          console.error(error)
        })
    },
    get() {
      console.log('获取参数')
      if (this.tagname == '' || this.tagname == null) {
        this.$modal.msgError('请输入设备参数名')
        return false
      }
      getWATagNameValues({ tagName: this.tagname }).then((response) => {
        console.log(response)
        this.message = response.message
      })
    },
    getdirect() {
      console.log('直接获取获取参数值')
      if (this.tagname == '' || this.tagname == null) {
        this.$modal.msgError('请输入设备参数名')
        return false
      }
      var param = { Tags: [
          { Name: this.tagname },
        ]}
        console.log(window._CONFIG)
      const instance = axios.create({
        baseURL: window._CONFIG['webaccessURL'] + '/WaWebService/Json', // 目标服务器的URL
        timeout: 3000, // 请求超时时间
        headers: {
          'Content-Type': 'application/json',
          Authorization: window._CONFIG['webaccessAUTHOR'],
        },
      })
      instance
        .post('/GetTagValue/瑞莱', param)
        .then((response) => {
          console.log(response.data.Values)
          this.message = response.data.Values[0].Value
        })
        .catch((error) => {
          // 处理错误
          console.error(error)
        })
    },
    getall() {
      // console.log('直接获取所有')
      var param = {
        Tags: [
          { Name: '运行方式设定' }, { Name: '设备状态' }, { Name: '试验运行时间' }, { Name: '全部循环设定值' },
          { Name: '内部循环设定值' }, { Name: '全部循环运行值' }, { Name: '内部循环运行值' }, { Name: '预计结束时间' },
          { Name: '内部循环1_启始段' }, { Name: '内部循环1_结束段' }, { Name: '内部循环1_次数' },
          { Name: '内部循环2_启始段' }, { Name: '内部循环2_结束段' }, { Name: '内部循环2_次数' },
          { Name: '内部循环3_启始段' }, { Name: '内部循环3_结束段' }, { Name: '内部循环3_次数' },
          { Name: '内部循环4_启始段' }, { Name: '内部循环4_结束段' }, { Name: '内部循环4_次数' },
          { Name: '总的运行段数' }, { Name: '总的设定段数' }, { Name: '当前运行程序号' }, { Name: '当前运行段号' },
          { Name: '程序段运行时间' }, { Name: '程序段设定时间' }, { Name: '当前湿度值' }, { Name: '当前温度值' },
          { Name: '温度输出率' }, { Name: '起始温度值' }, { Name: '温度设定值' }, { Name: '湿度输出率' },
          { Name: '起始湿度值' }, { Name: '湿度设定值' }, { Name: '定值温度斜率值' },
          { Name: 'A1压机吸气压力' }, { Name: 'A1压机排气压力' }, { Name: 'A2压机吸气压力' }, { Name: 'A2压机排气压力' },
          { Name: 'B1压机吸气压力' }, { Name: 'B1压机排气压力' }, { Name: 'B2压机吸气压力' }, { Name: 'B2压机排气压力' },
          { Name: 'C1压机吸气压力' }, { Name: 'C1压机排气压力' }, { Name: 'C2压机吸气压力' }, { Name: 'C2压机排气压力' },
          { Name: 'D1压机吸气压力' }, { Name: 'D1压机排气压力' }, { Name: 'D2压机吸气压力' }, { Name: 'D2压机排气压力' },
          { Name: 'E1压机吸气压力' }, { Name: 'E1压机排气压力' }, { Name: 'E2压机吸气压力' }, { Name: 'E2压机排气压力' },
          { Name: 'F1压机吸气压力' }, { Name: 'F1压机排气压力' }, { Name: 'F2压机吸气压力' }, { Name: 'F2压机排气压力' },
          { Name: 'G1压机吸气压力' }, { Name: 'G1压机排气压力' }, { Name: 'G2压机吸气压力' }, { Name: 'G2压机排气压力' },
          { Name: 'H1压机吸气压力' }, { Name: 'H1压机排气压力' }, { Name: 'H2压机吸气压力' }, { Name: 'H2压机排气压力' },
          { Name: 'I1压机吸气压力' }, { Name: 'I1压机排气压力' }, { Name: 'I2压机吸气压力' }, { Name: 'I2压机排气压力' },
          { Name: 'J1压机吸气压力' }, { Name: 'J1压机排气压力' }, { Name: 'J2压机吸气压力' }, { Name: 'J2压机排气压力' },
          { Name: '氧浓度1采集值' }, { Name: '氧浓度2采集值' }, { Name: '氧浓度3采集值' }, { Name: '氧浓度4采集值' },
          { Name: '压差1采集值' }, { Name: '压差2采集值' }, { Name: '前风道鼓风变频器' }, { Name: '后风道鼓风变频器' },
        ],
      }
      const instance = axios.create({
        baseURL: window._CONFIG['webaccessURL'] + '/WaWebService/Json', // 目标服务器的URL
        timeout: 10000, // 请求超时时间
        headers: {
          'Content-Type': 'application/json',
          Authorization: window._CONFIG['webaccessAUTHOR'],
        },
      })
      var begintime = new Date();
      instance
        .post('/GetTagValue/瑞莱', param)
        .then((response) => {
          console.log('刷新数据', { "设备实时值" : response.data.Values })
          this.message = "获取时长为：" + (new Date() - begintime) + " 毫秒";
        })
        .catch((error) => {
          // 处理错误
          console.error(error)
          this.message = "获取失败"
        })
    },
    set() {
      console.log('设置参数')
      if (this.tagname == '' || this.tagname == null) {
        this.$modal.msgError('请输入设备参数名')
        return false
      }
      if (this.tagvalue == '' || this.tagvalue == null) {
        this.$modal.msgError('请输入设备参数值')
        return false
      }
      setWATagNameValues({ tagName: this.tagname, tagValue: this.tagvalue }).then((response) => {
        console.log(response)
        this.message = response.message
      })
    },
    getlog() {
      console.log('获取历史数据')
      var begintime = new Date();
      getWADataLog().then((response) => {
        console.log(response)
          this.message = "获取成功，时长为：" + (new Date() - begintime) + " 毫秒";
      })
    },
    exit() {
      if (this.ws) {
        this.ws.close()
        this.ws = null
      }
    },
  },
}
</script>

