mirror of
https://github.com/dptech-corp/Uni-Lab-OS.git
synced 2026-02-06 23:15:10 +00:00
优化了全protocol的运行时间,除了pumptransfer相关的还没
This commit is contained in:
@@ -4,7 +4,7 @@ import time as time_module # 重命名time模块,避免与参数冲突
|
||||
from typing import Dict, Any
|
||||
|
||||
class VirtualHeatChill:
|
||||
"""Virtual heat chill device for HeatChillProtocol testing"""
|
||||
"""Virtual heat chill device for HeatChillProtocol testing 🌡️"""
|
||||
|
||||
def __init__(self, device_id: str = None, config: Dict[str, Any] = None, **kwargs):
|
||||
# 处理可能的不同调用方式
|
||||
@@ -31,31 +31,40 @@ class VirtualHeatChill:
|
||||
for key, value in kwargs.items():
|
||||
if key not in skip_keys and not hasattr(self, key):
|
||||
setattr(self, key, value)
|
||||
|
||||
print(f"🌡️ === 虚拟温控设备 {self.device_id} 已创建 === ✨")
|
||||
print(f"🔥 温度范围: {self._min_temp}°C ~ {self._max_temp}°C | 🌪️ 最大搅拌: {self._max_stir_speed} RPM")
|
||||
|
||||
async def initialize(self) -> bool:
|
||||
"""Initialize virtual heat chill"""
|
||||
self.logger.info(f"Initializing virtual heat chill {self.device_id}")
|
||||
"""Initialize virtual heat chill 🚀"""
|
||||
self.logger.info(f"🔧 初始化虚拟温控设备 {self.device_id} ✨")
|
||||
|
||||
# 初始化状态信息
|
||||
self.data.update({
|
||||
"status": "Idle",
|
||||
"status": "🏠 待机中",
|
||||
"operation_mode": "Idle",
|
||||
"is_stirring": False,
|
||||
"stir_speed": 0.0,
|
||||
"remaining_time": 0.0,
|
||||
})
|
||||
|
||||
self.logger.info(f"✅ 温控设备 {self.device_id} 初始化完成 🌡️")
|
||||
self.logger.info(f"📊 设备规格: 温度范围 {self._min_temp}°C ~ {self._max_temp}°C | 搅拌范围 0 ~ {self._max_stir_speed} RPM")
|
||||
return True
|
||||
|
||||
async def cleanup(self) -> bool:
|
||||
"""Cleanup virtual heat chill"""
|
||||
self.logger.info(f"Cleaning up virtual heat chill {self.device_id}")
|
||||
"""Cleanup virtual heat chill 🧹"""
|
||||
self.logger.info(f"🧹 清理虚拟温控设备 {self.device_id} 🔚")
|
||||
|
||||
self.data.update({
|
||||
"status": "Offline",
|
||||
"status": "💤 离线",
|
||||
"operation_mode": "Offline",
|
||||
"is_stirring": False,
|
||||
"stir_speed": 0.0,
|
||||
"remaining_time": 0.0
|
||||
})
|
||||
|
||||
self.logger.info(f"✅ 温控设备 {self.device_id} 清理完成 💤")
|
||||
return True
|
||||
|
||||
async def heat_chill(self, vessel: str, temp: float, time, stir: bool,
|
||||
@@ -72,62 +81,73 @@ class VirtualHeatChill:
|
||||
purpose = str(purpose)
|
||||
except (ValueError, TypeError) as e:
|
||||
error_msg = f"参数类型转换错误: temp={temp}({type(temp)}), time={time}({type(time)}), error={str(e)}"
|
||||
self.logger.error(error_msg)
|
||||
self.logger.error(f"❌ {error_msg}")
|
||||
self.data.update({
|
||||
"status": f"Error: {error_msg}",
|
||||
"status": f"❌ 错误: {error_msg}",
|
||||
"operation_mode": "Error"
|
||||
})
|
||||
return False
|
||||
|
||||
self.logger.info(f"HeatChill: vessel={vessel}, temp={temp}°C, time={time_value}s, stir={stir}, stir_speed={stir_speed}")
|
||||
# 确定温度操作emoji
|
||||
if temp > 25.0:
|
||||
temp_emoji = "🔥"
|
||||
operation_mode = "Heating"
|
||||
status_action = "加热"
|
||||
elif temp < 25.0:
|
||||
temp_emoji = "❄️"
|
||||
operation_mode = "Cooling"
|
||||
status_action = "冷却"
|
||||
else:
|
||||
temp_emoji = "🌡️"
|
||||
operation_mode = "Maintaining"
|
||||
status_action = "保温"
|
||||
|
||||
self.logger.info(f"🌡️ 开始温控操作: {vessel} → {temp}°C {temp_emoji}")
|
||||
self.logger.info(f" 🥽 容器: {vessel}")
|
||||
self.logger.info(f" 🎯 目标温度: {temp}°C {temp_emoji}")
|
||||
self.logger.info(f" ⏰ 持续时间: {time_value}s")
|
||||
self.logger.info(f" 🌪️ 搅拌: {stir} ({stir_speed} RPM)")
|
||||
self.logger.info(f" 📝 目的: {purpose}")
|
||||
|
||||
# 验证参数范围
|
||||
if temp > self._max_temp or temp < self._min_temp:
|
||||
error_msg = f"温度 {temp}°C 超出范围 ({self._min_temp}°C - {self._max_temp}°C)"
|
||||
self.logger.error(error_msg)
|
||||
error_msg = f"🌡️ 温度 {temp}°C 超出范围 ({self._min_temp}°C - {self._max_temp}°C) ⚠️"
|
||||
self.logger.error(f"❌ {error_msg}")
|
||||
self.data.update({
|
||||
"status": f"Error: {error_msg}",
|
||||
"status": f"❌ 错误: 温度超出范围 ⚠️",
|
||||
"operation_mode": "Error"
|
||||
})
|
||||
return False
|
||||
|
||||
if stir and stir_speed > self._max_stir_speed:
|
||||
error_msg = f"搅拌速度 {stir_speed} RPM 超出最大值 {self._max_stir_speed} RPM"
|
||||
self.logger.error(error_msg)
|
||||
error_msg = f"🌪️ 搅拌速度 {stir_speed} RPM 超出最大值 {self._max_stir_speed} RPM ⚠️"
|
||||
self.logger.error(f"❌ {error_msg}")
|
||||
self.data.update({
|
||||
"status": f"Error: {error_msg}",
|
||||
"status": f"❌ 错误: 搅拌速度超出范围 ⚠️",
|
||||
"operation_mode": "Error"
|
||||
})
|
||||
return False
|
||||
|
||||
if time_value <= 0:
|
||||
error_msg = f"时间 {time_value}s 必须大于0"
|
||||
self.logger.error(error_msg)
|
||||
error_msg = f"⏰ 时间 {time_value}s 必须大于0 ⚠️"
|
||||
self.logger.error(f"❌ {error_msg}")
|
||||
self.data.update({
|
||||
"status": f"Error: {error_msg}",
|
||||
"status": f"❌ 错误: 时间参数无效 ⚠️",
|
||||
"operation_mode": "Error"
|
||||
})
|
||||
return False
|
||||
|
||||
# 确定操作模式
|
||||
if temp > 25.0:
|
||||
operation_mode = "Heating"
|
||||
status_action = "加热"
|
||||
elif temp < 25.0:
|
||||
operation_mode = "Cooling"
|
||||
status_action = "冷却"
|
||||
else:
|
||||
operation_mode = "Maintaining"
|
||||
status_action = "保温"
|
||||
|
||||
# 🔧 修复:使用转换后的时间值
|
||||
start_time = time_module.time()
|
||||
total_time = time_value # 使用转换后的浮点数
|
||||
|
||||
self.logger.info(f"🚀 开始{status_action}程序! 预计用时 {total_time:.1f}秒 ⏱️")
|
||||
|
||||
# 开始操作
|
||||
stir_info = f" | 搅拌: {stir_speed} RPM" if stir else ""
|
||||
stir_info = f" | 🌪️ 搅拌: {stir_speed} RPM" if stir else ""
|
||||
|
||||
self.data.update({
|
||||
"status": f"运行中: {status_action} {vessel} 至 {temp}°C | 剩余: {total_time:.0f}s{stir_info}",
|
||||
"status": f"{temp_emoji} 运行中: {status_action} {vessel} 至 {temp}°C | ⏰ 剩余: {total_time:.0f}s{stir_info}",
|
||||
"operation_mode": operation_mode,
|
||||
"is_stirring": stir,
|
||||
"stir_speed": stir_speed if stir else 0.0,
|
||||
@@ -135,17 +155,25 @@ class VirtualHeatChill:
|
||||
})
|
||||
|
||||
# 在等待过程中每秒更新剩余时间
|
||||
last_logged_time = 0
|
||||
while True:
|
||||
current_time = time_module.time()
|
||||
elapsed = current_time - start_time
|
||||
remaining = max(0, total_time - elapsed)
|
||||
progress = (elapsed / total_time) * 100 if total_time > 0 else 100
|
||||
|
||||
# 更新剩余时间和状态
|
||||
self.data.update({
|
||||
"remaining_time": remaining,
|
||||
"status": f"运行中: {status_action} {vessel} 至 {temp}°C | 剩余: {remaining:.0f}s{stir_info}"
|
||||
"status": f"{temp_emoji} 运行中: {status_action} {vessel} 至 {temp}°C | ⏰ 剩余: {remaining:.0f}s{stir_info}",
|
||||
"progress": progress
|
||||
})
|
||||
|
||||
# 进度日志(每25%打印一次)
|
||||
if progress >= 25 and int(progress) % 25 == 0 and int(progress) != last_logged_time:
|
||||
self.logger.info(f"📊 {status_action}进度: {progress:.0f}% | ⏰ 剩余: {remaining:.0f}s | {temp_emoji} 目标: {temp}°C ✨")
|
||||
last_logged_time = int(progress)
|
||||
|
||||
# 如果时间到了,退出循环
|
||||
if remaining <= 0:
|
||||
break
|
||||
@@ -154,20 +182,30 @@ class VirtualHeatChill:
|
||||
await asyncio.sleep(1.0)
|
||||
|
||||
# 操作完成
|
||||
final_stir_info = f" | 搅拌: {stir_speed} RPM" if stir else ""
|
||||
final_stir_info = f" | 🌪️ 搅拌: {stir_speed} RPM" if stir else ""
|
||||
|
||||
self.data.update({
|
||||
"status": f"完成: {vessel} 已达到 {temp}°C | 用时: {total_time:.0f}s{final_stir_info}",
|
||||
"status": f"✅ 完成: {vessel} 已达到 {temp}°C {temp_emoji} | ⏱️ 用时: {total_time:.0f}s{final_stir_info}",
|
||||
"operation_mode": "Completed",
|
||||
"remaining_time": 0.0,
|
||||
"is_stirring": False,
|
||||
"stir_speed": 0.0
|
||||
"stir_speed": 0.0,
|
||||
"progress": 100.0
|
||||
})
|
||||
|
||||
self.logger.info(f"HeatChill completed for vessel {vessel} at {temp}°C after {total_time}s")
|
||||
self.logger.info(f"🎉 温控操作完成! ✨")
|
||||
self.logger.info(f"📊 操作结果:")
|
||||
self.logger.info(f" 🥽 容器: {vessel}")
|
||||
self.logger.info(f" 🌡️ 达到温度: {temp}°C {temp_emoji}")
|
||||
self.logger.info(f" ⏱️ 总用时: {total_time:.0f}s")
|
||||
if stir:
|
||||
self.logger.info(f" 🌪️ 搅拌速度: {stir_speed} RPM")
|
||||
self.logger.info(f" 📝 操作目的: {purpose} 🏁")
|
||||
|
||||
return True
|
||||
|
||||
async def heat_chill_start(self, vessel: str, temp: float, purpose: str) -> bool:
|
||||
"""Start continuous heat chill"""
|
||||
"""Start continuous heat chill 🔄"""
|
||||
|
||||
# 🔧 添加类型转换
|
||||
try:
|
||||
@@ -176,73 +214,82 @@ class VirtualHeatChill:
|
||||
purpose = str(purpose)
|
||||
except (ValueError, TypeError) as e:
|
||||
error_msg = f"参数类型转换错误: {str(e)}"
|
||||
self.logger.error(error_msg)
|
||||
self.logger.error(f"❌ {error_msg}")
|
||||
self.data.update({
|
||||
"status": f"Error: {error_msg}",
|
||||
"status": f"❌ 错误: {error_msg}",
|
||||
"operation_mode": "Error"
|
||||
})
|
||||
return False
|
||||
|
||||
self.logger.info(f"HeatChillStart: vessel={vessel}, temp={temp}°C")
|
||||
|
||||
# 验证参数
|
||||
if temp > self._max_temp or temp < self._min_temp:
|
||||
error_msg = f"温度 {temp}°C 超出范围 ({self._min_temp}°C - {self._max_temp}°C)"
|
||||
self.logger.error(error_msg)
|
||||
self.data.update({
|
||||
"status": f"Error: {error_msg}",
|
||||
"operation_mode": "Error"
|
||||
})
|
||||
return False
|
||||
|
||||
# 确定操作模式
|
||||
# 确定温度操作emoji
|
||||
if temp > 25.0:
|
||||
temp_emoji = "🔥"
|
||||
operation_mode = "Heating"
|
||||
status_action = "持续加热"
|
||||
elif temp < 25.0:
|
||||
temp_emoji = "❄️"
|
||||
operation_mode = "Cooling"
|
||||
status_action = "持续冷却"
|
||||
else:
|
||||
temp_emoji = "🌡️"
|
||||
operation_mode = "Maintaining"
|
||||
status_action = "恒温保持"
|
||||
|
||||
self.logger.info(f"🔄 启动持续温控: {vessel} → {temp}°C {temp_emoji}")
|
||||
self.logger.info(f" 🥽 容器: {vessel}")
|
||||
self.logger.info(f" 🎯 目标温度: {temp}°C {temp_emoji}")
|
||||
self.logger.info(f" 🔄 模式: {status_action}")
|
||||
self.logger.info(f" 📝 目的: {purpose}")
|
||||
|
||||
# 验证参数
|
||||
if temp > self._max_temp or temp < self._min_temp:
|
||||
error_msg = f"🌡️ 温度 {temp}°C 超出范围 ({self._min_temp}°C - {self._max_temp}°C) ⚠️"
|
||||
self.logger.error(f"❌ {error_msg}")
|
||||
self.data.update({
|
||||
"status": f"❌ 错误: 温度超出范围 ⚠️",
|
||||
"operation_mode": "Error"
|
||||
})
|
||||
return False
|
||||
|
||||
self.data.update({
|
||||
"status": f"启动: {status_action} {vessel} 至 {temp}°C | 持续运行",
|
||||
"status": f"🔄 启动: {status_action} {vessel} 至 {temp}°C {temp_emoji} | ♾️ 持续运行",
|
||||
"operation_mode": operation_mode,
|
||||
"is_stirring": False,
|
||||
"stir_speed": 0.0,
|
||||
"remaining_time": -1.0, # -1 表示持续运行
|
||||
})
|
||||
|
||||
self.logger.info(f"✅ 持续温控已启动! {temp_emoji} {status_action}模式 🚀")
|
||||
return True
|
||||
|
||||
async def heat_chill_stop(self, vessel: str) -> bool:
|
||||
"""Stop heat chill"""
|
||||
"""Stop heat chill 🛑"""
|
||||
|
||||
# 🔧 添加类型转换
|
||||
try:
|
||||
vessel = str(vessel)
|
||||
except (ValueError, TypeError) as e:
|
||||
error_msg = f"参数类型转换错误: {str(e)}"
|
||||
self.logger.error(error_msg)
|
||||
self.logger.error(f"❌ {error_msg}")
|
||||
return False
|
||||
|
||||
self.logger.info(f"HeatChillStop: vessel={vessel}")
|
||||
self.logger.info(f"🛑 停止温控: {vessel}")
|
||||
|
||||
self.data.update({
|
||||
"status": f"已停止: {vessel} 温控停止",
|
||||
"status": f"🛑 已停止: {vessel} 温控停止",
|
||||
"operation_mode": "Stopped",
|
||||
"is_stirring": False,
|
||||
"stir_speed": 0.0,
|
||||
"remaining_time": 0.0,
|
||||
})
|
||||
|
||||
self.logger.info(f"✅ 温控设备已停止 {vessel} 的温度控制 🏁")
|
||||
return True
|
||||
|
||||
# 状态属性
|
||||
@property
|
||||
def status(self) -> str:
|
||||
return self.data.get("status", "Idle")
|
||||
return self.data.get("status", "🏠 待机中")
|
||||
|
||||
@property
|
||||
def operation_mode(self) -> str:
|
||||
@@ -258,4 +305,20 @@ class VirtualHeatChill:
|
||||
|
||||
@property
|
||||
def remaining_time(self) -> float:
|
||||
return self.data.get("remaining_time", 0.0)
|
||||
return self.data.get("remaining_time", 0.0)
|
||||
|
||||
@property
|
||||
def progress(self) -> float:
|
||||
return self.data.get("progress", 0.0)
|
||||
|
||||
@property
|
||||
def max_temp(self) -> float:
|
||||
return self._max_temp
|
||||
|
||||
@property
|
||||
def min_temp(self) -> float:
|
||||
return self._min_temp
|
||||
|
||||
@property
|
||||
def max_stir_speed(self) -> float:
|
||||
return self._max_stir_speed
|
||||
Reference in New Issue
Block a user