Files
Feeding_control_system/common/helpers.py
2026-03-13 21:04:19 +08:00

44 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""工具函数模块"""
def get_f_block_positions(artifact_list):
"""获取artifact_list中F块的位置"""
positions = []
for i, artifact in enumerate(artifact_list):
if artifact.get("BlockNumber") == "F":
positions.append(i)
return positions
def cleanup_old_timestamps(artifact_timestamps, current_artifact_ids=None, max_age_hours=24):
"""
清理过期的时间戳记录
Args:
artifact_timestamps: 时间戳字典
current_artifact_ids: 当前活跃的artifact_id集合可选
max_age_hours: 时间戳最大保留时间小时默认24小时
"""
from datetime import datetime
current_time = datetime.now()
expired_ids = []
# 遍历所有存储的时间戳记录
for artifact_id, timestamp in artifact_timestamps.items():
# 如果提供了当前活跃ID列表且该ID不在当前活跃列表中则检查是否过期
if current_artifact_ids is None or artifact_id not in current_artifact_ids:
age = current_time - timestamp
# 如果超过最大保留时间,则标记为过期
if age.total_seconds() > max_age_hours * 3600:
expired_ids.append(artifact_id)
# 删除过期的时间戳记录
for artifact_id in expired_ids:
del artifact_timestamps[artifact_id]
if expired_ids:
print(f"清理了 {len(expired_ids)} 个过期的时间戳记录: {expired_ids}")
return len(expired_ids)