From 630620edbdb90f27141d29c3074ed5a3826b3425 Mon Sep 17 00:00:00 2001 From: Thomas Vogl Date: Thu, 4 Jan 2024 12:20:57 +0100 Subject: [PATCH] adapt to 4.1.x --- .../pom.xml | 2 +- .../config/MediolaAioBridgeConfig.java | 2 - .../MediolaAioDeviceDiscoveryService.java | 38 +++++----- .../handler/MediolaAioEleroDeviceHandler.java | 73 ++++++++++--------- .../MediolaAioGatewayBridgeHandler.java | 47 ++++++------ .../MediolaAioGatewayHandlerFactory.java | 3 +- .../internal/mediolaapi/AioEleroCommand.java | 36 +++++---- .../internal/mediolaapi/AioType.java | 2 - .../main/resources/OH-INF/thing/bridge.xml | 7 +- .../resources/OH-INF/thing/thing-types.xml | 17 +++-- bundles/pom.xml | 2 +- 11 files changed, 114 insertions(+), 115 deletions(-) diff --git a/bundles/org.openhab.binding.mediolaaiogateway/pom.xml b/bundles/org.openhab.binding.mediolaaiogateway/pom.xml index f5089ae10..65616c738 100644 --- a/bundles/org.openhab.binding.mediolaaiogateway/pom.xml +++ b/bundles/org.openhab.binding.mediolaaiogateway/pom.xml @@ -7,7 +7,7 @@ org.openhab.addons.bundles org.openhab.addons.reactor.bundles - 3.2.0 + 4.1.1-SNAPSHOT org.openhab.binding.mediolaaiogateway diff --git a/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/config/MediolaAioBridgeConfig.java b/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/config/MediolaAioBridgeConfig.java index 7b3aa9bcb..a49299e8b 100644 --- a/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/config/MediolaAioBridgeConfig.java +++ b/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/config/MediolaAioBridgeConfig.java @@ -12,8 +12,6 @@ */ package org.openhab.binding.mediolaaiogateway.internal.config; -import org.eclipse.jdt.annotation.NonNullByDefault; - /** * The {@link MediolaAioBridgeConfig} class contains fields mapping thing configuration parameters. * diff --git a/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/discovery/MediolaAioDeviceDiscoveryService.java b/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/discovery/MediolaAioDeviceDiscoveryService.java index a70ca05ba..8d082f53a 100644 --- a/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/discovery/MediolaAioDeviceDiscoveryService.java +++ b/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/discovery/MediolaAioDeviceDiscoveryService.java @@ -12,8 +12,12 @@ */ package org.openhab.binding.mediolaaiogateway.internal.discovery; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; +import static org.openhab.binding.mediolaaiogateway.internal.MediolaAioGatewayBindingConstants.THING_TYPE_ELERO_DEVICE; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.mediolaaiogateway.internal.exception.MediolaAioCommandError; @@ -30,11 +34,8 @@ import org.openhab.core.thing.binding.ThingHandlerService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; - -import static org.openhab.binding.mediolaaiogateway.internal.MediolaAioGatewayBindingConstants.THING_TYPE_ELERO_DEVICE; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; /** * The {@link MediolaAioDeviceDiscoveryService} is used to discover devices that are connected to a Homematic gateway. @@ -42,15 +43,11 @@ import static org.openhab.binding.mediolaaiogateway.internal.MediolaAioGatewayBi * @author Gerhard Riegler - Initial contribution */ -public class MediolaAioDeviceDiscoveryService extends AbstractDiscoveryService - implements ThingHandlerService { +public class MediolaAioDeviceDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService { private final Logger logger = LoggerFactory.getLogger(MediolaAioDeviceDiscoveryService.class); private static final int DISCOVER_TIMEOUT_SECONDS = 300; - private @NonNullByDefault({}) - MediolaAioGatewayBridgeHandler bridgeHandler; - - + private @NonNullByDefault({}) MediolaAioGatewayBridgeHandler bridgeHandler; public MediolaAioDeviceDiscoveryService() { super(Set.of(THING_TYPE_ELERO_DEVICE), DISCOVER_TIMEOUT_SECONDS, false); @@ -89,20 +86,19 @@ public class MediolaAioDeviceDiscoveryService extends AbstractDiscoveryService JsonElement devs = bridgeHandler.sendCommand(AioCommand.GetStates, params); devs.getAsJsonArray().forEach((e) -> { JsonObject o = e.getAsJsonObject(); - if ( o.has("type") && o.get("type").getAsString().equals(AioType.Elero)) { - int address = Integer.parseInt(o.get("adr").getAsString(),16); - int sid = Integer.parseInt(o.get("sid").getAsString(),16); + if (o.has("type") && o.get("type").getAsString().equals(AioType.Elero)) { + int address = Integer.parseInt(o.get("adr").getAsString(), 16); + int sid = Integer.parseInt(o.get("sid").getAsString(), 16); String strAddress = String.format("%02X", address); - ThingUID thingUID = new ThingUID(THING_TYPE_ELERO_DEVICE, bridgeHandler.getThing().getUID(), strAddress); + ThingUID thingUID = new ThingUID(THING_TYPE_ELERO_DEVICE, bridgeHandler.getThing().getUID(), + strAddress); DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID) .withBridge(bridgeHandler.getThing().getUID()) .withLabel(String.format("Elero Device %s", strAddress)) - .withProperty("deviceAddress", address) - .withProperty("deviceSid", sid) - .build(); + .withProperty("deviceAddress", address).withProperty("deviceSid", sid).build(); thingDiscovered(discoveryResult); } @@ -110,7 +106,7 @@ public class MediolaAioDeviceDiscoveryService extends AbstractDiscoveryService stopScan(); - } catch (MediolaAioCommandError|MediolaAioCommunicationError ignored) { + } catch (MediolaAioCommandError | MediolaAioCommunicationError ignored) { } logger.debug("Starting AIO Gateway discovery scan"); diff --git a/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/handler/MediolaAioEleroDeviceHandler.java b/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/handler/MediolaAioEleroDeviceHandler.java index 5547c2552..e7abfa390 100644 --- a/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/handler/MediolaAioEleroDeviceHandler.java +++ b/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/handler/MediolaAioEleroDeviceHandler.java @@ -14,7 +14,11 @@ package org.openhab.binding.mediolaaiogateway.internal.handler; import static org.openhab.binding.mediolaaiogateway.internal.MediolaAioGatewayBindingConstants.*; -import com.google.gson.JsonElement; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jetty.client.HttpClient; @@ -32,10 +36,7 @@ import org.openhab.core.types.RefreshType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; +import com.google.gson.JsonElement; /** * The {@link MediolaAioEleroDeviceHandler} is responsible for handling commands, which are @@ -46,7 +47,6 @@ import java.util.concurrent.TimeUnit; @NonNullByDefault public class MediolaAioEleroDeviceHandler extends BaseThingHandler { - private static final Map _commandMap = new HashMap<>(); private final Logger logger = LoggerFactory.getLogger(MediolaAioEleroDeviceHandler.class); @@ -82,14 +82,18 @@ public class MediolaAioEleroDeviceHandler extends BaseThingHandler { } public void stateUpdate(ChannelUID channelUID) { - @Nullable Bridge bridge = getBridge(); - if ( bridge == null) { + @Nullable + Bridge bridge = getBridge(); + if (bridge == null) { logger.warn("could not get bridge"); return; } - @Nullable MediolaAioGatewayBridgeHandler bHandler = (MediolaAioGatewayBridgeHandler) bridge.getHandler(); + @Nullable + MediolaAioGatewayBridgeHandler bHandler = (MediolaAioGatewayBridgeHandler) bridge.getHandler(); - if ( bHandler == null || config == null) { return; } + if (bHandler == null || config == null) { + return; + } Map params = new HashMap<>(); params.put("adr", String.format("%02x", config.deviceAddress)); @@ -102,27 +106,29 @@ public class MediolaAioEleroDeviceHandler extends BaseThingHandler { updateState(channelUID, new StringType(Integer.toString(state))); } catch (MediolaAioCommandError | MediolaAioCommunicationError err) { - logger.error("{}",err.getMessage()); + logger.error("{}", err.getMessage()); } } private void handleCommandString(String cmd) { - @Nullable Bridge bridge = getBridge(); - @Nullable MediolaAioGatewayBridgeHandler bHandler = (MediolaAioGatewayBridgeHandler) bridge.getHandler(); + @Nullable + Bridge bridge = getBridge(); + @Nullable + MediolaAioGatewayBridgeHandler bHandler = (MediolaAioGatewayBridgeHandler) bridge.getHandler(); - if ( bHandler == null || config == null) { return; } + if (bHandler == null || config == null) { + return; + } Integer eleroCmd = _commandMap.get(cmd); - if (eleroCmd != null ) { + if (eleroCmd != null) { try { bHandler.sendEleroCommand(config.deviceAddress, eleroCmd); } catch (MediolaAioCommandError mediolaAioCommandError) { logger.error("Command not successful: {}", mediolaAioCommandError.getMessage()); } catch (MediolaAioCommunicationError mediolaAioCommunicationError) { - updateStatus( - ThingStatus.OFFLINE, - ThingStatusDetail.COMMUNICATION_ERROR, + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, String.format("Could not reach Mediola AIO Gateway")); } } @@ -142,26 +148,24 @@ public class MediolaAioEleroDeviceHandler extends BaseThingHandler { else if (CHANNEL_CONTROL_BLIND.equals(id)) { if (command instanceof UpDownType) { handleCommandString(((UpDownType) command).toString()); - } - else if (command instanceof StopMoveType) { + } else if (command instanceof StopMoveType) { handleCommandString(((StopMoveType) command).toString()); - } - else if (command instanceof PercentType) { + } else if (command instanceof PercentType) { int val = ((PercentType) command).intValue(); - if (val == 0 ) { - //open position + if (val == 0) { + // open position handleCommandString("UP"); } - if ( 0 < val && val <= 50 ) { - //intermediate position + if (0 < val && val <= 50) { + // intermediate position handleCommandString("DOUBLE_TAP_DOWN"); } - if ( 50 < val && val <= 100 ) { - //ventilation position + if (50 < val && val <= 100) { + // ventilation position handleCommandString("DOUBLE_TAP_UP"); } - if ( val == 100 ) { - //close position + if (val == 100) { + // close position handleCommandString("DOWN"); } } @@ -182,7 +186,7 @@ public class MediolaAioEleroDeviceHandler extends BaseThingHandler { @Override public void dispose() { - if (pollingJob != null ) { + if (pollingJob != null) { pollingJob.cancel(true); pollingJob = null; } @@ -212,8 +216,11 @@ public class MediolaAioEleroDeviceHandler extends BaseThingHandler { updateStatus(ThingStatus.UNKNOWN); pollingJob = scheduler.scheduleWithFixedDelay(() -> { - @Nullable Channel ch = thing.getChannel(CHANNEL_STATE); - if ( ch == null ) {return; } + @Nullable + Channel ch = thing.getChannel(CHANNEL_STATE); + if (ch == null) { + return; + } this.stateUpdate(ch.getUID()); diff --git a/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/handler/MediolaAioGatewayBridgeHandler.java b/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/handler/MediolaAioGatewayBridgeHandler.java index 58cda2945..ecc22053d 100644 --- a/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/handler/MediolaAioGatewayBridgeHandler.java +++ b/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/handler/MediolaAioGatewayBridgeHandler.java @@ -12,9 +12,16 @@ */ package org.openhab.binding.mediolaaiogateway.internal.handler; -import com.google.gson.Gson; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import javax.ws.rs.HttpMethod; + import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jetty.client.HttpClient; @@ -37,14 +44,9 @@ import org.openhab.core.types.Command; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.ws.rs.HttpMethod; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; /** * The {@link MediolaAioGatewayBridgeHandler} is responsible for handling commands, which are @@ -75,34 +77,32 @@ public class MediolaAioGatewayBridgeHandler extends BaseBridgeHandler { return Collections.singleton(MediolaAioDeviceDiscoveryService.class); } - public JsonElement sendEleroCommand(Integer deviceAddress, Integer cmd) throws MediolaAioCommandError, MediolaAioCommunicationError { + public JsonElement sendEleroCommand(Integer deviceAddress, Integer cmd) + throws MediolaAioCommandError, MediolaAioCommunicationError { Map params = new HashMap<>(); params.put("type", AioType.Elero); params.put("data", String.format("%02x%02x", deviceAddress, cmd)); return this.sendCommand(AioCommand.SendSC, params); } - public JsonElement sendCommand(String command, Map params) throws MediolaAioCommandError, MediolaAioCommunicationError { + public JsonElement sendCommand(String command, Map params) + throws MediolaAioCommandError, MediolaAioCommunicationError { - Request request = _httpClient.newRequest(config.gatewayAddress, config.gatewayPort) - .scheme("http") - .agent("OpenHAB Mediola AIO Gateway Binding") - .version(HttpVersion.HTTP_1_1) - .method(HttpMethod.GET) - .path("/cmd") - .param("XC_FNC", command) - .timeout(5, TimeUnit.SECONDS); + Request request = _httpClient.newRequest(config.gatewayAddress, config.gatewayPort).scheme("http") + .agent("OpenHAB Mediola AIO Gateway Binding").version(HttpVersion.HTTP_1_1).method(HttpMethod.GET) + .path("/cmd").param("XC_FNC", command).timeout(5, TimeUnit.SECONDS); params.forEach(request::param); - @Nullable ContentResponse response; + @Nullable + ContentResponse response; byte[] content; synchronized (this) { try { response = request.send(); - if(!HttpStatus.isSuccess(response.getStatus())) { + if (!HttpStatus.isSuccess(response.getStatus())) { throw new MediolaAioCommunicationError("invalid http result code"); } content = response.getContent(); @@ -127,7 +127,6 @@ public class MediolaAioGatewayBridgeHandler extends BaseBridgeHandler { } } - @Override public void initialize() { config = getConfigAs(MediolaAioBridgeConfig.class); diff --git a/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/handler/MediolaAioGatewayHandlerFactory.java b/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/handler/MediolaAioGatewayHandlerFactory.java index c58285d9a..95dd0e659 100644 --- a/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/handler/MediolaAioGatewayHandlerFactory.java +++ b/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/handler/MediolaAioGatewayHandlerFactory.java @@ -47,7 +47,8 @@ public class MediolaAioGatewayHandlerFactory extends BaseThingHandlerFactory { httpClient = httpClientFactory.getCommonHttpClient(); } - private static final Set SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_BRIDGE, THING_TYPE_ELERO_DEVICE); + private static final Set SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_BRIDGE, + THING_TYPE_ELERO_DEVICE); @Override public boolean supportsThingType(ThingTypeUID thingTypeUID) { diff --git a/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/mediolaapi/AioEleroCommand.java b/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/mediolaapi/AioEleroCommand.java index 165730dfd..1249dbfda 100644 --- a/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/mediolaapi/AioEleroCommand.java +++ b/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/mediolaapi/AioEleroCommand.java @@ -12,7 +12,6 @@ */ package org.openhab.binding.mediolaaiogateway.internal.mediolaapi; - import org.eclipse.jdt.annotation.NonNullByDefault; /** @@ -20,24 +19,23 @@ import org.eclipse.jdt.annotation.NonNullByDefault; */ @NonNullByDefault public class AioEleroCommand { - public static final Integer DownOff = 0x00; - public static final Integer UpOn = 0x01; - public static final Integer Stop = 0x02; - public static final Integer UpStepBit = 0x03; - public static final Integer DownStepBit = 0x04; - public static final Integer ManuMode = 0x05; - public static final Integer AutoMode = 0x06; - public static final Integer ToggleMode = 0x07; - public static final Integer Up3s = 0x08; - public static final Integer Down3s = 0x09; - public static final Integer DoubleTapUp = 0x0A; - public static final Integer DoubleTapDown = 0x0B; - public static final Integer StartLearning = 0x0C; - public static final Integer OnPulseMove = 0x0D; - public static final Integer OffPulseMove = 0x0E; - public static final Integer ASClose = 0x0F; - public static final Integer ASMove = 0x10; + public static final Integer DownOff = 0x00; + public static final Integer UpOn = 0x01; + public static final Integer Stop = 0x02; + public static final Integer UpStepBit = 0x03; + public static final Integer DownStepBit = 0x04; + public static final Integer ManuMode = 0x05; + public static final Integer AutoMode = 0x06; + public static final Integer ToggleMode = 0x07; + public static final Integer Up3s = 0x08; + public static final Integer Down3s = 0x09; + public static final Integer DoubleTapUp = 0x0A; + public static final Integer DoubleTapDown = 0x0B; + public static final Integer StartLearning = 0x0C; + public static final Integer OnPulseMove = 0x0D; + public static final Integer OffPulseMove = 0x0E; + public static final Integer ASClose = 0x0F; + public static final Integer ASMove = 0x10; public static final Integer SaveVentilationPos = 0x14; public static final Integer SaveIntermediatePos = 0x12; - } diff --git a/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/mediolaapi/AioType.java b/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/mediolaapi/AioType.java index 33c9fc63e..8472e38bd 100644 --- a/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/mediolaapi/AioType.java +++ b/bundles/org.openhab.binding.mediolaaiogateway/src/main/java/org/openhab/binding/mediolaaiogateway/internal/mediolaapi/AioType.java @@ -12,8 +12,6 @@ */ package org.openhab.binding.mediolaaiogateway.internal.mediolaapi; - - import org.eclipse.jdt.annotation.NonNullByDefault; /** diff --git a/bundles/org.openhab.binding.mediolaaiogateway/src/main/resources/OH-INF/thing/bridge.xml b/bundles/org.openhab.binding.mediolaaiogateway/src/main/resources/OH-INF/thing/bridge.xml index a8e448c55..58078e4bb 100644 --- a/bundles/org.openhab.binding.mediolaaiogateway/src/main/resources/OH-INF/thing/bridge.xml +++ b/bundles/org.openhab.binding.mediolaaiogateway/src/main/resources/OH-INF/thing/bridge.xml @@ -1,7 +1,8 @@ - + diff --git a/bundles/org.openhab.binding.mediolaaiogateway/src/main/resources/OH-INF/thing/thing-types.xml b/bundles/org.openhab.binding.mediolaaiogateway/src/main/resources/OH-INF/thing/thing-types.xml index e33ef918d..e1189cf95 100644 --- a/bundles/org.openhab.binding.mediolaaiogateway/src/main/resources/OH-INF/thing/thing-types.xml +++ b/bundles/org.openhab.binding.mediolaaiogateway/src/main/resources/OH-INF/thing/thing-types.xml @@ -1,19 +1,20 @@ - + - + Elero Device (via AIO Gateway) - - - - + + + + diff --git a/bundles/pom.xml b/bundles/pom.xml index 5371316ba..5d0ea2a7e 100644 --- a/bundles/pom.xml +++ b/bundles/pom.xml @@ -229,7 +229,7 @@ org.openhab.binding.mcp23017 org.openhab.binding.meater org.openhab.binding.mecmeter - org.openhab.binding.mediolaaiogateway + org.openhab.binding.mediolaaiogateway org.openhab.binding.melcloud org.openhab.binding.mercedesme org.openhab.binding.meteoalerte