PduSchedule.vue 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. <template>
  2. <div class="l-flex--col center has-border radius has-padding">
  3. <i
  4. class="o-icon lg u-pointer"
  5. :class="iconClass"
  6. @click="invoke"
  7. />
  8. <div class="has-padding u-color--black u-bold">
  9. {{ powerStatusTip }}
  10. </div>
  11. <c-dialog
  12. ref="dialog"
  13. size="lg"
  14. title="PDU定时"
  15. :before-close="handleClose"
  16. @close="onClose"
  17. >
  18. <template #default>
  19. <tabbar
  20. :items="tabs"
  21. :active="active"
  22. @click="onClickTab"
  23. >
  24. <div
  25. v-if="hasChanged"
  26. class="u-font-size--sm u-color--error has-padding--h"
  27. >
  28. 设置需点击【应用】后生效
  29. </div>
  30. </tabbar>
  31. <div
  32. v-loading="loading"
  33. class="l-flex__auto l-flex--col"
  34. >
  35. <schema-table
  36. :key="active"
  37. ref="table"
  38. :schema="schema"
  39. />
  40. </div>
  41. </template>
  42. </c-dialog>
  43. <pdu-task-dialog
  44. ref="pduTaskDialog"
  45. :title="dialogTitle"
  46. :multi="isAdd"
  47. @confirm="onSave"
  48. >
  49. <template #default>
  50. <div class="c-grid-form u-align-self--center">
  51. <template v-if="isAdd">
  52. <schema-select
  53. v-model="taskType"
  54. class="u-width--sm"
  55. :schema="typeSelectSchema"
  56. />
  57. </template>
  58. <div
  59. v-else
  60. class="l-flex--row c-grid-form__option u-color--blue u-bold"
  61. >
  62. {{ taskType }}
  63. </div>
  64. </div>
  65. </template>
  66. </pdu-task-dialog>
  67. </div>
  68. </template>
  69. <script>
  70. import { mapGetters } from 'vuex'
  71. import {
  72. FOREVER, ONE_DAY, ThirdPartyDevice
  73. } from '@/constant.js'
  74. import {
  75. parseTime, transformScreenTaskInfo
  76. } from '@/utils'
  77. import {
  78. addInjectListener,
  79. addListener,
  80. GET_PDU_STATUS,
  81. GET_PDU_TASK,
  82. removeInjectListener,
  83. removeListener,
  84. Status
  85. } from '@/utils/adapter'
  86. import { toDate } from '@/utils/event.js'
  87. import baseMixin from './mixins/base.js'
  88. import {
  89. addPduTask, deletePduTask, getPduTasks, submitPduTasks, updatePduTask
  90. } from '@/api/external'
  91. /* const ErrorMessage = {
  92. TIMEOUT: '暂未获取到操作反馈,请回读查看',
  93. TIMEOUT_RETRY: '暂未获取到操作反馈,请稍后重试',
  94. DEFAULT: '操作异常,请稍后重试',
  95. BUSY: '终端被他人占用',
  96. PASSWORD: '登录密码错误,请联系管理员',
  97. [GET_PDU_STATUS]: '获取电源状态超时,请稍后重试',
  98. [GET_PDU_TASK]: '获取定时任务超时,请回读查看'
  99. } */
  100. export default {
  101. name: 'PduPowerSwitch',
  102. mixins: [baseMixin],
  103. data () {
  104. return {
  105. topic: '/multifunctionCard/invoke',
  106. powerStatus: Status.LOADING,
  107. active: GET_PDU_STATUS,
  108. tabs: [
  109. { key: GET_PDU_STATUS, name: '电源状态' }
  110. ],
  111. powerSchema: {
  112. props: {
  113. size: 'small'
  114. },
  115. nonPagination: true,
  116. list: this.getPowers,
  117. /* buttons: [
  118. { label: '一键开启', on: this.onSwitchOpen },
  119. { label: '一键关闭', on: this.onSwitchClose }
  120. ], */
  121. cols: [
  122. { prop: 'slot_no', label: '序号', 'align': 'center' },
  123. { prop: 'name', label: '电源端口', 'align': 'center' },
  124. { prop: 'state', label: '状态', 'align': 'center' }
  125. ]
  126. },
  127. typeSelectSchema: { options: [] },
  128. actionInfo: ['开启', '关闭'],
  129. timingStatus: 0,
  130. hasChanged: false,
  131. isAdd: true,
  132. taskType: ''
  133. }
  134. },
  135. computed: {
  136. ...mapGetters(['account']),
  137. powerStatusTip () {
  138. switch (this.powerStatus) {
  139. case Status.OK:
  140. return 'PDU定时'
  141. default:
  142. return '检测PDU中'
  143. }
  144. },
  145. iconClass () {
  146. return this.powerStatus === Status.OK ? 'ok' : ''
  147. },
  148. loading () {
  149. return this.active !== GET_PDU_STATUS && !this.timingStatus
  150. },
  151. schema () {
  152. switch (this.active) {
  153. case GET_PDU_STATUS:
  154. return this.powerSchema
  155. case GET_PDU_TASK:
  156. return this.taskSchema
  157. default:
  158. return null
  159. }
  160. },
  161. taskSchema () {
  162. return {
  163. props: {
  164. size: 'small'
  165. },
  166. nonPagination: true,
  167. list: () =>
  168. // 传递 device.id 作为参数
  169. // eslint-disable-next-line implicit-arrow-linebreak
  170. getPduTasks({ deviceId: this.device.id })
  171. .then(result => {
  172. // 在接口调用结束后结束 loading
  173. this.timingStatus = 1
  174. return result
  175. })
  176. .catch(error => {
  177. // 处理错误情况也要结束 loading
  178. this.timingStatus = 1
  179. throw error
  180. }),
  181. buttons: [
  182. { type: 'add', label: '新增', on: this.onAdd },
  183. { label: '应用', render: () => this.hasChanged, on: this.onSubmitPowerTasks }
  184. ].filter(Boolean),
  185. filters: [].filter(Boolean),
  186. cols: [
  187. {
  188. prop: 'slotNo',
  189. label: '插座序号',
  190. align: 'center',
  191. render: task => task.slotNo || '-'
  192. },
  193. {
  194. prop: 'startDate',
  195. label: '开始日期',
  196. render: ({ startDate }) => startDate ? startDate.split(' ')[0] : '-'
  197. },
  198. {
  199. prop: 'endDate',
  200. label: '结束日期',
  201. render: ({ endDate }) => endDate ? endDate.split(' ')[0] : '-'
  202. },
  203. {
  204. label: '开屏时间',
  205. align: 'center',
  206. render: ({ powerOnTime }) => powerOnTime || '-',
  207. 'show-overflow-tooltip': false
  208. },
  209. {
  210. label: '关屏时间',
  211. align: 'center',
  212. render: ({ powerOffTime }) => powerOffTime || '-',
  213. 'show-overflow-tooltip': false
  214. },
  215. {
  216. type: 'tag', render: task => this.isExpired(task)
  217. ? { type: 'warning', label: '已过期' }
  218. : task.status === 1
  219. ? { type: 'success', label: '启用' }
  220. : { type: 'danger', label: '停用' }
  221. },
  222. {
  223. type: 'invoke',
  224. render: [
  225. { label: ({ status }) => status === 1 ? '停用' : '启用', on: this.onToggle },
  226. { label: '编辑', on: this.onEdit },
  227. { label: '删除', on: this.onDel }
  228. ],
  229. width: 140
  230. }
  231. ]
  232. }
  233. },
  234. dialogTitle () {
  235. return this.isAdd ? '新增定时任务' : '编辑定时任务'
  236. }
  237. },
  238. mounted () {
  239. addListener(this.device.id, this.onCacheMessage)
  240. addInjectListener(this.device.id, this.onMessage)
  241. },
  242. beforeDestroy () {
  243. this.$openDialog = false
  244. removeListener(this.device.id, this.onCacheMessage)
  245. removeInjectListener(this.device.id, this.onMessage)
  246. },
  247. methods: {
  248. isExpired ({ endDate }) {
  249. // 如果是 FOREVER 常量,则永不过期
  250. if (endDate === FOREVER) {
  251. return false
  252. }
  253. // 从日期字符串中提取日期部分
  254. const dateStr = typeof endDate === 'string' ? endDate.split(' ')[0] : endDate
  255. // 判断是否过期
  256. return toDate(`${dateStr} 00:00:00`) <= Date.now() - ONE_DAY
  257. },
  258. handleClose (done) {
  259. if (this.hasChanged) {
  260. this.$confirm(
  261. '设置未保存,是否放弃修改?<p class="u-color--error">若需保存请点击【应用】</p>',
  262. '温馨提示',
  263. {
  264. dangerouslyUseHTMLString: true,
  265. type: 'warning',
  266. confirmButtonText: '放弃'
  267. }
  268. )
  269. .then(() => {
  270. this.$openDialog = false
  271. done()
  272. })
  273. return
  274. }
  275. this.$openDialog = false
  276. done()
  277. },
  278. onClickTab (val) {
  279. if (this.hasChanged) {
  280. this.$confirm(
  281. '设置未保存,是否放弃修改?<p class="u-color--error">若需保存请点击【应用】</p>',
  282. '温馨提示',
  283. {
  284. dangerouslyUseHTMLString: true,
  285. type: 'warning',
  286. confirmButtonText: '放弃'
  287. }
  288. )
  289. .then(() => {
  290. this.hasChanged = false
  291. this.onClickTab(val)
  292. })
  293. return
  294. }
  295. if (val !== GET_PDU_STATUS && (val !== this.active || !this.timingStatus)) {
  296. this.getTasksByKey(val)
  297. }
  298. this.active = val
  299. },
  300. invoke () {
  301. if (this.powerStatus !== Status.OK) {
  302. this.$message({
  303. type: 'warning',
  304. message: '获取PDU信息中,请稍后尝试'
  305. })
  306. return
  307. }
  308. this.$openDialog = true
  309. this.getPowerStatus()
  310. },
  311. onCacheMessage (value) {
  312. try {
  313. console.log('cache message', value)
  314. const pdu = value[ThirdPartyDevice.PDU]
  315. const socket = pdu[0]
  316. const pduStatus = socket.state
  317. this.$pdu = pdu
  318. console.log('pdu schedule socket', socket)
  319. if (pduStatus === 'ON' || pduStatus === 'OFF') {
  320. this.powerStatus = Status.OK
  321. } else {
  322. this.powerStatus = Status.LOADING
  323. }
  324. } catch (e) {
  325. console.log('cache pdu message error', e)
  326. }
  327. },
  328. /* fetchPowerStatus () {
  329. this.sendTopic(
  330. GET_PDU_STATUS,
  331. JSON.stringify({ sn: this.device.serialNumber }),
  332. true
  333. )
  334. }, */
  335. onMessage (message) {
  336. console.log('on message', message)
  337. },
  338. onClose () {
  339. this.$powers = []
  340. this.$taskPorts = []
  341. this.$tasks = []
  342. this.closeAsyncLoading()
  343. },
  344. getPowerStatus () {
  345. this.$powers = []
  346. this.hasChanged = false
  347. console.log('this.$pdu', this.$pdu)
  348. this.setCachePowerStatus(this.$pdu)
  349. if (this.$openDialog) {
  350. this.$refs.dialog?.show()
  351. }
  352. this.$refs.table?.pageTo(1)
  353. },
  354. setCachePowerStatus (powersData) {
  355. const map = {}
  356. const options = []
  357. console.log('powersData', powersData)
  358. // 将对象转换为数组
  359. let powersArray = []
  360. if (powersData && typeof powersData === 'object' && !Array.isArray(powersData)) {
  361. // 提取数字键的值组成数组
  362. powersArray = Object.keys(powersData)
  363. .filter(key => key !== 'timestamp')
  364. .map(key => powersData[key])
  365. } else if (Array.isArray(powersData)) {
  366. powersArray = powersData
  367. }
  368. console.log('powersArray', powersArray)
  369. if (powersArray.length === 0) {
  370. return
  371. }
  372. const timestamp = parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}')
  373. this.$powers = [{
  374. connectIndex: powersArray[0].slot_no,
  375. portIndex: powersArray[0].slot_no,
  376. powers: powersArray.map(power => {
  377. const { connectIndex, portIndex, type } = power
  378. if (type) {
  379. if (!map[type]) {
  380. map[type] = {
  381. connectIndex,
  382. portIndex,
  383. type
  384. }
  385. options.push({
  386. value: type,
  387. label: type
  388. })
  389. }
  390. }
  391. return {
  392. timestamp,
  393. ...power
  394. }
  395. })
  396. }]
  397. this.tabs = [
  398. { key: GET_PDU_STATUS, name: '电源状态' },
  399. { key: GET_PDU_TASK, name: '定时控制' }
  400. ]
  401. this.$typeMap = map
  402. this.typeSelectSchema = { options }
  403. this.$refs.table?.pageTo(1)
  404. },
  405. getPowers () {
  406. const data = []
  407. this.$powers.forEach(({ powers }) => {
  408. powers.forEach(power => {
  409. data.push(power)
  410. })
  411. })
  412. return Promise.resolve({ data })
  413. },
  414. /* onSwitchPower (power) {
  415. const { powerIndex, type, action } = power
  416. const targetAction = action ^ 1
  417. this.$confirm(
  418. `立即${this.actionInfo[targetAction]}电源 ${powerIndex} ${type}?<p class="u-color--blue">确认操作后请等待数据同步</p>`,
  419. '操作确认',
  420. {
  421. dangerouslyUseHTMLString: true,
  422. type: 'warning'
  423. }
  424. )
  425. .then(() => {
  426. savePowerLogger({
  427. description: `手动${this.actionInfo[targetAction]} 设备【${this.device.name}】 ${type} 端口${powerIndex}`,
  428. method: `${this.actionInfo[targetAction]}电源`,
  429. params: `${this.device.id} ${this.device.name}`
  430. })
  431. sendDeviceAlarm({
  432. deviceId: this.device.id,
  433. errorEnumId: targetAction ? 36 : 37,
  434. message: `用户【${this.account}】 手动${this.actionInfo[targetAction]} ${type} 端口${powerIndex}`
  435. })
  436. this.onSwitchPowerSingle(power)
  437. })
  438. }, */
  439. /* onSwitchPowerSingle (power) {
  440. const { connectIndex, portIndex, powerIndex, type } = power
  441. this.sendTopic(
  442. SET_POWER_STATUS,
  443. JSON.stringify({
  444. sn: this.device.serialNumber,
  445. info: {
  446. data: [{
  447. connectIndex, portIndex,
  448. conditions: this.$powers
  449. .find(item => item.portIndex === portIndex)
  450. .powers
  451. .filter(power => power.type === type)
  452. .map(power => {
  453. return {
  454. type,
  455. powerIndex: power.powerIndex,
  456. action: power.powerIndex === powerIndex ? power.action ^ 1 : power.action
  457. }
  458. })
  459. }]
  460. }
  461. }),
  462. this.getPowerStatus
  463. )
  464. }, */
  465. /* onSwitchTypePower (power) {
  466. const { type, action } = power
  467. const targetAction = action ^ 1
  468. this.$confirm(
  469. `立即${this.actionInfo[targetAction]}电源 ${type}?<p class="u-color--blue">确认操作后请等待数据同步</p>`,
  470. '操作确认',
  471. {
  472. dangerouslyUseHTMLString: true,
  473. type: 'warning'
  474. }
  475. ).then(() => {
  476. savePowerLogger({
  477. description: `手动${this.actionInfo[targetAction]} 设备【${this.device.name}】 ${type}`,
  478. method: `${this.actionInfo[targetAction]}电源`,
  479. params: `${this.device.id} ${this.device.name}`
  480. })
  481. sendDeviceAlarm({
  482. deviceId: this.device.id,
  483. errorEnumId: targetAction ? 36 : 37,
  484. message: `用户【${this.account}】 手动${this.actionInfo[targetAction]} ${type}`
  485. })
  486. this.onSwitchPowerMulti(power)
  487. })
  488. }, */
  489. /* onSwitchPowerMulti (power) {
  490. const { connectIndex, portIndex, type } = power
  491. const action = power.action ^ 1
  492. this.sendTopic(
  493. SET_POWER_STATUS,
  494. JSON.stringify({
  495. sn: this.device.serialNumber,
  496. info: {
  497. data: [{
  498. connectIndex, portIndex,
  499. conditions: [{
  500. powerIndex: 0,
  501. type,
  502. action
  503. }]
  504. }]
  505. }
  506. }),
  507. this.getPowerStatus
  508. )
  509. }, */
  510. /* onSwitchOpen () {
  511. this.onSwitch(PowerStatus.OPEN)
  512. }, */
  513. /* onSwitchClose () {
  514. this.onSwitch(PowerStatus.CLOSE)
  515. }, */
  516. /* onSwitch (action) {
  517. if (!this.$powers.length) {
  518. return
  519. }
  520. this.$confirm(
  521. `立即${this.actionInfo[action]}所有电源?<p class="u-color--blue">确认操作后请等待数据同步</p>`,
  522. '操作确认',
  523. {
  524. dangerouslyUseHTMLString: true,
  525. type: 'warning'
  526. }
  527. ).then(() => {
  528. savePowerLogger({
  529. description: `手动${this.actionInfo[action]} 设备【${this.device.name}】 所有电源`,
  530. method: `${this.actionInfo[action]}电源`,
  531. params: `${this.device.id} ${this.device.name}`
  532. })
  533. sendDeviceAlarm({
  534. deviceId: this.device.id,
  535. errorEnumId: action ? 36 : 37,
  536. message: `用户【${this.account}】 手动${this.actionInfo[action]} 所有电源`
  537. })
  538. this.sendTopic(
  539. SET_POWER_STATUS,
  540. JSON.stringify({
  541. sn: this.device.serialNumber,
  542. info: {
  543. data: this.$powers.map(({ connectIndex, portIndex, powers }) => {
  544. return {
  545. connectIndex, portIndex,
  546. conditions: powers.map(({ powerIndex, type }) => {
  547. return { powerIndex, type, action }
  548. })
  549. }
  550. })
  551. }
  552. }),
  553. this.getPowerStatus
  554. )
  555. })
  556. }, */
  557. getTasksByKey () {
  558. this.timingStatus = 0
  559. this.hasChanged = false
  560. this.$taskPorts = []
  561. this.$tasks = []
  562. this.$refs.table?.pageTo(1)
  563. /* this.sendTopic(
  564. key,
  565. JSON.stringify({ sn: this.device.serialNumber }),
  566. true
  567. ) */
  568. },
  569. getPowerTasks () {
  570. this.getTasksByKey(this.active)
  571. },
  572. setMultiPowerTasks (data) {
  573. console.log('GET_PDU_TASK', data)
  574. const tasks = []
  575. const ports = []
  576. const map = {}
  577. data.forEach(({ connectIndex, portIndex, conditions }) => {
  578. ports.push({ connectIndex, portIndex })
  579. conditions.forEach(task => {
  580. if (task.flag) {
  581. const flagArr = `${task.flag}`.split('_')
  582. const flag = flagArr.length > 2 ? `${flagArr[0]}_${flagArr[1]}` : task.flag
  583. if (!map[flag]) {
  584. tasks.push(map[flag] = {
  585. connectIndex, portIndex, flag,
  586. ...this.transfromDataToTask(task)
  587. })
  588. }
  589. if (flagArr.length > 2) {
  590. map[flag].executeTime[flagArr[2]] = {
  591. ...map[flag].executeTime[flagArr[2]],
  592. ...this.transformDataToExecuteTime(task)
  593. }
  594. } else {
  595. map[flag].executeTime.push(this.transformDataToExecuteTime(task))
  596. }
  597. } else {
  598. tasks.push({
  599. connectIndex, portIndex,
  600. ...this.transfromDataToTask(task)
  601. })
  602. }
  603. })
  604. })
  605. this.$tasks = tasks.map(task => {
  606. task.info = transformScreenTaskInfo(task.startDate, task.endDate, task.dayOfWeek, task.executeTime)
  607. return task
  608. })
  609. this.$taskPorts = ports
  610. this.timingStatus = 1
  611. this.$refs.table?.pageTo(1)
  612. },
  613. /* transfromDataToTask ({ enable, type, startTime, endTime, cron }) {
  614. const { dayOfWeek } = transformCron(cron)
  615. return {
  616. enable, type, dayOfWeek,
  617. startDate: startTime,
  618. endDate: endTime,
  619. executeTime: []
  620. }
  621. }, */
  622. /* transformDataToExecuteTime ({ action, cron }) {
  623. return action === PowerStatus.OPEN
  624. ? { start: transformCron(cron).executeTime }
  625. : { end: transformCron(cron).executeTime }
  626. }, */
  627. onSubmitPowerTasks () {
  628. const deviceId = this.device.id
  629. submitPduTasks({ deviceId })
  630. .then(
  631. () => {
  632. this.hasChanged = false
  633. this.$message({
  634. type: 'success',
  635. message: '应用成功'
  636. })
  637. }
  638. )
  639. },
  640. /* getMultiPowerTaskData () {
  641. const map = {}
  642. const data = []
  643. this.$taskPorts.forEach(({ connectIndex, portIndex }) => {
  644. if (!map[portIndex]) {
  645. data.push(map[portIndex] = {
  646. connectIndex,
  647. portIndex,
  648. enable: true,
  649. conditions: []
  650. })
  651. }
  652. })
  653. this.$tasks.forEach(({ connectIndex, portIndex, flag, type, enable, startDate, endDate, dayOfWeek, executeTime }) => {
  654. if (!map[portIndex]) {
  655. data.push(map[portIndex] = {
  656. connectIndex,
  657. portIndex,
  658. enable: true,
  659. conditions: []
  660. })
  661. }
  662. const tasks = []
  663. executeTime.forEach(({ start, end }, index) => {
  664. start && tasks.push({
  665. type,
  666. enable,
  667. powerIndex: 0,
  668. flag: `${flag}_${index}`,
  669. action: PowerStatus.OPEN,
  670. startTime: startDate,
  671. endTime: endDate,
  672. cron: [transformToCron(startDate, endDate, dayOfWeek, start)]
  673. })
  674. end && tasks.push({
  675. type,
  676. enable,
  677. powerIndex: 0,
  678. flag: `${flag}_${index}`,
  679. action: PowerStatus.CLOSE,
  680. startTime: startDate,
  681. endTime: endDate,
  682. cron: [transformToCron(startDate, endDate, dayOfWeek, end)]
  683. })
  684. })
  685. map[portIndex].conditions.push(...tasks)
  686. })
  687. savePowerLogger({
  688. description: `设置设备【${this.device.name}】的电源定时任务`,
  689. method: '电源定时任务设置',
  690. params: JSON.stringify({
  691. id: this.device.id,
  692. name: this.device.name,
  693. tasks: this.$tasks
  694. })
  695. })
  696. return data.filter(({ conditions }) => conditions.length)
  697. }, */
  698. onToggle (task) {
  699. this.hasChanged = true
  700. task.enable = task.status === 1
  701. console.log('task.enable : ', task.enable)
  702. task.status = task.enable ? 0 : 1
  703. updatePduTask(task)
  704. .then(() => {
  705. getPduTasks({ deviceId: this.device.id })
  706. this.$refs.table.pageTo(1)
  707. })
  708. .finally(() => {
  709. })
  710. },
  711. onAdd () {
  712. this.isAdd = true
  713. // this.taskType = this.typeSelectSchema.options[0]?.value
  714. this.$refs.pduTaskDialog.show()
  715. },
  716. onEdit (task) {
  717. // 创建 executeTime 数组,符合 PduTaskDialog 的期望格式
  718. const executeTime = []
  719. // 如果有 powerOnTime 或 powerOffTime,则创建执行时间对象
  720. if (task.powerOnTime || task.powerOffTime) {
  721. executeTime.push({
  722. start: task.powerOnTime || '',
  723. end: task.powerOffTime || ''
  724. })
  725. } else {
  726. // 如果都没有,则添加一个空的执行时间对象
  727. executeTime.push({ start: '', end: '' })
  728. }
  729. const taskId = task.id
  730. const { startDate, endDate } = task
  731. this.isAdd = false
  732. this.$task = task
  733. this.$refs.pduTaskDialog.show({
  734. startDate,
  735. endDate,
  736. executeTime,
  737. taskId
  738. })
  739. },
  740. onSave ({ value, done }) {
  741. if (this.isAdd) {
  742. const deviceId = this.device.id
  743. addPduTask(value, deviceId)
  744. .then(() => {
  745. // 重新加载表格数据
  746. getPduTasks({ deviceId: this.device.id })
  747. this.$refs.table.pageTo(1)
  748. })
  749. .finally(() => {
  750. done()
  751. })
  752. } else {
  753. const task = value[0]
  754. updatePduTask(task)
  755. .then(() => {
  756. // 重新加载表格数据
  757. getPduTasks({ deviceId: this.device.id })
  758. this.$refs.table.pageTo(1)
  759. })
  760. .finally(() => {
  761. done()
  762. })
  763. }
  764. this.hasChanged = true
  765. },
  766. onDel (task) {
  767. this.hasChanged = true
  768. deletePduTask({ id: task.id })
  769. .then(() => {
  770. // 重新加载表格数据
  771. getPduTasks({ deviceId: this.device.id })
  772. this.$refs.table.pageTo(1)
  773. })
  774. }
  775. }
  776. }
  777. </script>
  778. <style lang="scss" scoped>
  779. .o-icon {
  780. background-image: url("~@/assets/icon_screen_switch.png");
  781. &.ok {
  782. background-image: url("~@/assets/icon_device_switch.png");
  783. }
  784. }
  785. </style>