*/
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
/**
* 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());
+ }
}
@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();
}
}
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)
private final BPUPListener udpListener;
private final BondHttpApi api;
- private @Nullable BondBridgeConfiguration config;
+ private BondBridgeConfiguration config = new BondBridgeConfiguration();
private @Nullable BondDiscoveryService discoveryService;
}
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();
}
} 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");
* @param the {@link BPUPUpdate object}
*/
public void forwardUpdateToThing(BPUPUpdate pushUpdate) {
-
updateStatus(ThingStatus.ONLINE);
BondDeviceState updateState = pushUpdate.deviceState;
* 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;
}
/**
* 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);