import java.math.BigDecimal;
import java.math.RoundingMode;
+import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
// Static objects, should be recreated on each request
private @NonNullByDefault({}) ComputerSystem computerSystem;
private @NonNullByDefault({}) OperatingSystem operatingSystem;
- private @NonNullByDefault({}) NetworkIF[] networks;
- private @NonNullByDefault({}) Display[] displays;
- private @NonNullByDefault({}) OSFileStore[] fileStores;
- private @NonNullByDefault({}) PowerSource[] powerSources;
- private @NonNullByDefault({}) HWDiskStore[] drives;
+ private @NonNullByDefault({}) List<NetworkIF> networks;
+ private @NonNullByDefault({}) List<Display> displays;
+ private @NonNullByDefault({}) List<OSFileStore> fileStores;
+ private @NonNullByDefault({}) List<PowerSource> powerSources;
+ private @NonNullByDefault({}) List<HWDiskStore> drives;
public static final int PRECISION_AFTER_DECIMAL_SIGN = 1;
drives = hal.getDiskStores();
}
- private Object getDevice(Object @Nullable [] devices, int index) throws DeviceNotFoundException {
- if ((devices == null) || (devices.length <= index)) {
+ private <T> T getDevice(List<@Nullable T> devices, int index) throws DeviceNotFoundException {
+ if (devices.size() <= index) {
+ throw new DeviceNotFoundException("Device with index: " + index + " can not be found!");
+ }
+ return (T) devices.get(index);
+ }
+
+ private <T> T getDevice(T @Nullable [] devices, int index) throws DeviceNotFoundException {
+ if (devices == null || devices.length <= index) {
throw new DeviceNotFoundException("Device with index: " + index + " can not be found!");
}
return devices[index];
@Override
public DecimalType getStorageTotal(int index) throws DeviceNotFoundException {
- OSFileStore fileStore = (OSFileStore) getDevice(fileStores, index);
- fileStore.updateAtrributes();
+ OSFileStore fileStore = getDevice(fileStores, index);
+ fileStore.updateAttributes();
long totalSpace = fileStore.getTotalSpace();
totalSpace = getSizeInMB(totalSpace);
return new DecimalType(totalSpace);
@Override
public DecimalType getStorageAvailable(int index) throws DeviceNotFoundException {
- OSFileStore fileStore = (OSFileStore) getDevice(fileStores, index);
- fileStore.updateAtrributes();
+ OSFileStore fileStore = getDevice(fileStores, index);
+ fileStore.updateAttributes();
long freeSpace = fileStore.getUsableSpace();
freeSpace = getSizeInMB(freeSpace);
return new DecimalType(freeSpace);
@Override
public DecimalType getStorageUsed(int index) throws DeviceNotFoundException {
- OSFileStore fileStore = (OSFileStore) getDevice(fileStores, index);
- fileStore.updateAtrributes();
+ OSFileStore fileStore = getDevice(fileStores, index);
+ fileStore.updateAttributes();
long totalSpace = fileStore.getTotalSpace();
long freeSpace = fileStore.getUsableSpace();
long usedSpace = totalSpace - freeSpace;
@Override
public @Nullable DecimalType getStorageAvailablePercent(int deviceIndex) throws DeviceNotFoundException {
- OSFileStore fileStore = (OSFileStore) getDevice(fileStores, deviceIndex);
- fileStore.updateAtrributes();
+ OSFileStore fileStore = getDevice(fileStores, deviceIndex);
+ fileStore.updateAttributes();
long totalSpace = fileStore.getTotalSpace();
long freeSpace = fileStore.getUsableSpace();
if (totalSpace > 0) {
@Override
public @Nullable DecimalType getStorageUsedPercent(int deviceIndex) throws DeviceNotFoundException {
- OSFileStore fileStore = (OSFileStore) getDevice(fileStores, deviceIndex);
- fileStore.updateAtrributes();
+ OSFileStore fileStore = getDevice(fileStores, deviceIndex);
+ fileStore.updateAttributes();
long totalSpace = fileStore.getTotalSpace();
long freeSpace = fileStore.getUsableSpace();
long usedSpace = totalSpace - freeSpace;
@Override
public StringType getStorageName(int index) throws DeviceNotFoundException {
- OSFileStore fileStore = (OSFileStore) getDevice(fileStores, index);
+ OSFileStore fileStore = getDevice(fileStores, index);
String name = fileStore.getName();
return new StringType(name);
}
@Override
public StringType getStorageType(int deviceIndex) throws DeviceNotFoundException {
- OSFileStore fileStore = (OSFileStore) getDevice(fileStores, deviceIndex);
+ OSFileStore fileStore = getDevice(fileStores, deviceIndex);
String type = fileStore.getType();
return new StringType(type);
}
@Override
public StringType getStorageDescription(int index) throws DeviceNotFoundException {
- OSFileStore fileStore = (OSFileStore) getDevice(fileStores, index);
+ OSFileStore fileStore = getDevice(fileStores, index);
String description = fileStore.getDescription();
return new StringType(description);
}
@Override
public StringType getNetworkIp(int index) throws DeviceNotFoundException {
- NetworkIF netInterface = (NetworkIF) getDevice(networks, index);
+ NetworkIF netInterface = getDevice(networks, index);
netInterface.updateAttributes();
String[] ipAddresses = netInterface.getIPv4addr();
- String ipv4 = (String) getDevice(ipAddresses, 0);
+ String ipv4 = getDevice(ipAddresses, 0);
return new StringType(ipv4);
}
@Override
public StringType getNetworkName(int index) throws DeviceNotFoundException {
- NetworkIF netInterface = (NetworkIF) getDevice(networks, index);
+ NetworkIF netInterface = getDevice(networks, index);
String name = netInterface.getName();
return new StringType(name);
}
@Override
public StringType getNetworkDisplayName(int index) throws DeviceNotFoundException {
- NetworkIF netInterface = (NetworkIF) getDevice(networks, index);
+ NetworkIF netInterface = getDevice(networks, index);
String adapterName = netInterface.getDisplayName();
return new StringType(adapterName);
}
@Override
public StringType getDisplayInformation(int index) throws DeviceNotFoundException {
- Display display = (Display) getDevice(displays, index);
+ Display display = getDevice(displays, index);
byte[] edid = display.getEdid();
String manufacturer = EdidUtil.getManufacturerID(edid);
@Override
public @Nullable DecimalType getSensorsFanSpeed(int index) throws DeviceNotFoundException {
int[] fanSpeeds = sensors.getFanSpeeds();
- int speed = (int) getDevice(ArrayUtils.toObject(fanSpeeds), index);
+ int speed = getDevice(ArrayUtils.toObject(fanSpeeds), index);
return speed > 0 ? new DecimalType(speed) : null;
}
@Override
public @Nullable DecimalType getBatteryRemainingTime(int index) throws DeviceNotFoundException {
- PowerSource powerSource = (PowerSource) getDevice(powerSources, index);
+ PowerSource powerSource = getDevice(powerSources, index);
powerSource.updateAttributes();
double remainingTimeInSeconds = powerSource.getTimeRemainingEstimated();
// The getTimeRemaining() method returns (-1.0) if is calculating or (-2.0) if the time is unlimited.
@Override
public DecimalType getBatteryRemainingCapacity(int index) throws DeviceNotFoundException {
- PowerSource powerSource = (PowerSource) getDevice(powerSources, index);
+ PowerSource powerSource = getDevice(powerSources, index);
powerSource.updateAttributes();
double remainingCapacity = powerSource.getRemainingCapacityPercent();
BigDecimal remainingCapacityPercents = getPercentsValue(remainingCapacity);
@Override
public StringType getBatteryName(int index) throws DeviceNotFoundException {
- PowerSource powerSource = (PowerSource) getDevice(powerSources, index);
+ PowerSource powerSource = getDevice(powerSources, index);
String name = powerSource.getName();
return new StringType(name);
}
@Override
public StringType getDriveName(int deviceIndex) throws DeviceNotFoundException {
- HWDiskStore drive = (HWDiskStore) getDevice(drives, deviceIndex);
+ HWDiskStore drive = getDevice(drives, deviceIndex);
String name = drive.getName();
return new StringType(name);
}
@Override
public StringType getDriveModel(int deviceIndex) throws DeviceNotFoundException {
- HWDiskStore drive = (HWDiskStore) getDevice(drives, deviceIndex);
+ HWDiskStore drive = getDevice(drives, deviceIndex);
String model = drive.getModel();
return new StringType(model);
}
@Override
public StringType getDriveSerialNumber(int deviceIndex) throws DeviceNotFoundException {
- HWDiskStore drive = (HWDiskStore) getDevice(drives, deviceIndex);
+ HWDiskStore drive = getDevice(drives, deviceIndex);
String serialNumber = drive.getSerial();
return new StringType(serialNumber);
}
@Override
public StringType getNetworkMac(int networkIndex) throws DeviceNotFoundException {
- NetworkIF network = (NetworkIF) getDevice(networks, networkIndex);
+ NetworkIF network = getDevice(networks, networkIndex);
String mac = network.getMacaddr();
return new StringType(mac);
}
@Override
public DecimalType getNetworkPacketsReceived(int networkIndex) throws DeviceNotFoundException {
- NetworkIF network = (NetworkIF) getDevice(networks, networkIndex);
+ NetworkIF network = getDevice(networks, networkIndex);
network.updateAttributes();
long packRecv = network.getPacketsRecv();
return new DecimalType(packRecv);
@Override
public DecimalType getNetworkPacketsSent(int networkIndex) throws DeviceNotFoundException {
- NetworkIF network = (NetworkIF) getDevice(networks, networkIndex);
+ NetworkIF network = getDevice(networks, networkIndex);
network.updateAttributes();
long packSent = network.getPacketsSent();
return new DecimalType(packSent);
@Override
public DecimalType getNetworkDataSent(int networkIndex) throws DeviceNotFoundException {
- NetworkIF network = (NetworkIF) getDevice(networks, networkIndex);
+ NetworkIF network = getDevice(networks, networkIndex);
network.updateAttributes();
long bytesSent = network.getBytesSent();
return new DecimalType(getSizeInMB(bytesSent));
@Override
public DecimalType getNetworkDataReceived(int networkIndex) throws DeviceNotFoundException {
- NetworkIF network = (NetworkIF) getDevice(networks, networkIndex);
+ NetworkIF network = getDevice(networks, networkIndex);
network.updateAttributes();
long bytesRecv = network.getBytesRecv();
return new DecimalType(getSizeInMB(bytesRecv));