In Exercise: Guard the Good Morning Button, the button ran each day’s routine once and morning-log.json counted every run. Each section fixed one thing that goes wrong in the Lindqvists’ morning. On a real morning, those problems overlap. The blinds go offline while Saga presses Snooze, and yesterday’s report turns up late.
In this project, you’ll build one routine that copes with all of it in three graded steps. Each step joins pieces from two or three sections, so the work is in making them fit together. Start with what the finished routine has to do.
Read the Brief
One tap on “Good morning” starts the lights, the kettle, the blinds, and the heating. The routine has to handle these mornings:
- An ordinary morning: Every device starts at once, so the morning takes about as long as the slowest device.
- Two devices fail: The blinds go offline and the heating times out. The others still finish, and the report names every failed device and why.
- The kettle is slow: It gets five seconds, and only the kettle gives up when they run out.
- Twelve lights, one small hub: The hub passes on a few commands at a time, so the lights go on in batches.
- Someone presses Snooze: Everything still running stops at its next chance, and the report calmly says what was snoozed.
- Both of them tap at once: Erik and Saga tap within a second of each other, and the routine runs once.
- Snooze, then a second try: After a Snooze, the next tap that day starts the routine again.
- Yesterday’s reply turns up late: The panel keeps today’s report, but the log still records yesterday’s run.
Meet the Stand-in Devices
The course’s stand-in devices use async functions that wait a fixed time, then give back a status such as "Heating on" or reject with an error. The lights are fastest, then the blinds, then the heating, and the kettle is slowest. The kettle fills before it boils, and, as in Make Your Own Functions Cancellable, its fill can’t be stopped halfway.
Each device is an object with a name, such as "Blinds", and an async start() method. Its own error rarely names it. The heating device in the tests, for example, fails with Connection timed out, so naming the device is your code’s job.
In the exercises, delays are in milliseconds, and the kettle’s deadline and the hub’s limit reach your functions as arguments, so use those values, never a number typed by hand.
Build It in Three Steps
Each step is one exercise, so work through them in order. The tests only check what your code does, so you can structure the code inside each function your own way.