增强稳定性,使用线程实时读取PLC中db_100中的数据并存储起来,read_generic从存储的文件中读取
This commit is contained in:
@ -6,10 +6,12 @@ from plc_manager import PLCManager
|
||||
from cache_manager import CacheManager
|
||||
from api_server import APIServer
|
||||
from config_manager import ConfigManager
|
||||
from db100_reader import DB100ReaderThread
|
||||
|
||||
|
||||
class GatewayApp:
|
||||
"""PLC网关应用程序主类"""
|
||||
|
||||
|
||||
def __init__(self, config_path="../config/config.json"):
|
||||
self.config_path = config_path
|
||||
self.config_manager = ConfigManager(config_path)
|
||||
@ -18,35 +20,38 @@ class GatewayApp:
|
||||
self.api_server = None
|
||||
self.reload_flag = False
|
||||
self.reload_lock = threading.Lock()
|
||||
# DB100ReaderThread线程相关初始化
|
||||
self.db100_reader_thread = None
|
||||
|
||||
self.logger = logging.getLogger("GatewayApp")
|
||||
|
||||
|
||||
# 加载初始配置
|
||||
self.load_configuration()
|
||||
|
||||
|
||||
def load_configuration(self):
|
||||
"""加载配置并初始化组件"""
|
||||
# 加载配置
|
||||
if not self.config_manager.load_config():
|
||||
self.logger.error("Failed to load initial configuration")
|
||||
return False
|
||||
|
||||
|
||||
config = self.config_manager.get_config()
|
||||
|
||||
|
||||
# 重新初始化PLC连接
|
||||
if self.plc_manager:
|
||||
self.logger.info("Reinitializing PLC connections...")
|
||||
self.plc_manager = PLCManager(config["plcs"])
|
||||
self.plc_manager.connect_all()
|
||||
|
||||
|
||||
# 重新初始化缓存
|
||||
if self.cache_manager:
|
||||
self.logger.info("Stopping existing cache manager...")
|
||||
self.cache_manager.stop()
|
||||
|
||||
|
||||
self.logger.info("Initializing cache manager...")
|
||||
self.cache_manager = CacheManager(config, self.plc_manager, app=self)
|
||||
self.cache_manager.start()
|
||||
|
||||
|
||||
# 重新初始化API服务器
|
||||
if self.api_server:
|
||||
self.logger.info("API server already running")
|
||||
@ -54,10 +59,18 @@ class GatewayApp:
|
||||
self.logger.info("Starting API server...")
|
||||
self.api_server = APIServer(self.cache_manager, self.config_path)
|
||||
self.api_server.start()
|
||||
|
||||
|
||||
# 重新初始化DB100ReaderThread线程
|
||||
for plc in config["plcs"]:
|
||||
plc_name = plc["name"]
|
||||
client = self.plc_manager.get_plc(plc_name)
|
||||
self.logger.info("Starting db100_reader_thread...")
|
||||
self.db100_reader_thread = DB100ReaderThread(client, output_file="db100_latest_data.log")
|
||||
self.db100_reader_thread.start()
|
||||
|
||||
self.logger.info("Configuration loaded successfully")
|
||||
return True
|
||||
|
||||
|
||||
def check_for_reload(self):
|
||||
"""检查是否需要重载配置"""
|
||||
while True:
|
||||
@ -66,22 +79,22 @@ class GatewayApp:
|
||||
self.reload_flag = False
|
||||
self.load_configuration()
|
||||
time.sleep(1)
|
||||
|
||||
|
||||
def request_reload(self):
|
||||
"""请求重载配置"""
|
||||
with self.reload_lock:
|
||||
self.reload_flag = True
|
||||
self.logger.info("Configuration reload requested")
|
||||
|
||||
|
||||
def run(self):
|
||||
"""运行主程序"""
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(threadName)s - %(name)s - %(levelname)s - %(message)s'
|
||||
)
|
||||
|
||||
|
||||
self.logger.info("Starting PLC Gateway...")
|
||||
|
||||
|
||||
# 启动配置重载检查线程
|
||||
reload_thread = threading.Thread(
|
||||
target=self.check_for_reload,
|
||||
@ -89,7 +102,7 @@ class GatewayApp:
|
||||
daemon=True
|
||||
)
|
||||
reload_thread.start()
|
||||
|
||||
|
||||
try:
|
||||
# 保持主程序运行
|
||||
while True:
|
||||
@ -101,9 +114,11 @@ class GatewayApp:
|
||||
self.cache_manager.stop()
|
||||
self.logger.info("Shutdown complete")
|
||||
|
||||
|
||||
def main():
|
||||
app = GatewayApp()
|
||||
app.run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user