doum
7 天以前 e46bfa3ff94a8a1b4daf37c7fcb79c2fab22a72c
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<template>
  <TableLayout :permissions="['business:ywconditioner:query']">
    <el-form ref="searchForm" slot="search-form" :model="searchForm" label-width="100px" inline>
      <el-form-item label="设备信息" prop="devKeyword">
        <el-input v-model="searchForm.devKeyword" placeholder="名称/编号/房间" clearable @keypress.enter.native="search" />
      </el-form-item>
      <el-form-item label="在线状态" prop="onlineFilter">
        <el-select v-model="searchForm.onlineFilter" clearable placeholder="全部" style="min-width: 120px">
          <el-option label="在线" value="在线" />
          <el-option label="离线" value="离线" />
        </el-select>
      </el-form-item>
      <el-form-item label="开关状态" prop="pwrFilter">
        <el-select v-model="searchForm.pwrFilter" clearable placeholder="全部" style="min-width: 120px">
          <el-option label="开机" :value="1" />
          <el-option label="关机" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="网关MAC" prop="wgMacFilter">
        <el-select v-model="searchForm.wgMacFilter" clearable filterable placeholder="全部" style="min-width: 180px">
          <el-option v-for="item in gatewayOptions" :key="item.wgMac" :label="item.wgMac" :value="item.wgMac" />
        </el-select>
      </el-form-item>
      <section>
        <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
        <el-button icon="el-icon-refresh" @click="reset">重置</el-button>
      </section>
    </el-form>
    <template v-slot:table-wrap>
      <ul class="toolbar">
        <li>
          <el-button
            type="primary"
            :loading="isSyncingAll"
            v-permissions="['business:ywconditioner:sync']"
            @click="handleSyncAll"
          >一键同步空调多联机</el-button>
        </li>
        <li>
          <el-button
            type="primary"
            :loading="isSyncingDevices"
            v-permissions="['business:ywconditioner:sync']"
            @click="handleSyncDevices"
          >同步设备和状态</el-button>
        </li>
      </ul>
      <div v-loading="isWorking.search" class="card-grid-wrap">
        <div v-if="!tableData.list.length && !isWorking.search" class="empty-tip">暂无设备数据,请先同步内机</div>
        <div class="card-grid">
          <YwConditionerCard
            v-for="item in tableData.list"
            :key="item.id"
            :device="item"
            :power-loading="powerLoadingId === item.id"
            :lock-loading="lockLoadingId === item.id"
            @history="openHistory"
            @lock="openLock"
            @unlock="unlockDevice"
            @toggle-power="togglePower"
            @set-mode="setMode"
            @set-fan="setFan"
            @set-temp="setTemp"
          />
        </div>
      </div>
      <pagination
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
        :pagination="paginationConfig"
      />
    </template>
    <YwConditionerLockWindow ref="lockWindow" @success="refreshList" />
    <YwConditionerHistoryWindow ref="historyWindow" />
  </TableLayout>
</template>
 
<script>
import BaseTable from '@/components/base/BaseTable'
import TableLayout from '@/layouts/TableLayout'
import Pagination from '@/components/common/Pagination'
import * as conditionerApi from '@/api/business/ywconditioner'
import YwConditionerCard from './components/YwConditionerCard'
import YwConditionerLockWindow from './components/YwConditionerLockWindow'
import YwConditionerHistoryWindow from './components/YwConditionerHistoryWindow'
 
const ADJUST_OPERATE_INTERVAL_MS = 2000
const ADJUST_ACTION_TYPES = [2, 3, 4]
const OFFLINE_TIP = '设备离线了,请检查对应设备网络'
 
export default {
  name: 'YwConditioner',
  extends: BaseTable,
  components: {
    TableLayout,
    Pagination,
    YwConditionerCard,
    YwConditionerLockWindow,
    YwConditionerHistoryWindow
  },
  data () {
    return {
      searchForm: {
        devKeyword: '',
        onlineFilter: '',
        pwrFilter: null,
        wgMacFilter: ''
      },
      gatewayOptions: [],
      isSyncingAll: false,
      isSyncingDevices: false,
      powerLoadingId: null,
      lockLoadingId: null,
      deviceAdjustOperateAt: {}
    }
  },
  computed: {
    paginationConfig () {
      return {
        ...this.tableData.pagination,
        pageSizes: [15, 30, 45, 60]
      }
    }
  },
  created () {
    this.api = conditionerApi
    this.module = '空调内机'
    this.configData['field.id'] = 'id'
    this.configData['field.main'] = 'name'
    this.tableData.pagination.pageSize = 15
    this.loadGatewayOptions()
    this.search()
  },
  methods: {
    loadGatewayOptions () {
      conditionerApi.gatewayOptions()
        .then(list => { this.gatewayOptions = list || [] })
        .catch(() => {})
    },
    buildSearchModel () {
      const model = {}
      if (this.searchForm.devKeyword) model.devKeyword = this.searchForm.devKeyword
      if (this.searchForm.onlineFilter) model.onlineFilter = this.searchForm.onlineFilter
      if (this.searchForm.pwrFilter !== null && this.searchForm.pwrFilter !== '') {
        model.pwrFilter = this.searchForm.pwrFilter
      }
      if (this.searchForm.wgMacFilter) model.wgMacFilter = this.searchForm.wgMacFilter
      return model
    },
    handlePageChange (pageIndex) {
      const page = Number(pageIndex)
      if (page > 0) {
        this.tableData.pagination.pageIndex = page
      }
      this.isWorking.search = true
      conditionerApi.fetchCardPage({
        page: this.tableData.pagination.pageIndex,
        capacity: this.tableData.pagination.pageSize,
        model: this.buildSearchModel(),
        sorts: this.tableData.sorts
      })
        .then(data => {
          this.tableData.list = data.records || []
          this.tableData.pagination.total = data.total || 0
        })
        .catch(() => {})
        .finally(() => { this.isWorking.search = false })
    },
    refreshList () {
      this.handlePageChange(this.tableData.pagination.pageIndex)
    },
    patchDeviceInList (id, patch) {
      const idx = this.tableData.list.findIndex(item => item.id === id)
      if (idx === -1) return
      this.$set(this.tableData.list, idx, { ...this.tableData.list[idx], ...patch })
    },
    isDeviceOnline (row) {
      const o = row && row.online
      if (o === '在线' || o === '1' || o === 1) return true
      if (o === 88 || o === 66 || o === '88' || o === '66') return true
      if (typeof o === 'string' && o.toLowerCase() === 'online') return true
      return false
    },
    ensureDeviceOnline (row) {
      if (!this.isDeviceOnline(row)) {
        this.$tip.warning(OFFLINE_TIP)
        return false
      }
      return true
    },
    reserveAdjustOperate (deviceId) {
      const last = this.deviceAdjustOperateAt[deviceId]
      const now = Date.now()
      if (last && now - last < ADJUST_OPERATE_INTERVAL_MS) {
        const waitSec = ((ADJUST_OPERATE_INTERVAL_MS - (now - last)) / 1000).toFixed(1)
        this.$tip.warning(`操作过于频繁,请 ${waitSec} 秒后再试`)
        return false
      }
      this.$set(this.deviceAdjustOperateAt, deviceId, now)
      return true
    },
    reset () {
      this.searchForm = { devKeyword: '', onlineFilter: '', pwrFilter: null, wgMacFilter: '' }
      this.search()
    },
    handleSyncAll () {
      this.$dialog.actionConfirm('确认同时全量同步更新 网关、电表、计费系数、内机数据信息吗?', '一键同步空调多联机')
        .then(() => {
          this.isSyncingAll = true
          conditionerApi.syncAll()
            .then(res => {
              this.$tip.apiSuccess(res || '同步成功')
              this.loadGatewayOptions()
              this.search()
            })
            .catch(e => this.$tip.apiFailed(e))
            .finally(() => { this.isSyncingAll = false })
        })
        .catch(() => {})
    },
    handleSyncDevices () {
      this.$dialog.actionConfirm('确认从智精灵平台同步设备信息和最新设备状态吗?', '同步设备和状态')
        .then(() => {
          this.isSyncingDevices = true
          conditionerApi.syncDevicesAndStatus()
            .then(res => {
              this.$tip.apiSuccess(res || '同步成功')
              this.search()
            })
            .catch(e => this.$tip.apiFailed(e))
            .finally(() => { this.isSyncingDevices = false })
        })
        .catch(() => {})
    },
    openHistory (row) {
      this.$refs.historyWindow.open(row)
    },
    openLock (row) {
      if (!this.ensureDeviceOnline(row)) return
      this.$refs.lockWindow.open(row)
    },
    unlockDevice (row) {
      if (!this.ensureDeviceOnline(row)) return
      this.$dialog.actionConfirm('确认解锁该设备的锁定设置吗?', '设备解锁')
        .then(() => {
          this.lockLoadingId = row.id
          conditionerApi.lock({
            id: row.id,
            lockPwr: 0,
            pwr: -1,
            mode: -1,
            fan: -1,
            minTemp: -1,
            maxTemp: -1,
            source: 'admin'
          })
            .then(res => {
              this.$tip.apiSuccess(res || '解锁成功')
              this.patchDeviceInList(row.id, { ktLock: 0, lockPwr: 0 })
              this.refreshList()
            })
            .catch(e => this.$tip.apiFailed(e))
            .finally(() => { this.lockLoadingId = null })
        })
        .catch(() => {})
    },
    togglePower (row) {
      if (!this.ensureDeviceOnline(row)) return
      const next = row.pwr === 1 ? 0 : 1
      const msg = next === 1 ? '确认开机吗?' : '确认关机吗?'
      this.$dialog.actionConfirm(msg, '设备控制')
        .then(() => {
          this.powerLoadingId = row.id
          conditionerApi.operate({
            id: row.id,
            actionType: 1,
            setVal: next,
            source: 'admin'
          })
            .then(res => {
              this.$tip.apiSuccess(res || '操作成功')
              this.patchDeviceInList(row.id, { pwr: next })
              this.refreshList()
            })
            .catch(e => this.$tip.apiFailed(e))
            .finally(() => { this.powerLoadingId = null })
        })
        .catch(() => {})
    },
    doOperate (row, actionType, setVal, label, patch) {
      if (!this.ensureDeviceOnline(row)) return
      if (ADJUST_ACTION_TYPES.includes(actionType) && !this.reserveAdjustOperate(row.id)) {
        return
      }
      conditionerApi.operate({
        id: row.id,
        actionType,
        setVal,
        source: 'admin'
      })
        .then(res => {
          this.$tip.apiSuccess(res || label + '成功')
          if (patch) {
            this.patchDeviceInList(row.id, patch)
          }
        })
        .catch(e => this.$tip.apiFailed(e))
    },
    setMode (row, mode) {
      if (row.mode === mode) return
      this.doOperate(row, 2, mode, '模式设置', { mode })
    },
    setFan (row, fan) {
      const cur = row.fanSet != null ? row.fanSet : row.fan
      if (cur === fan) return
      this.doOperate(row, 3, fan, '风速设置', { fanSet: fan, fan })
    },
    setTemp (row, delta) {
      let cur = Number(row.tempSet)
      if (isNaN(cur)) cur = 260
      if (cur > 100) cur = cur / 10
      const next = Math.max(16, Math.min(32, cur + delta))
      this.doOperate(row, 4, next, '温度设置', { tempSet: next * 10 })
    }
  }
}
</script>
 
<style scoped>
.card-grid-wrap { min-height: 120px; }
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
  padding: 8px 0 16px;
}
.empty-tip {
  text-align: center;
  color: #909399;
  padding: 48px 0;
}
</style>