device.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. import {
  2. addDevice,
  3. addSubDevice,
  4. delDevice,
  5. delDeviceMul,
  6. delDeviceTag,
  7. getDeviceById,
  8. getDeviceCount,
  9. getDeviceList,
  10. getDeviceMsgList,
  11. addDeviceMsg,
  12. getDeviceTag,
  13. getSubDeviceList,
  14. updateDeviceLabel,
  15. addDeviceCmd,
  16. getDeviceCmdList,
  17. addDeviceTag,
  18. addDeviceGroup,
  19. listDeviceGroup,
  20. postGroupBindDevice,
  21. delGroupBindDevice,
  22. getDeviceByGroup,
  23. getDevicePage,
  24. delSubDevice,
  25. getDeviceAttribute,
  26. getDevicShadow,
  27. getDeviceTopology,
  28. getDeviceSession,
  29. getDeviceAttributes,
  30. getDeviceSecret,
  31. getOtaByDeviceId,
  32. otaUpgradationRecordByDeviceId,
  33. upgradationOtaByDeviceId,
  34. liveById,
  35. liveControlRts,
  36. liveCustomRtsUrl,
  37. getDeviceLabelsByLabel,
  38. removeDeviceByGroup, deviceSimulator, exportDeviceExcel
  39. } from '@/api/iot/device'
  40. import { DeviceMsgEnum, OtaStatusEnum } from '@/enum/common'
  41. import { message } from 'ant-design-vue'
  42. import dayjs from 'dayjs'
  43. import { number } from 'echarts'
  44. export class DeviceContriller {
  45. static deviceStatus = [
  46. { name: '初始化', key: 'INIT' },
  47. { name: '链接中', key: 'CONNECT' },
  48. { name: '断开链接', key: 'DISCONNECT' },
  49. { name: '禁止连接', key: 'DISABLED' }
  50. ]
  51. static transDeviceCountMap = new Map([
  52. ['TOTAL', { label: '总数', color: 'green' }],
  53. ['DISABLED', { label: '禁用数', color: 'grey' }],
  54. ['INIT', { label: '初始化状态数', color: 'orange' }],
  55. ['CONNECT', { label: '连接数', color: 'skyblue' }],
  56. ['DISCONNECT', { label: '断开连接数', color: 'red' }]
  57. ])
  58. static deviceStatusMap = new Map([
  59. ['INIT', { color: 'cyan', name: '初始化', key: 'INIT' }],
  60. ['CONNECT', { color: '#87d068', name: '链接中', key: 'CONNECT' }],
  61. ['DISCONNECT', { color: '#f50', name: '断开链接', key: 'DISCONNECT' }],
  62. ['DISABLED', { color: 'grey', name: '禁止连接', key: 'DISABLED' }]
  63. ])
  64. static deviceMag = new Map([
  65. [DeviceMsgEnum.QUEUED, { name: '队列中', key: DeviceMsgEnum.QUEUED }],
  66. [DeviceMsgEnum.SENT, { name: '已发送', key: DeviceMsgEnum.SENT }],
  67. [DeviceMsgEnum.DELIVERED, { name: '设备收到', key: DeviceMsgEnum.DELIVERED }],
  68. [DeviceMsgEnum.TIMEOUT, { name: '超时', key: DeviceMsgEnum.TIMEOUT }],
  69. [DeviceMsgEnum.FAILED, { name: '失败', key: DeviceMsgEnum.FAILED }]
  70. ])
  71. static searchKeyList = [
  72. { name: '设备ID', key: 'deviceId' },
  73. { name: '设备标识码', key: 'deviceCode' },
  74. { name: '设备名称', key: 'deviceLabel' }
  75. ]
  76. static otaStatusMap = new Map([
  77. [OtaStatusEnum.QUEUED, { key: OtaStatusEnum.QUEUED, label: '在消息队列中', color: 'orange' }],
  78. [OtaStatusEnum.SENT, { key: OtaStatusEnum.SENT, label: '已经发送', color: 'blue' }],
  79. [OtaStatusEnum.DELIVERED, { key: OtaStatusEnum.DELIVERED, label: '设备已收到', color: 'pink' }],
  80. [OtaStatusEnum.SUCCESSFUL, { key: OtaStatusEnum.SUCCESSFUL, label: '成功', color: 'green' }],
  81. [OtaStatusEnum.TIMEOUT, { key: OtaStatusEnum.TIMEOUT, label: '超时', color: 'red' }],
  82. [OtaStatusEnum.FAILED, { key: OtaStatusEnum.FAILED, label: '失败', color: 'grey' }]
  83. ])
  84. static otaStatusList = [
  85. { key: OtaStatusEnum.QUEUED, label: '在消息队列中' },
  86. { key: OtaStatusEnum.SENT, label: '已经发送' },
  87. { key: OtaStatusEnum.DELIVERED, label: '设备已收到' },
  88. { key: OtaStatusEnum.SUCCESSFUL, label: '成功' },
  89. { key: OtaStatusEnum.TIMEOUT, label: '超时' },
  90. { key: OtaStatusEnum.FAILED, label: '失败' }
  91. ]
  92. static async page (params: IOT.API.DEVICE.QueryPamars) {
  93. const { data: _data, sum } = await getDevicePage(params)
  94. const data = _data.map(item => {
  95. return {
  96. ...item,
  97. deviceStatus: DeviceContriller.deviceStatusMap.get(item.deviceStatus)
  98. }
  99. })
  100. return {
  101. data,
  102. sum
  103. }
  104. }
  105. /**
  106. *
  107. * @param deviceStatus 传递对应的key,就只会返回对应状态的设备,不填写返回全部
  108. * @returns
  109. */
  110. static async list (params: {modelId: string, deviceLabel: string, limit: number, lastId: string, deviceStatus: 'INIT' | 'CONNECT' | 'DISCONNECT' | 'DISABLED' | ''} = {
  111. modelId: '',
  112. deviceLabel: '',
  113. limit: 20,
  114. lastId: '',
  115. deviceStatus: ''
  116. }) {
  117. const data = await getDeviceList(params)
  118. return {
  119. ...data,
  120. data: params.deviceStatus === '' ? data.data : data.data.filter(item => item.deviceStatus === params.deviceStatus)
  121. }
  122. }
  123. static async post (data: IOT.API.DEVICE.BodyParams) {
  124. const { code } = await addDevice(data)
  125. if (code === 200) message.success('新增成功')
  126. }
  127. static async del (id: string | string[]) {
  128. if (typeof id === 'string') {
  129. await delDevice(id)
  130. } else {
  131. await delDeviceMul(id)
  132. }
  133. message.success('删除成功')
  134. }
  135. static async byId (id: string) {
  136. const { data } = await getDeviceById(id)
  137. return data
  138. }
  139. /** 分页获取子设备列表 */
  140. static async pageSub (id: string, params: IOT.API.DEVICE.QueryPamars) {
  141. return await getSubDeviceList(id, params)
  142. }
  143. /** 获取设备密匙 */
  144. static async secret (params: {deviceId: string, authType: 'SECRET' | 'X509CERT'}) {
  145. return await getDeviceSecret(params)
  146. }
  147. /** 新增子设备 */
  148. static async postSub (data: IOT.API.DEVICE.SubBodyParams) {
  149. const { code } = await addSubDevice(data)
  150. if (code === 200) message.success('新增成功')
  151. }
  152. static async delSub (id: string) {
  153. const { code } = await delSubDevice(id)
  154. if (code === 200) message.success('删除成功')
  155. }
  156. static async ListTag (params: {deviceId: number | string}) {
  157. return await getDeviceTag(params)
  158. }
  159. static async delTag (id: string) {
  160. const { code } = await delDeviceTag(id)
  161. if (code === 200) message.success('删除标签成功')
  162. }
  163. static async postTag (data: IOT.API.DEVICE.DeviceTag[]) {
  164. const { code } = await addDeviceTag(data)
  165. if (code === 200) message.success('新增标签成功')
  166. }
  167. static async updateLabel (data: {id: string, deviceLabel: string}) {
  168. const { code } = await updateDeviceLabel(data)
  169. if (code === 200) message.success('修改设备名称成功')
  170. }
  171. static async count () {
  172. return await getDeviceCount()
  173. }
  174. /** 设备统计 */
  175. static async statistics (params: {modelId: string}) {
  176. const { data } = await getDeviceCount(params)
  177. return Object.keys(data).map((key) => {
  178. return {
  179. key: key,
  180. label: DeviceContriller.transDeviceCountMap.get(key)?.label,
  181. color: DeviceContriller.transDeviceCountMap.get(key)?.color,
  182. value: data[key as keyof typeof data]
  183. }
  184. })
  185. }
  186. /** 消息下发 获取列表 */
  187. static async listDeviceMsg (params: {deviceId: string}) {
  188. const { data } = await getDeviceMsgList(params)
  189. return data
  190. }
  191. /** 消息下发 下发消息 */
  192. static async addDeviceMsg (data: {deviceId: string, msgPayload: string, msgLabel: string, topic: string}) {
  193. await addDeviceMsg(data)
  194. message.success('新增下发消息成功')
  195. }
  196. /** 命令下发 获取列表 */
  197. static async listDeviceCmd (params: {deviceId: string}) {
  198. const { data } = await getDeviceCmdList(params)
  199. return data
  200. }
  201. /** 命令下发 新增命令 */
  202. static async addDeviceCmd (data: { deviceId: string, cmdLabel: string, cmdParameters: any}) {
  203. const { code } = await addDeviceCmd(data)
  204. if (code === 200) message.success('新增命令成功')
  205. }
  206. /** 设备分组 新增分组 */
  207. static async postDeviceGroup (data: { groupLabel: string, upperGroupId: string }) {
  208. const { code } = await addDeviceGroup(data)
  209. if (code === 200) message.success('新增分组成功')
  210. }
  211. /** 设备分组 查询分组 */
  212. static async listDeviceGroup (params: { upperGroupId: string }) {
  213. return await listDeviceGroup(params)
  214. }
  215. static async removeDeviceGroup (params: string) {
  216. return await removeDeviceByGroup(params)
  217. }
  218. /** 设备分组 设备绑定分组 */
  219. static async postGroupBindDevice (data: { deviceGroupId: string, deviceId: string }[]) {
  220. const { code } = await postGroupBindDevice(data)
  221. if (code === 200) message.success('绑定成功')
  222. }
  223. /**
  224. * 此函数删除设备和设备组之间的绑定并显示成功消息。
  225. * @param data - 参数 `data` 是一个包含两个属性的对象:
  226. */
  227. static async delGroupBindDevice (data: { deviceGroupId: string, deviceId: string }[]) {
  228. const { code } = await delGroupBindDevice(data)
  229. if (code === 200) message.success('取消绑定成功')
  230. }
  231. /**
  232. * 此函数根据 TypeScript 中的组查询参数检索设备。
  233. * @param params -
  234. * 参数“params”的类型为“IOT.API.DEVICE.GroupQueryParams”,这可能是代码库中定义的接口或类型。它用作函数“getDeviceByGroup”的参数,该函数使用“await”关键字调用以生成函数
  235. * @returns 正在使用 params 参数调用 getDeviceByGroup 函数,并使用 return
  236. * 关键字返回该函数调用的结果。该函数被标记为“async”,因此它将返回一个解析为“getDeviceByGroup”函数调用结果的承诺。
  237. */
  238. static async getDeviceByGroup (params: IOT.API.DEVICE.GroupQueryParams) {
  239. return await getDeviceByGroup(params)
  240. }
  241. static async getLiveData (deviceId: string) {
  242. return await getDeviceAttribute(deviceId)
  243. }
  244. /** 获取设备属性 */
  245. static async getDeviceAttribute (deviceId: string) {
  246. return await getDeviceAttribute(deviceId)
  247. }
  248. static async getDevicShadow (deviceId: string) {
  249. return await getDevicShadow(deviceId)
  250. }
  251. static async getDeviceTopology (modelId: string) {
  252. return await getDeviceTopology(modelId)
  253. }
  254. static async getSession (params: {deviceId: string, start: number, end: number}) {
  255. const { data, sum } = await getDeviceSession(params)
  256. return {
  257. data: {
  258. sessionEntities: data.sessionEntities.map(item => {
  259. return {
  260. ...item,
  261. createAt: dayjs(item.createAt).format('YYYY/MM/DD HH:mm:ss')
  262. }
  263. }),
  264. offlineNum: data.offlineNum,
  265. onlineNum: data.onlineNum
  266. },
  267. sum
  268. }
  269. }
  270. static async getAttr (params: any) {
  271. return await getDeviceAttributes(params)
  272. }
  273. static async OtaByDeviceId (deviceId: string) {
  274. const data = await getOtaByDeviceId(deviceId)
  275. return {
  276. ...data,
  277. data: {
  278. ...data,
  279. otaTime: data.data.otaTime === 0 ? '尚未升级' : data.data.otaTime,
  280. otaPkgLabel: data.data.otaPkgLabel === '--' ? '尚未升级' : data.data.otaPkgLabel,
  281. otaPkgVersion: data.data.otaPkgVersion === '--' ? '尚未升级' : data.data.otaPkgVersion
  282. }
  283. }
  284. }
  285. static async OtaUpgradationRecordByDeviceId (params: IOT.API.DEVICE.OtaQueryParams) {
  286. const data = await otaUpgradationRecordByDeviceId(params)
  287. return {
  288. ...data,
  289. data: data.data.map(item => ({ ...item, otaTime: dayjs(item.otaTime).format('YYYY-MM-DD HH:mm:ss') }))
  290. }
  291. }
  292. static async upgradationOtaByDeviceId ({ deviceId, otaPkgId }: {deviceId: string, otaPkgId: string}) {
  293. await upgradationOtaByDeviceId(deviceId, otaPkgId)
  294. message.success('升级设备成功')
  295. }
  296. static async liveById (deviceId: string) {
  297. return liveById(deviceId)
  298. }
  299. static async liveControlRts (deviceId: string, channel: number) {
  300. return liveControlRts(deviceId, channel)
  301. }
  302. static async liveCustomRtsUrl (data: {id: string, rtsUrl: string}) {
  303. return liveCustomRtsUrl(data)
  304. }
  305. /**
  306. * @description 模糊查询:根据设备名称来查询全部的符合条件的设备名
  307. */
  308. static async labelsByLabel (deviceLabel: string) {
  309. return await getDeviceLabelsByLabel(deviceLabel)
  310. }
  311. static async deviceSimulator (data: { deviceId: string, payload: string }) {
  312. await deviceSimulator(data)
  313. }
  314. static async exportDeviceExcel (data: IOT.API.DEVICE.QueryPamars) {
  315. await exportDeviceExcel(data)
  316. }
  317. }