You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
157 lines
4.9 KiB
157 lines
4.9 KiB
/**
|
|
* 动态设备管理系统
|
|
* 可以根据设备实现的接口自动识别和管理设备功能
|
|
*/
|
|
import java.util.*;
|
|
|
|
public class DeviceManager {
|
|
private Map<String, SmartDevice> devices;
|
|
|
|
/**
|
|
* 构造函数
|
|
*/
|
|
public DeviceManager() {
|
|
this.devices = new HashMap<>();
|
|
}
|
|
|
|
/**
|
|
* 添加设备
|
|
* @param device 智能设备
|
|
*/
|
|
public void addDevice(SmartDevice device) {
|
|
if (device != null) {
|
|
devices.put(device.getDeviceId(), device);
|
|
System.out.println("成功添加设备: " + device.getDeviceName() + " (ID: " + device.getDeviceId() + ")");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 移除设备
|
|
* @param deviceId 设备ID
|
|
*/
|
|
public void removeDevice(String deviceId) {
|
|
SmartDevice device = devices.remove(deviceId);
|
|
if (device != null) {
|
|
System.out.println("成功移除设备: " + device.getDeviceName() + " (ID: " + deviceId + ")");
|
|
} else {
|
|
System.out.println("未找到设备ID: " + deviceId);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取设备
|
|
* @param deviceId 设备ID
|
|
* @return 智能设备
|
|
*/
|
|
public SmartDevice getDevice(String deviceId) {
|
|
return devices.get(deviceId);
|
|
}
|
|
|
|
/**
|
|
* 获取所有设备
|
|
* @return 所有设备的列表
|
|
*/
|
|
public List<SmartDevice> getAllDevices() {
|
|
return new ArrayList<>(devices.values());
|
|
}
|
|
|
|
/**
|
|
* 根据接口类型获取设备
|
|
* @param interfaceClass 接口类
|
|
* @return 实现了指定接口的设备列表
|
|
*/
|
|
public List<SmartDevice> getDevicesByInterface(Class<?> interfaceClass) {
|
|
List<SmartDevice> result = new ArrayList<>();
|
|
for (SmartDevice device : devices.values()) {
|
|
if (interfaceClass.isInstance(device)) {
|
|
result.add(device);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* 开启设备电源
|
|
* @param deviceId 设备ID
|
|
*/
|
|
public void turnOnDevice(String deviceId) {
|
|
SmartDevice device = devices.get(deviceId);
|
|
if (device instanceof PowerControl) {
|
|
((PowerControl) device).turnOn();
|
|
} else {
|
|
System.out.println(device.getDeviceName() + " 不支持电源控制功能");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 关闭设备电源
|
|
* @param deviceId 设备ID
|
|
*/
|
|
public void turnOffDevice(String deviceId) {
|
|
SmartDevice device = devices.get(deviceId);
|
|
if (device instanceof PowerControl) {
|
|
((PowerControl) device).turnOff();
|
|
} else {
|
|
System.out.println(device.getDeviceName() + " 不支持电源控制功能");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 设置设备温度
|
|
* @param deviceId 设备ID
|
|
* @param temperature 温度
|
|
*/
|
|
public void setDeviceTemperature(String deviceId, double temperature) {
|
|
SmartDevice device = devices.get(deviceId);
|
|
if (device instanceof TemperatureControl) {
|
|
((TemperatureControl) device).setTemperature(temperature);
|
|
} else {
|
|
System.out.println(device.getDeviceName() + " 不支持温度控制功能");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 设置灯光亮度
|
|
* @param deviceId 设备ID
|
|
* @param brightness 亮度
|
|
*/
|
|
public void setDeviceBrightness(String deviceId, int brightness) {
|
|
SmartDevice device = devices.get(deviceId);
|
|
if (device instanceof LightControl) {
|
|
((LightControl) device).setBrightness(brightness);
|
|
} else {
|
|
System.out.println(device.getDeviceName() + " 不支持灯光控制功能");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 设置音频音量
|
|
* @param deviceId 设备ID
|
|
* @param volume 音量
|
|
*/
|
|
public void setDeviceVolume(String deviceId, int volume) {
|
|
SmartDevice device = devices.get(deviceId);
|
|
if (device instanceof AudioControl) {
|
|
((AudioControl) device).setVolume(volume);
|
|
} else {
|
|
System.out.println(device.getDeviceName() + " 不支持音频控制功能");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 显示所有设备信息
|
|
*/
|
|
public void displayAllDevices() {
|
|
System.out.println("\n===== 所有设备信息 =====");
|
|
for (SmartDevice device : devices.values()) {
|
|
System.out.println(device.getStatusDescription());
|
|
// 显示设备支持的接口
|
|
System.out.println(" 支持的功能接口:");
|
|
if (device instanceof PowerControl) System.out.println(" - 电源控制 (PowerControl)");
|
|
if (device instanceof TemperatureControl) System.out.println(" - 温度控制 (TemperatureControl)");
|
|
if (device instanceof LightControl) System.out.println(" - 灯光控制 (LightControl)");
|
|
if (device instanceof AudioControl) System.out.println(" - 音频控制 (AudioControl)");
|
|
System.out.println();
|
|
}
|
|
}
|
|
} |