Skip to content

Commit

Permalink
[bondhome] compile warnings and sat (#13986)
Browse files Browse the repository at this point in the history
* fix some compile warnings and checkstyle
* replace deprecated gson calls
* Refactor config init

Signed-off-by: lsiepel <leosiepel@gmail.com>
  • Loading branch information
lsiepel authored and jlaur committed Dec 30, 2022
1 parent 8c175ba commit 9f7be41
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
@NonNullByDefault
public class BondException extends Exception {
private static final long serialVersionUID = 1L;

private boolean wasBridgeSetOffline;

public BondException(String message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,11 @@ private void sendBPUPKeepAlive() {
sock.receive(inPacket);
BPUPUpdate response = transformUpdatePacket(inPacket);
if (response != null) {
if (!response.bondId.equalsIgnoreCase(bridgeHandler.getBridgeId())) {
@Nullable
String bondId = response.bondId;
if (bondId == null || !bondId.equalsIgnoreCase(bridgeHandler.getBridgeId())) {
logger.warn("Response isn't from expected Bridge! Expected: {} Got: {}",
bridgeHandler.getBridgeId(), response.bondId);
bridgeHandler.getBridgeId(), bondId);
} else {
bridgeHandler.setBridgeOnline(inPacket.getAddress().getHostAddress());
numberOfKeepAliveTimeouts = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
*/
package org.openhab.binding.bondhome.internal.api;

import static org.openhab.binding.bondhome.internal.BondHomeBindingConstants.*;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
*/
package org.openhab.binding.bondhome.internal.api;

import static org.openhab.binding.bondhome.internal.BondHomeBindingConstants.*;

import java.util.Arrays;
import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
*/
package org.openhab.binding.bondhome.internal.api;

import static org.openhab.binding.bondhome.internal.BondHomeBindingConstants.*;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
*/
package org.openhab.binding.bondhome.internal.api;

import static org.openhab.binding.bondhome.internal.BondHomeBindingConstants.*;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
*/
package org.openhab.binding.bondhome.internal.api;

import static org.openhab.binding.bondhome.internal.BondHomeBindingConstants.*;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,10 @@ public BondSysVersion getBridgeVersion() throws BondException {
* @throws BondException
*/
public List<String> getDevices() throws BondException {

List<String> list = new ArrayList<>();
String json = request("/v2/devices/");
try {
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(json);
JsonElement element = JsonParser.parseString(json);
JsonObject obj = element.getAsJsonObject();
Set<Map.Entry<String, JsonElement>> entries = obj.entrySet();
for (Map.Entry<String, JsonElement> entry : entries) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
*/
package org.openhab.binding.bondhome.internal.api;

import static org.openhab.binding.bondhome.internal.BondHomeBindingConstants.*;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
*/
package org.openhab.binding.bondhome.internal.config;

import static org.openhab.binding.bondhome.internal.BondHomeBindingConstants.*;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

/**
* The {@link BondBridgeConfiguration} class contains fields mapping thing
Expand All @@ -29,15 +26,19 @@ public class BondBridgeConfiguration {
/**
* Configuration for a Bond Bridge
*/
public @Nullable String serialNumber;
public @Nullable String localToken;
public @Nullable String ipAddress;
public String serialNumber = "";
public String localToken = "";
public String ipAddress = "";

public @Nullable String getIpAddress() {
public String getIpAddress() {
return ipAddress;
}

public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}

public boolean isValid() {
return !(serialNumber.isEmpty() || localToken.isEmpty() || ipAddress.isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
*/
package org.openhab.binding.bondhome.internal.config;

import static org.openhab.binding.bondhome.internal.BondHomeBindingConstants.*;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ public void deactivate() {
@Override
public void setThingHandler(@Nullable ThingHandler handler) {
if (handler instanceof BondBridgeHandler) {
bridgeHandler = (BondBridgeHandler) handler;
bridgeHandler.setDiscoveryService(this);
api = bridgeHandler.getBridgeAPI();
@Nullable
BondBridgeHandler localHandler = (BondBridgeHandler) handler;
bridgeHandler = localHandler;
localHandler.setDiscoveryService(this);
api = localHandler.getBridgeAPI();
}
}

Expand Down Expand Up @@ -94,7 +96,7 @@ protected synchronized void startScan() {
for (final String deviceId : deviceList) {
BondDevice thisDevice = api.getDevice(deviceId);
String deviceName;
if (thisDevice != null && (deviceName = thisDevice.name) != null) {
if ((deviceName = thisDevice.name) != null) {
final ThingUID deviceUid = new ThingUID(thisDevice.type.getThingTypeUID(), bridgeUid, deviceId);
final DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(deviceUid)
.withBridge(bridgeUid).withLabel(thisDevice.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class BondBridgeHandler extends BaseBridgeHandler {
private final BPUPListener udpListener;
private final BondHttpApi api;

private @Nullable BondBridgeConfiguration config;
private BondBridgeConfiguration config = new BondBridgeConfiguration();

private @Nullable BondDiscoveryService discoveryService;

Expand Down Expand Up @@ -99,16 +99,15 @@ public void initialize() {
}

private void initializeThing() {
BondBridgeConfiguration localConfig = config;
if (localConfig.localToken == null) {
if (config.localToken.isEmpty()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"@text/offline.conf-error.incorrect-local-token");
this.initializer = null;
return;
}
if (localConfig.ipAddress == null) {
if (config.ipAddress.isEmpty()) {
try {
String lookupAddress = localConfig.serialNumber + ".local";
String lookupAddress = config.serialNumber + ".local";
logger.debug("Attempting to get IP address for Bond Bridge {}", lookupAddress);
InetAddress ia = InetAddress.getByName(lookupAddress);
String ip = ia.getHostAddress();
Expand All @@ -124,7 +123,7 @@ private void initializeThing() {
}
} else {
try {
InetAddress.getByName(localConfig.ipAddress);
InetAddress.getByName(config.ipAddress);
} catch (UnknownHostException ignored) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"@text/offline.conf-error.invalid-host");
Expand Down Expand Up @@ -204,7 +203,6 @@ public void childHandlerDisposed(ThingHandler childHandler, Thing childThing) {
* @param the {@link BPUPUpdate object}
*/
public void forwardUpdateToThing(BPUPUpdate pushUpdate) {

updateStatus(ThingStatus.ONLINE);

BondDeviceState updateState = pushUpdate.deviceState;
Expand Down Expand Up @@ -242,23 +240,21 @@ public void forwardUpdateToThing(BPUPUpdate pushUpdate) {
* Returns the Id of the bridge associated with the handler
*/
public String getBridgeId() {
String serialNumber = config.serialNumber;
return serialNumber == null ? "" : serialNumber;
return config.serialNumber;
}

/**
* Returns the Ip Address of the bridge associated with the handler as a string
*/
public @Nullable String getBridgeIpAddress() {
public String getBridgeIpAddress() {
return config.ipAddress;
}

/**
* Returns the local token of the bridge associated with the handler as a string
*/
public String getBridgeToken() {
String localToken = config.localToken;
return localToken == null ? "" : localToken;
return config.localToken;
}

/**
Expand Down Expand Up @@ -288,9 +284,11 @@ public void setBridgeOffline(ThingStatusDetail detail, String description) {
* Called by the UDP listener when it gets a proper response.
*/
public void setBridgeOnline(String bridgeAddress) {
BondBridgeConfiguration localConfig = config;
if (localConfig.ipAddress == null || !localConfig.ipAddress.equals(bridgeAddress)) {
logger.debug("IP address of Bond {} has changed to {}", localConfig.serialNumber, bridgeAddress);
if (!config.isValid()) {
logger.warn("Configuration error, cannot set the bridghe online without configuration");
return;
} else if (!config.ipAddress.equals(bridgeAddress)) {
logger.debug("IP address of Bond {} has changed to {}", config.serialNumber, bridgeAddress);
Configuration c = editConfiguration();
c.put(CONFIG_IP_ADDRESS, bridgeAddress);
updateConfiguration(c);
Expand Down

0 comments on commit 9f7be41

Please sign in to comment.