Crunchy
2025-01-13 300a480751c63fa970e3879965ff64c5bbfb4eac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<template>
  <div>
    <basic-container>
      <el-form label-width="100px" :inline="true">
        <el-form-item label="设备:" required>
          <el-select
            v-model="deviceId"
            placeholder="请选择"
            @change="getMqttListsSelect"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.name"
              :value="item.device_id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间:">
          <el-radio-group
            v-model="checkboxGroup3"
            size="small"
            @change="destroyTimerAgainStart"
          >
            <el-radio-button v-for="city in cities" :label="city" :key="city">{{
              city
            }}</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="历史查询:">
          <el-tooltip
            class="item"
            effect="dark"
            content="注意:历史时间为空会刷新数据,不为空则不会刷新数据!"
            placement="top"
          >
            <el-date-picker
              v-model="datePicker"
              size="small"
              :picker-options="pickerOptions"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              @change="datePickerHistory"
              type="datetimerange"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['12:00:00']"
            >
            </el-date-picker>
          </el-tooltip>
        </el-form-item>
      </el-form>
    </basic-container>
    <basic-container>
      <div
        v-if="!largeAreaChartList.length > 0"
        style="height: 75vh; padding-top: 0.4em; display: flex; align-items: center; justify-content: center;"
      >
        暂无数据
      </div>
      <el-row>
        <el-col
          v-for="(v, k) in largeAreaChartList"
          :key="k"
          style="height: 40vh; padding-top: 0.4em;"
          :span="12"
          ><largeAreaChart
            ref="largeAreaChartRef"
            :yAxisMonth="v.yAxisMonth"
            :seriesData="v.listData"
            :otherData="v"
          />
        </el-col>
      </el-row>
    </basic-container>
  </div>
</template>
 
<script>
import largeAreaChart from './components/largeAreaChart.vue'
import { getDeviceId, mqttList } from '@/api/equipment/equipment'
 
export default {
  components: {
    largeAreaChart: largeAreaChart
  },
  data() {
    return {
      largeAreaChartList: [],
      cities: ['10 分钟', '30 分钟', '60 分钟'],
      checkboxGroup3: '10 分钟',
      options: [],
      deviceId: '',
      timer: '',
      datePicker: [],
      choiceDate: null,
      pickerOptions: {
        onPick: ({ maxDate, minDate }) => {
          // 把选择的第一个日期赋值给一个变量。
          this.choiceDate = minDate.getTime()
          // 如何你选择了两个日期了,就把那个变量置空
          if (maxDate) this.choiceDate = ''
        },
        disabledDate: (time) => {
          // 如何选择了一个日期
          if (this.choiceDate) {
            // 7天的时间戳
            const one = 7 * 24 * 3600 * 1000
            // 当前日期 - one = 7天之前
            const minTime = this.choiceDate - one
            // 当前日期 + one = 7天之后
            const maxTime = this.choiceDate + one
            return (
              time.getTime() < minTime ||
              time.getTime() > maxTime ||
              // 限制不能选择今天及以后
              time.getTime() > Date.now()
            )
          } else {
            // 如果没有选择日期,就要限制不能选择今天及以后
            return time.getTime() > Date.now()
          }
        }
      }
    }
  },
  // 初始化调用 查询设备 折线图数据
  async created() {
    await this.getDeviceIdFun()
    this.destroyTimerAgainStart()
  },
  // 关闭页面销毁定时器
  beforeDestroy() {
    clearInterval(this.timer)
  },
  watch: {
    datePicker(newVal) {
      // 存在时间 销毁定时器
      if (newVal) {
        clearInterval(this.timer)
      } else {
        this.destroyTimerAgainStart()
      }
    }
  },
  methods: {
    // 选择时间后调用
    datePickerHistory() {
      if (this.datePicker) {
        this.getMqttLists()
      }
    },
    // 选择设备后调用刷新接口
    getMqttListsSelect() {
      this.largeAreaChartList = []
      this.getMqttLists()
    },
    // 获取设备下拉框
    async getDeviceIdFun() {
      await getDeviceId().then((res) => {
        this.options = res.data.data
        this.deviceId = this.options[0].device_id
      })
    },
    // 获取折线图数据
    async getMqttLists() {
      const obj = {
        deviceId: this.deviceId,
        collectionTime: this.checkboxGroup3.split(' ')[0]
      }
      if (this.datePicker) {
        obj.startTime = this.datePicker[0]
        obj.endTime = this.datePicker[1]
      }
      // 调用数据接口
      const res = await mqttList(obj)
      const data = res.data.data
      for (const i of data) {
        i.listData = []
        i.yAxisMonth = []
        if (i.listMqttTableData.length > 0) {
          i.deviceId = i.listMqttTableData[0].deviceId
        }
        for (const j of i.listMqttTableData) {
          i.listData.push(j.value)
          i.yAxisMonth.push(j.collectionTime)
        }
        i.listMqttTableData = []
      }
      this.largeAreaChartList = data
      // 处理数据,适配折线图格式
      for (const [k, i] of this.largeAreaChartList.entries()) {
        this.$refs.largeAreaChartRef[k].refreshData(i.listData, i.yAxisMonth)
      }
    },
    // 开启定时器
    destroyTimerAgainStart() {
      this.largeAreaChartList = []
      clearInterval(this.timer)
      this.timer = setInterval(this.getMqttLists, 1000 * 3)
      this.getMqttLists()
    }
  }
}
</script>
 
<style></style>