config_loader.py 3.9 KB

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