料袋末尾检测
This commit is contained in:
223
CU/drop.py
223
CU/drop.py
@ -3,11 +3,12 @@ import configparser
|
||||
import os
|
||||
from typing import Optional
|
||||
from Model.Position import Real_Position
|
||||
from Model.FeedModel import PositionModel
|
||||
from Model.FeedModel import LineModel, PositionModel
|
||||
import Constant
|
||||
|
||||
|
||||
class DropPositionManager:
|
||||
def __init__(self, config_path="CU/drop.ini"):
|
||||
def __init__(self, config_path=Constant.dropLine_set_file):
|
||||
self.config_path = config_path
|
||||
self.config = configparser.ConfigParser()
|
||||
self._load_config()
|
||||
@ -17,8 +18,7 @@ class DropPositionManager:
|
||||
self._current_point_id: Optional[int] = None
|
||||
self._current_path: list = [] # 当前路径点列表
|
||||
self._current_index: int = 0 # 当前路径中的索引
|
||||
|
||||
|
||||
self._current_drop_section:dict = {} # 使用字典存储每个main_section对应的最大drop-section
|
||||
|
||||
def _load_config(self):
|
||||
"""加载配置文件"""
|
||||
@ -77,24 +77,31 @@ class DropPositionManager:
|
||||
|
||||
# 1. 加载 DropMidPoint{point_id}-*(按 level 升序)
|
||||
mid_points = []
|
||||
# _mid_section_start=f"DropMidPoint{point_id}-"
|
||||
_mid_section_start="DropMidPoint1-"
|
||||
for sec in self.config.sections():
|
||||
if sec.startswith(f"DropMidPoint{point_id}-") and self.config.getint(sec, "lineid") == lineid:
|
||||
if sec.startswith(_mid_section_start) and self.config.getint(sec, "lineid") == lineid:
|
||||
try:
|
||||
level = int(sec.split('-')[1])
|
||||
pos = self._read_position_from_section(sec)
|
||||
mid_points.append((level, pos))
|
||||
break
|
||||
except Exception as e:
|
||||
print(f"❌ 解析 {sec} 失败: {e}")
|
||||
|
||||
mid_points.sort(key=lambda x: x[0])
|
||||
|
||||
# 2. 加载 ResetPoint{point_id}-*(按 level 升序)
|
||||
reset_points = []
|
||||
# _reset_section_start=f"ResetPoint{point_id}-"
|
||||
_reset_section_start="ResetPoint1-"
|
||||
for sec in self.config.sections():
|
||||
if sec.startswith(f"ResetPoint{point_id}-") and self.config.getint(sec, "lineid") == lineid:
|
||||
if sec.startswith(_reset_section_start) and self.config.getint(sec, "lineid") == lineid:
|
||||
try:
|
||||
level = int(sec.split('-')[1])
|
||||
pos = self._read_position_from_section(sec)
|
||||
reset_points.append((level, pos))
|
||||
break
|
||||
except Exception as e:
|
||||
print(f"❌ 解析 {sec} 失败: {e}")
|
||||
reset_points.sort(key=lambda x: x[0])
|
||||
@ -140,10 +147,167 @@ class DropPositionManager:
|
||||
|
||||
model.lineType=self.config.getint(section, "linetype")
|
||||
model.status=self.config.getint(section, "status")
|
||||
|
||||
model.id=self.config.getint(section, "id")
|
||||
model.order=self.config.getint(section, "order")
|
||||
model.lineId=self.config.getint(section, "lineid")
|
||||
# 保存section名称,用于排序
|
||||
model.section = section
|
||||
|
||||
return model
|
||||
|
||||
def _read_position_return_leveL(self,config_reader,section,lineid)->Optional[tuple]:
|
||||
try:
|
||||
if config_reader.getint(section, "lineid") == lineid:
|
||||
# 提取 - 后的数字
|
||||
level = int(section.split('-')[1])
|
||||
position_model = self._read_position_from_section(section)
|
||||
return (level, position_model)
|
||||
except Exception as e:
|
||||
print(f"{__name__}:_read_position_return_leveL:{e}")
|
||||
return None
|
||||
|
||||
|
||||
|
||||
def load_path_points(self,lineid: int) ->Optional[LineModel]:
|
||||
"""根据lineid加载所有码垛的路径信息"""
|
||||
#默认码垛的lineid从10开始
|
||||
_lineid=lineid+10
|
||||
line_model = LineModel(_lineid)
|
||||
line_model.line_category = 2
|
||||
line_model.id = _lineid
|
||||
|
||||
# 查找主表 DropLineX
|
||||
main_section = f"{Constant.dropLine_set_section}{lineid}"
|
||||
if self.config.has_section(main_section):
|
||||
line_model.name = self.config.get(main_section, "name", fallback=f"路径{lineid}")
|
||||
else:
|
||||
return None
|
||||
|
||||
# 先收集所有 DropPoints 开头的 section
|
||||
drop_points_sections = []
|
||||
for sec in self.config.sections():
|
||||
if sec.startswith("DropPoints"):
|
||||
try:
|
||||
if self.config.getint(sec, "lineid") == lineid:
|
||||
# 提取数字部分
|
||||
num_part = sec.replace("DropPoints", "")
|
||||
if num_part.isdigit():
|
||||
drop_points_sections.append((sec, int(num_part)))
|
||||
except Exception as e:
|
||||
print(f"{__name__}:_load_path_points异常1:{e}")
|
||||
continue
|
||||
|
||||
# 按数字排序 DropPoints sections
|
||||
drop_points_sections.sort(key=lambda x: x[1])
|
||||
# 使用main_section作为键存储当前最大的drop-section编号
|
||||
self._current_drop_section[main_section] = drop_points_sections[-1][1] if drop_points_sections else 1
|
||||
mid_points_other=[] #最后点没有匹配的droppoints
|
||||
# 遍历每个 DropPoints,按照_load_point_path的逻辑加载对应的中间点和复位点
|
||||
for sec, point_num in drop_points_sections:
|
||||
try:
|
||||
# 1. 加载 DropMidPoint{point_num}-*(按 level 升序)
|
||||
mid_points = []
|
||||
for s in self.config.sections():
|
||||
if s.startswith(f"DropMidPoint{point_num}-"):
|
||||
_mid=self._read_position_return_leveL(self.config,s,lineid)
|
||||
if _mid:
|
||||
mid_points.append(_mid)
|
||||
if point_num==(len(drop_points_sections)):
|
||||
if s.startswith(f"DropMidPoint{point_num+1}-"):
|
||||
_mid=self._read_position_return_leveL(self.config,s,lineid)
|
||||
if _mid:
|
||||
mid_points_other.append(_mid)
|
||||
|
||||
|
||||
# 按level升序排序
|
||||
mid_points.sort(key=lambda x: x[0])
|
||||
# 添加中间点到路径
|
||||
for _, pos in mid_points:
|
||||
line_model.positions.append(pos)
|
||||
|
||||
# 加载 ResetPoint{point_num}-*(按 level 升序)
|
||||
reset_points = []
|
||||
for s in self.config.sections():
|
||||
if s.startswith(f"ResetPoint{point_num}-"):
|
||||
_reset=self._read_position_return_leveL(self.config,s,lineid)
|
||||
if _reset:
|
||||
reset_points.append(_reset)
|
||||
# 按level升序排序
|
||||
reset_points.sort(key=lambda x: x[0])
|
||||
# 添加复位点到路径
|
||||
for _, pos in reset_points:
|
||||
line_model.positions.append(pos)
|
||||
#添加当前 DropPoints
|
||||
try:
|
||||
position_model = self._read_position_from_section(sec)
|
||||
line_model.positions.append(position_model)
|
||||
except Exception as e:
|
||||
print(f"{__name__}:_load_path_points异常3:{e}")
|
||||
except Exception as e:
|
||||
print(f"{__name__}:_load_path_points异常4:{e}")
|
||||
|
||||
if mid_points_other:
|
||||
mid_points_other.sort(key=lambda x: x[0])
|
||||
for _, pos in mid_points_other:
|
||||
line_model.positions.append(pos)
|
||||
return line_model
|
||||
|
||||
def save_path_points(self, line_model: LineModel):
|
||||
"""根据lineid保存所有码垛的路径信息"""
|
||||
#默认码垛的lineid从10开始,保存的时候减一
|
||||
_lineid=line_model.id-10
|
||||
if _lineid<=0:
|
||||
return
|
||||
self.config.read(Constant.dropLine_set_file, encoding='utf-8')
|
||||
# 查找主表 DropLineX
|
||||
main_section = f"{Constant.dropLine_set_section}{_lineid}"
|
||||
if not self.config.has_section(main_section):
|
||||
self.config.add_section(main_section)
|
||||
self.config.set(main_section, "name", line_model.name)
|
||||
self.config.set(main_section, "id", str(_lineid))
|
||||
_current_reset_index=1
|
||||
_current_mid_index=1
|
||||
_current_drop_section_val=self._current_drop_section[main_section]
|
||||
# 保存每个DropPoints
|
||||
for i, pos in enumerate(line_model.positions):
|
||||
if pos.lineId == _lineid or pos.lineId == line_model.id:
|
||||
#最后一个扔包点
|
||||
if pos.section.startswith(f"DropMidPoint{_current_drop_section_val+1}"):
|
||||
_current_mid_index=int(pos.section.split('-')[-1])+1
|
||||
if pos.section.startswith(f"ResetPoint{_current_drop_section_val}"):
|
||||
_current_reset_index=int(pos.section.split('-')[-1])+1
|
||||
#新增的数据,如果是前点,需要获取后点的数据
|
||||
if pos.section.startswith("Position"):
|
||||
pos.lineId = _lineid
|
||||
|
||||
if pos.status is None:
|
||||
continue
|
||||
# FDropMid = 7
|
||||
elif pos.status==7:
|
||||
#只有一个
|
||||
pos.section = f"DropMidPoint{_current_drop_section_val+1}-{_current_mid_index}"
|
||||
_current_mid_index+=1
|
||||
|
||||
elif pos.status==9:
|
||||
pos.section = f"DropPoints{_current_drop_section_val+1}"
|
||||
_current_drop_section_val+=1
|
||||
_current_mid_index=1
|
||||
_current_reset_index=1
|
||||
|
||||
elif pos.status==10:
|
||||
pos.section = f"ResetPoint{_current_drop_section_val}-{_current_reset_index}"
|
||||
_current_reset_index+=1
|
||||
#保存数据
|
||||
pos.save_position_model(self.config)
|
||||
|
||||
with open(Constant.dropLine_set_file, 'w', encoding='utf-8') as f:
|
||||
self.config.write(f)
|
||||
|
||||
def del_drop_point(self,section):
|
||||
self.config.read(Constant.dropLine_set_file, encoding = 'utf-8')
|
||||
self.config.remove_section(section)
|
||||
with open(Constant.dropLine_set_file, 'w', encoding='utf-8') as f:
|
||||
self.config.write(f)
|
||||
|
||||
def _get_point_debug_info(manager, pos, model):
|
||||
config = manager.config
|
||||
@ -184,47 +348,4 @@ if __name__ == "__main__":
|
||||
# manager = DropPositionManager("drop.ini")
|
||||
manager = DropPositionManager()
|
||||
lineid = 1
|
||||
|
||||
print(f"\n🔁 测试:通过 point 参数切换路径集合\n")
|
||||
|
||||
# 先走 point=1
|
||||
print("=" * 60)
|
||||
print("📦 开始执行 point=1 的路径")
|
||||
print("=" * 60)
|
||||
for i in range(10):
|
||||
pos_model = manager.get_next_drop_position(lineid=lineid, point=1)
|
||||
if pos_model is None:
|
||||
print("🔚 point=1 路径结束")
|
||||
break
|
||||
pos = pos_model.get_position()
|
||||
info = _get_point_debug_info(manager, pos, pos_model)
|
||||
print(f" 🚀 {info}")
|
||||
if pos_model.status == 9:
|
||||
print(" 💥 执行【扔包】操作!")
|
||||
|
||||
# 再走 point=2
|
||||
print("\n" + "=" * 60)
|
||||
print("📦 开始执行 point=2 的路径")
|
||||
print("=" * 60)
|
||||
for i in range(10):
|
||||
pos_model = manager.get_next_drop_position(lineid=lineid, point=2)
|
||||
if pos_model is None:
|
||||
print("🔚 point=2 路径结束")
|
||||
break
|
||||
pos = pos_model.get_position()
|
||||
info = _get_point_debug_info(manager, pos, pos_model)
|
||||
print(f" 🚀 {info}")
|
||||
if pos_model.status == 9:
|
||||
print(" 💥 执行【扔包】操作!")
|
||||
|
||||
# 再切回 point=1
|
||||
print("\n" + "=" * 60)
|
||||
print("🔄 切换回 point=1,从头开始")
|
||||
print("=" * 60)
|
||||
for i in range(3):
|
||||
pos_model = manager.get_next_drop_position(lineid=lineid, point=1)
|
||||
if pos_model is None:
|
||||
break
|
||||
pos = pos_model.get_position()
|
||||
info = _get_point_debug_info(manager, pos, pos_model)
|
||||
print(f" 🚀 {info}")
|
||||
manager.load_path_points(1)
|
||||
|
||||
Reference in New Issue
Block a user