2025-10-20 15:07:30 +08:00
|
|
|
|
import { AreaControllerApi, DeviceApi } from '../api/device.js';
|
2025-09-19 14:25:20 +08:00
|
|
|
|
|
|
|
|
|
|
class DeviceService {
|
2025-10-01 00:29:21 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取所有设备和区域主控的列表,并将其合并为树形结构所需的数据
|
|
|
|
|
|
* @returns {Promise<Array>} 合并后的设备列表
|
|
|
|
|
|
*/
|
|
|
|
|
|
async getDevices() {
|
|
|
|
|
|
try {
|
2025-10-20 15:15:43 +08:00
|
|
|
|
// 1. 并行发起两个独立的API请求:一个获取区域主控,一个获取普通设备
|
|
|
|
|
|
const [areaControllersResponse, devicesResponse] = await Promise.all([
|
|
|
|
|
|
AreaControllerApi.list(), // 调用 GET /api/v1/area-controllers
|
|
|
|
|
|
DeviceApi.list() // 调用 GET /api/v1/devices
|
2025-10-01 00:29:21 +08:00
|
|
|
|
]);
|
2025-09-30 22:20:50 +08:00
|
|
|
|
|
2025-10-20 15:15:43 +08:00
|
|
|
|
// 2. 处理区域主控数据,并添加前端所需的'type'标识
|
2025-10-01 00:29:21 +08:00
|
|
|
|
const areaControllers = (areaControllersResponse.data || []).map(controller => ({
|
|
|
|
|
|
...controller,
|
2025-10-20 15:15:43 +08:00
|
|
|
|
type: 'area_controller'
|
2025-10-01 00:29:21 +08:00
|
|
|
|
}));
|
2025-09-30 22:20:50 +08:00
|
|
|
|
|
2025-10-20 15:15:43 +08:00
|
|
|
|
// 3. 处理普通设备数据,并添加'type'和'parent_id'以构建树形结构
|
|
|
|
|
|
const devices = (devicesResponse.data || []).map(device => ({
|
|
|
|
|
|
...device,
|
|
|
|
|
|
type: 'device',
|
|
|
|
|
|
parent_id: device.area_controller_id
|
|
|
|
|
|
}));
|
2025-09-30 22:20:50 +08:00
|
|
|
|
|
2025-10-20 15:15:43 +08:00
|
|
|
|
// 4. 合并两份数据,形成完整的列表,返回给UI组件
|
2025-10-01 00:29:21 +08:00
|
|
|
|
return [...areaControllers, ...devices];
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('获取设备列表失败:', error);
|
|
|
|
|
|
throw error;
|
|
|
|
|
|
}
|
2025-09-19 14:25:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-01 00:29:21 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 创建新设备或区域主控
|
2025-10-20 15:07:30 +08:00
|
|
|
|
* @param {Object} data - 设备或区域主控信息,包含type字段
|
2025-10-01 00:29:21 +08:00
|
|
|
|
* @returns {Promise<Object>} 创建结果
|
|
|
|
|
|
*/
|
2025-10-20 15:07:30 +08:00
|
|
|
|
async createDevice(data) {
|
2025-10-01 00:29:21 +08:00
|
|
|
|
try {
|
2025-10-20 15:07:30 +08:00
|
|
|
|
if (data.type === 'area_controller') {
|
|
|
|
|
|
const response = await AreaControllerApi.create(data);
|
2025-10-01 00:29:21 +08:00
|
|
|
|
return {...response.data, type: 'area_controller'};
|
|
|
|
|
|
} else {
|
2025-10-20 15:07:30 +08:00
|
|
|
|
const response = await DeviceApi.create(data);
|
2025-10-01 00:29:21 +08:00
|
|
|
|
return {...response.data, type: 'device', parent_id: response.data.area_controller_id};
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('创建设备失败:', error);
|
|
|
|
|
|
throw error;
|
|
|
|
|
|
}
|
2025-09-19 14:25:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-01 00:29:21 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取设备或区域主控详情
|
2025-10-20 15:07:30 +08:00
|
|
|
|
* @param {number} id - ID
|
|
|
|
|
|
* @param {string} type - 类型 ('area_controller' 或 'device')
|
2025-10-01 00:29:21 +08:00
|
|
|
|
* @returns {Promise<Object>} 详情
|
|
|
|
|
|
*/
|
|
|
|
|
|
async getDevice(id, type) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (type === 'area_controller') {
|
2025-10-20 14:52:25 +08:00
|
|
|
|
const response = await AreaControllerApi.getById(id);
|
2025-10-01 00:29:21 +08:00
|
|
|
|
return {...response.data, type: 'area_controller'};
|
|
|
|
|
|
} else {
|
2025-10-20 14:52:25 +08:00
|
|
|
|
const response = await DeviceApi.getById(id);
|
2025-10-01 00:29:21 +08:00
|
|
|
|
return {...response.data, type: 'device', parent_id: response.data.area_controller_id};
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('获取设备详情失败:', error);
|
|
|
|
|
|
throw error;
|
|
|
|
|
|
}
|
2025-09-19 23:56:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-01 00:29:21 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 更新设备或区域主控信息
|
2025-10-20 15:07:30 +08:00
|
|
|
|
* @param {number} id - ID
|
|
|
|
|
|
* @param {Object} data - 更新的设备或区域主控信息,包含type字段
|
2025-10-01 00:29:21 +08:00
|
|
|
|
* @returns {Promise<Object>} 更新后的信息
|
|
|
|
|
|
*/
|
2025-10-20 15:07:30 +08:00
|
|
|
|
async updateDevice(id, data) {
|
2025-10-01 00:29:21 +08:00
|
|
|
|
try {
|
2025-10-20 15:07:30 +08:00
|
|
|
|
if (data.type === 'area_controller') {
|
|
|
|
|
|
const response = await AreaControllerApi.update(id, data);
|
2025-10-01 00:29:21 +08:00
|
|
|
|
return {...response.data, type: 'area_controller'};
|
|
|
|
|
|
} else {
|
2025-10-20 15:07:30 +08:00
|
|
|
|
const response = await DeviceApi.update(id, data);
|
2025-10-01 00:29:21 +08:00
|
|
|
|
return {...response.data, type: 'device', parent_id: response.data.area_controller_id};
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('更新设备失败:', error);
|
|
|
|
|
|
throw error;
|
|
|
|
|
|
}
|
2025-09-19 14:25:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-01 00:29:21 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 删除设备或区域主控
|
2025-10-20 15:07:30 +08:00
|
|
|
|
* @param {Object} device - 包含id和type属性的设备或区域主控对象
|
2025-10-01 00:29:21 +08:00
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
|
*/
|
|
|
|
|
|
async deleteDevice(device) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (device.type === 'area_controller') {
|
|
|
|
|
|
await AreaControllerApi.delete(device.id);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
await DeviceApi.delete(device.id);
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('删除设备失败:', error);
|
|
|
|
|
|
throw error;
|
|
|
|
|
|
}
|
2025-09-19 14:25:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 导出设备服务实例
|
2025-09-30 22:20:50 +08:00
|
|
|
|
export default new DeviceService();
|