From 60fadcfca092630368fa7d6344371256abe3a0b2 Mon Sep 17 00:00:00 2001 From: Laurent Garnier Date: Fri, 26 Jun 2020 14:23:46 +0200 Subject: [PATCH] [hue] Reduce the number of warnings Siugned-off-by: Laurent Garnier --- .../hue/internal/config/HueBridgeConfig.java | 5 ++- .../internal/handler/HueBridgeHandler.java | 29 +++++++------- .../hue/internal/handler/HueGroupHandler.java | 35 ++++++++--------- .../hue/internal/handler/HueLightHandler.java | 38 +++++++++---------- .../internal/handler/HueSensorHandler.java | 5 ++- 5 files changed, 54 insertions(+), 58 deletions(-) diff --git a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/config/HueBridgeConfig.java b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/config/HueBridgeConfig.java index 59182703c9cba..01320d0ce701e 100644 --- a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/config/HueBridgeConfig.java +++ b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/config/HueBridgeConfig.java @@ -26,14 +26,14 @@ public class HueBridgeConfig { public static final String HTTP = "http"; public static final String HTTPS = "https"; - private @NonNullByDefault({}) String ipAddress; + private @Nullable String ipAddress; private @Nullable Integer port; private String protocol = HTTP; private @Nullable String userName; private int pollingInterval = 10; private int sensorPollingInterval = 500; - public String getIpAddress() { + public @Nullable String getIpAddress() { return ipAddress; } @@ -42,6 +42,7 @@ public void setIpAddress(String ipAddress) { } public int getPort() { + Integer port = this.port; return (port != null) ? port.intValue() : HTTPS.equals(protocol) ? 443 : 80; } diff --git a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueBridgeHandler.java b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueBridgeHandler.java index 22ca30517626b..a3e90a5d147d8 100644 --- a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueBridgeHandler.java +++ b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueBridgeHandler.java @@ -89,14 +89,14 @@ public class HueBridgeHandler extends ConfigStatusBridgeHandler implements HueCl private final Logger logger = LoggerFactory.getLogger(HueBridgeHandler.class); - private final Map lastLightStates = new ConcurrentHashMap<>(); - private final Map lastSensorStates = new ConcurrentHashMap<>(); - private final Map lastGroupStates = new ConcurrentHashMap<>(); + private final Map lastLightStates = new ConcurrentHashMap<>(); + private final Map lastSensorStates = new ConcurrentHashMap<>(); + private final Map lastGroupStates = new ConcurrentHashMap<>(); private @Nullable HueLightDiscoveryService discoveryService; - private final Map lightStatusListeners = new ConcurrentHashMap<>(); - private final Map sensorStatusListeners = new ConcurrentHashMap<>(); - private final Map groupStatusListeners = new ConcurrentHashMap<>(); + private final Map lightStatusListeners = new ConcurrentHashMap<>(); + private final Map sensorStatusListeners = new ConcurrentHashMap<>(); + private final Map groupStatusListeners = new ConcurrentHashMap<>(); final ReentrantLock pollingLock = new ReentrantLock(); @@ -170,7 +170,7 @@ private boolean isReachable(String ipAddress) { private final Runnable sensorPollingRunnable = new PollingRunnable() { @Override protected void doConnectedRun() throws IOException, ApiException { - Map lastSensorStateCopy = new HashMap<>(lastSensorStates); + Map lastSensorStateCopy = new HashMap<>(lastSensorStates); final HueLightDiscoveryService discovery = discoveryService; @@ -204,7 +204,7 @@ protected void doConnectedRun() throws IOException, ApiException { sensorStatusListener.onSensorRemoved(); } - if (discovery != null) { + if (discovery != null && sensor != null) { discovery.removeSensorDiscovery(sensor); } }); @@ -214,7 +214,7 @@ protected void doConnectedRun() throws IOException, ApiException { private final Runnable lightPollingRunnable = new PollingRunnable() { @Override protected void doConnectedRun() throws IOException, ApiException { - Map lastLightStateCopy = new HashMap<>(lastLightStates); + Map lastLightStateCopy = new HashMap<>(lastLightStates); List lights; if (ApiVersionUtils.supportsFullLights(hueBridge.getVersion())) { @@ -255,12 +255,12 @@ protected void doConnectedRun() throws IOException, ApiException { lightStatusListener.onLightRemoved(); } - if (discovery != null) { + if (discovery != null && light != null) { discovery.removeLightDiscovery(light); } }); - Map lastGroupStateCopy = new HashMap<>(lastGroupStates); + Map lastGroupStateCopy = new HashMap<>(lastGroupStates); for (final FullGroup fullGroup : hueBridge.getGroups()) { State groupState = new State(); @@ -339,7 +339,7 @@ protected void doConnectedRun() throws IOException, ApiException { groupStatusListener.onGroupRemoved(); } - if (discovery != null) { + if (discovery != null && group != null) { discovery.removeGroupDiscovery(group); } }); @@ -917,7 +917,7 @@ public void startSearch(List serialNumbers) { }); } - private T withReAuthentication(String taskDescription, Callable runnable) { + private @Nullable T withReAuthentication(String taskDescription, Callable runnable) { if (hueBridge != null) { try { try { @@ -941,7 +941,8 @@ public Collection getConfigStatus() { Collection configStatusMessages; // Check whether an IP address is provided - if (hueBridgeConfig.getIpAddress() == null || hueBridgeConfig.getIpAddress().isEmpty()) { + String ip = hueBridgeConfig.getIpAddress(); + if (ip == null || ip.isEmpty()) { configStatusMessages = Collections.singletonList(ConfigStatusMessage.Builder.error(HOST) .withMessageKeySuffix(HueConfigStatusMessage.IP_ADDRESS_MISSING).withArguments(HOST).build()); } else { diff --git a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueGroupHandler.java b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueGroupHandler.java index d176e6a1bc994..9aaffa12dfd04 100644 --- a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueGroupHandler.java +++ b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueGroupHandler.java @@ -135,8 +135,9 @@ public void dispose() { } ThingHandler handler = bridge.getHandler(); if (handler instanceof HueBridgeHandler) { - hueClient = (HueClient) handler; - hueClient.registerGroupStatusListener(this); + HueClient bridgeHandler = (HueClient) handler; + hueClient = bridgeHandler; + bridgeHandler.registerGroupStatusListener(this); } else { return null; } @@ -164,6 +165,7 @@ public void handleCommand(String channel, Command command, long fadeTime) { return; } + Integer lastColorTemp; StateUpdate groupState = null; switch (channel) { case CHANNEL_COLOR: @@ -174,15 +176,11 @@ public void handleCommand(String channel, Command command, long fadeTime) { } else { groupState = LightStateConverter.toColorLightState(hsbCommand, group.getState()); groupState.setOn(true); - if (groupState != null) { - groupState.setTransitionTime(fadeTime); - } + groupState.setTransitionTime(fadeTime); } } else if (command instanceof PercentType) { groupState = LightStateConverter.toBrightnessLightState((PercentType) command); - if (groupState != null) { - groupState.setTransitionTime(fadeTime); - } + groupState.setTransitionTime(fadeTime); } else if (command instanceof OnOffType) { groupState = LightStateConverter.toOnOffLightState((OnOffType) command); } else if (command instanceof IncreaseDecreaseType) { @@ -195,9 +193,7 @@ public void handleCommand(String channel, Command command, long fadeTime) { case CHANNEL_COLORTEMPERATURE: if (command instanceof PercentType) { groupState = LightStateConverter.toColorTemperatureLightState((PercentType) command); - if (groupState != null) { - groupState.setTransitionTime(fadeTime); - } + groupState.setTransitionTime(fadeTime); } else if (command instanceof OnOffType) { groupState = LightStateConverter.toOnOffLightState((OnOffType) command); } else if (command instanceof IncreaseDecreaseType) { @@ -210,9 +206,7 @@ public void handleCommand(String channel, Command command, long fadeTime) { case CHANNEL_BRIGHTNESS: if (command instanceof PercentType) { groupState = LightStateConverter.toBrightnessLightState((PercentType) command); - if (groupState != null) { - groupState.setTransitionTime(fadeTime); - } + groupState.setTransitionTime(fadeTime); } else if (command instanceof OnOffType) { groupState = LightStateConverter.toOnOffLightState((OnOffType) command); } else if (command instanceof IncreaseDecreaseType) { @@ -221,10 +215,11 @@ public void handleCommand(String channel, Command command, long fadeTime) { groupState.setTransitionTime(fadeTime); } } - if (groupState != null && lastSentColorTemp != null) { + lastColorTemp = this.lastSentColorTemp; + if (groupState != null && lastColorTemp != null) { // make sure that the light also has the latest color temp // this might not have been yet set in the light, if it was off - groupState.setColorTemperature(lastSentColorTemp); + groupState.setColorTemperature(lastColorTemp); groupState.setTransitionTime(fadeTime); } break; @@ -232,10 +227,11 @@ public void handleCommand(String channel, Command command, long fadeTime) { if (command instanceof OnOffType) { groupState = LightStateConverter.toOnOffLightState((OnOffType) command); } - if (groupState != null && lastSentColorTemp != null) { + lastColorTemp = this.lastSentColorTemp; + if (groupState != null && lastColorTemp != null) { // make sure that the light also has the latest color temp // this might not have been yet set in the light, if it was off - groupState.setColorTemperature(lastSentColorTemp); + groupState.setColorTemperature(lastColorTemp); groupState.setTransitionTime(fadeTime); } break; @@ -431,9 +427,10 @@ private void scheduleAlertStateRestore(Command command) { * restoration. */ private void cancelScheduledFuture() { + ScheduledFuture scheduledFuture = this.scheduledFuture; if (scheduledFuture != null) { scheduledFuture.cancel(true); - scheduledFuture = null; + this.scheduledFuture = null; } } diff --git a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueLightHandler.java b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueLightHandler.java index a376766040208..295d41b58dd04 100644 --- a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueLightHandler.java +++ b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueLightHandler.java @@ -112,8 +112,7 @@ public class HueLightHandler extends BaseThingHandler implements LightStatusList private @Nullable HueClient hueClient; - @Nullable - ScheduledFuture scheduledFuture; + private @Nullable ScheduledFuture scheduledFuture; public HueLightHandler(Thing hueLight) { super(hueLight); @@ -231,14 +230,13 @@ public void handleCommand(String channel, Command command, long fadeTime) { return; } + Integer lastColorTemp; StateUpdate lightState = null; switch (channel) { case CHANNEL_COLORTEMPERATURE: if (command instanceof PercentType) { lightState = LightStateConverter.toColorTemperatureLightState((PercentType) command); - if (lightState != null) { - lightState.setTransitionTime(fadeTime); - } + lightState.setTransitionTime(fadeTime); } else if (command instanceof OnOffType) { lightState = LightStateConverter.toOnOffLightState((OnOffType) command); if (isOsramPar16) { @@ -255,9 +253,7 @@ public void handleCommand(String channel, Command command, long fadeTime) { case CHANNEL_BRIGHTNESS: if (command instanceof PercentType) { lightState = LightStateConverter.toBrightnessLightState((PercentType) command); - if (lightState != null) { - lightState.setTransitionTime(fadeTime); - } + lightState.setTransitionTime(fadeTime); } else if (command instanceof OnOffType) { lightState = LightStateConverter.toOnOffLightState((OnOffType) command); if (isOsramPar16) { @@ -269,10 +265,11 @@ public void handleCommand(String channel, Command command, long fadeTime) { lightState.setTransitionTime(fadeTime); } } - if (lightState != null && lastSentColorTemp != null) { + lastColorTemp = this.lastSentColorTemp; + if (lightState != null && lastColorTemp != null) { // make sure that the light also has the latest color temp // this might not have been yet set in the light, if it was off - lightState.setColorTemperature(lastSentColorTemp); + lightState.setColorTemperature(lastColorTemp); lightState.setTransitionTime(fadeTime); } break; @@ -284,10 +281,11 @@ public void handleCommand(String channel, Command command, long fadeTime) { lightState = addOsramSpecificCommands(lightState, (OnOffType) command); } } - if (lightState != null && lastSentColorTemp != null) { + lastColorTemp = this.lastSentColorTemp; + if (lightState != null && lastColorTemp != null) { // make sure that the light also has the latest color temp // this might not have been yet set in the light, if it was off - lightState.setColorTemperature(lastSentColorTemp); + lightState.setColorTemperature(lastColorTemp); lightState.setTransitionTime(fadeTime); } break; @@ -298,15 +296,11 @@ public void handleCommand(String channel, Command command, long fadeTime) { lightState = LightStateConverter.toOnOffLightState(OnOffType.OFF); } else { lightState = LightStateConverter.toColorLightState(hsbCommand, light.getState()); - if (lightState != null) { - lightState.setTransitionTime(fadeTime); - } + lightState.setTransitionTime(fadeTime); } } else if (command instanceof PercentType) { lightState = LightStateConverter.toBrightnessLightState((PercentType) command); - if (lightState != null) { - lightState.setTransitionTime(fadeTime); - } + lightState.setTransitionTime(fadeTime); } else if (command instanceof OnOffType) { lightState = LightStateConverter.toOnOffLightState((OnOffType) command); } else if (command instanceof IncreaseDecreaseType) { @@ -428,8 +422,9 @@ private StateUpdate createBrightnessStateUpdate(int currentBrightness, int newBr } ThingHandler handler = bridge.getHandler(); if (handler instanceof HueClient) { - hueClient = (HueClient) handler; - hueClient.registerLightStatusListener(this); + HueClient bridgeHandler = (HueClient) handler; + hueClient = bridgeHandler; + bridgeHandler.registerLightStatusListener(this); } else { return null; } @@ -576,9 +571,10 @@ private void scheduleAlertStateRestore(Command command) { * restoration. */ private void cancelScheduledFuture() { + ScheduledFuture scheduledFuture = this.scheduledFuture; if (scheduledFuture != null) { scheduledFuture.cancel(true); - scheduledFuture = null; + this.scheduledFuture = null; } } diff --git a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueSensorHandler.java b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueSensorHandler.java index d8b876a51b4ff..e58673834de5a 100644 --- a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueSensorHandler.java +++ b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueSensorHandler.java @@ -149,8 +149,9 @@ public void dispose() { } ThingHandler handler = bridge.getHandler(); if (handler instanceof HueBridgeHandler) { - hueClient = (HueClient) handler; - hueClient.registerSensorStatusListener(this); + HueClient bridgeHandler = (HueClient) handler; + hueClient = bridgeHandler; + bridgeHandler.registerSensorStatusListener(this); } else { return null; }