Smart Home Automation Tool Java, API
👤 Sharing: AI
```java
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class SmartHomeAutomation {
private Map<String, Device> devices;
private static final String API_KEY = "your_super_secret_api_key"; // Replace with your actual API key
public SmartHomeAutomation() {
this.devices = new HashMap<>();
}
// API endpoint to add a device
public String addDevice(String apiKey, String deviceName, String deviceType) {
if (!isValidApiKey(apiKey)) {
return "Error: Invalid API key.";
}
if (devices.containsKey(deviceName)) {
return "Error: Device with name '" + deviceName + "' already exists.";
}
Device newDevice = new Device(deviceName, deviceType);
devices.put(deviceName, newDevice);
return "Device '" + deviceName + "' added successfully.";
}
// API endpoint to turn on/off a device
public String controlDevice(String apiKey, String deviceName, String action) {
if (!isValidApiKey(apiKey)) {
return "Error: Invalid API key.";
}
if (!devices.containsKey(deviceName)) {
return "Error: Device '" + deviceName + "' not found.";
}
Device device = devices.get(deviceName);
if (action.equalsIgnoreCase("on")) {
device.turnOn();
return "Device '" + deviceName + "' turned on.";
} else if (action.equalsIgnoreCase("off")) {
device.turnOff();
return "Device '" + deviceName + "' turned off.";
} else {
return "Error: Invalid action. Use 'on' or 'off'.";
}
}
// API endpoint to get device status
public String getDeviceStatus(String apiKey, String deviceName) {
if (!isValidApiKey(apiKey)) {
return "Error: Invalid API key.";
}
if (!devices.containsKey(deviceName)) {
return "Error: Device '" + deviceName + "' not found.";
}
Device device = devices.get(deviceName);
return "Device '" + deviceName + "' is currently " + (device.isOn() ? "on" : "off") + ".";
}
// Helper function to validate API key
private boolean isValidApiKey(String apiKey) {
return API_KEY.equals(apiKey);
}
public static void main(String[] args) {
SmartHomeAutomation smartHome = new SmartHomeAutomation();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\nSmart Home Automation Menu:");
System.out.println("1. Add Device");
System.out.println("2. Control Device (On/Off)");
System.out.println("3. Get Device Status");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
System.out.print("Enter device name: ");
String deviceName = scanner.nextLine();
System.out.print("Enter device type: ");
String deviceType = scanner.nextLine();
System.out.println(smartHome.addDevice(API_KEY, deviceName, deviceType));
break;
case 2:
System.out.print("Enter device name: ");
String controlDeviceName = scanner.nextLine();
System.out.print("Enter action (on/off): ");
String action = scanner.nextLine();
System.out.println(smartHome.controlDevice(API_KEY, controlDeviceName, action));
break;
case 3:
System.out.print("Enter device name: ");
String statusDeviceName = scanner.nextLine();
System.out.println(smartHome.getDeviceStatus(API_KEY, statusDeviceName));
break;
case 4:
System.out.println("Exiting Smart Home Automation.");
scanner.close();
return;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
// Inner class representing a smart home device
static class Device {
private String name;
private String type;
private boolean isOn;
public Device(String name, String type) {
this.name = name;
this.type = type;
this.isOn = false;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public boolean isOn() {
return isOn;
}
public void turnOn() {
this.isOn = true;
}
public void turnOff() {
this.isOn = false;
}
}
}
```
👁️ Viewed: 19
Comments