From f6566d0c4c87d3d50a2cf1d5fb719b960840b55c Mon Sep 17 00:00:00 2001 From: Kai Kreuzer Date: Tue, 28 Dec 2021 19:27:45 +0100 Subject: [PATCH] Various small enhancements (#11874) - do not go from INITIALIZING to OFFLINE and then ONLINE, but skip OFFLINE for panels, if the controller is ONLINE - reduce logging to debug when receiving trigger events - only update Thing configuration if necessary to avoid Thing updated events - fix description of model types, which was outdated due to new models being available by now Signed-off-by: Kai Kreuzer Signed-off-by: Michael Schmidt --- .../handler/NanoleafControllerHandler.java | 70 ++++++++++++------- .../handler/NanoleafPanelHandler.java | 1 - .../resources/OH-INF/i18n/nanoleaf.properties | 4 +- .../OH-INF/i18n/nanoleaf_de.properties | 4 +- 4 files changed, 47 insertions(+), 32 deletions(-) diff --git a/bundles/org.openhab.binding.nanoleaf/src/main/java/org/openhab/binding/nanoleaf/internal/handler/NanoleafControllerHandler.java b/bundles/org.openhab.binding.nanoleaf/src/main/java/org/openhab/binding/nanoleaf/internal/handler/NanoleafControllerHandler.java index 0ab54cc9a41ad..f72ff5ba8305f 100644 --- a/bundles/org.openhab.binding.nanoleaf/src/main/java/org/openhab/binding/nanoleaf/internal/handler/NanoleafControllerHandler.java +++ b/bundles/org.openhab.binding.nanoleaf/src/main/java/org/openhab/binding/nanoleaf/internal/handler/NanoleafControllerHandler.java @@ -144,6 +144,7 @@ private void initializeTouchHttpClient() { logger.debug("Using long SSE httpClient={} for {}}", httpClientSSETouchEvent, httpClientName); } + @Override public void initialize() { logger.debug("Initializing the controller (bridge)"); updateStatus(ThingStatus.UNKNOWN); @@ -199,6 +200,7 @@ public void initialize() { } } + @Override public void handleCommand(ChannelUID channelUID, Command command) { logger.debug("Received command {} for channel {}", command, channelUID); if (!ThingStatus.ONLINE.equals(getThing().getStatusInfo().getStatus())) { @@ -565,7 +567,7 @@ private synchronized void runTouchDetection() { private void handleTouchEvents(TouchEvents touchEvents) { touchEvents.getEvents().forEach((event) -> { - logger.info("panel: {} gesture id: {}", event.getPanelId(), event.getGesture()); + logger.debug("panel: {} gesture id: {}", event.getPanelId(), event.getGesture()); // Swipes go to the controller, taps go to the individual panel if (event.getPanelId().equals(CONTROLLER_PANEL_ID)) { logger.debug("Triggering controller {} with gesture {}.", thing.getUID(), event.getGesture()); @@ -628,8 +630,8 @@ private void updateFromControllerInfo() throws NanoleafException { hue = min == null ? 0 : min; Integer max = colorTemperature.getMax(); saturation = max == null ? 0 : max; - colorTempPercent = (float) ((colorTemperature.getValue() - hue) / (saturation - hue) - * PercentType.HUNDRED.intValue()); + colorTempPercent = (colorTemperature.getValue() - hue) / (saturation - hue) + * PercentType.HUNDRED.intValue(); } updateState(CHANNEL_COLOR_TEMPERATURE, new PercentType(Float.toString(colorTempPercent))); @@ -650,30 +652,6 @@ private void updateFromControllerInfo() throws NanoleafException { updateState(CHANNEL_RHYTHM_MODE, new DecimalType(controllerInfo.getRhythm().getRhythmMode())); updateState(CHANNEL_RHYTHM_STATE, controllerInfo.getRhythm().getRhythmConnected() ? OnOffType.ON : OnOffType.OFF); - // update bridge properties which may have changed, or are not present during discovery - Map properties = editProperties(); - properties.put(Thing.PROPERTY_SERIAL_NUMBER, controllerInfo.getSerialNo()); - properties.put(Thing.PROPERTY_FIRMWARE_VERSION, controllerInfo.getFirmwareVersion()); - properties.put(Thing.PROPERTY_MODEL_ID, controllerInfo.getModel()); - properties.put(Thing.PROPERTY_VENDOR, controllerInfo.getManufacturer()); - updateProperties(properties); - Configuration config = editConfiguration(); - if (hasTouchSupport(controllerInfo.getModel())) { - config.put(NanoleafControllerConfig.DEVICE_TYPE, DEVICE_TYPE_TOUCHSUPPORT); - logger.debug("Set to device type {}", DEVICE_TYPE_TOUCHSUPPORT); - } else { - config.put(NanoleafControllerConfig.DEVICE_TYPE, DEVICE_TYPE_LIGHTPANELS); - logger.debug("Set to device type {}", DEVICE_TYPE_LIGHTPANELS); - } - updateConfiguration(config); - - getConfig().getProperties().forEach((key, value) -> { - logger.trace("Configuration property: key {} value {}", key, value); - }); - - getThing().getProperties().forEach((key, value) -> { - logger.debug("Thing property: key {} value {}", key, value); - }); // update the color channels of each panel getThing().getThings().forEach(child -> { @@ -684,11 +662,49 @@ private void updateFromControllerInfo() throws NanoleafException { } }); + updateProperties(); + updateConfiguration(); + for (NanoleafControllerListener controllerListener : controllerListeners) { controllerListener.onControllerInfoFetched(getThing().getUID(), controllerInfo); } } + private void updateConfiguration() { + // only update the Thing config if value isn't set yet + if (getConfig().get(NanoleafControllerConfig.DEVICE_TYPE) == null) { + Configuration config = editConfiguration(); + if (hasTouchSupport(controllerInfo.getModel())) { + config.put(NanoleafControllerConfig.DEVICE_TYPE, DEVICE_TYPE_TOUCHSUPPORT); + logger.debug("Set to device type {}", DEVICE_TYPE_TOUCHSUPPORT); + } else { + config.put(NanoleafControllerConfig.DEVICE_TYPE, DEVICE_TYPE_LIGHTPANELS); + logger.debug("Set to device type {}", DEVICE_TYPE_LIGHTPANELS); + } + updateConfiguration(config); + if (logger.isTraceEnabled()) { + getConfig().getProperties().forEach((key, value) -> { + logger.trace("Configuration property: key {} value {}", key, value); + }); + } + } + } + + private void updateProperties() { + // update bridge properties which may have changed, or are not present during discovery + Map properties = editProperties(); + properties.put(Thing.PROPERTY_SERIAL_NUMBER, controllerInfo.getSerialNo()); + properties.put(Thing.PROPERTY_FIRMWARE_VERSION, controllerInfo.getFirmwareVersion()); + properties.put(Thing.PROPERTY_MODEL_ID, controllerInfo.getModel()); + properties.put(Thing.PROPERTY_VENDOR, controllerInfo.getManufacturer()); + updateProperties(properties); + if (logger.isTraceEnabled()) { + getThing().getProperties().forEach((key, value) -> { + logger.trace("Thing property: key {} value {}", key, value); + }); + } + } + private ControllerInfo receiveControllerInfo() throws NanoleafException, NanoleafUnauthorizedException { ContentResponse controllerlInfoJSON = OpenAPIUtils.sendOpenAPIRequest(OpenAPIUtils.requestBuilder(httpClient, getControllerConfig(), API_GET_CONTROLLER_INFO, HttpMethod.GET)); diff --git a/bundles/org.openhab.binding.nanoleaf/src/main/java/org/openhab/binding/nanoleaf/internal/handler/NanoleafPanelHandler.java b/bundles/org.openhab.binding.nanoleaf/src/main/java/org/openhab/binding/nanoleaf/internal/handler/NanoleafPanelHandler.java index 020f55f57b7b2..37dffd6159d8e 100644 --- a/bundles/org.openhab.binding.nanoleaf/src/main/java/org/openhab/binding/nanoleaf/internal/handler/NanoleafPanelHandler.java +++ b/bundles/org.openhab.binding.nanoleaf/src/main/java/org/openhab/binding/nanoleaf/internal/handler/NanoleafPanelHandler.java @@ -90,7 +90,6 @@ public NanoleafPanelHandler(Thing thing, HttpClientFactory httpClientFactory) { @Override public void initialize() { logger.debug("Initializing handler for panel {}", getThing().getUID()); - updateStatus(ThingStatus.OFFLINE); Bridge controller = getBridge(); if (controller == null) { initializePanel(new ThingStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED, "")); diff --git a/bundles/org.openhab.binding.nanoleaf/src/main/resources/OH-INF/i18n/nanoleaf.properties b/bundles/org.openhab.binding.nanoleaf/src/main/resources/OH-INF/i18n/nanoleaf.properties index bbd4ea80963b1..01abe9afac3ed 100644 --- a/bundles/org.openhab.binding.nanoleaf/src/main/resources/OH-INF/i18n/nanoleaf.properties +++ b/bundles/org.openhab.binding.nanoleaf/src/main/resources/OH-INF/i18n/nanoleaf.properties @@ -17,8 +17,8 @@ thing-type.config.nanoleaf.controller.authToken.description = Authorization toke thing-type.config.nanoleaf.controller.refreshInterval.label = Refresh Interval thing-type.config.nanoleaf.controller.refreshInterval.description = Interval (in seconds) to refresh the controller channels status thing-type.config.nanoleaf.controller.deviceType.label = Nanoleaf Device Type -thing-type.config.nanoleaf.controller.deviceType.description = Light Panels (triangles) or Canvas (squares) -thing-type.config.nanoleaf.lightpanel.id.label = ID Of The Panel +thing-type.config.nanoleaf.controller.deviceType.description = Light Panels (older triangle models) or Canvas/Shapes (newer models as squares, triangles, hexagons, etc.) +thing-type.config.nanoleaf.lightpanel.id.label = Panel ID thing-type.config.nanoleaf.lightpanel.id.description = The ID of the panel assigned by the controller # channel types diff --git a/bundles/org.openhab.binding.nanoleaf/src/main/resources/OH-INF/i18n/nanoleaf_de.properties b/bundles/org.openhab.binding.nanoleaf/src/main/resources/OH-INF/i18n/nanoleaf_de.properties index 3612a5858b4bc..37da0e06e1fbf 100644 --- a/bundles/org.openhab.binding.nanoleaf/src/main/resources/OH-INF/i18n/nanoleaf_de.properties +++ b/bundles/org.openhab.binding.nanoleaf/src/main/resources/OH-INF/i18n/nanoleaf_de.properties @@ -17,8 +17,8 @@ thing-type.config.nanoleaf.controller.authToken.description = Token zur Authenti thing-type.config.nanoleaf.controller.refreshInterval.label = Aktualisierungsintervall thing-type.config.nanoleaf.controller.refreshInterval.description = Intervall (in Sekunden) in dem die Kanäle aktualisiert werden thing-type.config.nanoleaf.controller.deviceType.label = Nanoleaf Gerätetyp -thing-type.config.nanoleaf.controller.deviceType.description = Light Panels (Dreiecke) oder Canvas (Quadrate) -thing-type.config.nanoleaf.lightpanel.id.label = Paneel ID +thing-type.config.nanoleaf.controller.deviceType.description = Light Panels (alte Dreieck-Modelle) oder Canvas/Shapes (neue Modelle als Quadrat, Dreieck, Hexagon, etc.) +thing-type.config.nanoleaf.lightpanel.id.label = Panel ID thing-type.config.nanoleaf.lightpanel.id.description = Vom Controller vergebene ID eines Paneels # channel types