Skip to content

Commit

Permalink
[homekit] Simplify start level handling (openhab#13914)
Browse files Browse the repository at this point in the history
Signed-off-by: Kai Kreuzer <kai@openhab.org>
Signed-off-by: Andras Uhrin <andras.uhrin@gmail.com>
  • Loading branch information
kaikreuzer authored and andrasU committed Jan 5, 2024
1 parent ff0059a commit 15ec17f
Showing 1 changed file with 9 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public HomekitImpl(@Reference StorageService storageService, @Reference ItemRegi
this.metadataRegistry = metadataRegistry;
this.readyService = readyService;
networkAddressService.addNetworkAddressChangeListener(this);
readyService.registerTracker(this, new ReadyMarkerFilter().withType(StartLevelService.STARTLEVEL_MARKER_TYPE));
readyService.registerTracker(this, new ReadyMarkerFilter().withType(StartLevelService.STARTLEVEL_MARKER_TYPE)
.withIdentifier(Integer.toString(StartLevelService.STARTLEVEL_STATES)));
}

private HomekitSettings processConfig(Map<String, Object> properties) {
Expand Down Expand Up @@ -152,8 +153,9 @@ protected synchronized void modified(Map<String, Object> config) {
try {
HomekitSettings oldSettings = settings;
settings = processConfig(config);
if ((oldSettings == null) || (settings == null))
if ((oldSettings == null) || (settings == null)) {
return;
}
if (!oldSettings.name.equals(settings.name) || !oldSettings.pin.equals(settings.pin)
|| !oldSettings.setupId.equals(settings.setupId)
|| (oldSettings.networkInterface != null
Expand All @@ -177,32 +179,16 @@ protected synchronized void modified(Map<String, Object> config) {

@Override
public synchronized void onReadyMarkerAdded(ReadyMarker readyMarker) {
int newLevel = Integer.parseInt(readyMarker.getIdentifier());
currentStartLevel = newLevel;

if (newLevel >= StartLevelService.STARTLEVEL_STATES) {
try {
startHomekitServer();
} catch (IOException | InvalidAlgorithmParameterException e) {
logger.warn("could not initialize HomeKit bridge: {}", e.getMessage());
}
try {
startHomekitServer();
} catch (IOException | InvalidAlgorithmParameterException e) {
logger.warn("could not initialize HomeKit bridge: {}", e.getMessage());
}
}

@Override
@SuppressWarnings("PMD.EmptyWhileStmt")
public synchronized void onReadyMarkerRemoved(ReadyMarker readyMarker) {
int newLevel = Integer.parseInt(readyMarker.getIdentifier());

if (currentStartLevel > newLevel) {
while (newLevel-- > 0 && !readyService
.isReady(new ReadyMarker(StartLevelService.STARTLEVEL_MARKER_TYPE, Integer.toString(newLevel)))) {
}
currentStartLevel = newLevel;
if (currentStartLevel < StartLevelService.STARTLEVEL_STATES) {
stopHomekitServer();
}
}
stopHomekitServer();
}

private HomekitRoot startBridge(HomekitServer homekitServer, HomekitAuthInfoImpl authInfo,
Expand Down

0 comments on commit 15ec17f

Please sign in to comment.