From 37b6aec7e3b8ec4366f608ad146d4f7f58b0bd8a Mon Sep 17 00:00:00 2001 From: Arne Seime Date: Mon, 7 Sep 2020 11:26:14 +0200 Subject: [PATCH] [sensibo] Fix for dynamic channels disappearing when device goes intermittent offline (#8400) Signed-off-by: Arne Seime --- .../internal/client/RequestLogger.java | 29 ++++++++++++------- .../handler/SensiboAccountHandler.java | 2 +- .../internal/handler/SensiboSkyHandler.java | 11 ++++--- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/bundles/org.openhab.binding.sensibo/src/main/java/org/openhab/binding/sensibo/internal/client/RequestLogger.java b/bundles/org.openhab.binding.sensibo/src/main/java/org/openhab/binding/sensibo/internal/client/RequestLogger.java index 98aea7b12cc24..81946ecb167b1 100644 --- a/bundles/org.openhab.binding.sensibo/src/main/java/org/openhab/binding/sensibo/internal/client/RequestLogger.java +++ b/bundles/org.openhab.binding.sensibo/src/main/java/org/openhab/binding/sensibo/internal/client/RequestLogger.java @@ -46,12 +46,12 @@ public final class RequestLogger { private final String prefix; public RequestLogger(final String prefix, final Gson gson) { - this.parser = new JsonParser(); + parser = new JsonParser(); this.gson = gson; this.prefix = prefix; } - private void dump(final Request request) { + private void dump(final Request request, String[] stringsToRemove) { final long idV = nextId.getAndIncrement(); if (logger.isDebugEnabled()) { final String id = prefix + "-" + idV; @@ -65,14 +65,14 @@ private void dump(final Request request) { }); final StringBuilder contentBuffer = new StringBuilder(); request.onRequestContent((theRequest, content) -> contentBuffer - .append(reformatJson(getCharset(theRequest.getHeaders()).decode(content).toString()))); + .append(getCharset(theRequest.getHeaders()).decode(content).toString())); request.onRequestSuccess(theRequest -> { if (contentBuffer.length() > 0) { group.append("\n"); - group.append(contentBuffer); + group.append(reformatJson(contentBuffer.toString())); } String dataToLog = group.toString(); - logger.debug(dataToLog); + scrambleAndLog(stringsToRemove, dataToLog); contentBuffer.delete(0, contentBuffer.length()); group.delete(0, group.length()); }); @@ -91,18 +91,27 @@ private void dump(final Request request) { } }); request.onResponseContent((theResponse, content) -> contentBuffer - .append(reformatJson(getCharset(theResponse.getHeaders()).decode(content).toString()))); + .append(getCharset(theResponse.getHeaders()).decode(content).toString())); request.onResponseSuccess(theResponse -> { if (contentBuffer.length() > 0) { group.append("\n"); - group.append(contentBuffer); + group.append(reformatJson(contentBuffer.toString())); } + String dataToLog = group.toString(); - logger.debug(dataToLog); + scrambleAndLog(stringsToRemove, dataToLog); }); } } + private void scrambleAndLog(String[] stringsToRemove, String dataToLog) { + String modifiedData = dataToLog; + for (String stringToRemove : stringsToRemove) { + modifiedData = modifiedData.replace(stringToRemove, ""); + } + logger.debug("{}", modifiedData); + } + private Charset getCharset(final HttpFields headers) { final String contentType = headers.get(HttpHeader.CONTENT_TYPE); if (contentType == null) { @@ -117,8 +126,8 @@ private Charset getCharset(final HttpFields headers) { return Charset.forName(encoding); } - public Request listenTo(final Request request) { - dump(request); + public Request listenTo(final Request request, String[] stringToRemove) { + dump(request, stringToRemove); return request; } diff --git a/bundles/org.openhab.binding.sensibo/src/main/java/org/openhab/binding/sensibo/internal/handler/SensiboAccountHandler.java b/bundles/org.openhab.binding.sensibo/src/main/java/org/openhab/binding/sensibo/internal/handler/SensiboAccountHandler.java index d106d3b5282ef..74605541a3d29 100644 --- a/bundles/org.openhab.binding.sensibo/src/main/java/org/openhab/binding/sensibo/internal/handler/SensiboAccountHandler.java +++ b/bundles/org.openhab.binding.sensibo/src/main/java/org/openhab/binding/sensibo/internal/handler/SensiboAccountHandler.java @@ -282,7 +282,7 @@ private Request buildRequest(final AbstractRequest req) { "application/json"); } - requestLogger.listenTo(request); + requestLogger.listenTo(request, new String[] { config.apiKey }); return request; } diff --git a/bundles/org.openhab.binding.sensibo/src/main/java/org/openhab/binding/sensibo/internal/handler/SensiboSkyHandler.java b/bundles/org.openhab.binding.sensibo/src/main/java/org/openhab/binding/sensibo/internal/handler/SensiboSkyHandler.java index 47ec23e8a125b..9708e9fc6cb54 100644 --- a/bundles/org.openhab.binding.sensibo/src/main/java/org/openhab/binding/sensibo/internal/handler/SensiboSkyHandler.java +++ b/bundles/org.openhab.binding.sensibo/src/main/java/org/openhab/binding/sensibo/internal/handler/SensiboSkyHandler.java @@ -154,7 +154,10 @@ private void updateTimer(@Nullable Integer secondsFromNowUntilSwitchOff) { protected void handleCommand(final ChannelUID channelUID, final Command command, final SensiboModel model) { model.findSensiboSkyByMacAddress(getMacAddress()).ifPresent(sensiboSky -> { if (sensiboSky.isAlive()) { - updateStatus(ThingStatus.ONLINE); // In case it has been offline + if (getThing().getStatus() != ThingStatus.ONLINE) { + addDynamicChannelsAndProperties(sensiboSky); + updateStatus(ThingStatus.ONLINE); // In case it has been offline + } switch (channelUID.getId()) { case CHANNEL_CURRENT_HUMIDITY: handleCurrentHumidityCommand(channelUID, command, sensiboSky); @@ -311,9 +314,9 @@ public void initialize() { config = Optional.ofNullable(getConfigAs(SensiboSkyConfiguration.class)); logger.debug("Initializing SensiboSky using config {}", config); getSensiboModel().findSensiboSkyByMacAddress(getMacAddress()).ifPresent(pod -> { - addDynamicChannelsAndProperties(pod); if (pod.isAlive()) { + addDynamicChannelsAndProperties(pod); updateStatus(ThingStatus.ONLINE); } else { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, @@ -327,6 +330,7 @@ private boolean isDynamicChannel(final ChannelTypeUID uid) { } private void addDynamicChannelsAndProperties(final SensiboSky sensiboSky) { + logger.debug("Updating dynamic channels for {}", sensiboSky.getId()); final List newChannels = new ArrayList<>(); for (final Channel channel : getThing().getChannels()) { final ChannelTypeUID channelTypeUID = channel.getChannelTypeUID(); @@ -335,8 +339,6 @@ private void addDynamicChannelsAndProperties(final SensiboSky sensiboSky) { } } - generatedChannelTypes.clear(); - newChannels.addAll(createDynamicChannels(sensiboSky)); Map properties = sensiboSky.getThingProperties(); updateThing(editThing().withChannels(newChannels).withProperties(properties).build()); @@ -344,6 +346,7 @@ private void addDynamicChannelsAndProperties(final SensiboSky sensiboSky) { public List createDynamicChannels(final SensiboSky sensiboSky) { final List newChannels = new ArrayList<>(); + generatedChannelTypes.clear(); sensiboSky.getCurrentModeCapabilities().ifPresent(capabilities -> { // Not all modes have swing and fan level