1.修改部分代码风格,实现风格统一 2.添加数据刷新时间到config中 3.将html单独放到文件中并且添加一些相关说明
This commit is contained in:
@ -55,9 +55,12 @@ class CacheManager:
|
||||
def refresh_cache(self):
|
||||
"""后台线程:定期刷新缓存"""
|
||||
while self.running:
|
||||
start_time = time.time()
|
||||
|
||||
try:
|
||||
for plc in self.config["plcs"]:
|
||||
plc_name = plc["name"]
|
||||
refresh_interval = plc.get("refresh_interval", 0.5)
|
||||
client = self.plc_manager.get_plc(plc_name)
|
||||
|
||||
# 检查PLC连接状态
|
||||
@ -96,6 +99,23 @@ class CacheManager:
|
||||
with self.lock:
|
||||
self.cache[plc_name][name]["status"] = self.plc_connection_status[plc_name]
|
||||
self.logger.warning(f"Error updating status for {plc_name}/{name}: {e}")
|
||||
|
||||
"""计算刷新一个PLC的时间"""
|
||||
# 计算实际执行时间
|
||||
execution_time = time.time() - start_time
|
||||
#计算需要睡眠的时间,确保总等于refresh_time
|
||||
sleep_time = max(0, refresh_interval - execution_time)
|
||||
time.sleep(sleep_time)
|
||||
print(f"plc_name: {plc_name},"
|
||||
f"Cache refresh completed.Execution time: {execution_time:.3f}s,"
|
||||
f"Sleep time: {sleep_time:.3f}s,"
|
||||
f"Total interval: {execution_time + sleep_time:.3f}s")
|
||||
|
||||
# 记录实际刷新间隔
|
||||
self.logger.debug(f"plc_name: {plc_name},"
|
||||
f"Cache refresh completed.Execution time: {execution_time:.3f}s,"
|
||||
f"Sleep time: {sleep_time:.3f}s,"
|
||||
f"Total interval: {execution_time + sleep_time:.3f}s")
|
||||
|
||||
time.sleep(self.refresh_interval)
|
||||
except Exception as e:
|
||||
@ -291,7 +311,6 @@ class CacheManager:
|
||||
return False, f"PLC not connected (status: {plc_status})", plc_status, 0
|
||||
|
||||
try:
|
||||
print(data)
|
||||
success = client.write_db(area["db_number"], area["offset"] + offset, data)
|
||||
if success:
|
||||
# 更新缓存中的这部分数据
|
||||
@ -333,9 +352,7 @@ class CacheManager:
|
||||
return False, f"PLC not connected (status: {plc_status})", plc_status, 0
|
||||
|
||||
try:
|
||||
print("data:", data)
|
||||
for i, byte in enumerate(data):
|
||||
print("i,byte:", i, byte)
|
||||
byte_data = bytes([byte])
|
||||
current_offset = offset + (i * 2)
|
||||
byte_value = byte_data[0]
|
||||
@ -345,7 +362,6 @@ class CacheManager:
|
||||
set_int(value, 0, byte_value)
|
||||
data = value
|
||||
|
||||
print(area["db_number"], current_offset, data)
|
||||
success = client.batch_write_db(area["db_number"], current_offset, data)
|
||||
if success:
|
||||
# 更新缓存中的这部分数据
|
||||
@ -388,11 +404,9 @@ class CacheManager:
|
||||
try:
|
||||
value = bytearray(offset + 1)
|
||||
for bit, bit_value in enumerate(data):
|
||||
print("i,byte:", bit, bit_value)
|
||||
set_bool(value, offset, bit, bit_value)
|
||||
data = value
|
||||
|
||||
print(area["db_number"], offset, data)
|
||||
success = client.batch_write_db_bool(area["db_number"], offset, data)
|
||||
if success:
|
||||
# 更新缓存中的这部分数据
|
||||
|
||||
Reference in New Issue
Block a user