Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[systeminfo] QuantityTypes and state descriptions cleanup #13804

Merged
merged 7 commits into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 67 additions & 67 deletions bundles/org.openhab.binding.systeminfo/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ private State getInfoForChannel(ChannelUID channelUID) {
state = systeminfo.getBatteryName(deviceIndex);
break;
case CHANNEL_BATTERY_REMAINING_CAPACITY:
state = systeminfo.getBatteryRemainingCapacity(deviceIndex);
state = new QuantityType<>(systeminfo.getBatteryRemainingCapacity(deviceIndex), Units.PERCENT);
break;
case CHANNEL_BATTERY_REMAINING_TIME:
state = systeminfo.getBatteryRemainingTime(deviceIndex);
Expand Down Expand Up @@ -511,10 +511,13 @@ private State getInfoForChannel(ChannelUID channelUID) {
state = systeminfo.getMemoryTotal();
break;
case CHANNEL_MEMORY_AVAILABLE_PERCENT:
state = systeminfo.getMemoryAvailablePercent();
PercentType memoryAvailablePercent = systeminfo.getMemoryAvailablePercent();
state = (memoryAvailablePercent != null) ? new QuantityType<>(memoryAvailablePercent, Units.PERCENT)
: null;
break;
case CHANNEL_MEMORY_USED_PERCENT:
state = systeminfo.getMemoryUsedPercent();
PercentType memoryUsedPercent = systeminfo.getMemoryUsedPercent();
state = (memoryUsedPercent != null) ? new QuantityType<>(memoryUsedPercent, Units.PERCENT) : null;
break;
case CHANNEL_SWAP_AVAILABLE:
state = systeminfo.getSwapAvailable();
Expand All @@ -526,10 +529,13 @@ private State getInfoForChannel(ChannelUID channelUID) {
state = systeminfo.getSwapTotal();
break;
case CHANNEL_SWAP_AVAILABLE_PERCENT:
state = systeminfo.getSwapAvailablePercent();
PercentType swapAvailablePercent = systeminfo.getSwapAvailablePercent();
state = (swapAvailablePercent != null) ? new QuantityType<>(swapAvailablePercent, Units.PERCENT)
: null;
break;
case CHANNEL_SWAP_USED_PERCENT:
state = systeminfo.getSwapUsedPercent();
PercentType swapUsedPercent = systeminfo.getSwapUsedPercent();
state = (swapUsedPercent != null) ? new QuantityType<>(swapUsedPercent, Units.PERCENT) : null;
break;
case CHANNEL_DRIVE_MODEL:
state = systeminfo.getDriveModel(deviceIndex);
Expand Down Expand Up @@ -559,10 +565,14 @@ private State getInfoForChannel(ChannelUID channelUID) {
state = systeminfo.getStorageType(deviceIndex);
break;
case CHANNEL_STORAGE_AVAILABLE_PERCENT:
state = systeminfo.getStorageAvailablePercent(deviceIndex);
PercentType storageAvailablePercent = systeminfo.getStorageAvailablePercent(deviceIndex);
state = (storageAvailablePercent != null)
? new QuantityType<>(storageAvailablePercent, Units.PERCENT)
: null;
break;
case CHANNEL_STORAGE_USED_PERCENT:
state = systeminfo.getStorageUsedPercent(deviceIndex);
PercentType storageUsedPercent = systeminfo.getStorageUsedPercent(deviceIndex);
state = (storageUsedPercent != null) ? new QuantityType<>(storageUsedPercent, Units.PERCENT) : null;
break;
case CHANNEL_NETWORK_IP:
state = systeminfo.getNetworkIp(deviceIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@
import java.util.List;
import java.util.Map;

import javax.measure.quantity.ElectricPotential;
import javax.measure.quantity.Temperature;
import javax.measure.quantity.Time;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.library.dimension.DataAmount;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.PercentType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.library.unit.SIUnits;
import org.openhab.core.library.unit.Units;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -53,6 +61,7 @@
* CentralProcessor#getSystemSerialNumber()
* @author Wouter Born - Update to OSHI 4.0.0 and add null annotations
* @author Mark Herwege - Add dynamic creation of extra channels
* @author Mark Herwege - Use units of measure
*
* @see <a href="https://github.com/oshi/oshi">OSHI GitHub repository</a>
*/
Expand Down Expand Up @@ -189,74 +198,74 @@ public DecimalType getCpuPhysicalCores() {
}

@Override
public DecimalType getMemoryTotal() {
public QuantityType<DataAmount> getMemoryTotal() {
long totalMemory = memory.getTotal();
totalMemory = getSizeInMB(totalMemory);
return new DecimalType(totalMemory);
return new QuantityType<>(totalMemory, Units.MEBIBYTE);
}

@Override
public DecimalType getMemoryAvailable() {
public QuantityType<DataAmount> getMemoryAvailable() {
long availableMemory = memory.getAvailable();
availableMemory = getSizeInMB(availableMemory);
return new DecimalType(availableMemory);
return new QuantityType<>(availableMemory, Units.MEBIBYTE);
}

@Override
public DecimalType getMemoryUsed() {
public QuantityType<DataAmount> getMemoryUsed() {
long totalMemory = memory.getTotal();
long availableMemory = memory.getAvailable();
long usedMemory = totalMemory - availableMemory;
usedMemory = getSizeInMB(usedMemory);
return new DecimalType(usedMemory);
return new QuantityType<>(usedMemory, Units.MEBIBYTE);
}

@Override
public DecimalType getStorageTotal(int index) throws DeviceNotFoundException {
public QuantityType<DataAmount> getStorageTotal(int index) throws DeviceNotFoundException {
OSFileStore fileStore = getDevice(fileStores, index);
fileStore.updateAttributes();
long totalSpace = fileStore.getTotalSpace();
totalSpace = getSizeInMB(totalSpace);
return new DecimalType(totalSpace);
return new QuantityType<>(totalSpace, Units.MEBIBYTE);
}

@Override
public DecimalType getStorageAvailable(int index) throws DeviceNotFoundException {
public QuantityType<DataAmount> getStorageAvailable(int index) throws DeviceNotFoundException {
OSFileStore fileStore = getDevice(fileStores, index);
fileStore.updateAttributes();
long freeSpace = fileStore.getUsableSpace();
freeSpace = getSizeInMB(freeSpace);
return new DecimalType(freeSpace);
return new QuantityType<>(freeSpace, Units.MEBIBYTE);
}

@Override
public DecimalType getStorageUsed(int index) throws DeviceNotFoundException {
public QuantityType<DataAmount> getStorageUsed(int index) throws DeviceNotFoundException {
OSFileStore fileStore = getDevice(fileStores, index);
fileStore.updateAttributes();
long totalSpace = fileStore.getTotalSpace();
long freeSpace = fileStore.getUsableSpace();
long usedSpace = totalSpace - freeSpace;
usedSpace = getSizeInMB(usedSpace);
return new DecimalType(usedSpace);
return new QuantityType<>(usedSpace, Units.MEBIBYTE);
}

@Override
public @Nullable DecimalType getStorageAvailablePercent(int deviceIndex) throws DeviceNotFoundException {
public @Nullable PercentType getStorageAvailablePercent(int deviceIndex) throws DeviceNotFoundException {
OSFileStore fileStore = getDevice(fileStores, deviceIndex);
fileStore.updateAttributes();
long totalSpace = fileStore.getTotalSpace();
long freeSpace = fileStore.getUsableSpace();
if (totalSpace > 0) {
double freePercentDecimal = (double) freeSpace / (double) totalSpace;
BigDecimal freePercent = getPercentsValue(freePercentDecimal);
return new DecimalType(freePercent);
return new PercentType(freePercent);
} else {
return null;
}
}

@Override
public @Nullable DecimalType getStorageUsedPercent(int deviceIndex) throws DeviceNotFoundException {
public @Nullable PercentType getStorageUsedPercent(int deviceIndex) throws DeviceNotFoundException {
OSFileStore fileStore = getDevice(fileStores, deviceIndex);
fileStore.updateAttributes();
long totalSpace = fileStore.getTotalSpace();
Expand All @@ -265,7 +274,7 @@ public DecimalType getStorageUsed(int index) throws DeviceNotFoundException {
if (totalSpace > 0) {
double usedPercentDecimal = (double) usedSpace / (double) totalSpace;
BigDecimal usedPercent = getPercentsValue(usedPercentDecimal);
return new DecimalType(usedPercent);
return new PercentType(usedPercent);
} else {
return null;
}
Expand Down Expand Up @@ -332,17 +341,17 @@ public StringType getDisplayInformation(int index) throws DeviceNotFoundExceptio
}

@Override
public @Nullable DecimalType getSensorsCpuTemperature() {
public @Nullable QuantityType<Temperature> getSensorsCpuTemperature() {
BigDecimal cpuTemp = new BigDecimal(sensors.getCpuTemperature());
cpuTemp = cpuTemp.setScale(PRECISION_AFTER_DECIMAL_SIGN, RoundingMode.HALF_UP);
return cpuTemp.signum() == 1 ? new DecimalType(cpuTemp) : null;
return cpuTemp.signum() == 1 ? new QuantityType<>(cpuTemp, SIUnits.CELSIUS) : null;
}

@Override
public @Nullable DecimalType getSensorsCpuVoltage() {
public @Nullable QuantityType<ElectricPotential> getSensorsCpuVoltage() {
BigDecimal cpuVoltage = new BigDecimal(sensors.getCpuVoltage());
cpuVoltage = cpuVoltage.setScale(PRECISION_AFTER_DECIMAL_SIGN, RoundingMode.HALF_UP);
return cpuVoltage.signum() == 1 ? new DecimalType(cpuVoltage) : null;
return cpuVoltage.signum() == 1 ? new QuantityType<>(cpuVoltage, Units.VOLT) : null;
}

@Override
Expand All @@ -358,22 +367,22 @@ public StringType getDisplayInformation(int index) throws DeviceNotFoundExceptio
}

@Override
public @Nullable DecimalType getBatteryRemainingTime(int index) throws DeviceNotFoundException {
public @Nullable QuantityType<Time> getBatteryRemainingTime(int index) throws DeviceNotFoundException {
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.
BigDecimal remainingTime = getTimeInMinutes(remainingTimeInSeconds);
return remainingTime.signum() == 1 ? new DecimalType(remainingTime) : null;
return remainingTime.signum() == 1 ? new QuantityType<>(remainingTime, Units.MINUTE) : null;
}

@Override
public DecimalType getBatteryRemainingCapacity(int index) throws DeviceNotFoundException {
public PercentType getBatteryRemainingCapacity(int index) throws DeviceNotFoundException {
PowerSource powerSource = getDevice(powerSources, index);
powerSource.updateAttributes();
double remainingCapacity = powerSource.getRemainingCapacityPercent();
BigDecimal remainingCapacityPercents = getPercentsValue(remainingCapacity);
return new DecimalType(remainingCapacityPercents);
return new PercentType(remainingCapacityPercents);
}

@Override
Expand All @@ -384,27 +393,27 @@ public StringType getBatteryName(int index) throws DeviceNotFoundException {
}

@Override
public @Nullable DecimalType getMemoryAvailablePercent() {
public @Nullable PercentType getMemoryAvailablePercent() {
long availableMemory = memory.getAvailable();
long totalMemory = memory.getTotal();
if (totalMemory > 0) {
double freePercentDecimal = (double) availableMemory / (double) totalMemory;
BigDecimal freePercent = getPercentsValue(freePercentDecimal);
return new DecimalType(freePercent);
return new PercentType(freePercent);
} else {
return null;
}
}

@Override
public @Nullable DecimalType getMemoryUsedPercent() {
public @Nullable PercentType getMemoryUsedPercent() {
long availableMemory = memory.getAvailable();
long totalMemory = memory.getTotal();
long usedMemory = totalMemory - availableMemory;
if (totalMemory > 0) {
double usedPercentDecimal = (double) usedMemory / (double) totalMemory;
BigDecimal usedPercent = getPercentsValue(usedPercentDecimal);
return new DecimalType(usedPercent);
return new PercentType(usedPercent);
} else {
return null;
}
Expand Down Expand Up @@ -432,50 +441,50 @@ public StringType getDriveSerialNumber(int deviceIndex) throws DeviceNotFoundExc
}

@Override
public @Nullable DecimalType getSwapTotal() {
public QuantityType<DataAmount> getSwapTotal() {
long swapTotal = memory.getVirtualMemory().getSwapTotal();
swapTotal = getSizeInMB(swapTotal);
return new DecimalType(swapTotal);
return new QuantityType<>(swapTotal, Units.MEBIBYTE);
}

@Override
public @Nullable DecimalType getSwapAvailable() {
public QuantityType<DataAmount> getSwapAvailable() {
long swapTotal = memory.getVirtualMemory().getSwapTotal();
long swapUsed = memory.getVirtualMemory().getSwapUsed();
long swapAvailable = swapTotal - swapUsed;
swapAvailable = getSizeInMB(swapAvailable);
return new DecimalType(swapAvailable);
return new QuantityType<>(swapAvailable, Units.MEBIBYTE);
}

@Override
public @Nullable DecimalType getSwapUsed() {
public QuantityType<DataAmount> getSwapUsed() {
long swapUsed = memory.getVirtualMemory().getSwapUsed();
swapUsed = getSizeInMB(swapUsed);
return new DecimalType(swapUsed);
return new QuantityType<>(swapUsed, Units.MEBIBYTE);
}

@Override
public @Nullable DecimalType getSwapAvailablePercent() {
public @Nullable PercentType getSwapAvailablePercent() {
long swapTotal = memory.getVirtualMemory().getSwapTotal();
long swapUsed = memory.getVirtualMemory().getSwapUsed();
long swapAvailable = swapTotal - swapUsed;
if (swapTotal > 0) {
double swapAvailablePercentDecimal = (double) swapAvailable / (double) swapTotal;
BigDecimal swapAvailablePercent = getPercentsValue(swapAvailablePercentDecimal);
return new DecimalType(swapAvailablePercent);
return new PercentType(swapAvailablePercent);
} else {
return null;
}
}

@Override
public @Nullable DecimalType getSwapUsedPercent() {
public @Nullable PercentType getSwapUsedPercent() {
long swapTotal = memory.getVirtualMemory().getSwapTotal();
long swapUsed = memory.getVirtualMemory().getSwapUsed();
if (swapTotal > 0) {
double swapUsedPercentDecimal = (double) swapUsed / (double) swapTotal;
BigDecimal swapUsedPercent = getPercentsValue(swapUsedPercentDecimal);
return new DecimalType(swapUsedPercent);
return new PercentType(swapUsedPercent);
} else {
return null;
}
Expand Down Expand Up @@ -561,9 +570,9 @@ private BigDecimal getAvarageCpuLoad(int timeInMinutes) {
}

@Override
public DecimalType getCpuUptime() {
public QuantityType<Time> getCpuUptime() {
long seconds = operatingSystem.getSystemUptime();
return new DecimalType(getTimeInMinutes(seconds));
return new QuantityType<>(getTimeInMinutes(seconds), Units.MINUTE);
}

@Override
Expand Down Expand Up @@ -596,19 +605,19 @@ public DecimalType getNetworkPacketsSent(int networkIndex) throws DeviceNotFound
}

@Override
public DecimalType getNetworkDataSent(int networkIndex) throws DeviceNotFoundException {
public QuantityType<DataAmount> getNetworkDataSent(int networkIndex) throws DeviceNotFoundException {
NetworkIF network = getDevice(networks, networkIndex);
network.updateAttributes();
long bytesSent = network.getBytesSent();
return new DecimalType(getSizeInMB(bytesSent));
return new QuantityType<>(getSizeInMB(bytesSent), Units.MEBIBYTE);
}

@Override
public DecimalType getNetworkDataReceived(int networkIndex) throws DeviceNotFoundException {
public QuantityType<DataAmount> getNetworkDataReceived(int networkIndex) throws DeviceNotFoundException {
NetworkIF network = getDevice(networks, networkIndex);
network.updateAttributes();
long bytesRecv = network.getBytesRecv();
return new DecimalType(getSizeInMB(bytesRecv));
return new QuantityType<>(getSizeInMB(bytesRecv), Units.MEBIBYTE);
}

@Override
Expand Down Expand Up @@ -642,12 +651,12 @@ public int getCurrentProcessID() {
}

@Override
public @Nullable DecimalType getProcessMemoryUsage(int pid) throws DeviceNotFoundException {
public @Nullable QuantityType<DataAmount> getProcessMemoryUsage(int pid) throws DeviceNotFoundException {
if (pid > 0) {
OSProcess process = getProcess(pid);
long memortInBytes = process.getResidentSetSize();
long memoryInMB = getSizeInMB(memortInBytes);
return new DecimalType(memoryInMB);
return new QuantityType<>(memoryInMB, Units.MEBIBYTE);
} else {
return null;
}
Expand Down
Loading