device.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import {
  2. addDevice, addSubDevice, delDevice, delDeviceMul, delDeviceTag,
  3. getDeviceById, getDeviceCount, getDeviceList, getDeviceMsgList, addDeviceMsg, getDeviceTag,
  4. getSubDeviceList, updateDeviceLabel, addDeviceCmd, getDeviceCmdList, addDeviceTag, addDeviceGroup,
  5. listDeviceGroup, postGroupBindDevice, delGroupBindDevice, getDeviceByGroup
  6. } from '@/api/iot/device'
  7. import { DeviceMsgEnum } from '@/enum/common'
  8. import { message } from 'ant-design-vue'
  9. export class DeviceContriller {
  10. static deviceStatus = [
  11. { name: '初始化', key: 'INIT' },
  12. { name: '链接中', key: 'CONNECT' },
  13. { name: '断开链接', key: 'DISCONNECT' },
  14. { name: '禁止连接', key: 'DISABLED' }
  15. ]
  16. static transDeviceCountMap = new Map([
  17. ['TOTAL', { label: '总数', color: 'green' }],
  18. ['DISABLED', { label: '禁用数', color: 'grey' }],
  19. ['INIT', { label: '初始化状态数', color: 'orange' }],
  20. ['CONNECT', { label: '连接数', color: 'skyblue' }],
  21. ['DISCONNECT', { label: '断开连接数', color: 'red' }]
  22. ])
  23. static deviceStatusMap = new Map([
  24. ['INIT', { color: 'cyan', name: '初始化', key: 'INIT' }],
  25. ['CONNECT', { color: '#87d068', name: '链接中', key: 'CONNECT' }],
  26. ['DISCONNECT', { color: '#f50', name: '断开链接', key: 'DISCONNECT' }],
  27. ['DISABLED', { color: 'grey', name: '禁止连接', key: 'DISABLED' }]
  28. ])
  29. static deviceMag = new Map([
  30. [DeviceMsgEnum.QUEUED, { name: '队列中', key: DeviceMsgEnum.QUEUED }],
  31. [DeviceMsgEnum.SENT, { name: '已发送', key: DeviceMsgEnum.SENT }],
  32. [DeviceMsgEnum.DELIVERED, { name: '设备收到', key: DeviceMsgEnum.DELIVERED }],
  33. [DeviceMsgEnum.TIMEOUT, { name: '超时', key: DeviceMsgEnum.TIMEOUT }],
  34. [DeviceMsgEnum.FAILED, { name: '失败', key: DeviceMsgEnum.FAILED }]
  35. ])
  36. static searchKeyList = [
  37. { name: '设备ID', key: 'deviceId' },
  38. { name: '设备标识码', key: 'deviceCode' },
  39. { name: '设备名称', key: 'deviceLabel' }
  40. ]
  41. static async page (params: IOT.API.DEVICE.QueryPamars) {
  42. const { data: _data, sum } = await getDeviceList(params)
  43. const data = _data.map(item => {
  44. return {
  45. ...item,
  46. deviceStatus: DeviceContriller.deviceStatusMap.get(item.deviceStatus)
  47. }
  48. })
  49. return {
  50. data,
  51. sum
  52. }
  53. }
  54. static async post (data: IOT.API.DEVICE.BodyParams) {
  55. await addDevice(data)
  56. message.success('新增成功')
  57. }
  58. static async del (id: string | string[]) {
  59. if (typeof id === 'string') {
  60. await delDevice(id)
  61. } else {
  62. await delDeviceMul(id)
  63. }
  64. message.success('删除成功')
  65. }
  66. static async byId (id: string) {
  67. const { data } = await getDeviceById(id)
  68. return data
  69. }
  70. /** 分页获取子设备列表 */
  71. static async pageSub (id: string, params: IOT.API.DEVICE.QueryPamars) {
  72. return await getSubDeviceList(id, params)
  73. }
  74. /** 新增子设备 */
  75. static async postSub (data: IOT.API.DEVICE.SubBodyParams) {
  76. await addSubDevice(data)
  77. message.success('新增成功')
  78. }
  79. static async ListTag (params: {deviceId: number | string}) {
  80. return await getDeviceTag(params)
  81. }
  82. static async delTag (id: string) {
  83. await delDeviceTag(id)
  84. message.success('删除标签成功')
  85. }
  86. static async postTag (data: IOT.API.DEVICE.DeviceTag[]) {
  87. await addDeviceTag(data)
  88. message.success('新增标签成功')
  89. }
  90. static async updateLabel (data: {id: string, deviceLabel: string}) {
  91. await updateDeviceLabel(data)
  92. message.success('修改设备名称成功')
  93. }
  94. /** 设备统计 */
  95. static async statistics (params: {modelId: string}) {
  96. const { data } = await getDeviceCount(params)
  97. return Object.keys(data).map((key) => {
  98. return {
  99. key: key,
  100. label: DeviceContriller.transDeviceCountMap.get(key)?.label,
  101. color: DeviceContriller.transDeviceCountMap.get(key)?.color,
  102. value: data[key as keyof typeof data]
  103. }
  104. })
  105. }
  106. /** 消息下发 获取列表 */
  107. static async listDeviceMsg (params: {deviceId: string}) {
  108. const { data } = await getDeviceMsgList(params)
  109. return data
  110. }
  111. /** 消息下发 下发消息 */
  112. static async addDeviceMsg (data: {deviceId: string, msgPayload: string, msgLabel: string, topic: string}) {
  113. await addDeviceMsg(data)
  114. message.success('新增下发消息成功')
  115. }
  116. /** 命令下发 获取列表 */
  117. static async listDeviceCmd (params: {deviceId: string}) {
  118. const { data } = await getDeviceCmdList(params)
  119. return data
  120. }
  121. /** 命令下发 新增命令 */
  122. static async addDeviceCmd (data: { deviceId: string, cmdLabel: string, cmdParameters: any}) {
  123. await addDeviceCmd(data)
  124. message.success('新增命令成功')
  125. }
  126. /** 设备分组 新增分组 */
  127. static async postDeviceGroup (data: { groupLabel: string, upperGroupId: string }) {
  128. await addDeviceGroup(data)
  129. message.success('新增分组成功')
  130. }
  131. /** 设备分组 查询分组 */
  132. static async listDeviceGroup (params: { upperGroupId: string }) {
  133. return await listDeviceGroup(params)
  134. }
  135. /** 设备分组 设备绑定分组 */
  136. static async postGroupBindDevice (data: { deviceGroupId: string, deviceId: string }[]) {
  137. await postGroupBindDevice(data)
  138. message.success('绑定成功')
  139. }
  140. /**
  141. * 此函数删除设备和设备组之间的绑定并显示成功消息。
  142. * @param data - 参数 `data` 是一个包含两个属性的对象:
  143. */
  144. static async delGroupBindDevice (data: { deviceGroupId: string, deviceId: string }[]) {
  145. await delGroupBindDevice(data)
  146. message.success('取消绑定成功')
  147. }
  148. /**
  149. * 此函数根据 TypeScript 中的组查询参数检索设备。
  150. * @param params -
  151. * 参数“params”的类型为“IOT.API.DEVICE.GroupQueryParams”,这可能是代码库中定义的接口或类型。它用作函数“getDeviceByGroup”的参数,该函数使用“await”关键字调用以生成函数
  152. * @returns 正在使用 params 参数调用 getDeviceByGroup 函数,并使用 return
  153. * 关键字返回该函数调用的结果。该函数被标记为“async”,因此它将返回一个解析为“getDeviceByGroup”函数调用结果的承诺。
  154. */
  155. static async getDeviceByGroup (params: IOT.API.DEVICE.GroupQueryParams) {
  156. return await getDeviceByGroup(params)
  157. }
  158. }