import logging import os import sys from time import sleep import signal import configSer import logSet import tcp_Ser import upload.DataReporter import videoDetection import videoPush from upload import mqttHelper tcp_service = None video_processor = None reporter = None def signal_handler(sig, frame): global tcp_service,video_processor,reporter print(f"收到退出信号 sig={sig},程序准备退出") tcp_service.stop() tcp_service.join() video_processor.stop() videoPush.stop() reporter.stop() print(f"===释放完毕,退出===") sys.exit(0) def read_config_path() ->str : config_name = "config.json" # determine if application is a script file or frozen exe if getattr(sys, 'frozen', False): application_path = os.path.dirname(sys.executable) elif __file__: application_path = os.path.dirname(__file__) config_path = os.path.join(application_path, config_name) print(f"命令路径:{os.getcwd()}, config_path_abs路径:{config_path}") return config_path signal.signal(signal.SIGINT, signal_handler) # 捕获 Ctrl+C 信号 if __name__ == '__main__': logSet.log_init() if __debug__: print("Debug 模式") else: print("release 模式") config_path_abs=read_config_path() # 读取配置文件 config_obj= configSer.ConfigOperate(config_path_abs) json_str = config_obj.config_info.to_json(indent=4) logging.info(f"当前配置:{json_str}") tcp_service = tcp_Ser.TcpSer("0.0.0.0", config_obj.config_info.server.port) tcp_service.start() reporter = upload.DataReporter.DataReporter(data_fps=config_obj.config_info.fps.data,video_fps=config_obj.config_info.fps.video) reporter.register_handler(tcp_service.broadcast_message) if config_obj.config_info.upload.enable: mq_config=config_obj.config_info.upload.mqtt mq = upload.mqttHelper.MQTTClient(broker=mq_config.broker, port=mq_config.port, topic=mq_config.topic, username=mq_config.username, password=mq_config.password, client_id=mq_config.client_id) if mq.start(): reporter.register_handler(mq.publish) else: logging.warn("mq链接失败,不上报") reporter.start() # 启动video videoDetection.configObj=config_obj videoDetection.reporter=reporter video_processor = videoDetection.VideoProcessor() # 添加订阅者processor tcp_service.add_subscribe(video_processor) # 推流 videoPush.video_server_run() # 启动 video_processor.video_mode(config_obj.config_info.capture) logging("==over==")