Design a Smart Home Automation System

Medium
Real World SystemsObserver PatternCommand PatternDevice ManagementAutomation RulesIoT

Problem Statement

Design a smart home automation system where users control IoT devices (lights, thermostats, locks, cameras), set automation rules (if X then Y), and manage scenes (preset device configurations).

Requirements

Functional Requirements

  • Device types: LIGHT (on/off, brightness, color), THERMOSTAT (temperature, mode), DOOR_LOCK (locked/unlocked), CAMERA (recording on/off), FAN (speed), OUTLET (on/off)
  • User can control any device: turn on/off, set attributes
  • Scenes: named preset configurations (e.g., 'Movie Mode': dim lights, lower thermostat, lock doors)
  • Automation rules: condition → action (e.g., 'If time = 10pm, turn off all lights')
  • Rule conditions: time-based, sensor-based (motion detected, door opened), or device-state-based
  • Room grouping: devices belong to rooms; control all devices in a room at once
  • Device status history log
  • Remote control via mobile (simulated as API calls)

Non-Functional Requirements

  • Thread-safe device state updates
  • Observer pattern for rule evaluation on state changes
  • Command pattern for device actions (supports undo)

Key Entities

  • Device (abstract): id, name, room, status, stateHistory; subclasses per device type
  • Room: id, name, devices
  • Scene: id, name, deviceStates (Map<DeviceId, TargetState>)
  • AutomationRule: id, condition (Predicate), action (Command), enabled
  • Command (interface): execute(), undo() — DeviceOnCommand, DeviceOffCommand, SetThermostatCommand etc.
  • HomeController: Singleton; manages devices, rooms, scenes, rules

Constraints

  • Automation rules evaluated asynchronously on device state change
  • Scene activation applies all device states atomically
  • Device command history stored for undo support (last 10 commands per device)
  • Rules can be enabled/disabled without deletion

!Constraints

- Scene activation is atomic — all device states applied together - Automation rules evaluated asynchronously on trigger - Last 10 commands per device stored for undo support - Thread-safe device state updates - Rules disabled at creation time by default (must be explicitly enabled)
Output

Run or submit your code to see output here.