mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-10 15:11:59 +01:00
[bondhome] compile warnings and sat (#13986)
* fix some compile warnings and checkstyle * replace deprecated gson calls * Refactor config init Signed-off-by: lsiepel <leosiepel@gmail.com>
This commit is contained in:
parent
3a4a387931
commit
1e7ec9bde9
@ -21,6 +21,8 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class BondException extends Exception {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private boolean wasBridgeSetOffline;
|
||||
|
||||
public BondException(String message) {
|
||||
|
@ -130,9 +130,11 @@ public class BPUPListener implements Runnable {
|
||||
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;
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
@ -90,12 +90,10 @@ public class BondHttpApi {
|
||||
* @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) {
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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
|
||||
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
|
@ -59,9 +59,11 @@ public class BondDiscoveryService extends AbstractDiscoveryService implements Th
|
||||
@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();
|
||||
}
|
||||
}
|
||||
|
||||
@ -94,7 +96,7 @@ public class BondDiscoveryService extends AbstractDiscoveryService implements Th
|
||||
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)
|
||||
|
@ -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;
|
||||
|
||||
@ -99,16 +99,15 @@ public class BondBridgeHandler extends BaseBridgeHandler {
|
||||
}
|
||||
|
||||
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();
|
||||
@ -124,7 +123,7 @@ public class BondBridgeHandler extends BaseBridgeHandler {
|
||||
}
|
||||
} 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");
|
||||
@ -204,7 +203,6 @@ public class BondBridgeHandler extends BaseBridgeHandler {
|
||||
* @param the {@link BPUPUpdate object}
|
||||
*/
|
||||
public void forwardUpdateToThing(BPUPUpdate pushUpdate) {
|
||||
|
||||
updateStatus(ThingStatus.ONLINE);
|
||||
|
||||
BondDeviceState updateState = pushUpdate.deviceState;
|
||||
@ -242,14 +240,13 @@ public class BondBridgeHandler extends BaseBridgeHandler {
|
||||
* 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;
|
||||
}
|
||||
|
||||
@ -257,8 +254,7 @@ public class BondBridgeHandler extends BaseBridgeHandler {
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -288,9 +284,11 @@ public class BondBridgeHandler extends BaseBridgeHandler {
|
||||
* 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);
|
||||
|
Loading…
Reference in New Issue
Block a user