| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- import datetime
- import random
- import logging
- class LightSampler:
- """
- 根据一天中的时间,以概率方式为光照设置生成下一个状态。
- """
- def __init__(self, light_ids: list, light_period_start: datetime.time = None, light_period_end: datetime.time = None):
- """
- 初始化采样器。
- :param light_ids: 此采样器应为其生成状态的所有灯光的ID列表。
- :param light_period_start: 光周期的开始时间 (datetime.time object)。
- :param light_period_end: 光周期的结束时间 (datetime.time object)。
- """
- self.light_ids = light_ids
- self.light_period_start = light_period_start or datetime.time(6, 0)
- self.light_period_end = light_period_end or datetime.time(22, 0)
- logging.info(f"光照采样器已初始化,光周期为 {self.light_period_start.strftime('%H:%M')} - {self.light_period_end.strftime('%H:%M')}。")
- def sample_next_light_state(self) -> dict:
- """
- 为每个灯光独立采样下一个光照状态,步长为 5%。
- :return: 一个状态字典,格式为 {light_id: intensity}。
- """
- current_time = datetime.datetime.now().time()
- next_state = {}
-
- # 定义所有可能的亮度值 (0.0, 0.05, ..., 1.0)
- possible_intensities = [round(i * 0.05, 2) for i in range(21)]
- # 判断当前是否在光周期内
- is_light_period = self.light_period_start <= current_time < self.light_period_end
- logging.info(f"开始为 {len(self.light_ids)} 个灯光独立采样,当前为 {'光周期' if is_light_period else '暗周期'}。")
- for light_id in self.light_ids:
- intensity = 0.0
- if is_light_period:
- # 光周期:大概率高光照,小概率低光照
- if random.random() < 0.95: # 95% 的概率为正常高光照
- # 从 [0.8, 0.85, ..., 1.0] 中选择
- high_options = [i for i in possible_intensities if 0.8 <= i <= 1.0]
- intensity = random.choice(high_options)
- else: # 5% 的概率为特殊情况的低光照
- # 从 [0.1, 0.15, ..., 0.4] 中选择
- low_options = [i for i in possible_intensities if 0.1 <= i <= 0.4]
- intensity = random.choice(low_options)
- else:
- # 暗周期:大概率关闭,极小概率施加光照
- if random.random() < 0.99: # 99% 的概率为正常关闭
- intensity = 0.0
- else: # 1% 的概率为特殊情况的突然光照
- # 从 [0.5, 0.55, ..., 1.0] 中选择
- sudden_light_options = [i for i in possible_intensities if 0.5 <= i <= 1.0]
- intensity = random.choice(sudden_light_options)
-
- next_state[light_id] = intensity
- logging.debug(f" - 灯光 {light_id}: 采样亮度 {intensity:.2f}")
- logging.info("所有灯光采样完成。")
- return next_state
|