将读取文件添加PLC的名字
This commit is contained in:
@ -30,6 +30,8 @@ class CacheManager:
|
|||||||
self.logger = logging.getLogger("CacheManager")
|
self.logger = logging.getLogger("CacheManager")
|
||||||
self.init_cache()
|
self.init_cache()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def init_cache(self):
|
def init_cache(self):
|
||||||
"""初始化缓存结构"""
|
"""初始化缓存结构"""
|
||||||
for plc in self.config["plcs"]:
|
for plc in self.config["plcs"]:
|
||||||
@ -643,6 +645,8 @@ class CacheManager:
|
|||||||
plc_status = self.plc_connection_status.get(plc_name, "unknown")
|
plc_status = self.plc_connection_status.get(plc_name, "unknown")
|
||||||
return None, "Area is read-only", plc_status, 0
|
return None, "Area is read-only", plc_status, 0
|
||||||
|
|
||||||
|
cache_file = f"{plc_name}_area_{area_name}.log"
|
||||||
|
|
||||||
# 计算实际DB偏移
|
# 计算实际DB偏移
|
||||||
db_offset = area["offset"] + offset
|
db_offset = area["offset"] + offset
|
||||||
|
|
||||||
@ -669,7 +673,7 @@ class CacheManager:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
# 使用Snap7Client的read_generic方法
|
# 使用Snap7Client的read_generic方法
|
||||||
result = client.read_generic(area["db_number"], db_offset, data_type, count)
|
result = client.read_generic(area["db_number"], db_offset, data_type, cache_file, count)
|
||||||
if result is None:
|
if result is None:
|
||||||
return None, "Read failed", plc_status, 0
|
return None, "Read failed", plc_status, 0
|
||||||
|
|
||||||
@ -873,4 +877,5 @@ class CacheManager:
|
|||||||
"plc_connection_status": plc_status,
|
"plc_connection_status": plc_status,
|
||||||
"last_update": last_update,
|
"last_update": last_update,
|
||||||
"last_update_formatted": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(last_update)) if last_update > 0 else "Never"
|
"last_update_formatted": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(last_update)) if last_update > 0 else "Never"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -89,9 +89,10 @@ class GatewayApp:
|
|||||||
# 创建并启动线程
|
# 创建并启动线程
|
||||||
read_thread = PLCDataReaderThread(
|
read_thread = PLCDataReaderThread(
|
||||||
plc_client=plc_client,
|
plc_client=plc_client,
|
||||||
|
plc_name=plc_config["name"],
|
||||||
area_config=area_config,
|
area_config=area_config,
|
||||||
update_interval=0.03,
|
update_interval=0.03,
|
||||||
output_file_prefix="plc_area_"
|
output_file_prefix="area_"
|
||||||
)
|
)
|
||||||
read_thread.start()
|
read_thread.start()
|
||||||
# 存入线程列表,便于后续停止
|
# 存入线程列表,便于后续停止
|
||||||
|
|||||||
@ -15,7 +15,7 @@ from snap7.util import get_real, get_int, get_bool, get_word, get_dint # 导入
|
|||||||
|
|
||||||
|
|
||||||
class PLCDataReaderThread(threading.Thread):
|
class PLCDataReaderThread(threading.Thread):
|
||||||
def __init__(self, plc_client, area_config, update_interval=0.03, output_file_prefix="plc_area_"):
|
def __init__(self, plc_client, plc_name, area_config, update_interval=0.03, output_file_prefix="area_"):
|
||||||
"""
|
"""
|
||||||
初始化PLC数据读取线程(配置驱动,支持多区域)
|
初始化PLC数据读取线程(配置驱动,支持多区域)
|
||||||
参数:
|
参数:
|
||||||
@ -26,11 +26,12 @@ class PLCDataReaderThread(threading.Thread):
|
|||||||
output_file_prefix: 输出文件前缀,最终文件名为“前缀+区域名.log”
|
output_file_prefix: 输出文件前缀,最终文件名为“前缀+区域名.log”
|
||||||
"""
|
"""
|
||||||
# 线程名包含区域名,便于日志区分(如"PLCDataReader_DB100_Read")
|
# 线程名包含区域名,便于日志区分(如"PLCDataReader_DB100_Read")
|
||||||
thread_name = f"PLCDataReader_{area_config['name']}"
|
thread_name = f"{plc_name}_Reader_{area_config['name']}"
|
||||||
super().__init__(name=thread_name, daemon=True)
|
super().__init__(name=thread_name, daemon=True)
|
||||||
|
|
||||||
# 1. 核心依赖(PLC客户端+区域配置)
|
# 1. 核心依赖(PLC客户端+区域配置)
|
||||||
self.plc_client = plc_client
|
self.plc_client = plc_client
|
||||||
|
self.plc_name = plc_name
|
||||||
self.area_config = area_config # 动态区域配置,不再硬编码DB100
|
self.area_config = area_config # 动态区域配置,不再硬编码DB100
|
||||||
self.area_name = area_config["name"]
|
self.area_name = area_config["name"]
|
||||||
self.db_number = area_config["db_number"]
|
self.db_number = area_config["db_number"]
|
||||||
@ -40,7 +41,7 @@ class PLCDataReaderThread(threading.Thread):
|
|||||||
|
|
||||||
# 2. 线程与输出配置
|
# 2. 线程与输出配置
|
||||||
self.update_interval = update_interval
|
self.update_interval = update_interval
|
||||||
self.output_file = f"{output_file_prefix}DB{self.db_number}.log" # 每个区域独立文件
|
self.output_file = f"{self.plc_name}_{output_file_prefix}{self.area_name}.log" # 输出文件名}{output_file_prefix}DB{self.db_number}.log" # 每个区域独立文件
|
||||||
|
|
||||||
# 3. 数据缓存(新增结构化数据存储)
|
# 3. 数据缓存(新增结构化数据存储)
|
||||||
self.running = False
|
self.running = False
|
||||||
|
|||||||
@ -54,7 +54,7 @@ class Snap7Client:
|
|||||||
|
|
||||||
self.last_connect_attempt = current_time
|
self.last_connect_attempt = current_time
|
||||||
try:
|
try:
|
||||||
with self.lock:
|
with self.lock: # 进入锁,其他线程需等待
|
||||||
self.client.connect(self.ip, self.rack, self.slot)
|
self.client.connect(self.ip, self.rack, self.slot)
|
||||||
|
|
||||||
# 验证连接是否真正有效
|
# 验证连接是否真正有效
|
||||||
@ -94,7 +94,7 @@ class Snap7Client:
|
|||||||
return None # 返回None而不是零填充数据
|
return None # 返回None而不是零填充数据
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with self.lock:
|
with self.lock: # 进入锁,其他线程需等待
|
||||||
data = self.client.db_read(db_number, offset, size)
|
data = self.client.db_read(db_number, offset, size)
|
||||||
# 验证返回数据的有效性
|
# 验证返回数据的有效性
|
||||||
if data is None or len(data) != size:
|
if data is None or len(data) != size:
|
||||||
@ -456,7 +456,7 @@ class Snap7Client:
|
|||||||
self.logger.error(f"❌ 读取DB{db_number}缓存文件异常: {e}", exc_info=True)
|
self.logger.error(f"❌ 读取DB{db_number}缓存文件异常: {e}", exc_info=True)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def read_generic(self, db_number, offset, data_type, count=1):
|
def read_generic(self, db_number, offset, data_type, cache_file, count=1):
|
||||||
"""
|
"""
|
||||||
通用读取接口(改进):
|
通用读取接口(改进):
|
||||||
- DB100:优先从缓存文件读取 → 再内存缓存 → 最后PLC
|
- DB100:优先从缓存文件读取 → 再内存缓存 → 最后PLC
|
||||||
@ -465,7 +465,7 @@ class Snap7Client:
|
|||||||
# 1、处理DB数据块:优先从缓存文件读取
|
# 1、处理DB数据块:优先从缓存文件读取
|
||||||
raw_data = None
|
raw_data = None
|
||||||
if raw_data is None:
|
if raw_data is None:
|
||||||
cache_file = f"plc_area_DB{db_number}.log"
|
cache_file = cache_file
|
||||||
raw_data = self.read_db_from_file_cache(db_number, offset, count, cache_file)
|
raw_data = self.read_db_from_file_cache(db_number, offset, count, cache_file)
|
||||||
if raw_data is not None:
|
if raw_data is not None:
|
||||||
print(f"从缓存文件中读取DB{db_number}的数据")
|
print(f"从缓存文件中读取DB{db_number}的数据")
|
||||||
|
|||||||
Reference in New Issue
Block a user