Functional Scope (In-Scope)
- Executable & Reversible Commands: Support executing and undoing operations via slot-to-command registries.
- Composite Scene Macros: Trigger grouped device operations simultaneously (e.g., a "Good Night" scene).
- Observer Monitoring Status: Keep telemetry listeners updated on all device actions.
- Thread Safe Operations State: Synchronize concurrent requests across remote keys, schedules, and observers.
Explicit Boundaries (Out-of-Scope)
- No electrical driver code signals: Bypasses raw IoT Zigbee or Z-Wave hardware register maps.
- No dynamic network connectivity check: Excludes handling router socket disconnections or wireless drops.
Clean reference designs demonstrating Command patterns in Java and Python:
// ─── JAVA BLUEPRINT ──────────────────────────────────────────────────────────
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.*;
interface DeviceObserver {
void onDeviceStateChanged(String deviceName, String newState);
}
interface SmartDevice {
String getName();
String getStatus();
}
class SmartLight implements SmartDevice {
private final String name;
private boolean isOn = false;
private int brightness = 100;
private final ReentrantLock lock = new ReentrantLock();
private final List<DeviceObserver> observers = new CopyOnWriteArrayList<>();
public SmartLight(String name) { this.name = name; }
public void addObserver(DeviceObserver obs) { observers.add(obs); }
public void turnOn() {
lock.lock();
try {
this.isOn = true;
notifyObservers();
} finally {
lock.unlock();
}
}
public void turnOff() {
lock.lock();
try {
this.isOn = false;
notifyObservers();
} finally {
lock.unlock();
}
}
public void setBrightness(int level) {
lock.lock();
try {
this.brightness = Math.max(0, Math.min(100, level));
notifyObservers();
} finally {
lock.unlock();
}
}
public boolean isOn() { return isOn; }
public int getBrightness() { return brightness; }
private void notifyObservers() {
String status = getStatus();
for (DeviceObserver obs : observers) obs.onDeviceStateChanged(name, status);
}
@Override public String getName() { return name; }
@Override public String getStatus() { return "Light is " + (isOn ? "ON" : "OFF") + ", Brightness: " + brightness + "%"; }
}
class SmartLock implements SmartDevice {
private final String name;
private boolean isLocked = true;
private final ReentrantLock lock = new ReentrantLock();
private final List<DeviceObserver> observers = new CopyOnWriteArrayList<>();
public SmartLock(String name) { this.name = name; }
public void addObserver(DeviceObserver obs) { observers.add(obs); }
public void lock() {
lock.lock();
try {
this.isLocked = true;
notifyObservers();
} finally {
lock.unlock();
}
}
public void unlock() {
lock.lock();
try {
this.isLocked = false;
notifyObservers();
} finally {
lock.unlock();
}
}
public boolean isLocked() { return isLocked; }
private void notifyObservers() {
String status = getStatus();
for (DeviceObserver obs : observers) obs.onDeviceStateChanged(name, status);
}
@Override public String getName() { return name; }
@Override public String getStatus() { return "Lock is " + (isLocked ? "LOCKED" : "UNLOCKED"); }
}
interface Command {
void execute();
void undo();
}
class LightOnCommand implements Command {
private final SmartLight light;
private int prevBrightness;
public LightOnCommand(SmartLight light) { this.light = light; }
@Override
public void execute() {
prevBrightness = light.getBrightness();
light.turnOn();
}
@Override
public void undo() {
light.turnOff();
light.setBrightness(prevBrightness);
}
}
class LightOffCommand implements Command {
private final SmartLight light;
private boolean wasOn;
public LightOffCommand(SmartLight light) { this.light = light; }
@Override
public void execute() {
wasOn = light.isOn();
light.turnOff();
}
@Override
public void undo() {
if (wasOn) light.turnOn();
}
}
class LockCommand implements Command {
private final SmartLock smartLock;
private boolean wasLocked;
public LockCommand(SmartLock smartLock) { this.smartLock = smartLock; }
@Override
public void execute() {
wasLocked = smartLock.isLocked();
smartLock.lock();
}
@Override
public void undo() {
if (!wasLocked) smartLock.unlock();
}
}
class UnlockCommand implements Command {
private final SmartLock smartLock;
private boolean wasLocked;
public UnlockCommand(SmartLock smartLock) { this.smartLock = smartLock; }
@Override
public void execute() {
wasLocked = smartLock.isLocked();
smartLock.unlock();
}
@Override
public void undo() {
if (wasLocked) smartLock.lock();
}
}
class MacroCommand implements Command {
private final List<Command> commands;
public MacroCommand(List<Command> commands) {
this.commands = new ArrayList<>(commands);
}
@Override
public void execute() {
for (Command cmd : commands) cmd.execute();
}
@Override
public void undo() {
for (int i = commands.size() - 1; i >= 0; i--) {
commands.get(i).undo();
}
}
}
class RemoteControl {
private final Deque<Command> history = new ArrayDeque<>();
private final int maxHistorySize = 10;
private final ReentrantLock historyLock = new ReentrantLock();
public void executeCommand(Command cmd) {
cmd.execute();
historyLock.lock();
try {
if (history.size() >= maxHistorySize) {
history.pollFirst();
}
history.addLast(cmd);
} finally {
historyLock.unlock();
}
}
public void undoLastCommand() {
historyLock.lock();
try {
if (!history.isEmpty()) {
Command cmd = history.removeLast();
cmd.undo();
}
} finally {
historyLock.unlock();
}
}
}
public class Main {
public static void main(String[] args) {
System.out.println("=== Advanced Smart Home System ===");
DeviceObserver logger = (dev, state) -> System.out.println("[Telemetry] " + dev + " -> " + state);
SmartLight livingRoomLight = new SmartLight("Living Room Light");
SmartLock frontDoorLock = new SmartLock("Front Door Lock");
livingRoomLight.addObserver(logger);
frontDoorLock.addObserver(logger);
RemoteControl remote = new RemoteControl();
System.out.println("\n--- Execute Single Command ---");
remote.executeCommand(new LightOnCommand(livingRoomLight));
System.out.println("\n--- Execute Scene Macro ---");
List<Command> goodNightOps = Arrays.asList(
new LightOffCommand(livingRoomLight),
new LockCommand(frontDoorLock)
);
remote.executeCommand(new MacroCommand(goodNightOps));
System.out.println("\n--- Undo Last Scene Macro ---");
remote.undoLastCommand();
}
}