diff --git a/src/services/bintask-manager.service.ts b/src/services/bintask-manager.service.ts
index 7681500..8426ecd 100644
--- a/src/services/bintask-manager.service.ts
+++ b/src/services/bintask-manager.service.ts
@@ -23,44 +23,15 @@ export class BinTaskManagerService {
public getBinTaskData(pointName: string, locationName: string): BinTaskItem[] {
if (!pointName || !locationName) return [];
- try {
- const rawData = this.editor.getBinLocationsList();
- if (!rawData) return [];
+ const targetLocation = this.findBinLocation(pointName, locationName);
+ if (!targetLocation) return [];
- const binLocationGroups = Array.isArray(rawData)
- ? (rawData as BinLocationGroup[])
- : (rawData as BinLocationsList)?.binLocationsList;
-
- if (!binLocationGroups || !Array.isArray(binLocationGroups)) return [];
-
- const allBinLocations = binLocationGroups
- .filter((group) => group && Array.isArray(group.binLocationList))
- .flatMap((group) => group.binLocationList)
- .filter((item) => item && typeof item === 'object');
-
- const targetLocation = allBinLocations.find(
- (item) => item.pointName === pointName && item.instanceName === locationName
- );
-
- if (!targetLocation || !targetLocation.property || !Array.isArray(targetLocation.property)) {
- return [];
- }
-
- const binTaskProperty = targetLocation.property.find((prop) => prop && prop.key === 'binTask');
- if (!binTaskProperty || !binTaskProperty.stringValue) {
- return [];
- }
-
- try {
- return JSON.parse(binTaskProperty.stringValue.replace(/\n/g, '').trim());
- } catch (error) {
- console.error('解析BinTask数据失败:', error);
- return [];
- }
- } catch (error) {
- console.error('获取BinTask数据失败:', error);
+ const binTaskProperty = targetLocation.property?.find((prop) => prop && prop.key === 'binTask');
+ if (!binTaskProperty || !binTaskProperty.stringValue) {
return [];
}
+
+ return this.parseBinTask(binTaskProperty.stringValue);
}
/**
@@ -71,49 +42,19 @@ export class BinTaskManagerService {
public getPointBinTaskData(pointName: string): Record {
if (!pointName) return {};
- try {
- const rawData = this.editor.getBinLocationsList();
- if (!rawData) return {};
+ const pointLocations = this.getPointBinLocations(pointName);
+ const result: Record = {};
- const binLocationGroups = Array.isArray(rawData)
- ? (rawData as BinLocationGroup[])
- : (rawData as BinLocationsList)?.binLocationsList;
+ pointLocations.forEach((location) => {
+ if (location.instanceName) {
+ const binTaskProperty = location.property?.find((prop) => prop && prop.key === 'binTask');
+ result[location.instanceName] = binTaskProperty && binTaskProperty.stringValue
+ ? this.parseBinTask(binTaskProperty.stringValue)
+ : [];
+ }
+ });
- if (!binLocationGroups || !Array.isArray(binLocationGroups)) return {};
-
- const allBinLocations = binLocationGroups
- .filter((group) => group && Array.isArray(group.binLocationList))
- .flatMap((group) => group.binLocationList)
- .filter((item) => item && typeof item === 'object');
-
- const pointLocations = allBinLocations.filter(
- (item) => item.pointName === pointName
- );
-
- const result: Record = {};
-
- pointLocations.forEach((location) => {
- if (location.instanceName && location.property && Array.isArray(location.property)) {
- const binTaskProperty = location.property.find((prop) => prop && prop.key === 'binTask');
- if (binTaskProperty && binTaskProperty.stringValue) {
- try {
- const binTasks = JSON.parse(binTaskProperty.stringValue.replace(/\n/g, '').trim());
- result[location.instanceName] = binTasks;
- } catch (error) {
- console.error('解析库位BinTask数据失败:', error, location);
- result[location.instanceName] = [];
- }
- } else {
- result[location.instanceName] = [];
- }
- }
- });
-
- return result;
- } catch (error) {
- console.error('获取动作点BinTask数据失败:', error);
- return {};
- }
+ return result;
}
/**
@@ -138,6 +79,66 @@ export class BinTaskManagerService {
return binTaskData.length;
}
+ /**
+ * 解析库位任务数据
+ * @param binTaskString 库位任务字符串
+ * @returns 解析后的任务数据
+ */
+ public parseBinTask(binTaskString: string): BinTaskItem[] {
+ try {
+ return JSON.parse(binTaskString.replace(/\n/g, '').trim());
+ } catch (error) {
+ console.error('解析库位任务数据失败:', error);
+ return [];
+ }
+ }
+
+ /**
+ * 获取任务中的所有操作
+ * @param task 任务对象
+ * @returns 操作对象映射
+ */
+ public getTaskOperations(task: BinTaskItem): Record {
+ if (!task || typeof task !== 'object') return {};
+
+ const operations: Record = {};
+
+ // 遍历任务对象的所有属性,查找操作对象
+ for (const [key, value] of Object.entries(task)) {
+ if (value && typeof value === 'object' && 'operation' in value) {
+ operations[key] = value;
+ }
+ }
+
+ return operations;
+ }
+
+ /**
+ * 获取当前动作点对应的库位任务数据(用于显示)
+ * @param pointName 动作点名称
+ * @returns 库位任务数据列表
+ */
+ public getPointBinTaskDataForDisplay(pointName: string): Array<{
+ instanceName: string;
+ binTasks: BinTaskItem[];
+ }> {
+ if (!pointName) return [];
+
+ const pointLocations = this.getPointBinLocations(pointName);
+
+ return pointLocations
+ .map((item) => {
+ const binTaskProperty = item.property?.find((prop) => prop && prop.key === 'binTask');
+ return {
+ instanceName: item.instanceName || '未知库位',
+ binTasks: binTaskProperty && binTaskProperty.stringValue
+ ? this.parseBinTask(binTaskProperty.stringValue)
+ : [],
+ };
+ })
+ .filter((item) => item.binTasks.length > 0);
+ }
+
//#endregion
//#region 数据转换相关
@@ -399,6 +400,41 @@ export class BinTaskManagerService {
return Array.isArray(binLocationGroups) ? binLocationGroups : [];
}
+ /**
+ * 获取所有库位数据
+ * @returns 所有库位数据数组
+ */
+ private getAllBinLocations(): any[] {
+ const binLocationGroups = this.getBinLocationGroups();
+ return binLocationGroups
+ .filter((group) => group && Array.isArray(group.binLocationList))
+ .flatMap((group) => group.binLocationList)
+ .filter((item) => item && typeof item === 'object');
+ }
+
+ /**
+ * 查找指定动作点和库位的库位数据
+ * @param pointName 动作点名称
+ * @param locationName 库位名称
+ * @returns 库位数据,如果未找到则返回null
+ */
+ private findBinLocation(pointName: string, locationName: string): any | null {
+ const allBinLocations = this.getAllBinLocations();
+ return allBinLocations.find(
+ (item) => item.pointName === pointName && item.instanceName === locationName
+ ) || null;
+ }
+
+ /**
+ * 获取指定动作点的所有库位数据
+ * @param pointName 动作点名称
+ * @returns 库位数据数组
+ */
+ private getPointBinLocations(pointName: string): any[] {
+ const allBinLocations = this.getAllBinLocations();
+ return allBinLocations.filter((item) => item.pointName === pointName);
+ }
+
/**
* 检查库位项是否存在
* @param pointName 动作点名称