Skip to content

Commit

Permalink
restart HomeKit bridge on network changes (openhab#11720)
Browse files Browse the repository at this point in the history
Signed-off-by: Eugen Freiter <freiter@gmx.de>

Co-authored-by: Eugen Freiter <freiter@gmx.de>
Signed-off-by: Nick Waterton <n.waterton@outlook.com>
  • Loading branch information
2 people authored and NickWaterton committed Dec 30, 2021
1 parent b694c73 commit 2d78100
Showing 1 changed file with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.openhab.core.io.transport.mdns.MDNSClient;
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.items.MetadataRegistry;
import org.openhab.core.net.CidrAddress;
import org.openhab.core.net.NetworkAddressChangeListener;
import org.openhab.core.net.NetworkAddressService;
import org.openhab.core.storage.StorageService;
import org.openhab.io.homekit.Homekit;
Expand Down Expand Up @@ -61,7 +63,7 @@
Constants.SERVICE_PID + "=org.openhab.homekit", "port:Integer=9123" })
@ConfigurableService(category = "io", label = "HomeKit Integration", description_uri = "io:homekit")
@NonNullByDefault
public class HomekitImpl implements Homekit {
public class HomekitImpl implements Homekit, NetworkAddressChangeListener {
private final Logger logger = LoggerFactory.getLogger(HomekitImpl.class);

private final NetworkAddressService networkAddressService;
Expand All @@ -88,13 +90,14 @@ public HomekitImpl(@Reference StorageService storageService, @Reference ItemRegi
this.configAdmin = configAdmin;
this.settings = processConfig(properties);
this.mdnsClient = mdnsClient;
networkAddressService.addNetworkAddressChangeListener(this);
this.changeListener = new HomekitChangeListener(itemRegistry, settings, metadataRegistry, storageService);
try {
authInfo = new HomekitAuthInfoImpl(storageService.getStorage(HomekitAuthInfoImpl.STORAGE_KEY), settings.pin,
settings.setupId);
startHomekitServer();
} catch (IOException | InvalidAlgorithmParameterException e) {
logger.warn("Cannot activate HomeKit binding. {}", e.getMessage());
logger.warn("cannot activate HomeKit binding. {}", e.getMessage());
throw e;
}
}
Expand All @@ -107,7 +110,7 @@ private HomekitSettings processConfig(Map<String, Object> properties) {
config = configAdmin.getConfiguration(HomekitSettings.CONFIG_PID);
props = config.getProperties();
} catch (IOException e) {
logger.warn("Cannot retrieve config admin {}", e.getMessage());
logger.warn("cannot retrieve config admin {}", e.getMessage());
}

if (props == null) { // if null, the configuration is new
Expand All @@ -133,7 +136,7 @@ private HomekitSettings processConfig(Map<String, Object> properties) {
try {
config.updateIfDifferent(props);
} catch (IOException e) {
logger.warn("Cannot update configuration {}", e.getMessage());
logger.warn("cannot update configuration {}", e.getMessage());
}
}
return settings;
Expand All @@ -158,7 +161,7 @@ protected synchronized void modified(Map<String, Object> config) {
startHomekitServer();
}
} catch (IOException e) {
logger.warn("Could not initialize HomeKit connector: {}", e.getMessage());
logger.warn("could not initialize HomeKit bridge: {}", e.getMessage());
}
}

Expand All @@ -185,7 +188,7 @@ private void startBridge() throws IOException {
int currentAccessoryCount = changeListener.getAccessories().size();
if (currentAccessoryCount < lastAccessoryCount) {
logger.debug(
"It looks like not all items were initialized yet. Old configuration had {} accessories, the current one has only {} accessories. Delay HomeKit bridge start for {} seconds.",
"it looks like not all items were initialized yet. Old configuration had {} accessories, the current one has only {} accessories. Delay HomeKit bridge start for {} seconds.",
lastAccessoryCount, currentAccessoryCount, settings.startDelay);
scheduler.schedule(() -> {
if (currentAccessoryCount < lastAccessoryCount) {
Expand All @@ -205,17 +208,18 @@ private void startBridge() throws IOException {
}

private void startHomekitServer() throws IOException {
logger.trace("start HomeKit bridge");
if (homekitServer == null) {
if (settings.useOHmDNS) {
if ((settings.networkInterface == null) || (settings.networkInterface.isEmpty())) {
logger.trace(
"No IP address configured in HomeKit settings. HomeKit will use the first configured address of openHAB");
"no IP address configured in HomeKit settings. HomeKit will use the first configured address of openHAB");
homekitServer = new HomekitServer(mdnsClient.getClientInstances().iterator().next(), settings.port);
} else {
networkInterface = InetAddress.getByName(settings.networkInterface);
for (JmDNS mdns : mdnsClient.getClientInstances()) {
if (mdns.getInetAddress().equals(networkInterface)) {
logger.trace("Suitable mDNS client for IP {} found and will be used for HomeKit",
logger.trace("suitable mDNS client for IP {} found and will be used for HomeKit",
networkInterface);
homekitServer = new HomekitServer(mdns, settings.port);
}
Expand All @@ -224,9 +228,9 @@ private void startHomekitServer() throws IOException {
}
if (homekitServer == null) {
if (settings.useOHmDNS) {
logger.trace("Not suitable mDNS server for IP {} found", networkInterface);
logger.trace("no suitable mDNS server for IP {} found", networkInterface);
}
logger.trace("Create HomeKit server with dedicated mDNS server");
logger.trace("create HomeKit server with dedicated mDNS server");
homekitServer = new HomekitServer(networkInterface, settings.port);
}
startBridge();
Expand All @@ -236,6 +240,7 @@ private void startHomekitServer() throws IOException {
}

private void stopHomekitServer() {
logger.trace("stop HomeKit bridge");
final @Nullable HomekitServer homekit = this.homekitServer;
if (homekit != null) {
if (bridge != null) {
Expand All @@ -248,6 +253,7 @@ private void stopHomekitServer() {

@Deactivate
protected void deactivate() {
networkAddressService.removeNetworkAddressChangeListener(this);
changeListener.clearAccessories();
stopHomekitServer();
changeListener.stop();
Expand Down Expand Up @@ -280,7 +286,18 @@ public void clearHomekitPairings() {
authInfo.clear();
refreshAuthInfo();
} catch (Exception e) {
logger.warn("Could not clear HomeKit pairings", e);
logger.warn("could not clear HomeKit pairings", e);
}
}

@Override
public void onChanged(final List<CidrAddress> list, final List<CidrAddress> list1) {
logger.trace("restarting homekit bridge on network interface changes.");
stopHomekitServer();
try {
startHomekitServer();
} catch (IOException e) {
logger.warn("could not initialize HomeKit bridge: {}", e.getMessage());
}
}
}

0 comments on commit 2d78100

Please sign in to comment.