from pathlib import Path from PFAL_SysControl.Controller.light import LightSystem from PFAL_SysControl.Controller.camera import CameraSystem from PFAL_SysControl.Utils.database import DatabaseManager from PFAL_SysControl.Utils.light_sampler import LightSampler class CultivationRack: """ 代表一个完整的栽培架,封装了所有硬件控制器。 它可以通过依赖注入的方式,获得数据库和采样器等服务,以执行复杂任务。 """ def __init__(self, name: str, config: dict, db_manager: DatabaseManager = None, light_sampler: LightSampler = None): self.name = name self.config = config try: light_conf = self.config['lights'][name] camera_conf_list = self.config['cameras'][name] except KeyError as e: raise ValueError(f"在配置中找不到控制器 '{name}' 的硬件信息: {e}") # 1. 实例化硬件控制器 self.light_system = LightSystem(name, light_conf) # 2. 实例化相机控制器,并传入所有依赖(包括从外部注入的服务) self.camera_system = CameraSystem( camera_configs=camera_conf_list, light_controller=self.light_system, db_manager=db_manager, light_sampler=light_sampler ) # --- Camera Facade Methods --- def capture_all_photos(self, task_name: str): """提供一个高级接口来执行并发拍照任务。""" if not self.camera_system.db_manager or not self.camera_system.light_sampler: raise RuntimeError("数据库或光照采样器未被注入,无法执行此复杂任务。") base_path = Path(self.config['cameras']['settings']['download_path']) return self.camera_system.capture_all_with_lighting(base_path, task_name) def capture_photo_by_name(self, camera_name: str): """提供一个高级接口来对单个摄像头拍照。""" base_path = Path(self.config['cameras']['settings']['download_path']) return self.camera_system.capture_single_by_name(camera_name, base_path) # --- Light Facade Methods --- def set_layer_intensity(self, layer_id: int, intensity: float): """设置指定层所有灯光的亮度。""" with self.light_system as ls: ls.set_layer_intensity(layer_id, intensity) def turn_on_all(self): """打开此栽培架上的所有灯光。""" with self.light_system as ls: ls.turn_on_all() def turn_off_all(self): """关闭此栽培架上的所有灯光。""" with self.light_system as ls: ls.turn_off_all() def set_light_by_name(self, name: str, intensity: float): """按名称设置单个灯光的亮度。""" with self.light_system as ls: ls.set_light_by_name(name, intensity) def set_light_by_id(self, light_id: int, intensity: float): """按ID设置单个灯光的亮度。""" with self.light_system as ls: ls.set_light_by_id(light_id, intensity) def turn_on_layer(self, layer_id: int): """打开指定层的所有灯光。""" with self.light_system as ls: ls.turn_on_layer(layer_id) def turn_off_layer(self, layer_id: int): """关闭指定层的所有灯光。""" with self.light_system as ls: ls.turn_off_layer(layer_id)