import { addDevice, addSubDevice, delDevice, delDeviceMul, delDeviceTag, getDeviceById, getDeviceCount, getDeviceList, getDeviceMsgList, addDeviceMsg, getDeviceTag, getSubDeviceList, updateDeviceLabel, addDeviceCmd, getDeviceCmdList } from '@/api/iot/device' import { DeviceMsgEnum } from '@/enum/common' import { message } from 'ant-design-vue' export class DeviceContriller { static deviceStatus = [ { name: '初始化', key: 'INIT' }, { name: '链接中', key: 'CONNECT' }, { name: '断开链接', key: 'DISCONNECT' }, { name: '禁止连接', key: 'DISABLED' } ] static transDeviceCountMap = new Map([ ['TOTAL', { label: '总数', color: 'green' }], ['DISABLED', { label: '禁用数', color: 'grey' }], ['INIT', { label: '初始化状态数', color: 'orange' }], ['CONNECT', { label: '连接数', color: 'skyblue' }], ['DISCONNECT', { label: '断开连接数', color: 'red' }] ]) static deviceStatusMap = new Map([ ['INIT', { color: 'cyan', name: '初始化', key: 'INIT' }], ['CONNECT', { color: '#87d068', name: '链接中', key: 'CONNECT' }], ['DISCONNECT', { color: '#f50', name: '断开链接', key: 'DISCONNECT' }], ['DISABLED', { color: 'grey', name: '禁止连接', key: 'DISABLED' }] ]) static deviceMag = new Map([ [DeviceMsgEnum.QUEUED, { name: '队列中', key: DeviceMsgEnum.QUEUED }], [DeviceMsgEnum.SENT, { name: '已发送', key: DeviceMsgEnum.SENT }], [DeviceMsgEnum.DELIVERED, { name: '设备收到', key: DeviceMsgEnum.DELIVERED }], [DeviceMsgEnum.TIMEOUT, { name: '超时', key: DeviceMsgEnum.TIMEOUT }], [DeviceMsgEnum.FAILED, { name: '失败', key: DeviceMsgEnum.FAILED }] ]) static searchKeyList = [ { name: '设备ID', key: 'deviceId' }, { name: '设备标识码', key: 'deviceCode' }, { name: '设备名称', key: 'deviceLabel' } ] static async page (params: IOT.API.DEVICE.QueryPamars) { const { data: _data, sum } = await getDeviceList(params) const data = _data.map(item => { return { ...item, deviceStatus: DeviceContriller.deviceStatusMap.get(item.deviceStatus) } }) return { data, sum } } static async post (data: IOT.API.DEVICE.BodyParams) { await addDevice(data) message.success('新增成功') } static async del (id: string | string[]) { if (typeof id === 'string') { await delDevice(id) } else { await delDeviceMul(id) } message.success('删除成功') } static async byId (id: string) { const { data } = await getDeviceById(id) return data } /** 分页获取子设备列表 */ static async pageSub (id: string, params: IOT.API.DEVICE.QueryPamars) { return await getSubDeviceList(id, params) } /** 新增子设备 */ static async postSub (data: IOT.API.DEVICE.SubBodyParams) { await addSubDevice(data) message.success('新增成功') } static async pageTag (params: {deviceId: number | string}) { return await getDeviceTag(params) } static async delTag (id: string) { await delDeviceTag(id) message.success('删除标签成功') } static async updateLabel (data: {id: string, deviceLabel: string}) { await updateDeviceLabel(data) } /** 设备统计 */ static async statistics (params: {modelId: string}) { const { data } = await getDeviceCount(params) return Object.keys(data).map((key) => { return { key: key, label: DeviceContriller.transDeviceCountMap.get(key)?.label, color: DeviceContriller.transDeviceCountMap.get(key)?.color, value: data[key as keyof typeof data] } }) } /** 消息下发 获取列表 */ static async listDeviceMsg (params: {deviceId: string}) { const { data } = await getDeviceMsgList(params) return data } /** 消息下发 下发消息 */ static async addDeviceMsg (data: {deviceId: string, msgPayload: string, msgLabel: string, topic: string}) { await addDeviceMsg(data) message.success('新增下发消息成功') } /** 命令下发 获取列表 */ static async listDeviceCmd (params: {deviceId: string}) { const { data } = await getDeviceCmdList(params) return data } /** 命令下发 新增命令 */ static async addDeviceCmd (data: { deviceId: string, cmdLabel: string, cmdParameters: any}) { await addDeviceCmd(data) message.success('新增命令成功') } }