Files
AILab/vue2/apps/web-antd/src/api/iot/device.ts
T
2025-12-29 16:30:04 +08:00

100 lines
2.0 KiB
TypeScript

import type { Recordable } from '@vben/types';
import { sha256 } from 'js-sha256';
import { pyRequestClient } from '#/api/request';
export namespace SystemDeviceApi {
export interface SystemDevice {
[key: string]: any;
id: string;
remark?: string;
name: string;
roles: string[];
status: 0 | 1;
is_superuser: 0 | 1;
dept_id?: string;
dept_name?: string;
}
}
/**
* 获取设备列表数据
*/
async function getDeviceList(params: Recordable<any>) {
return pyRequestClient.get<Array<SystemDeviceApi.SystemDevice>>(
'/iot/common/device/list',
{ params },
);
}
/**
* 创建设备
* @param data 设备数据
*/
async function createDevice(data: Omit<SystemDeviceApi.SystemDevice, 'id'>) {
data.password_hash = sha256(data.password?.toString() || '');
delete data.password;
return pyRequestClient.post('/iot/common/device', data);
}
/**
* 更新设备
*
* @param id 设备 ID
* @param data 设备数据
*/
async function updateDevice(
id: string,
data: Omit<SystemDeviceApi.SystemDevice, 'id'>,
) {
return pyRequestClient.put(`/iot/common/device/${id}`, data);
}
/**
* 更新设备(部分更新)
*
* @param id 设备 ID
* @param data 需要更新的字段(部分字段即可)
*/
async function updateDevicePatch(
id: string,
data: Partial<Omit<SystemDeviceApi.SystemDevice, 'id'>>,
) {
return pyRequestClient.patch(`/iot/common/device/${id}`, data);
}
/**
* 删除设备
* @param id 设备 ID
*/
async function deleteDevice(id: string) {
return pyRequestClient.delete(`/iot/common/device/${id}`);
}
/**
* 发送指令
* @param data 设备数据
*/
async function iotSendCommand(
id: string,
command: string,
project: string,
device_type: string,
) {
return pyRequestClient.post('/iot/common/device/command', {
id,
command,
project,
device_type,
});
}
export {
createDevice,
deleteDevice,
getDeviceList,
iotSendCommand,
updateDevice,
updateDevicePatch,
};