Skip to content

Commit

Permalink
[sensibo] Fix for dynamic channels disappearing when device goes inte…
Browse files Browse the repository at this point in the history
…rmittent offline (openhab#8400)

Signed-off-by: Arne Seime <[email protected]>
  • Loading branch information
seime authored and andrewfg committed Oct 8, 2020
1 parent 310951e commit 37b6aec
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
});
Expand All @@ -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, "<HIDDEN>");
}
logger.debug("{}", modifiedData);
}

private Charset getCharset(final HttpFields headers) {
final String contentType = headers.get(HttpHeader.CONTENT_TYPE);
if (contentType == null) {
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ private Request buildRequest(final AbstractRequest req) {
"application/json");
}

requestLogger.listenTo(request);
requestLogger.listenTo(request, new String[] { config.apiKey });

return request;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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<Channel> newChannels = new ArrayList<>();
for (final Channel channel : getThing().getChannels()) {
final ChannelTypeUID channelTypeUID = channel.getChannelTypeUID();
Expand All @@ -335,15 +339,14 @@ private void addDynamicChannelsAndProperties(final SensiboSky sensiboSky) {
}
}

generatedChannelTypes.clear();

newChannels.addAll(createDynamicChannels(sensiboSky));
Map<String, String> properties = sensiboSky.getThingProperties();
updateThing(editThing().withChannels(newChannels).withProperties(properties).build());
}

public List<Channel> createDynamicChannels(final SensiboSky sensiboSky) {
final List<Channel> newChannels = new ArrayList<>();
generatedChannelTypes.clear();

sensiboSky.getCurrentModeCapabilities().ifPresent(capabilities -> {
// Not all modes have swing and fan level
Expand Down

0 comments on commit 37b6aec

Please sign in to comment.