[nanoleaf] Removed dependency on org.apache.commons (#9811)

* Removed dependency on org.apache.commons
* Incorporated changes from review

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
Christoph Weitkamp 2021-01-14 01:40:11 +01:00 committed by GitHub
parent 3f85b32502
commit b53c61b1e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -27,7 +27,6 @@ import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.HttpClient;
@ -131,31 +130,31 @@ public class NanoleafControllerHandler extends BaseBridgeHandler {
setRefreshIntervall(config.refreshInterval); setRefreshIntervall(config.refreshInterval);
setAuthToken(config.authToken); setAuthToken(config.authToken);
@Nullable Map<String, String> properties = getThing().getProperties();
String property = getThing().getProperties().get(Thing.PROPERTY_MODEL_ID); String propertyModelId = properties.get(Thing.PROPERTY_MODEL_ID);
if (MODEL_ID_CANVAS.equals(property)) { if (MODEL_ID_CANVAS.equals(propertyModelId)) {
config.deviceType = DEVICE_TYPE_CANVAS; config.deviceType = DEVICE_TYPE_CANVAS;
} else { } else {
config.deviceType = DEVICE_TYPE_LIGHTPANELS; config.deviceType = DEVICE_TYPE_LIGHTPANELS;
} }
setDeviceType(config.deviceType); setDeviceType(config.deviceType);
String propertyFirmwareVersion = properties.get(Thing.PROPERTY_FIRMWARE_VERSION);
try { try {
Map<String, String> properties = getThing().getProperties(); if (config.address.isEmpty() || String.valueOf(config.port).isEmpty()) {
if (StringUtils.isEmpty(getAddress()) || StringUtils.isEmpty(String.valueOf(getPort()))) {
logger.warn("No IP address and port configured for the Nanoleaf controller"); logger.warn("No IP address and port configured for the Nanoleaf controller");
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING, updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING,
"@text/error.nanoleaf.controller.noIp"); "@text/error.nanoleaf.controller.noIp");
stopAllJobs(); stopAllJobs();
} else if (!StringUtils.isEmpty(properties.get(Thing.PROPERTY_FIRMWARE_VERSION)) } else if (propertyFirmwareVersion != null && !propertyFirmwareVersion.isEmpty() && !OpenAPIUtils
&& !OpenAPIUtils.checkRequiredFirmware(properties.get(Thing.PROPERTY_MODEL_ID), .checkRequiredFirmware(properties.get(Thing.PROPERTY_MODEL_ID), propertyFirmwareVersion)) {
properties.get(Thing.PROPERTY_FIRMWARE_VERSION))) {
logger.warn("Nanoleaf controller firmware is too old: {}. Must be equal or higher than {}", logger.warn("Nanoleaf controller firmware is too old: {}. Must be equal or higher than {}",
properties.get(Thing.PROPERTY_FIRMWARE_VERSION), API_MIN_FW_VER_LIGHTPANELS); propertyFirmwareVersion, API_MIN_FW_VER_LIGHTPANELS);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"@text/error.nanoleaf.controller.incompatibleFirmware"); "@text/error.nanoleaf.controller.incompatibleFirmware");
stopAllJobs(); stopAllJobs();
} else if (StringUtils.isEmpty(getAuthToken())) { } else if (config.authToken == null || config.authToken.isEmpty()) {
logger.debug("No token found. Start pairing background job"); logger.debug("No token found. Start pairing background job");
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING, updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING,
"@text/error.nanoleaf.controller.noToken"); "@text/error.nanoleaf.controller.noToken");
@ -270,11 +269,11 @@ public class NanoleafControllerHandler extends BaseBridgeHandler {
public NanoleafControllerConfig getControllerConfig() { public NanoleafControllerConfig getControllerConfig() {
NanoleafControllerConfig config = new NanoleafControllerConfig(); NanoleafControllerConfig config = new NanoleafControllerConfig();
config.address = this.getAddress(); config.address = Objects.requireNonNullElse(getAddress(), "");
config.port = this.getPort(); config.port = getPort();
config.refreshInterval = this.getRefreshIntervall(); config.refreshInterval = getRefreshIntervall();
config.authToken = this.getAuthToken(); config.authToken = getAuthToken();
config.deviceType = this.getDeviceType(); config.deviceType = Objects.requireNonNullElse(getDeviceType(), "");
return config; return config;
} }
@ -294,7 +293,8 @@ public class NanoleafControllerHandler extends BaseBridgeHandler {
} }
private synchronized void startUpdateJob() { private synchronized void startUpdateJob() {
if (StringUtils.isNotEmpty(getAuthToken())) { String localAuthToken = getAuthToken();
if (localAuthToken != null && !localAuthToken.isEmpty()) {
if (updateJob == null || updateJob.isCancelled()) { if (updateJob == null || updateJob.isCancelled()) {
logger.debug("Start controller status job, repeat every {} sec", getRefreshIntervall()); logger.debug("Start controller status job, repeat every {} sec", getRefreshIntervall());
updateJob = scheduler.scheduleWithFixedDelay(this::runUpdate, 0, getRefreshIntervall(), updateJob = scheduler.scheduleWithFixedDelay(this::runUpdate, 0, getRefreshIntervall(),
@ -342,7 +342,8 @@ public class NanoleafControllerHandler extends BaseBridgeHandler {
logger.debug("Starting TouchJob for Panel {}", this.getThing().getUID()); logger.debug("Starting TouchJob for Panel {}", this.getThing().getUID());
} }
if (StringUtils.isNotEmpty(getAuthToken())) { String localAuthToken = getAuthToken();
if (localAuthToken != null && !localAuthToken.isEmpty()) {
if (touchJob == null || touchJob.isCancelled()) { if (touchJob == null || touchJob.isCancelled()) {
logger.debug("Starting Touchjob now"); logger.debug("Starting Touchjob now");
touchJob = scheduler.schedule(this::runTouchDetection, 0, TimeUnit.SECONDS); touchJob = scheduler.schedule(this::runTouchDetection, 0, TimeUnit.SECONDS);
@ -374,7 +375,8 @@ public class NanoleafControllerHandler extends BaseBridgeHandler {
logger.warn("Status update unauthorized: {}", nae.getMessage()); logger.warn("Status update unauthorized: {}", nae.getMessage());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/error.nanoleaf.controller.invalidToken"); "@text/error.nanoleaf.controller.invalidToken");
if (StringUtils.isEmpty(getAuthToken())) { String localAuthToken = getAuthToken();
if (localAuthToken == null || localAuthToken.isEmpty()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING, updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING,
"@text/error.nanoleaf.controller.noToken"); "@text/error.nanoleaf.controller.noToken");
} }
@ -391,7 +393,8 @@ public class NanoleafControllerHandler extends BaseBridgeHandler {
private void runPairing() { private void runPairing() {
logger.debug("Run pairing job"); logger.debug("Run pairing job");
try { try {
if (StringUtils.isNotEmpty(getAuthToken())) { String localAuthToken = getAuthToken();
if (localAuthToken != null && !localAuthToken.isEmpty()) {
if (pairingJob != null) { if (pairingJob != null) {
pairingJob.cancel(false); pairingJob.cancel(false);
} }
@ -409,20 +412,19 @@ public class NanoleafControllerHandler extends BaseBridgeHandler {
authTokenResponse.getStatus()); authTokenResponse.getStatus());
} else { } else {
// get auth token from response // get auth token from response
@Nullable AuthToken authTokenObject = gson.fromJson(authTokenResponse.getContentAsString(), AuthToken.class);
AuthToken authToken = gson.fromJson(authTokenResponse.getContentAsString(), AuthToken.class); localAuthToken = authTokenObject.getAuthToken();
if (localAuthToken != null && !localAuthToken.isEmpty()) {
if (StringUtils.isNotEmpty(authToken.getAuthToken())) {
logger.debug("Pairing succeeded."); logger.debug("Pairing succeeded.");
// Update and save the auth token in the thing configuration // Update and save the auth token in the thing configuration
Configuration config = editConfiguration(); Configuration config = editConfiguration();
config.put(NanoleafControllerConfig.AUTH_TOKEN, authToken.getAuthToken()); config.put(NanoleafControllerConfig.AUTH_TOKEN, localAuthToken);
updateConfiguration(config); updateConfiguration(config);
updateStatus(ThingStatus.ONLINE); updateStatus(ThingStatus.ONLINE);
// Update local field // Update local field
setAuthToken(authToken.getAuthToken()); setAuthToken(localAuthToken);
stopPairingJob(); stopPairingJob();
startUpdateJob(); startUpdateJob();
@ -466,7 +468,8 @@ public class NanoleafControllerHandler extends BaseBridgeHandler {
logger.warn("Panel discovery unauthorized: {}", nue.getMessage()); logger.warn("Panel discovery unauthorized: {}", nue.getMessage());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/error.nanoleaf.controller.invalidToken"); "@text/error.nanoleaf.controller.invalidToken");
if (StringUtils.isEmpty(getAuthToken())) { String localAuthToken = getAuthToken();
if (localAuthToken == null || localAuthToken.isEmpty()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING, updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING,
"@text/error.nanoleaf.controller.noToken"); "@text/error.nanoleaf.controller.noToken");
} }
@ -824,8 +827,8 @@ public class NanoleafControllerHandler extends BaseBridgeHandler {
OpenAPIUtils.sendOpenAPIRequest(setNewRhythmRequest); OpenAPIUtils.sendOpenAPIRequest(setNewRhythmRequest);
} }
private String getAddress() { private @Nullable String getAddress() {
return StringUtils.defaultString(this.address); return address;
} }
private void setAddress(String address) { private void setAddress(String address) {
@ -848,16 +851,16 @@ public class NanoleafControllerHandler extends BaseBridgeHandler {
this.refreshIntervall = refreshIntervall; this.refreshIntervall = refreshIntervall;
} }
private String getAuthToken() { private @Nullable String getAuthToken() {
return StringUtils.defaultString(authToken); return authToken;
} }
private void setAuthToken(@Nullable String authToken) { private void setAuthToken(@Nullable String authToken) {
this.authToken = authToken; this.authToken = authToken;
} }
private String getDeviceType() { private @Nullable String getDeviceType() {
return StringUtils.defaultString(deviceType); return deviceType;
} }
private void setDeviceType(String deviceType) { private void setDeviceType(String deviceType) {