[knx] Improve rounding when encoding DPT 232.60000 (#14772)

* [knx] Improve rounding when encoding DPT 232.60000
* [knx] SAT and warnings

Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
This commit is contained in:
Holger Friedrich 2023-04-14 20:23:32 +02:00 committed by GitHub
parent eb668bfe0c
commit b3dfba8cf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 30 additions and 19 deletions

View File

@ -251,7 +251,7 @@ public abstract class AbstractKNXClient implements NetworkLinkListener, KNXClien
state = ClientState.RUNNING; state = ClientState.RUNNING;
return true; return true;
} catch (InterruptedException e) { } catch (InterruptedException e) {
final var lastState = state; ClientState lastState = state;
state = ClientState.INTERRUPTED; state = ClientState.INTERRUPTED;
logger.trace("Bridge {}, connection interrupted", thingUID); logger.trace("Bridge {}, connection interrupted", thingUID);
@ -458,7 +458,7 @@ public abstract class AbstractKNXClient implements NetworkLinkListener, KNXClien
@Override @Override
public boolean isConnected() { public boolean isConnected() {
final var tmpLink = link; KNXNetworkLink tmpLink = link;
return tmpLink != null && tmpLink.isOpen(); return tmpLink != null && tmpLink.isOpen();
} }

View File

@ -310,8 +310,8 @@ public class ValueDecoder {
if (stringY == null) { if (stringY == null) {
return ColorUtil.xyToHsb(new double[] { x, y }); return ColorUtil.xyToHsb(new double[] { x, y });
} else { } else {
double Y = Double.parseDouble(stringY.replace(",", ".")); double pY = Double.parseDouble(stringY.replace(",", "."));
return ColorUtil.xyToHsb(new double[] { x, y, Y }); return ColorUtil.xyToHsb(new double[] { x, y, pY });
} }
} }
} }

View File

@ -154,7 +154,7 @@ public class ValueEncoder {
case "232.60000": case "232.60000":
// MDT specific: mis-use 232.600 for hsv instead of rgb // MDT specific: mis-use 232.600 for hsv instead of rgb
int hue = hsb.getHue().toBigDecimal().multiply(BigDecimal.valueOf(255)) int hue = hsb.getHue().toBigDecimal().multiply(BigDecimal.valueOf(255))
.divide(BigDecimal.valueOf(360), 2, RoundingMode.HALF_UP).intValue(); .divide(BigDecimal.valueOf(360), 0, RoundingMode.HALF_UP).intValue();
return "r:" + hue + " g:" + convertPercentToByte(hsb.getSaturation()) + " b:" return "r:" + hue + " g:" + convertPercentToByte(hsb.getSaturation()) + " b:"
+ convertPercentToByte(hsb.getBrightness()); + convertPercentToByte(hsb.getBrightness());
case "242.600": case "242.600":
@ -188,14 +188,20 @@ public class ValueEncoder {
|| DPTXlator2ByteFloat.DPT_KELVIN_PER_PERCENT.getID().equals(dptId)) { || DPTXlator2ByteFloat.DPT_KELVIN_PER_PERCENT.getID().equals(dptId)) {
// match unicode character or °C // match unicode character or °C
if (value.toString().contains(SIUnits.CELSIUS.getSymbol()) || value.toString().contains("°C")) { if (value.toString().contains(SIUnits.CELSIUS.getSymbol()) || value.toString().contains("°C")) {
unit = unit.replace("K", "°C"); if (unit != null) {
unit = unit.replace("K", "°C");
}
} else if (value.toString().contains("°F")) { } else if (value.toString().contains("°F")) {
unit = unit.replace("K", "°F"); if (unit != null) {
unit = unit.replace("K", "°F");
}
value = ((QuantityType<?>) value).multiply(BigDecimal.valueOf(5.0 / 9.0)); value = ((QuantityType<?>) value).multiply(BigDecimal.valueOf(5.0 / 9.0));
} }
} else if (DPTXlator4ByteFloat.DPT_LIGHT_QUANTITY.getID().equals(dptId)) { } else if (DPTXlator4ByteFloat.DPT_LIGHT_QUANTITY.getID().equals(dptId)) {
if (!value.toString().contains("J")) { if (!value.toString().contains("J")) {
unit = unit.replace("J", "lm*s"); if (unit != null) {
unit = unit.replace("J", "lm*s");
}
} }
} else if (DPTXlator4ByteFloat.DPT_ELECTRIC_FLUX.getID().equals(dptId)) { } else if (DPTXlator4ByteFloat.DPT_ELECTRIC_FLUX.getID().equals(dptId)) {
// use alternate definition of flux // use alternate definition of flux
@ -250,6 +256,6 @@ public class ValueEncoder {
*/ */
private static int convertPercentToByte(PercentType percent) { private static int convertPercentToByte(PercentType percent) {
return percent.toBigDecimal().multiply(BigDecimal.valueOf(255)) return percent.toBigDecimal().multiply(BigDecimal.valueOf(255))
.divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP).intValue(); .divide(BigDecimal.valueOf(100), 0, RoundingMode.HALF_UP).intValue();
} }
} }

View File

@ -505,12 +505,12 @@ public class DeviceThingHandler extends BaseThingHandler implements GroupAddress
} }
protected void detachFromClient() { protected void detachFromClient() {
final var pollingJobSynced = pollingJob; ScheduledFuture<?> pollingJobSynced = pollingJob;
if (pollingJobSynced != null) { if (pollingJobSynced != null) {
pollingJobSynced.cancel(true); pollingJobSynced.cancel(true);
pollingJob = null; pollingJob = null;
} }
final var descriptionJobSynced = descriptionJob; ScheduledFuture<?> descriptionJobSynced = descriptionJob;
if (descriptionJobSynced != null) { if (descriptionJobSynced != null) {
descriptionJobSynced.cancel(true); descriptionJobSynced.cancel(true);
descriptionJob = null; descriptionJob = null;

View File

@ -171,12 +171,13 @@ public class IPBridgeThingHandler extends KNXBridgeBaseThingHandler {
if (!config.getLocalIp().isEmpty()) { if (!config.getLocalIp().isEmpty()) {
localEndPoint = new InetSocketAddress(config.getLocalIp(), 0); localEndPoint = new InetSocketAddress(config.getLocalIp(), 0);
} else { } else {
if (networkAddressService == null) { NetworkAddressService localNetworkAddressService = networkAddressService;
if (localNetworkAddressService == null) {
logger.debug("NetworkAddressService not available, cannot create bridge {}", thing.getUID()); logger.debug("NetworkAddressService not available, cannot create bridge {}", thing.getUID());
updateStatus(ThingStatus.OFFLINE); updateStatus(ThingStatus.OFFLINE);
return; return;
} else { } else {
localEndPoint = new InetSocketAddress(networkAddressService.getPrimaryIpv4HostAddress(), 0); localEndPoint = new InetSocketAddress(localNetworkAddressService.getPrimaryIpv4HostAddress(), 0);
} }
} }
@ -186,7 +187,7 @@ public class IPBridgeThingHandler extends KNXBridgeBaseThingHandler {
secureTunnel.user, secureTunnel.userKey, thing.getUID(), config.getResponseTimeout(), secureTunnel.user, secureTunnel.userKey, thing.getUID(), config.getResponseTimeout(),
config.getReadingPause(), config.getReadRetriesLimit(), getScheduler(), this); config.getReadingPause(), config.getReadRetriesLimit(), getScheduler(), this);
final var tmpClient = client; IPClient tmpClient = client;
if (tmpClient != null) { if (tmpClient != null) {
tmpClient.initialize(); tmpClient.initialize();
} }
@ -196,7 +197,7 @@ public class IPBridgeThingHandler extends KNXBridgeBaseThingHandler {
@Override @Override
public void dispose() { public void dispose() {
final var tmpInitJob = initJob; Future<?> tmpInitJob = initJob;
if (tmpInitJob != null) { if (tmpInitJob != null) {
while (!tmpInitJob.isDone()) { while (!tmpInitJob.isDone()) {
logger.trace("Bridge {}, shutdown during init, trying to cancel", thing.getUID()); logger.trace("Bridge {}, shutdown during init, trying to cancel", thing.getUID());
@ -209,7 +210,7 @@ public class IPBridgeThingHandler extends KNXBridgeBaseThingHandler {
} }
initJob = null; initJob = null;
} }
final var tmpClient = client; IPClient tmpClient = client;
if (tmpClient != null) { if (tmpClient != null) {
tmpClient.dispose(); tmpClient.dispose();
client = null; client = null;

View File

@ -65,7 +65,7 @@ public class SerialBridgeThingHandler extends KNXBridgeBaseThingHandler {
} }
public void initializeLater() { public void initializeLater() {
final var tmpClient = client; SerialClient tmpClient = client;
if (tmpClient != null) { if (tmpClient != null) {
tmpClient.initialize(); tmpClient.initialize();
} }
@ -73,7 +73,7 @@ public class SerialBridgeThingHandler extends KNXBridgeBaseThingHandler {
@Override @Override
public void dispose() { public void dispose() {
final var tmpInitJob = initJob; Future<?> tmpInitJob = initJob;
if (tmpInitJob != null) { if (tmpInitJob != null) {
if (!tmpInitJob.isDone()) { if (!tmpInitJob.isDone()) {
logger.trace("Bridge {}, shutdown during init, trying to cancel", thing.getUID()); logger.trace("Bridge {}, shutdown during init, trying to cancel", thing.getUID());
@ -87,7 +87,7 @@ public class SerialBridgeThingHandler extends KNXBridgeBaseThingHandler {
initJob = null; initJob = null;
} }
final var tmpClient = client; SerialClient tmpClient = client;
if (tmpClient != null) { if (tmpClient != null) {
tmpClient.dispose(); tmpClient.dispose();
client = null; client = null;

View File

@ -326,6 +326,10 @@ class DPTTest {
assertEquals(173.6, hsbType.getHue().doubleValue(), 0.1); assertEquals(173.6, hsbType.getHue().doubleValue(), 0.1);
assertEquals(17.6, hsbType.getSaturation().doubleValue(), 0.1); assertEquals(17.6, hsbType.getSaturation().doubleValue(), 0.1);
assertEquals(26.3, hsbType.getBrightness().doubleValue(), 0.1); assertEquals(26.3, hsbType.getBrightness().doubleValue(), 0.1);
String encoded = ValueEncoder.encode(hsbType, "232.60000");
assertNotNull(encoded);
assertEquals(encoded, "r:" + data[0] + " g:" + data[1] + " b:" + data[2]);
} }
@Test @Test