Skip to content

Commit

Permalink
[max] Remove org.apache.commons dependency and added annotations (ope…
Browse files Browse the repository at this point in the history
…nhab#8458)

* [max] cleanup several warnings

* cleared many warnings mainly notnullbydefault
* replace apache base64

Signed-off-by: Marcel Verpaalen <marcel@verpaalen.com>
  • Loading branch information
marcelrv authored and andrewfg committed Oct 8, 2020
1 parent 2b07c6e commit ff40520
Show file tree
Hide file tree
Showing 37 changed files with 220 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.smarthome.config.core.ConfigConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -27,6 +28,7 @@
*
* @author Marcel Verpaalen - Initial contribution
*/
@NonNullByDefault
public class MaxBackupUtils {

private final Logger logger = LoggerFactory.getLogger(MaxBackupUtils.class);
Expand All @@ -35,8 +37,8 @@ public class MaxBackupUtils {
private final String dbFolderName;
private final String backupId;
private boolean inProgress = false;
private StringBuilder msg;
private String cube;
private StringBuilder msg = new StringBuilder();
private String cube = "";

public MaxBackupUtils(String backupId) {
this.backupId = backupId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@
import java.util.List;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingRegistry;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.eclipse.smarthome.core.thing.binding.ThingHandler;
import org.eclipse.smarthome.io.console.Console;
import org.eclipse.smarthome.io.console.extensions.AbstractConsoleCommandExtension;
import org.eclipse.smarthome.io.console.extensions.ConsoleCommandExtension;
import org.openhab.binding.max.internal.handler.MaxCubeBridgeHandler;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

Expand All @@ -37,15 +39,17 @@
* @author Marcel Verpaalen - Initial contribution
*/
@Component(service = ConsoleCommandExtension.class)
@NonNullByDefault
public class MaxConsoleCommandExtension extends AbstractConsoleCommandExtension {

private static final String SUBCMD_BACKUP = "backup";
private static final String SUBCMD_REBOOT = "reboot";
private final ThingRegistry thingRegistry;

private ThingRegistry thingRegistry;

public MaxConsoleCommandExtension() {
@Activate
public MaxConsoleCommandExtension(@Reference ThingRegistry thingRegistry) {
super("max", "Additional EQ3 MAX! commands.");
this.thingRegistry = thingRegistry;
}

@Override
Expand Down Expand Up @@ -85,11 +89,11 @@ private void handleReboot(String[] args, Console console) {
if (handler != null) {
handler.cubeReboot();
} else {
console.println(String.format("Could not find cube %s", args[1]));
console.println(String.format("Could not find MAX! cube %s", args[1]));
printMaxDevices(console, SUPPORTED_BRIDGE_THING_TYPES_UIDS);
}
} else {
console.println("Specify cube to reboot.");
console.println("Specify MAX! cube to reboot.");
printMaxDevices(console, SUPPORTED_BRIDGE_THING_TYPES_UIDS);
}
}
Expand All @@ -104,16 +108,14 @@ private List<Thing> findDevices(Set<ThingTypeUID> deviceTypes) {
return devs;
}

private MaxCubeBridgeHandler getHandler(String thingId) {
private @Nullable MaxCubeBridgeHandler getHandler(String thingId) {
MaxCubeBridgeHandler handler = null;
try {
ThingUID bridgeUID = new ThingUID(thingId);
Thing thing = thingRegistry.get(bridgeUID);
if (thing != null) {
ThingHandler thingHandler = thing.getHandler();
if (thingHandler instanceof MaxCubeBridgeHandler) {
handler = (MaxCubeBridgeHandler) thingHandler;
}
if ((thing != null) && (thing.getHandler() != null)
&& (thing.getHandler() instanceof MaxCubeBridgeHandler)) {
handler = (MaxCubeBridgeHandler) thing.getHandler();
}
} catch (Exception e) {
handler = null;
Expand All @@ -131,16 +133,7 @@ private void printMaxDevices(Console console, Set<ThingTypeUID> deviceTypes) {

@Override
public List<String> getUsages() {
return Arrays.asList(new String[] { buildCommandUsage(SUBCMD_BACKUP, "Backup cube data"),
buildCommandUsage(SUBCMD_REBOOT + " <thingUID>", "Reset cube") });
}

@Reference
protected void setThingRegistry(ThingRegistry thingRegistry) {
this.thingRegistry = thingRegistry;
}

protected void unsetThingRegistry(ThingRegistry thingRegistry) {
this.thingRegistry = null;
return Arrays.asList(new String[] { buildCommandUsage(SUBCMD_BACKUP, "Backup MAX! cube data"),
buildCommandUsage(SUBCMD_REBOOT + " <thingUID>", "Reset MAX! cube") });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@

import java.util.Date;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* Utility class for common tasks within the MAX! binding package.
*
* @author Andreas Heil (info@aheil.de) - Initial contribution
* @author Marcel Verpaalen - OH2 update
*
*/
@NonNullByDefault
public final class Utils {

/**
Expand Down Expand Up @@ -169,9 +172,6 @@ public static byte[] hexStringToByteArray(String s) {
static final String HEXES = "0123456789ABCDEF";

public static String getHex(byte[] raw) {
if (raw == null) {
return null;
}
final StringBuilder hex = new StringBuilder(3 * raw.length);
for (final byte b : raw) {
hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F))).append(" ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.util.Base64;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.max.internal.Utils;
import org.openhab.binding.max.internal.device.Device;
Expand Down Expand Up @@ -161,13 +161,11 @@ public String getCommandString() {

byte[] dst = { 0x01 };
message.write(dst);

} catch (IOException e) {
logger.debug("Error while generating m: command: {}", e.getMessage(), e);

}

final String encodedString = Base64.encodeBase64StringUnChunked(message.toByteArray());
final String encodedString = Base64.getEncoder().encodeToString(message.toByteArray());
final StringBuilder commandStringBuilder = new StringBuilder();
int parts = (int) Math.round(encodedString.length() / MAX_MSG_LENGTH + 0.5);
for (int i = 0; i < parts; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
*/
package org.openhab.binding.max.internal.command;

import org.apache.commons.net.util.Base64;
import java.util.Base64;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.max.internal.Utils;
import org.openhab.binding.max.internal.device.ThermostatModeType;
Expand Down Expand Up @@ -98,9 +99,9 @@ public String getCommandString() {

final String commandString = baseString + rfAddress + Utils.toHex(roomId) + Utils.toHex(bits);

final String encodedString = Base64.encodeBase64String(Utils.hexStringToByteArray(commandString));
final String encodedString = Base64.getEncoder().encodeToString(Utils.hexStringToByteArray(commandString));

return "s:" + encodedString;
return "s:" + encodedString + "\r\n";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
*/
package org.openhab.binding.max.internal.command;

import org.apache.commons.net.util.Base64;
import java.util.Base64;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.max.internal.Utils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -153,8 +154,8 @@ public String getCommandString() {
commandString = commandString + Utils.toHex(roomId) + commandConfigString;
}

String encodedString = Base64.encodeBase64String(Utils.hexStringToByteArray(commandString));
return "s:" + encodedString;
String encodedString = Base64.getEncoder().encodeToString(Utils.hexStringToByteArray(commandString));
return "s:" + encodedString + "\r\n";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
package org.openhab.binding.max.internal.command;

import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.net.util.Base64;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.max.internal.Utils;

Expand Down Expand Up @@ -53,10 +53,9 @@ public String getCommandString() {
for (String rfAddress : rfAddresses) {
commandArray = ArrayUtils.addAll(Utils.hexStringToByteArray(rfAddress), commandArray);
}
String encodedString = Base64.encodeBase64StringUnChunked(commandArray);
String encodedString = Base64.getEncoder().encodeToString(commandArray);

return "t:" + String.format("%02d", rfAddresses.size()) + "," + updateForced + "," + encodedString + '\r'
+ '\n';
return "t:" + String.format("%02d", rfAddresses.size()) + "," + updateForced + "," + encodedString + "\r\n";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.Hashtable;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.config.core.Configuration;
import org.eclipse.smarthome.config.discovery.DiscoveryService;
import org.eclipse.smarthome.core.thing.Bridge;
Expand All @@ -41,20 +43,21 @@
*
* @author Marcel Verpaalen - Initial contribution
*/
@NonNullByDefault
@Component(service = ThingHandlerFactory.class, configurationPid = "binding.max")
public class MaxCubeHandlerFactory extends BaseThingHandlerFactory {

private final Logger logger = LoggerFactory.getLogger(MaxCubeHandlerFactory.class);
private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();

@Override
public Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration, ThingUID thingUID,
ThingUID bridgeUID) {
public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
@Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
if (CUBEBRIDGE_THING_TYPE.equals(thingTypeUID)) {
ThingUID cubeBridgeUID = getBridgeThingUID(thingTypeUID, thingUID, configuration);
return super.createThing(thingTypeUID, configuration, cubeBridgeUID, null);
}
if (supportsThingType(thingTypeUID)) {
if (supportsThingType(thingTypeUID) && bridgeUID != null) {
ThingUID deviceUID = getMaxCubeDeviceUID(thingTypeUID, thingUID, configuration, bridgeUID);
return super.createThing(thingTypeUID, configuration, deviceUID, bridgeUID);
}
Expand All @@ -66,16 +69,17 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
}

private ThingUID getBridgeThingUID(ThingTypeUID thingTypeUID, ThingUID thingUID, Configuration configuration) {
private ThingUID getBridgeThingUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
Configuration configuration) {
if (thingUID == null) {
String serialNumber = (String) configuration.get(Thing.PROPERTY_SERIAL_NUMBER);
return new ThingUID(thingTypeUID, serialNumber);
}
return thingUID;
}

private ThingUID getMaxCubeDeviceUID(ThingTypeUID thingTypeUID, ThingUID thingUID, Configuration configuration,
ThingUID bridgeUID) {
private ThingUID getMaxCubeDeviceUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
Configuration configuration, ThingUID bridgeUID) {
String serialNumber = (String) configuration.get(Thing.PROPERTY_SERIAL_NUMBER);

if (thingUID == null) {
Expand All @@ -96,20 +100,18 @@ private synchronized void registerDeviceDiscoveryService(MaxCubeBridgeHandler ma
protected synchronized void removeHandler(ThingHandler thingHandler) {
if (thingHandler instanceof MaxCubeBridgeHandler) {
ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
if (serviceReg != null) {
// remove discovery service, if bridge handler is removed
MaxDeviceDiscoveryService service = (MaxDeviceDiscoveryService) bundleContext
.getService(serviceReg.getReference());
serviceReg.unregister();
if (service != null) {
service.deactivate();
}
// remove discovery service, if bridge handler is removed
MaxDeviceDiscoveryService service = (MaxDeviceDiscoveryService) bundleContext
.getService(serviceReg.getReference());
serviceReg.unregister();
if (service != null) {
service.deactivate();
}
}
}

@Override
protected ThingHandler createHandler(Thing thing) {
protected @Nullable ThingHandler createHandler(Thing thing) {
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
if (CUBEBRIDGE_THING_TYPE.equals(thingTypeUID)) {
MaxCubeBridgeHandler handler = new MaxCubeBridgeHandler((Bridge) thing);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ private void processMMessage(MMessage msg) {
}

private void processNMessage(NMessage nMessage) {
if (nMessage.getRfAddress() != null) {
if (!nMessage.getRfAddress().isEmpty()) {
logger.debug("New {} found. Serial: {}, rfaddress: {}", nMessage.getDeviceType(),
nMessage.getSerialNumber(), nMessage.getRfAddress());
// Send C command to get the configuration so it will be added to discovery
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
*/
package org.openhab.binding.max.internal.message;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.slf4j.Logger;

/**
* The {@link AMessage} Acknowledge the execution of a command
*
* @author Marcel Verpaalen - Initial contribution
*/
@NonNullByDefault
public final class AMessage extends Message {

public AMessage(String raw) {
Expand Down
Loading

0 comments on commit ff40520

Please sign in to comment.