Skip to content

Commit

Permalink
Use core unit classes (openhab#9261)
Browse files Browse the repository at this point in the history
Add-ons should not depend on tec.uom.se classes but use core unit classes instead so they are less impacted when this library is replaced or upgraded.

Signed-off-by: Wouter Born <github@maindrain.net>
  • Loading branch information
wborn authored and boehan committed Apr 12, 2021
1 parent b743b71 commit bf825d4
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.unit.ImperialUnits;
import org.openhab.core.library.unit.SIUnits;
import org.openhab.core.library.unit.SmartHomeUnits;
import org.openhab.core.types.State;
import org.openhab.core.types.UnDefType;

import com.google.gson.annotations.SerializedName;

import tec.uom.se.unit.Units;

/**
* The {@code Measure} class defines the dto for Smarther API measure object.
*
Expand Down Expand Up @@ -101,7 +100,8 @@ public State toState() throws SmartherIllegalPropertyValueException {
.orElse(UnDefType.UNDEF);
break;
case PERCENTAGE:
state = optValue.<State> map(t -> new QuantityType<Dimensionless>(new DecimalType(t), Units.PERCENT))
state = optValue
.<State> map(t -> new QuantityType<Dimensionless>(new DecimalType(t), SmartHomeUnits.PERCENT))
.orElse(UnDefType.UNDEF);
break;
case DIMENSIONLESS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.unit.SmartHomeUnits;
import org.openhab.core.types.State;
import org.openhab.core.types.UnDefType;

import tec.uom.se.AbstractUnit;

/**
* The {@link CoronaStatsCountry} class holds the internal data representation of each Country
*
Expand Down Expand Up @@ -59,7 +58,7 @@ protected State parseToState(int count) {
if (count == -1) {
return UnDefType.NULL;
} else {
return new QuantityType<Dimensionless>(count, AbstractUnit.ONE);
return new QuantityType<Dimensionless>(count, SmartHomeUnits.ONE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@
import org.openhab.core.library.types.IncreaseDecreaseType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.types.UpDownType;
import org.openhab.core.library.unit.SmartHomeUnits;
import org.openhab.core.types.Command;
import org.openhab.core.types.StateDescriptionFragmentBuilder;
import org.openhab.core.types.UnDefType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import tec.uom.se.AbstractUnit;

/**
* Implements a number value.
*
Expand Down Expand Up @@ -111,7 +110,7 @@ public void update(Command command) throws IllegalArgumentException {
} else if (command instanceof QuantityType<?>) {
QuantityType<?> qType = (QuantityType<?>) command;

if (qType.getUnit().isCompatible(AbstractUnit.ONE)) {
if (qType.getUnit().isCompatible(SmartHomeUnits.ONE)) {
newValue = qType.toBigDecimal();
} else {
qType = qType.toUnit(unit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.library.types.UpDownType;
import org.openhab.core.library.unit.SmartHomeUnits;
import org.openhab.core.types.Command;
import org.openhab.core.types.StateDescriptionFragmentBuilder;
import org.openhab.core.types.UnDefType;

import tec.uom.se.unit.Units;

/**
* Implements a percentage value. Minimum and maximum are definable.
*
Expand Down Expand Up @@ -88,7 +87,7 @@ public void update(Command command) throws IllegalArgumentException {
} else //
// A quantity type need to be converted according to the current min/max values
if (command instanceof QuantityType) {
QuantityType<?> qty = ((QuantityType<?>) command).toUnit(Units.PERCENT);
QuantityType<?> qty = ((QuantityType<?>) command).toUnit(SmartHomeUnits.PERCENT);
if (qty != null) {
BigDecimal v = qty.toBigDecimal();
v = v.subtract(min).multiply(HUNDRED).divide(max.subtract(min), MathContext.DECIMAL128);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.neohub.internal.NeoHubAbstractDeviceData.AbstractRecord;
import org.openhab.binding.neohub.internal.NeoHubBindingConstants.NeoHubReturnResult;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.unit.SIUnits;
import org.openhab.core.library.unit.SmartHomeUnits;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
Expand All @@ -46,8 +48,6 @@

import com.google.gson.JsonSyntaxException;

import tec.uom.se.unit.Units;

/**
* The {@link NeoHubHandler} is the openHAB Handler for NeoHub devices
*
Expand Down Expand Up @@ -212,7 +212,7 @@ public synchronized NeoHubReturnResult toNeoHubSendChannelValue(String commandSt

/**
* sends a JSON request to the NeoHub to read the device data
*
*
* @return a class that contains the full status of all devices
*/
protected @Nullable NeoHubAbstractDeviceData fromNeoHubGetDeviceData() {
Expand Down Expand Up @@ -259,7 +259,7 @@ public synchronized NeoHubReturnResult toNeoHubSendChannelValue(String commandSt
if (deviceData instanceof NeoHubLiveDeviceData) {
/*
* note: time-stamps are measured in seconds from 1970-01-01T00:00:00Z
*
*
* new API: discard systemData if its time-stamp is older than the system
* time-stamp on the hub
*/
Expand All @@ -269,7 +269,7 @@ public synchronized NeoHubReturnResult toNeoHubSendChannelValue(String commandSt
} else {
/*
* note: time-stamps are measured in seconds from 1970-01-01T00:00:00Z
*
*
* legacy API: discard systemData if its time-stamp is older than one hour
*/
if (systemData.timeStamp < Instant.now().minus(1, ChronoUnit.HOURS).getEpochSecond()) {
Expand All @@ -288,7 +288,7 @@ public synchronized NeoHubReturnResult toNeoHubSendChannelValue(String commandSt

/**
* sends a JSON request to the NeoHub to read the system data
*
*
* @return a class that contains the status of the system
*/
protected @Nullable NeoHubReadDcbResponse fromNeoHubReadSystemData() {
Expand Down Expand Up @@ -369,7 +369,7 @@ private synchronized void lazyPollingSchedulerExecute() {
onlineDeviceCount++;
}
}
state = new QuantityType<>((100.0 * onlineDeviceCount) / totalDeviceCount, Units.PERCENT);
state = new QuantityType<>((100.0 * onlineDeviceCount) / totalDeviceCount, SmartHomeUnits.PERCENT);
}
updateState(CHAN_MESH_NETWORK_QOS, state);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.openhab.binding.opensprinkler.internal.config.OpenSprinklerStationConfig;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.unit.SmartHomeUnits;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
Expand All @@ -37,8 +38,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import tec.uom.se.unit.Units;

/**
* @author Florian Schmidt - Refactoring
*/
Expand Down Expand Up @@ -91,7 +90,7 @@ private void handleNextDurationCommand(ChannelUID channelUID, Command command) {
return;
}
QuantityType<?> quantity = (QuantityType<?>) command;
this.nextDurationTime = quantity.toUnit(Units.SECOND).toBigDecimal();
this.nextDurationTime = quantity.toUnit(SmartHomeUnits.SECOND).toBigDecimal();
updateState(channelUID, quantity);
}

Expand Down Expand Up @@ -183,7 +182,7 @@ private OnOffType getStationState(int stationId) {
+ " for the OpenSprinkler device. Exception received: " + exp);
}

return new QuantityType<>(remainingWaterTime, Units.SECOND);
return new QuantityType<>(remainingWaterTime, SmartHomeUnits.SECOND);
}

@Override
Expand All @@ -204,7 +203,7 @@ protected void updateChannel(@NonNull ChannelUID channel) {
case NEXT_DURATION:
BigDecimal duration = nextDurationValue();
if (duration != null) {
updateState(channel, new QuantityType<>(duration, Units.SECOND));
updateState(channel, new QuantityType<>(duration, SmartHomeUnits.SECOND));
}
break;
case STATION_QUEUED:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@
*/
package org.openhab.binding.sensibo.internal.handler;

import static org.openhab.binding.sensibo.internal.SensiboBindingConstants.CHANNEL_CURRENT_HUMIDITY;
import static org.openhab.binding.sensibo.internal.SensiboBindingConstants.CHANNEL_CURRENT_TEMPERATURE;
import static org.openhab.binding.sensibo.internal.SensiboBindingConstants.CHANNEL_FAN_LEVEL;
import static org.openhab.binding.sensibo.internal.SensiboBindingConstants.CHANNEL_MASTER_SWITCH;
import static org.openhab.binding.sensibo.internal.SensiboBindingConstants.CHANNEL_MODE;
import static org.openhab.binding.sensibo.internal.SensiboBindingConstants.CHANNEL_SWING_MODE;
import static org.openhab.binding.sensibo.internal.SensiboBindingConstants.CHANNEL_TARGET_TEMPERATURE;
import static org.openhab.binding.sensibo.internal.SensiboBindingConstants.CHANNEL_TIMER;
import static org.openhab.binding.sensibo.internal.SensiboBindingConstants.*;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -55,6 +48,7 @@
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.SmartHomeUnits;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
Expand All @@ -75,8 +69,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import tec.uom.se.unit.Units;

/**
* The {@link SensiboSkyHandler} is responsible for handling commands, which are
* sent to one of the channels.
Expand Down Expand Up @@ -300,7 +292,7 @@ private void handleCurrentTemperatureCommand(ChannelUID channelUID, Command comm

private void handleCurrentHumidityCommand(ChannelUID channelUID, Command command, SensiboSky sensiboSky) {
if (command instanceof RefreshType) {
updateState(channelUID, new QuantityType<>(sensiboSky.getHumidity(), Units.PERCENT));
updateState(channelUID, new QuantityType<>(sensiboSky.getHumidity(), SmartHomeUnits.PERCENT));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@
import org.openhab.binding.shelly.internal.api.ShellyApiJsonDTO.ShellyStatusSensor;
import org.openhab.binding.shelly.internal.config.ShellyThingConfiguration;
import org.openhab.core.library.unit.ImperialUnits;
import org.openhab.core.library.unit.SIUnits;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import tec.uom.se.unit.Units;

/**
* {@link ShellyHttpApi} wraps the Shelly REST API and provides various low level function to access the device api (not
* cloud api).
Expand Down Expand Up @@ -187,10 +186,10 @@ public ShellyStatusSensor getSensorStatus() throws ShellyApiException {
if (profile.isSense) {
// complete reported data, map C to F or vice versa: C=(F - 32) * 0.5556;
status.tmp.tC = status.tmp.units.equals(SHELLY_TEMP_CELSIUS) ? status.tmp.value
: ImperialUnits.FAHRENHEIT.getConverterTo(Units.CELSIUS).convert(getDouble(status.tmp.value))
: ImperialUnits.FAHRENHEIT.getConverterTo(SIUnits.CELSIUS).convert(getDouble(status.tmp.value))
.doubleValue();
status.tmp.tF = status.tmp.units.equals(SHELLY_TEMP_FAHRENHEIT) ? status.tmp.value
: Units.CELSIUS.getConverterTo(ImperialUnits.FAHRENHEIT).convert(getDouble(status.tmp.value))
: SIUnits.CELSIUS.getConverterTo(ImperialUnits.FAHRENHEIT).convert(getDouble(status.tmp.value))
.doubleValue();
}
if ((status.charger == null) && (status.externalPower != null)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import tec.uom.se.unit.Units;

/**
* The {@link ShellyCoIoTVersion1} implements the parsing for CoIoT version 1
*
Expand Down Expand Up @@ -84,7 +82,7 @@ public boolean handleStatusUpdate(List<CoIotSensor> sensorUpdates, CoIotDescrSen
case "temperature": // Sensor Temp
if (getString(getProfile().settings.temperatureUnits)
.equalsIgnoreCase(SHELLY_TEMP_FAHRENHEIT)) {
value = ImperialUnits.FAHRENHEIT.getConverterTo(Units.CELSIUS).convert(getDouble(s.value))
value = ImperialUnits.FAHRENHEIT.getConverterTo(SIUnits.CELSIUS).convert(getDouble(s.value))
.doubleValue();
}
updateChannel(updates, CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_TEMP,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
import org.openhab.core.library.unit.SIUnits;
import org.openhab.core.library.unit.SmartHomeUnits;

import tec.uom.se.unit.Units;

/***
* The{@link ShellyComponents} implements updates for supplemental components
* Meter will be used by Relay + Light; Sensor is part of H&T, Flood, Door Window, Sense
Expand Down Expand Up @@ -276,7 +274,7 @@ public static boolean updateSensors(ShellyBaseHandler thingHandler, ShellySettin
: getDouble(sdata.tmp.tF);
if (getString(sdata.tmp.units).toUpperCase().equals(SHELLY_TEMP_FAHRENHEIT)) {
// convert Fahrenheit to Celsius
temp = ImperialUnits.FAHRENHEIT.getConverterTo(Units.CELSIUS).convert(temp).doubleValue();
temp = ImperialUnits.FAHRENHEIT.getConverterTo(SIUnits.CELSIUS).convert(temp).doubleValue();
}
updated |= thingHandler.updateChannel(CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_TEMP,
toQuantityType(temp.doubleValue(), DIGITS_TEMP, SIUnits.CELSIUS));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@
import org.openhab.core.library.types.StringType;
import org.openhab.core.library.unit.ImperialUnits;
import org.openhab.core.library.unit.SIUnits;
import org.openhab.core.library.unit.SmartHomeUnits;
import org.openhab.core.types.State;
import org.openhab.core.types.UnDefType;

import com.google.gson.annotations.SerializedName;

import tec.uom.se.AbstractUnit;
import tec.uom.se.unit.Units;

/**
* private class: a generic data point
*
Expand Down Expand Up @@ -142,14 +140,14 @@ public Unit<?> getUnit() {
return ImperialUnits.FAHRENHEIT;
}
case DEGREES_KELVIN: {
return Units.KELVIN;
return SmartHomeUnits.KELVIN;
}
case PERCENT_RELATIVE_HUMIDITY: {
return Units.PERCENT;
return SmartHomeUnits.PERCENT;
}
}
}
return AbstractUnit.ONE;
return SmartHomeUnits.ONE;
}

/*
Expand Down
Loading

0 comments on commit bf825d4

Please sign in to comment.