[neeo] improve code (#9028)

* create forwardactionsservlet only if forwardchain is not empty
* remove dependency on apache commons

Signed-off-by: Jan N. Klug <jan.n.klug@rub.de>
This commit is contained in:
J-N-K 2020-11-18 01:25:46 +01:00 committed by GitHub
parent 158cbe0c65
commit 6e3c640043
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 134 additions and 167 deletions

View File

@ -13,7 +13,10 @@
package org.openhab.binding.neeo.internal;
import java.io.IOException;
import java.lang.reflect.Type;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
@ -31,6 +34,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
/**
* The class provides the API for communicating with a NEEO brain
@ -162,7 +166,7 @@ public class NeeoBrainApi implements AutoCloseable {
* @return the non-null, possibly empty list of active scenarios keys
* @throws IOException Signals that an I/O exception has occurred.
*/
public String[] getActiveScenarios() throws IOException {
public List<String> getActiveScenarios() throws IOException {
final String url = urlBuilder.append(NeeoConstants.GET_ACTIVESCENARIOS).toString();
final HttpRequest rqst = request.get();
@ -171,7 +175,9 @@ public class NeeoBrainApi implements AutoCloseable {
throw resp.createException();
}
return Objects.requireNonNull(gson.fromJson(resp.getContent(), String[].class));
Type arrayListType = new TypeToken<ArrayList<String>>() {
}.getType();
return Objects.requireNonNull(gson.fromJson(resp.getContent(), arrayListType));
}
/**

View File

@ -132,4 +132,11 @@ public class NeeoBrainConfig {
public void setCheckStatusInterval(int checkStatusInterval) {
this.checkStatusInterval = checkStatusInterval;
}
@Override
public String toString() {
return "NeeoBrainConfig{" + "ipAddress='" + ipAddress + '\'' + ", enableForwardActions=" + enableForwardActions
+ ", forwardChain='" + forwardChain + '\'' + ", discoverEmptyRooms=" + discoverEmptyRooms
+ ", checkStatusInterval=" + checkStatusInterval + '}';
}
}

View File

@ -13,11 +13,11 @@
package org.openhab.binding.neeo.internal;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.neeo.internal.models.ExecuteResult;
@ -53,7 +53,7 @@ public class NeeoRoomProtocol {
private final NeeoRoom neeoRoom;
/** The currently active scenarios */
private final AtomicReference<String[]> activeScenarios = new AtomicReference<>(new String[0]);
private final AtomicReference<List<String>> activeScenarios = new AtomicReference<>(new ArrayList<>());
/**
* Instantiates a new neeo room protocol.
@ -95,8 +95,8 @@ public class NeeoRoomProtocol {
Objects.requireNonNull(action, "action cannot be null");
final NeeoRecipes recipes = neeoRoom.getRecipes();
final boolean launch = StringUtils.equalsIgnoreCase(NeeoRecipe.LAUNCH, action.getAction());
final boolean poweroff = StringUtils.equalsIgnoreCase(NeeoRecipe.POWEROFF, action.getAction());
final boolean launch = NeeoRecipe.LAUNCH.equalsIgnoreCase(action.getAction());
final boolean poweroff = NeeoRecipe.POWEROFF.equalsIgnoreCase(action.getAction());
// Can't be both true but if both false - it's neither one
if (launch == poweroff) {
@ -107,7 +107,7 @@ public class NeeoRoomProtocol {
final NeeoRecipe recipe = recipeName == null ? null : recipes.getRecipeByName(recipeName);
final String scenarioKey = recipe == null ? null : recipe.getScenarioKey();
if (scenarioKey != null && StringUtils.isNotEmpty(scenarioKey)) {
if (scenarioKey != null && !scenarioKey.isEmpty()) {
processScenarioChange(scenarioKey, launch);
} else {
logger.debug("Could not find a recipe named '{}' for the action {}", recipeName, action);
@ -123,18 +123,27 @@ public class NeeoRoomProtocol {
private void processScenarioChange(String scenarioKey, boolean launch) {
NeeoUtil.requireNotEmpty(scenarioKey, "scenarioKey cannot be empty");
final String[] activeScenarios = this.activeScenarios.get();
final int idx = ArrayUtils.indexOf(activeScenarios, scenarioKey);
List<String> oldActiveScenarios;
List<String> newActiveScenarios;
// already set that way
if ((idx < 0 && !launch) || (idx >= 0 && launch)) {
return;
}
do {
oldActiveScenarios = this.activeScenarios.get();
newActiveScenarios = new ArrayList<>(oldActiveScenarios);
final String[] newScenarios = idx >= 0 ? (String[]) ArrayUtils.remove(activeScenarios, idx)
: (String[]) ArrayUtils.add(activeScenarios, scenarioKey);
this.activeScenarios.set(newScenarios);
if (newActiveScenarios.contains(scenarioKey)) {
if (launch) {
return;
} else {
newActiveScenarios.remove(scenarioKey);
}
} else {
if (launch) {
newActiveScenarios.add(scenarioKey);
} else {
return;
}
}
} while (!this.activeScenarios.compareAndSet(oldActiveScenarios, newActiveScenarios));
refreshScenarioStatus(scenarioKey);
}
@ -247,10 +256,9 @@ public class NeeoRoomProtocol {
final NeeoScenario scenario = neeoRoom.getScenarios().getScenario(scenarioKey);
if (scenario != null) {
final String[] active = activeScenarios.get();
final boolean isActive = ArrayUtils.contains(active, scenarioKey);
final boolean isActive = activeScenarios.get().contains(scenarioKey);
callback.stateChanged(UidUtils.createChannelId(NeeoConstants.ROOM_GROUP_SCENARIO_ID,
NeeoConstants.ROOM_CHANNEL_STATUS, scenarioKey), isActive ? OnOffType.ON : OnOffType.OFF);
NeeoConstants.ROOM_CHANNEL_STATUS, scenarioKey), OnOffType.from(isActive));
}
}
@ -263,19 +271,13 @@ public class NeeoRoomProtocol {
logger.debug("API is null [likely bridge is offline]");
} else {
try {
final String[] activeScenarios = api.getActiveScenarios();
final String[] oldScenarios = this.activeScenarios.getAndSet(activeScenarios);
final List<String> activeScenarios = api.getActiveScenarios();
final List<String> oldScenarios = this.activeScenarios.getAndSet(activeScenarios);
if (!ArrayUtils.isEquals(activeScenarios, oldScenarios)) {
for (String scenario : activeScenarios) {
refreshScenarioStatus(scenario);
}
for (String oldScenario : oldScenarios) {
if (!ArrayUtils.contains(activeScenarios, oldScenario)) {
refreshScenarioStatus(oldScenario);
}
}
if (!activeScenarios.equals(oldScenarios)) {
activeScenarios.forEach(this::refreshScenarioStatus);
oldScenarios.removeIf(activeScenarios::contains);
oldScenarios.forEach(this::refreshScenarioStatus);
}
} catch (IOException e) {
logger.debug("Exception requesting active scenarios: {}", e.getMessage(), e);
@ -292,7 +294,7 @@ public class NeeoRoomProtocol {
private void sendCurrentStepTrigger(@Nullable String step) {
callback.triggerEvent(
UidUtils.createChannelId(NeeoConstants.ROOM_GROUP_STATE_ID, NeeoConstants.ROOM_CHANNEL_CURRENTSTEP),
step == null || StringUtils.isEmpty(step) ? "" : step);
step == null || step.isEmpty() ? "" : step);
}
/**
@ -312,10 +314,10 @@ public class NeeoRoomProtocol {
if (recipe != null) {
if (recipe.isEnabled()) {
final boolean isLaunch = StringUtils.equalsIgnoreCase(NeeoRecipe.LAUNCH, recipe.getType());
final boolean isLaunch = NeeoRecipe.LAUNCH.equalsIgnoreCase(recipe.getType());
try {
if (isLaunch || scenarioKey == null || StringUtils.isEmpty(scenarioKey)) {
if (isLaunch || scenarioKey == null || scenarioKey.isEmpty()) {
handleExecuteResult(scenarioKey, recipeKey, true, api.executeRecipe(roomKey, recipeKey));
} else {
handleExecuteResult(scenarioKey, recipeKey, false, api.stopScenario(roomKey, scenarioKey));
@ -345,7 +347,7 @@ public class NeeoRoomProtocol {
start ? NeeoRecipe.LAUNCH : NeeoRecipe.POWEROFF);
final String recipeKey = recipe == null ? null : recipe.getKey();
if (recipe != null && recipeKey != null && StringUtils.isNotEmpty(recipeKey)) {
if (recipe != null && recipeKey != null && !recipeKey.isEmpty()) {
if (recipe.isEnabled()) {
startRecipe(recipeKey);
} else {
@ -370,7 +372,7 @@ public class NeeoRoomProtocol {
NeeoUtil.requireNotEmpty(recipeKey, "recipeKey cannot be empty");
int nextStep = 0;
if (scenarioKey != null && StringUtils.isNotEmpty(scenarioKey)) {
if (scenarioKey != null && !scenarioKey.isEmpty()) {
callback.scheduleTask(() -> {
processScenarioChange(scenarioKey, launch);
}, 1);

View File

@ -12,10 +12,8 @@
*/
package org.openhab.binding.neeo.internal;
import java.util.Objects;
import java.util.concurrent.Future;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.neeo.internal.models.NeeoDevices;
@ -104,8 +102,7 @@ public class NeeoUtil {
* @throws IllegalArgumentException if value is an empty string
*/
public static void requireNotEmpty(@Nullable String value, String msg) {
Objects.requireNonNull(value, msg);
if (StringUtils.isEmpty(value)) {
if (value == null || value.isEmpty()) {
throw new IllegalArgumentException(msg);
}
}

View File

@ -12,9 +12,6 @@
*/
package org.openhab.binding.neeo.internal;
import java.util.Objects;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.neeo.internal.models.NeeoDevice;
@ -66,8 +63,6 @@ public class UidUtils {
* @return the non-null, non empty (only 1 or 2 element) list of parts
*/
public static String[] parseChannelId(ChannelUID uid) {
Objects.requireNonNull(uid, "uid cannot be null");
final String channelId = uid.getIdWithoutGroup();
final int idx = channelId.indexOf(DELIMITER);
if (idx < 0 || idx == channelId.length() - 1) {
@ -101,8 +96,8 @@ public class UidUtils {
public static String createChannelId(@Nullable String groupId, String channelId, @Nullable String channelKey) {
NeeoUtil.requireNotEmpty(channelId, "channelId cannot be empty");
return (StringUtils.isEmpty(groupId) ? "" : (groupId + "#"))
+ (StringUtils.isEmpty(channelKey) ? channelId : (channelId + DELIMITER + channelKey));
return ((groupId == null || groupId.isEmpty()) ? "" : (groupId + "#"))
+ ((channelKey == null || channelKey.isEmpty()) ? channelId : (channelId + DELIMITER + channelKey));
}
/**
@ -129,6 +124,6 @@ public class UidUtils {
public static ChannelUID createChannelUID(ThingUID thingUid, String groupId, String channelId,
@Nullable String channelKey) {
return new ChannelUID(thingUid, groupId,
channelId + (StringUtils.isEmpty(channelKey) ? "" : (DELIMITER + channelKey)));
channelId + ((channelKey == null || channelKey.isEmpty()) ? "" : (DELIMITER + channelKey)));
}
}

View File

@ -24,7 +24,6 @@ import java.util.Set;
import javax.jmdns.ServiceInfo;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.neeo.internal.NeeoConstants;
@ -100,7 +99,7 @@ public class NeeoBrainDiscovery implements MDNSDiscoveryParticipant {
}
logger.debug("getThingUID is evaluating: {}", service);
if (!StringUtils.equals("neeo", service.getApplication())) {
if (!"neeo".equals(service.getApplication())) {
logger.debug("Application not 'neeo' in MDNS serviceinfo: {}", service);
return null;
}

View File

@ -17,7 +17,6 @@ import java.util.Collections;
import java.util.Objects;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.neeo.internal.NeeoBrainApi;
import org.openhab.binding.neeo.internal.NeeoConstants;
@ -73,7 +72,7 @@ public class NeeoDeviceDiscoveryService extends AbstractDiscoveryService {
final ThingUID roomUid = roomBridge.getUID();
final String brainId = roomHandler.getNeeoBrainId();
if (brainId == null || StringUtils.isEmpty(brainId)) {
if (brainId == null || brainId.isEmpty()) {
logger.debug("Unknown brain ID for roomHandler: {}", roomHandler);
return;
}
@ -86,7 +85,7 @@ public class NeeoDeviceDiscoveryService extends AbstractDiscoveryService {
final NeeoRoomConfig config = roomBridge.getConfiguration().as(NeeoRoomConfig.class);
final String roomKey = config.getRoomKey();
if (roomKey == null || StringUtils.isEmpty(roomKey)) {
if (roomKey == null || roomKey.isEmpty()) {
logger.debug("RoomKey wasn't configured for {} - skipping", brainId);
return;
}
@ -103,7 +102,7 @@ public class NeeoDeviceDiscoveryService extends AbstractDiscoveryService {
logger.debug("Room {} found, scanning {} devices in it", room.getName(), devices.length);
for (NeeoDevice device : devices) {
final String deviceKey = device.getKey();
if (deviceKey == null || StringUtils.isEmpty(deviceKey)) {
if (deviceKey == null || deviceKey.isEmpty()) {
logger.debug("Device key wasn't found for device: {}", device);
continue;
}

View File

@ -17,7 +17,6 @@ import java.util.Collections;
import java.util.Objects;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.neeo.internal.NeeoBrainApi;
import org.openhab.binding.neeo.internal.NeeoBrainConfig;
@ -91,7 +90,7 @@ public class NeeoRoomDiscoveryService extends AbstractDiscoveryService {
logger.debug("Brain {} ({}) found, scanning {} rooms in it", brain.getName(), brainId, rooms.length);
for (NeeoRoom room : rooms) {
final String roomKey = room.getKey();
if (roomKey == null || StringUtils.isEmpty(roomKey)) {
if (roomKey == null || roomKey.isEmpty()) {
logger.debug("Room didn't have a room key: {}", room);
continue;
}

View File

@ -14,9 +14,7 @@ package org.openhab.binding.neeo.internal.handler;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.neeo.internal.NeeoConstants;
import org.openhab.binding.neeo.internal.UidUtils;
@ -47,18 +45,16 @@ class ChannelUtils {
* @return a non-null but possibly empty list of {@link Channel} s
*/
static List<Channel> generateChannels(ThingUID thingUid, NeeoDevice device) {
Objects.requireNonNull(thingUid, "thingUid cannot be null");
Objects.requireNonNull(device, "device cannot be null");
final List<Channel> channels = new ArrayList<>();
for (NeeoMacro macro : device.getMacros().getMacros()) {
final String key = macro.getKey();
if (key != null && StringUtils.isNotEmpty(key)) {
final String label = StringUtils.isEmpty(macro.getName()) ? macro.getLabel() : macro.getName();
if (key != null && !key.isEmpty()) {
String name = macro.getName();
final String label = (name == null || name.isEmpty()) ? macro.getLabel() : name;
channels.add(ChannelBuilder
.create(UidUtils.createChannelUID(thingUid, NeeoConstants.DEVICE_GROUP_MACROS_ID,
NeeoConstants.DEVICE_CHANNEL_STATUS, key), "Switch")
.withLabel(label == null || StringUtils.isEmpty(label) ? key : label)
.withLabel((label == null || label.isEmpty()) ? key : label)
.withType(NeeoConstants.DEVICE_MACRO_STATUS_UID).build());
}
}
@ -74,9 +70,6 @@ class ChannelUtils {
* @return a non-null but possibly empty list of {@link Channel} s
*/
static List<Channel> generateChannels(ThingUID thingUid, NeeoRoom room) {
Objects.requireNonNull(thingUid, "thingUid cannot be null");
Objects.requireNonNull(room, "room cannot be null");
final List<Channel> channels = new ArrayList<>();
channels.addAll(generateStateChannels(thingUid));
channels.addAll(generateScenarioChannels(thingUid, room.getScenarios()));
@ -91,8 +84,6 @@ class ChannelUtils {
* @return a non-null but possibly empty list of {@link Channel} s
*/
private static List<Channel> generateStateChannels(ThingUID thingUid) {
Objects.requireNonNull(thingUid, "thingUid cannot be null");
final List<Channel> channels = new ArrayList<>();
channels.add(ChannelBuilder
.create(UidUtils.createChannelUID(thingUid, NeeoConstants.ROOM_GROUP_STATE_ID,
@ -109,20 +100,15 @@ class ChannelUtils {
* @return a non-null but possibly empty list of {@link Channel} s
*/
private static List<Channel> generateScenarioChannels(ThingUID thingUid, NeeoScenarios scenarios) {
Objects.requireNonNull(thingUid, "thingUid cannot be null");
Objects.requireNonNull(scenarios, "scenarios cannot be null");
final List<Channel> channels = new ArrayList<>();
for (NeeoScenario scenario : scenarios.getScenarios()) {
final String key = scenario.getKey();
if (key != null && StringUtils.isNotEmpty(key)) {
final String scenarioLabel = StringUtils.isEmpty(scenario.getName()) ? null : scenario.getName();
final String nameLabel = (scenarioLabel == null || StringUtils.isEmpty(scenarioLabel) ? key
: scenarioLabel) + " Name";
final String configuredLabel = (scenarioLabel == null || StringUtils.isEmpty(scenarioLabel) ? key
: scenarioLabel) + " Configured";
final String statusLabel = (scenarioLabel == null || StringUtils.isEmpty(scenarioLabel) ? key
: scenarioLabel) + " Status";
if (key != null && !key.isEmpty()) {
final String name = scenario.getName();
final String scenarioLabel = (name == null || name.isEmpty()) ? key : name;
final String nameLabel = scenarioLabel + " Name";
final String configuredLabel = scenarioLabel + " Configured";
final String statusLabel = scenarioLabel + " Status";
channels.add(ChannelBuilder
.create(UidUtils.createChannelUID(thingUid, NeeoConstants.ROOM_GROUP_SCENARIO_ID,
@ -149,22 +135,16 @@ class ChannelUtils {
* @return a non-null but possibly empty list of {@link Channel} s
*/
private static List<Channel> generateRecipeChannels(ThingUID thingUid, NeeoRecipes recipes) {
Objects.requireNonNull(thingUid, "thingUid cannot be null");
Objects.requireNonNull(recipes, "recipes cannot be null");
final List<Channel> channels = new ArrayList<>();
for (NeeoRecipe recipe : recipes.getRecipes()) {
final String key = recipe.getKey();
if (key != null && StringUtils.isNotEmpty(key)) {
final String recipeLabel = StringUtils.isEmpty(recipe.getName()) ? null : recipe.getName();
final String nameLabel = (recipeLabel == null || StringUtils.isEmpty(recipeLabel) ? key : recipeLabel)
+ " Name (" + recipe.getType() + ")";
final String typeLabel = (recipeLabel == null || StringUtils.isEmpty(recipeLabel) ? key : recipeLabel)
+ " Type (" + recipe.getType() + ")";
final String enabledLabel = (recipeLabel == null || StringUtils.isEmpty(recipeLabel) ? key
: recipeLabel) + " Enabled (" + recipe.getType() + ")";
final String statusLabel = (recipeLabel == null || StringUtils.isEmpty(recipeLabel) ? key : recipeLabel)
+ " Status (" + recipe.getType() + ")";
if (key != null && !key.isEmpty()) {
final String name = recipe.getName();
final String recipeLabel = (name == null || name.isEmpty()) ? key : name;
final String nameLabel = recipeLabel + " Name (" + recipe.getType() + ")";
final String typeLabel = recipeLabel + " Type (" + recipe.getType() + ")";
final String enabledLabel = recipeLabel + " Enabled (" + recipe.getType() + ")";
final String statusLabel = recipeLabel + " Status (" + recipe.getType() + ")";
channels.add(ChannelBuilder
.create(UidUtils.createChannelUID(thingUid, NeeoConstants.ROOM_GROUP_RECIPE_ID,

View File

@ -31,7 +31,6 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
import javax.servlet.ServletException;
import javax.ws.rs.client.ClientBuilder;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.neeo.internal.NeeoBrainApi;
@ -130,7 +129,7 @@ public class NeeoBrainHandler extends BaseBridgeHandler {
}
/**
* Handles any {@Commands} sent - this bridge has no commands and does nothing
* Handles any {@link Command} sent - this bridge has no commands and does nothing
*
* @see
* org.openhab.core.thing.binding.ThingHandler#handleCommand(org.openhab.core.thing.ChannelUID,
@ -163,8 +162,10 @@ public class NeeoBrainHandler extends BaseBridgeHandler {
NeeoUtil.checkInterrupt();
final NeeoBrainConfig config = getBrainConfig();
logger.trace("Brain-UID {}: config is {}", thing.getUID(), config);
final String ipAddress = config.getIpAddress();
if (ipAddress == null || StringUtils.isEmpty(ipAddress)) {
if (ipAddress == null || ipAddress.isEmpty()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Brain IP Address must be specified");
return;
@ -186,7 +187,8 @@ public class NeeoBrainHandler extends BaseBridgeHandler {
addProperty(properties, "Last Change", String.valueOf(brain.getLastChange()));
updateProperties(properties);
if (config.isEnableForwardActions()) {
String forwardChain = config.getForwardChain();
if (config.isEnableForwardActions() && forwardChain != null && !forwardChain.isEmpty()) {
NeeoUtil.checkInterrupt();
forwardActionServlet = new NeeoForwardActionsServlet(scheduler, json -> {
@ -200,7 +202,7 @@ public class NeeoBrainHandler extends BaseBridgeHandler {
((NeeoRoomHandler) th).processAction(action);
}
}
}, config.getForwardChain(), clientBuilder);
}, forwardChain, clientBuilder);
NeeoUtil.checkInterrupt();
try {
@ -240,7 +242,7 @@ public class NeeoBrainHandler extends BaseBridgeHandler {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Exception occurred connecting to brain: " + e.getMessage());
} catch (InterruptedException e) {
logger.debug("Initializtion was interrupted", e);
logger.debug("Initialization was interrupted", e);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_INITIALIZING_ERROR,
"Initialization was interrupted");
} finally {
@ -256,9 +258,7 @@ public class NeeoBrainHandler extends BaseBridgeHandler {
* @param value a possibly null, possibly empty key
*/
private void addProperty(Map<String, String> properties, String key, @Nullable String value) {
Objects.requireNonNull(properties, "properties cannot be null");
NeeoUtil.requireNotEmpty(key, "key cannot be empty");
if (value != null && StringUtils.isNotEmpty(value)) {
if (value != null && !value.isEmpty()) {
properties.put(key, value);
}
}

View File

@ -13,6 +13,7 @@
package org.openhab.binding.neeo.internal.handler;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@ -20,8 +21,8 @@ import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.neeo.internal.NeeoBrainApi;
@ -104,11 +105,11 @@ public class NeeoDeviceHandler extends BaseThingHandler {
}
final String localGroupId = channelUID.getGroupId();
final String groupId = localGroupId == null || StringUtils.isEmpty(localGroupId) ? "" : localGroupId;
final String groupId = localGroupId == null || localGroupId.isEmpty() ? "" : localGroupId;
final String channelId = channelIds[0];
final String channelKey = channelIds.length > 1 ? channelIds[1] : "";
if (StringUtils.isEmpty(groupId)) {
if (groupId.isEmpty()) {
logger.debug("GroupID for channel is null - ignoring command: {}", channelUID);
return;
}
@ -176,14 +177,14 @@ public class NeeoDeviceHandler extends BaseThingHandler {
final NeeoDeviceConfig config = getConfigAs(NeeoDeviceConfig.class);
final String roomKey = getRoomKey();
if (roomKey == null || StringUtils.isEmpty(roomKey)) {
if (roomKey == null || roomKey.isEmpty()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Room key (from the parent room bridge) was not found");
return;
}
final String deviceKey = config.getDeviceKey();
if (deviceKey == null || StringUtils.isEmpty(deviceKey)) {
if (deviceKey == null || deviceKey.isEmpty()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Device key was not found or empty");
return;
@ -225,7 +226,8 @@ public class NeeoDeviceHandler extends BaseThingHandler {
properties.put("Shutdown Delay", toString(timing.getShutdownDelay()));
}
properties.put("Device Capabilities", StringUtils.join(details.getDeviceCapabilities(), ','));
properties.put("Device Capabilities",
Arrays.stream(details.getDeviceCapabilities()).collect(Collectors.joining(",")));
}
final ThingBuilder thingBuilder = editThing();
@ -292,7 +294,7 @@ public class NeeoDeviceHandler extends BaseThingHandler {
private void addProperty(Map<String, String> properties, String key, @Nullable String value) {
Objects.requireNonNull(properties, "properties cannot be null");
NeeoUtil.requireNotEmpty(key, "key cannot be empty");
if (value != null && StringUtils.isNotEmpty(value)) {
if (value != null && !value.isEmpty()) {
properties.put(key, value);
}
}

View File

@ -13,15 +13,14 @@
package org.openhab.binding.neeo.internal.handler;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.ScheduledExecutorService;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.client.ClientBuilder;
import org.apache.commons.io.IOUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.http.HttpStatus;
@ -48,7 +47,6 @@ public class NeeoForwardActionsServlet extends HttpServlet {
private final Callback callback;
/** The forwarding chain */
@Nullable
private final String forwardChain;
/** The {@link ClientBuilder} to use */
@ -64,13 +62,10 @@ public class NeeoForwardActionsServlet extends HttpServlet {
* @param callback a non-null {@link Callback}
* @param forwardChain a possibly null, possibly empty forwarding chain
*/
NeeoForwardActionsServlet(ScheduledExecutorService scheduler, Callback callback, @Nullable String forwardChain,
NeeoForwardActionsServlet(ScheduledExecutorService scheduler, Callback callback, String forwardChain,
ClientBuilder clientBuilder) {
super();
Objects.requireNonNull(scheduler, "scheduler cannot be null");
Objects.requireNonNull(callback, "callback cannot be null");
this.scheduler = scheduler;
this.callback = callback;
this.forwardChain = forwardChain;
@ -78,7 +73,7 @@ public class NeeoForwardActionsServlet extends HttpServlet {
}
/**
* Processes the post action from the NEEO brain. Simply get's the specified json and then forwards it on (if
* Processes the post action from the NEEO brain. Simply gets the specified json and then forwards it on (if
* needed)
*
* @param req the non-null request
@ -91,27 +86,24 @@ public class NeeoForwardActionsServlet extends HttpServlet {
return;
}
final String json = IOUtils.toString(req.getReader());
final String json = req.getReader().lines().collect(Collectors.joining("\n"));
logger.debug("handleForwardActions {}", json);
callback.post(json);
final String fc = forwardChain;
if (fc != null && !fc.isEmpty()) {
scheduler.execute(() -> {
try (final HttpRequest request = new HttpRequest(clientBuilder)) {
for (final String forwardUrl : fc.split(",")) {
if (forwardUrl != null && !forwardUrl.isEmpty()) {
final HttpResponse httpResponse = request.sendPostJsonCommand(forwardUrl, json);
if (httpResponse.getHttpCode() != HttpStatus.OK_200) {
logger.debug("Cannot forward event {} to {}: {}", json, forwardUrl,
httpResponse.getHttpCode());
}
scheduler.execute(() -> {
try (final HttpRequest request = new HttpRequest(clientBuilder)) {
for (final String forwardUrl : forwardChain.split(",")) {
if (forwardUrl != null && !forwardUrl.isEmpty()) {
final HttpResponse httpResponse = request.sendPostJsonCommand(forwardUrl, json);
if (httpResponse.getHttpCode() != HttpStatus.OK_200) {
logger.debug("Cannot forward event {} to {}: {}", json, forwardUrl,
httpResponse.getHttpCode());
}
}
}
});
}
}
});
}
interface Callback {

View File

@ -20,7 +20,6 @@ import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.neeo.internal.NeeoBrainApi;
@ -99,7 +98,7 @@ public class NeeoRoomHandler extends BaseBridgeHandler {
}
final String localGroupId = channelUID.getGroupId();
final String groupId = localGroupId == null || StringUtils.isEmpty(localGroupId) ? "" : localGroupId;
final String groupId = localGroupId == null || localGroupId.isEmpty() ? "" : localGroupId;
final String channelId = channelIds[0];
final String channelKey = channelIds.length > 1 ? channelIds[1] : "";
@ -195,7 +194,7 @@ public class NeeoRoomHandler extends BaseBridgeHandler {
final NeeoRoomConfig config = getConfigAs(NeeoRoomConfig.class);
final String roomKey = config.getRoomKey();
if (roomKey == null || StringUtils.isEmpty(roomKey)) {
if (roomKey == null || roomKey.isEmpty()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Room key (from the parent room bridge) was not found");
return;

View File

@ -12,7 +12,8 @@
*/
package org.openhab.binding.neeo.internal.models;
import org.apache.commons.lang.StringUtils;
import java.util.Arrays;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@ -123,8 +124,8 @@ public class NeeoDeviceDetails {
@Override
public String toString() {
return "NeeoDeviceDetails [sourceName=" + sourceName + ", adapterName=" + adapterName + ", type=" + type
+ ", manufacturer=" + manufacturer + ", name=" + name + ", timing=" + timing + ", deviceCapabilities="
+ StringUtils.join(deviceCapabilities, ',') + "]";
return "NeeoDeviceDetails{" + "sourceName='" + sourceName + '\'' + ", adapterName='" + adapterName + '\''
+ ", type='" + type + '\'' + ", manufacturer='" + manufacturer + '\'' + ", name='" + name + '\''
+ ", timing=" + timing + ", deviceCapabilities=" + Arrays.toString(deviceCapabilities) + '}';
}
}

View File

@ -15,7 +15,6 @@ package org.openhab.binding.neeo.internal.models;
import java.util.Arrays;
import java.util.Objects;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@ -59,7 +58,7 @@ public class NeeoDevices {
@Nullable
public NeeoDevice getDevice(String key) {
for (NeeoDevice device : getDevices()) {
if (StringUtils.equalsIgnoreCase(key, device.getKey())) {
if (key.equalsIgnoreCase(device.getKey())) {
return device;
}
}

View File

@ -15,7 +15,6 @@ package org.openhab.binding.neeo.internal.models;
import java.util.Arrays;
import java.util.Objects;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@ -59,7 +58,7 @@ public class NeeoMacros {
@Nullable
public NeeoMacro getMacro(String key) {
for (NeeoMacro macro : getMacros()) {
if (StringUtils.equalsIgnoreCase(key, macro.getKey())) {
if (key.equalsIgnoreCase(macro.getKey())) {
return macro;
}
}

View File

@ -13,9 +13,7 @@
package org.openhab.binding.neeo.internal.models;
import java.util.Arrays;
import java.util.Objects;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@ -28,7 +26,7 @@ import org.eclipse.jdt.annotation.Nullable;
public class NeeoRecipes {
/** The recipes. */
private NeeoRecipe @Nullable [] recipes;
private NeeoRecipe[] recipes;
/**
* Creates the recipes from the given recipes
@ -36,7 +34,6 @@ public class NeeoRecipes {
* @param recipes the recipes
*/
NeeoRecipes(NeeoRecipe[] recipes) {
Objects.requireNonNull(recipes, "recipes cannot be null");
this.recipes = recipes;
}
@ -46,8 +43,7 @@ public class NeeoRecipes {
* @return the recipes
*/
public NeeoRecipe[] getRecipes() {
final NeeoRecipe[] localRecipes = recipes;
return localRecipes == null ? new NeeoRecipe[0] : localRecipes;
return recipes;
}
/**
@ -58,12 +54,12 @@ public class NeeoRecipes {
*/
@Nullable
public NeeoRecipe getRecipe(String key) {
if (recipes == null || StringUtils.isEmpty(key)) {
if (key.isEmpty()) {
return null;
}
for (NeeoRecipe recipe : getRecipes()) {
if (StringUtils.equalsIgnoreCase(key, recipe.getKey())) {
for (NeeoRecipe recipe : recipes) {
if (key.equalsIgnoreCase(recipe.getKey())) {
return recipe;
}
}
@ -79,13 +75,12 @@ public class NeeoRecipes {
*/
@Nullable
public NeeoRecipe getRecipeByScenarioKey(String key, String type) {
if (recipes == null || StringUtils.isEmpty(key)) {
if (key.isEmpty()) {
return null;
}
for (NeeoRecipe recipe : getRecipes()) {
if (StringUtils.equalsIgnoreCase(key, recipe.getScenarioKey())
&& StringUtils.equalsIgnoreCase(type, recipe.getType())) {
for (NeeoRecipe recipe : recipes) {
if (key.equalsIgnoreCase(recipe.getScenarioKey()) && type.equalsIgnoreCase(recipe.getType())) {
return recipe;
}
}
@ -100,12 +95,12 @@ public class NeeoRecipes {
*/
@Nullable
public NeeoRecipe getRecipeByName(String name) {
if (recipes == null || StringUtils.isEmpty(name)) {
if (name.isEmpty()) {
return null;
}
for (NeeoRecipe recipe : getRecipes()) {
if (StringUtils.equalsIgnoreCase(name, recipe.getName())) {
for (NeeoRecipe recipe : recipes) {
if (name.equalsIgnoreCase(recipe.getName())) {
return recipe;
}
}

View File

@ -15,7 +15,6 @@ package org.openhab.binding.neeo.internal.models;
import java.util.Arrays;
import java.util.Objects;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@ -58,7 +57,7 @@ public class NeeoRooms {
*/
NeeoRoom getRoom(String key) {
for (NeeoRoom room : getRooms()) {
if (StringUtils.equalsIgnoreCase(key, room.getKey())) {
if (key.equalsIgnoreCase(room.getKey())) {
return room;
}
}

View File

@ -15,7 +15,6 @@ package org.openhab.binding.neeo.internal.models;
import java.util.Arrays;
import java.util.Objects;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@ -59,7 +58,7 @@ public class NeeoScenarios {
@Nullable
public NeeoScenario getScenario(String key) {
for (NeeoScenario scenario : getScenarios()) {
if (StringUtils.equalsIgnoreCase(key, scenario.getKey())) {
if (key.equalsIgnoreCase(scenario.getKey())) {
return scenario;
}
}

View File

@ -21,7 +21,6 @@ import java.util.Objects;
import javax.ws.rs.core.Response;
import org.apache.commons.io.IOUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@ -59,7 +58,7 @@ public class HttpResponse {
if (response.hasEntity()) {
InputStream is = response.readEntity(InputStream.class);
contents = IOUtils.toByteArray(is);
contents = is.readAllBytes();
} else {
contents = null;
}

View File

@ -30,7 +30,6 @@
<parameter name="forwardChain" type="text">
<label>Forward Chaining</label>
<description>Comma delimited list of URLs to forward NEEO brain actions to</description>
<default>true</default>
<advanced>true</advanced>
</parameter>
<parameter name="checkStatusInterval" type="integer">