Skip to content

Commit

Permalink
Add a mystery option to the controller
Browse files Browse the repository at this point in the history
  • Loading branch information
cecille committed Oct 15, 2024
1 parent 31ed684 commit 1a1d5c6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
4 changes: 2 additions & 2 deletions examples/valve/controller/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ To compile the chip-repl, from the root of the chip tree:
. scripts/activate.sh
./scripts/build_python.sh -i out/pyenv
source out/pyenv/activate
out/pyenv/chip-repl --
out/pyenv/bin/chip-repl
```

The chip-repl is a shell that lets you directly call python functions. It
Expand All @@ -60,7 +60,7 @@ from chip import ChipDeviceCtrl
await devCtrl.CommissionOnNetwork(nodeId=1, setupPinCode=20202021, filterType=ChipDeviceCtrl.DiscoveryFilterType.LONG_DISCRIMINATOR, filter=3840)
```

### Interacting with teh valve app
### Interacting with the valve app

To create a drinks machine controller:

Expand Down
62 changes: 62 additions & 0 deletions examples/valve/controller/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import random

from enum import StrEnum
from chip import ChipDeviceCtrl
from chip.clusters.Types import NullValue
import chip.clusters as Clusters


MYSTERY_DRINK = "mystery mirrorball"


class Bottle(StrEnum):
kBourbon = "bourbon"
kGin = "gin"
Expand All @@ -21,6 +26,12 @@ def time(self):
# One oz == approx 12s.
return self.oz * 12

def __eq__(self, other):
return self.oz == other.oz

def __lt__(self, other):
return self.oz < other.oz


class DrinkMachine:
def __init__(self, devCtrl: ChipDeviceCtrl, node_id: int):
Expand All @@ -39,6 +50,11 @@ def __init__(self, devCtrl: ChipDeviceCtrl, node_id: int):
self.add_recipe("old fashioned", {Bottle.kBourbon: Oz(2), Bottle.kSimpleSyrup: Oz(0.125)})
self.add_recipe("shot of bourbon", {Bottle.kBourbon: Oz(1.5)})
self.add_recipe("shot of gin", {Bottle.kGin: Oz(1.5)})
self.add_recipe("gin sour", {Bottle.kGin: Oz(2), Bottle.kSourMix: Oz(1), Bottle.kSimpleSyrup: Oz(0.5)})
self.add_recipe("manhattan", {Bottle.kBourbon: Oz(2), Bottle.kVermouth: Oz(1)})
self.add_recipe("campari sour", {Bottle.kGin: Oz(1), Bottle.kCampari: Oz(1),
Bottle.kSourMix: Oz(0.75), Bottle.kSimpleSyrup: Oz(0.5)})
self.add_recipe(MYSTERY_DRINK, {})

def set_bottle_names(self, bottles: dict[Bottle, int]) -> bool:
''' Bottle is a dict of bottle name to endpoint and should contain all 6 endpoints at once'''
Expand All @@ -56,7 +72,50 @@ def add_recipe(self, name: str, ingredients: dict[Bottle, Oz]):
# TODO: should store somewhere permanent - simplest is to write out to file. In the meanwhile, we have a few pre-populated
self.recipes[name] = ingredients

async def _dispense_mystery(self):
num_ingredients = random.randint(1, 3)
bottles = set()
choice = random.choice(list(Bottle))
bottles.add(choice)
while len(bottles) < num_ingredients:
# If this matches something in the bottle list already, it won't be added
choice = random.choice(list(Bottle))
bottles.add(choice)

# we're going to aim for a 2.5 Oz shot with min 0.5 shot sizes
goal = 2.5
print(f"Creating your mystery beverage:")
ingredients = {}
total = 0
for bottle in bottles:
remaining = num_ingredients - len(ingredients.keys())
if remaining == 1:
oz = goal-total
else:
max_oz = goal - total - 0.5*(remaining-1)
# multiply by 2 because randrange likes ints
oz = random.randrange(1, max_oz*2, 1)/2
total += oz
ingredients[bottle] = Oz(oz)
print(f"\t{oz} Oz of {bottle}")

largest = max(ingredients, key=ingredients.get)
mystery_prefix = ['', 'giant', 'potent', 'disco-tastic', 'shiny', 'Dr.']
mystery_suffix = ['blaster', 'concoction', 'blend', 'cocktail']
mystery_extra_suffix = ['', 'jr', 'of wonder', 'esq']
name = []
name.append(random.choice(mystery_prefix))
name.append(largest)
name.append(random.choice(mystery_suffix))
name.append(random.choice(mystery_extra_suffix))
print(f'The {" ".join(name).strip()}')
print('Please enjoy responsibly.')
await self._dispense_ingredients(ingredients)

async def dispense(self, recipe: str):
if recipe == MYSTERY_DRINK:
return await self._dispense_mystery()

# TODO: be a bit nicer on the comparison here. Strings as keys aren't great, but I want the flexibility to add non-standard recipes
if recipe not in self.recipes.keys():
print(f"Unable to find the specified recipe. Available Recipes: {self.recipes.keys()}")
Expand All @@ -69,6 +128,9 @@ async def dispense(self, recipe: str):
return

ingredients = self.recipes[recipe]
self._dispense_ingredients(ingredients)

async def _dispense_ingredients(self, ingredients: dict[Bottle, Oz]):
for bottle, amount in ingredients.items():
ep = self.bottles[bottle]
time = amount.time()
Expand Down

0 comments on commit 1a1d5c6

Please sign in to comment.