config_loader.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import configparser
  2. import re
  3. from .models import *
  4. def load_config(config_path: str) -> Config:
  5. cfg = configparser.ConfigParser()
  6. cfg.read(config_path, encoding='utf-8')
  7. config = Config()
  8. config.app_name = cfg.get("App", "name", fallback="钢包工位视频监测系统")
  9. config.copyright = cfg.get("App", "copyright", fallback="恒莱达")
  10. config.screen = cfg.getint("App", "screen", fallback=0)
  11. module_type_str = cfg.get("Hardware", "module_type", fallback="RFID")
  12. config.module_type = ModuleType.RFID if module_type_str == "RFID" else ModuleType.HD3200
  13. if cfg.has_section("Video"):
  14. config.video.rtsp_timeout = cfg.getfloat("Video", "rtsp_timeout", fallback=5.0)
  15. # RFID
  16. if config.module_type == ModuleType.RFID:
  17. config.rfid = SocketConfig(
  18. ip=cfg.get("RFID", "ip"),
  19. port=cfg.getint("RFID", "port"),
  20. stability_time=cfg.getint("RFID", "stability_time", fallback=20),
  21. stability_count=cfg.getint("RFID", "stability_count", fallback=3)
  22. )
  23. # Modbus
  24. if config.module_type == ModuleType.HD3200:
  25. config.modbus = ModbusConfig(
  26. ip=cfg.get("Modbus", "ip"),
  27. port=cfg.getint("Modbus", "port", fallback=502),
  28. frequency=cfg.getint("Modbus", "frequency", fallback=100),
  29. stability_count=cfg.getint("Modbus", "stability_count", fallback=3)
  30. )
  31. # AI
  32. config.ai.enabled = cfg.getboolean("AI", "enabled", fallback=False)
  33. config.ai.tts_enabled = cfg.getboolean("AI", "tts_enabled", fallback=False)
  34. # AI Modules (自动扫描 AI:N 段)
  35. for section in sorted(
  36. (s for s in cfg.sections() if re.match(r'^AI:\d+$', s)),
  37. key=lambda s: int(s.split(':')[1])
  38. ):
  39. idx = int(section.split(':')[1])
  40. topics_str = cfg.get(section, "topics", fallback="")
  41. config.ai_modules.append(AIModuleModel(
  42. id=idx,
  43. broker=cfg.get(section, "broker"),
  44. port=cfg.getint(section, "port", fallback=1883),
  45. topics=[t.strip() for t in topics_str.split('\n') if t.strip()]
  46. ))
  47. # Buckets (自动扫描 Bucket:N 段)
  48. bucket_sections = sorted(
  49. (s for s in cfg.sections() if re.match(r'^Bucket:\d+$', s)),
  50. key=lambda s: int(s.split(':')[1])
  51. )
  52. for section in bucket_sections:
  53. bucket = BucketModel(
  54. id=cfg.getint(section, "id"),
  55. description=cfg.get(section, "description"),
  56. code_x=cfg.get(section, "code_x"),
  57. code_y=cfg.get(section, "code_y", fallback=""),
  58. ai_module_id=cfg.getint(section, "ai_module", fallback=None)
  59. )
  60. left_rtsp = cfg.get(section, "left_rtsp")
  61. right_rtsp = cfg.get(section, "right_rtsp")
  62. left_topic = cfg.get(section, "left_topic", fallback="")
  63. right_topic = cfg.get(section, "right_topic", fallback="")
  64. cameras = []
  65. if left_rtsp:
  66. cameras.append(CameraModel(position=CameraPosition.LEFT, rtsp_url=left_rtsp, topic=left_topic))
  67. if right_rtsp:
  68. cameras.append(CameraModel(position=CameraPosition.RIGHT, rtsp_url=right_rtsp, topic=right_topic))
  69. bucket.cameras = cameras
  70. config.buckets.append(bucket)
  71. return config