device.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import {
  2. addDevice, addSubDevice, delDevice, delDeviceMul, delDeviceTag,
  3. getDeviceById, getDeviceCount, getDeviceList, getDeviceMsgList, addDeviceMsg, getDeviceTag, getSubDeviceList, updateDeviceLabel, addDeviceCmd, getDeviceCmdList
  4. } from '@/api/iot/device'
  5. import { DeviceMsgEnum } from '@/enum/common'
  6. import { message } from 'ant-design-vue'
  7. export class DeviceContriller {
  8. static deviceStatus = [
  9. { name: '初始化', key: 'INIT' },
  10. { name: '链接中', key: 'CONNECT' },
  11. { name: '断开链接', key: 'DISCONNECT' },
  12. { name: '禁止连接', key: 'DISABLED' }
  13. ]
  14. static transDeviceCountMap = new Map([
  15. ['TOTAL', { label: '总数', color: 'green' }],
  16. ['DISABLED', { label: '禁用数', color: 'grey' }],
  17. ['INIT', { label: '初始化状态数', color: 'orange' }],
  18. ['CONNECT', { label: '连接数', color: 'skyblue' }],
  19. ['DISCONNECT', { label: '断开连接数', color: 'red' }]
  20. ])
  21. static deviceStatusMap = new Map([
  22. ['INIT', { color: 'cyan', name: '初始化', key: 'INIT' }],
  23. ['CONNECT', { color: '#87d068', name: '链接中', key: 'CONNECT' }],
  24. ['DISCONNECT', { color: '#f50', name: '断开链接', key: 'DISCONNECT' }],
  25. ['DISABLED', { color: 'grey', name: '禁止连接', key: 'DISABLED' }]
  26. ])
  27. static deviceMag = new Map([
  28. [DeviceMsgEnum.QUEUED, { name: '队列中', key: DeviceMsgEnum.QUEUED }],
  29. [DeviceMsgEnum.SENT, { name: '已发送', key: DeviceMsgEnum.SENT }],
  30. [DeviceMsgEnum.DELIVERED, { name: '设备收到', key: DeviceMsgEnum.DELIVERED }],
  31. [DeviceMsgEnum.TIMEOUT, { name: '超时', key: DeviceMsgEnum.TIMEOUT }],
  32. [DeviceMsgEnum.FAILED, { name: '失败', key: DeviceMsgEnum.FAILED }]
  33. ])
  34. static searchKeyList = [
  35. { name: '设备ID', key: 'deviceId' },
  36. { name: '设备标识码', key: 'deviceCode' },
  37. { name: '设备名称', key: 'deviceLabel' }
  38. ]
  39. static async page (params: IOT.API.DEVICE.QueryPamars) {
  40. const { data: _data, sum } = await getDeviceList(params)
  41. const data = _data.map(item => {
  42. return {
  43. ...item,
  44. deviceStatus: DeviceContriller.deviceStatusMap.get(item.deviceStatus)
  45. }
  46. })
  47. return {
  48. data,
  49. sum
  50. }
  51. }
  52. static async post (data: IOT.API.DEVICE.BodyParams) {
  53. await addDevice(data)
  54. message.success('新增成功')
  55. }
  56. static async del (id: string | string[]) {
  57. if (typeof id === 'string') {
  58. await delDevice(id)
  59. } else {
  60. await delDeviceMul(id)
  61. }
  62. message.success('删除成功')
  63. }
  64. static async byId (id: string) {
  65. const { data } = await getDeviceById(id)
  66. return data
  67. }
  68. /** 分页获取子设备列表 */
  69. static async pageSub (id: string, params: IOT.API.DEVICE.QueryPamars) {
  70. return await getSubDeviceList(id, params)
  71. }
  72. /** 新增子设备 */
  73. static async postSub (data: IOT.API.DEVICE.SubBodyParams) {
  74. await addSubDevice(data)
  75. message.success('新增成功')
  76. }
  77. static async pageTag (params: {deviceId: number | string}) {
  78. return await getDeviceTag(params)
  79. }
  80. static async delTag (id: string) {
  81. await delDeviceTag(id)
  82. message.success('删除标签成功')
  83. }
  84. static async updateLabel (data: {id: string, deviceLabel: string}) {
  85. await updateDeviceLabel(data)
  86. }
  87. /** 设备统计 */
  88. static async statistics (params: {modelId: string}) {
  89. const { data } = await getDeviceCount(params)
  90. return Object.keys(data).map((key) => {
  91. return {
  92. key: key,
  93. label: DeviceContriller.transDeviceCountMap.get(key)?.label,
  94. color: DeviceContriller.transDeviceCountMap.get(key)?.color,
  95. value: data[key as keyof typeof data]
  96. }
  97. })
  98. }
  99. /** 消息下发 获取列表 */
  100. static async listDeviceMsg (params: {deviceId: string}) {
  101. const { data } = await getDeviceMsgList(params)
  102. return data
  103. }
  104. /** 消息下发 下发消息 */
  105. static async addDeviceMsg (data: {deviceId: string, msgPayload: string, msgLabel: string, topic: string}) {
  106. await addDeviceMsg(data)
  107. message.success('新增下发消息成功')
  108. }
  109. /** 命令下发 获取列表 */
  110. static async listDeviceCmd (params: {deviceId: string}) {
  111. const { data } = await getDeviceCmdList(params)
  112. return data
  113. }
  114. /** 命令下发 新增命令 */
  115. static async addDeviceCmd (data: { deviceId: string, cmdLabel: string, cmdParameters: any}) {
  116. await addDeviceCmd(data)
  117. message.success('新增命令成功')
  118. }
  119. }