from __future__ import annotations from copy import deepcopy from enum import Enum from ..draw_utils import draw_clock class Recipe: def __init__(self, name: str, steps: list[Step]): self.name = name self.steps = steps def __str__(self): return self.name def copy(self) -> Recipe: return deepcopy(self) class StepType(Enum): SECTION = 0 WEIGH = 1 WEIGH_WITH_TIMER = 2 TARE = 3 def render(self, draw, position, fill='black', **kwargs) -> str: font_size = kwargs.get('font_size', 10) if self == StepType.SECTION: draw.text(position, "T", fill=fill, **kwargs) elif self == StepType.WEIGH: draw.text(position, "W", fill=fill, **kwargs) elif self == StepType.WEIGH_WITH_TIMER: draw.text(position, "W", fill=fill, **kwargs) r = max(3, font_size // 4) draw_clock(draw, (position[0] + font_size + 2, position[1] + font_size // 2), radius=r, color=fill) elif self == StepType.TARE: draw.text(position, "0.0g", fill=fill, **kwargs) class Step: def __init__(self, step_type: StepType, value: float | str = None, goal_time: float = 0.0): self.step_type = step_type self.value = value self.goal_time = goal_time @property def value_str(self) -> str: if self.step_type == StepType.WEIGH: return f"{self.value}g" elif self.step_type == StepType.WEIGH_WITH_TIMER: s = f"{self.value}g" if self.goal_time > 0: minutes = int(self.goal_time) // 60 seconds = int(self.goal_time) % 60 if minutes == 0: s += f" {seconds}s" else: s += f" {minutes}:{seconds:02d}" return s elif self.step_type == StepType.SECTION: return str(self.value) elif self.step_type == StepType.TARE: return "Tare" else: return "" ###### example recipes ######### V60 = Recipe( "V60", [ Step(StepType.SECTION, "Grind"), Step(StepType.TARE), Step(StepType.WEIGH, 15), Step(StepType.SECTION, "Brew"), Step(StepType.TARE), Step(StepType.WEIGH_WITH_TIMER, 50, goal_time=45), Step(StepType.WEIGH, 250), ] ) ESPRESSO = Recipe( "Espresso", [ Step(StepType.SECTION, "Grind"), Step(StepType.TARE), Step(StepType.WEIGH, 18), Step(StepType.SECTION, "Brew"), Step(StepType.TARE), Step(StepType.WEIGH_WITH_TIMER, 40, goal_time=0.0), ] )