2025-09-30 22:20:50 +08:00
|
|
|
|
import { AreaControllerApi, DeviceApi } from '../api/device.js';
|
2025-09-19 14:25:20 +08:00
|
|
|
|
|
|
|
|
|
|
class DeviceService {
|
|
|
|
|
|
/**
|
2025-09-30 22:20:50 +08:00
|
|
|
|
* 获取所有设备和区域主控的列表,并将其合并为树形结构所需的数据
|
|
|
|
|
|
* @returns {Promise<Array>} 合并后的设备列表
|
2025-09-19 14:25:20 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async getDevices() {
|
|
|
|
|
|
try {
|
2025-09-30 22:20:50 +08:00
|
|
|
|
const [areaControllersResponse, devicesResponse] = await Promise.all([
|
|
|
|
|
|
AreaControllerApi.list(),
|
|
|
|
|
|
DeviceApi.list()
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
const areaControllers = (areaControllersResponse.data || []).map(controller => ({
|
|
|
|
|
|
...controller,
|
|
|
|
|
|
type: 'area_controller' // 添加类型标识
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
const devices = (devicesResponse.data || []).map(device => ({
|
|
|
|
|
|
...device,
|
|
|
|
|
|
type: 'device', // 添加类型标识
|
|
|
|
|
|
parent_id: device.area_controller_id // 适配前端树形结构
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
return [...areaControllers, ...devices];
|
2025-09-19 14:25:20 +08:00
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('获取设备列表失败:', error);
|
|
|
|
|
|
throw error;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-30 22:20:50 +08:00
|
|
|
|
* 创建新设备或区域主控
|
|
|
|
|
|
* @param {Object} deviceData 设备或区域主控信息,包含type字段
|
|
|
|
|
|
* @returns {Promise<Object>} 创建结果
|
2025-09-19 14:25:20 +08:00
|
|
|
|
*/
|
2025-09-30 22:20:50 +08:00
|
|
|
|
async createDevice(deviceData) {
|
2025-09-19 14:25:20 +08:00
|
|
|
|
try {
|
2025-09-30 22:20:50 +08:00
|
|
|
|
if (deviceData.type === 'area_controller') {
|
|
|
|
|
|
const response = await AreaControllerApi.create(deviceData);
|
|
|
|
|
|
return { ...response.data, type: 'area_controller' };
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 默认创建普通设备
|
|
|
|
|
|
const response = await DeviceApi.create(deviceData);
|
|
|
|
|
|
return { ...response.data, type: 'device', parent_id: response.data.area_controller_id };
|
|
|
|
|
|
}
|
2025-09-19 14:25:20 +08:00
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('创建设备失败:', error);
|
|
|
|
|
|
throw error;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-19 23:56:30 +08:00
|
|
|
|
/**
|
2025-09-30 22:20:50 +08:00
|
|
|
|
* 获取设备或区域主控详情
|
|
|
|
|
|
* @param {number} id ID
|
|
|
|
|
|
* @param {string} type 类型 ('area_controller' 或 'device')
|
|
|
|
|
|
* @returns {Promise<Object>} 详情
|
2025-09-19 23:56:30 +08:00
|
|
|
|
*/
|
2025-09-30 22:20:50 +08:00
|
|
|
|
async getDevice(id, type) {
|
2025-09-19 23:56:30 +08:00
|
|
|
|
try {
|
2025-09-30 22:20:50 +08:00
|
|
|
|
if (type === 'area_controller') {
|
|
|
|
|
|
const response = await AreaControllerApi.get(id);
|
|
|
|
|
|
return { ...response.data, type: 'area_controller' };
|
|
|
|
|
|
} else {
|
|
|
|
|
|
const response = await DeviceApi.get(id);
|
|
|
|
|
|
return { ...response.data, type: 'device', parent_id: response.data.area_controller_id };
|
|
|
|
|
|
}
|
2025-09-19 23:56:30 +08:00
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('获取设备详情失败:', error);
|
|
|
|
|
|
throw error;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-19 14:25:20 +08:00
|
|
|
|
/**
|
2025-09-30 22:20:50 +08:00
|
|
|
|
* 更新设备或区域主控信息
|
|
|
|
|
|
* @param {number} id ID
|
|
|
|
|
|
* @param {Object} deviceData 更新的设备或区域主控信息,包含type字段
|
|
|
|
|
|
* @returns {Promise<Object>} 更新后的信息
|
2025-09-19 14:25:20 +08:00
|
|
|
|
*/
|
2025-09-30 22:20:50 +08:00
|
|
|
|
async updateDevice(id, deviceData) {
|
2025-09-19 14:25:20 +08:00
|
|
|
|
try {
|
2025-09-30 22:20:50 +08:00
|
|
|
|
if (deviceData.type === 'area_controller') {
|
|
|
|
|
|
const response = await AreaControllerApi.update(id, deviceData);
|
|
|
|
|
|
return { ...response.data, type: 'area_controller' };
|
|
|
|
|
|
} else {
|
|
|
|
|
|
const response = await DeviceApi.update(id, deviceData);
|
|
|
|
|
|
return { ...response.data, type: 'device', parent_id: response.data.area_controller_id };
|
|
|
|
|
|
}
|
2025-09-19 14:25:20 +08:00
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('更新设备失败:', error);
|
|
|
|
|
|
throw error;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-30 22:20:50 +08:00
|
|
|
|
* 删除设备或区域主控
|
|
|
|
|
|
* @param {Object} device 包含id和type属性的设备或区域主控对象
|
2025-09-19 14:25:20 +08:00
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
|
*/
|
2025-09-30 22:20:50 +08:00
|
|
|
|
async deleteDevice(device) {
|
2025-09-19 14:25:20 +08:00
|
|
|
|
try {
|
2025-09-30 22:20:50 +08:00
|
|
|
|
if (device.type === 'area_controller') {
|
|
|
|
|
|
await AreaControllerApi.delete(device.id);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
await DeviceApi.delete(device.id);
|
|
|
|
|
|
}
|
2025-09-19 14:25:20 +08:00
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('删除设备失败:', error);
|
|
|
|
|
|
throw error;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 导出设备服务实例
|
2025-09-30 22:20:50 +08:00
|
|
|
|
export default new DeviceService();
|