Skip to content

Commit

Permalink
Various small enhancements (openhab#11874)
Browse files Browse the repository at this point in the history
- 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 <kai.kreuzer@telekom.de>
Signed-off-by: Andras Uhrin <andras.uhrin@gmail.com>
  • Loading branch information
kaikreuzer authored and andrasU committed Nov 12, 2022
1 parent 71b8ade commit 3363843
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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())) {
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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)));
Expand All @@ -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<String, String> 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 -> {
Expand All @@ -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<String, String> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, ""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3363843

Please sign in to comment.