Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[homekit] Improve multiple instance management #14016

Merged
merged 4 commits into from
Jan 3, 2023

Conversation

ccutrer
Copy link
Contributor

@ccutrer ccutrer commented Dec 19, 2022

No description provided.

 * allow addressing individual instances for most console commands
 * don't restart all instances if simply adding/removing instances on
   config change
 * clear stored info when removing instances

Signed-off-by: Cody Cutrer <cody@cutrer.us>
Signed-off-by: Cody Cutrer <cody@cutrer.us>
@ccutrer ccutrer changed the title Improve HomeKit instance management [homekit] Improve multiple instance management Dec 19, 2022
@jlaur jlaur added rebuild Triggers Jenkins PR build and removed rebuild Triggers Jenkins PR build labels Dec 20, 2022
@@ -206,4 +254,12 @@ private void printAccessory(String id, Console console) {
}
});
}

private Collection<HomekitAccessory> getIntanceAccessories(@Nullable Integer instance) {
if (instance != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a shame we need two different code paths for this. Could we just refer to instance 0 in the case of non-multi-instance setups?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OHHHH I get it now. Instance being null means "all accessories on all instances".

Any opinion on using Optional over nullable integer, instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really. openHAB requires null-type annotations on everything. HAP-Java doesn't use annotations, but instead uses Optional all over. So the homekit add-on seems to be a free-for-all. Honestly, I lean slightly towards annotations - they make the null-safety-checks seem as if they're more part of the language, than an add-on library. Yes, map and orElse are a bit nicer than nesting a conditional block, but the .get() for direct access when you know it can't possibly be null is slightly more annoying.

Anyhow, I'll add a javadoc to this method more clearly spelling out what a null instance means

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough :)

@@ -45,17 +45,42 @@ public interface Homekit {
void allowUnauthenticatedRequests(boolean allow);

/**
* returns list of HomeKit accessories registered at bridge.
* returns list of HomeKit accessories registered on all bridges.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* returns list of HomeKit accessories registered on all bridges.
* returns list of HomeKit accessories registered on all bridge instances.

@Override
public void clearHomekitPairings(int instance) {
if (instance < 1 || instance > authInfos.size()) {
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we log a warning here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I can. It shouldn't happen from the command extension, which is already doing range checking, but if this ever gets called from say a REST API in the future, then it would be good to have.

@Override
public void pruneDummyAccessories(int instance) {
if (instance < 1 || instance > authInfos.size()) {
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log a warning?

logger.warn("cannot resolve the Pv4 address / hostname {}.",
networkAddressService.getPrimaryIpv4HostAddress());
private void startHomekitServer(int instance) throws IOException, InvalidAlgorithmParameterException {
logger.trace("starting HomeKit bridge instance {}", instance + 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like instance is sometimes 0 based, as seen in this function, and in other functions, 1 based (ie pruneDummyAccessories). This seems likely that some poor soul will get it wrong (and by poor soul, I mean myself). I'm wondering if we should normalize as 0-based at the UI level (parsing the command), and be consistent?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think we need to standardize on if the variable is index, it's 0-based, but if it's instance it's one-based. I'm not sure I want to be exposing to the user that things are actually 0-based, since for the average user they'll only have 1 instance, and having that instance be "instance 0" would be confusing. The instance number (as 1-based) is also part of the bridge name when you add it in HomeKit (after index 0). @yfre WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree, even for me who used to count many things starting from 0 in the past :) would be a little strange to refer to "instance 0". i would vote to use "1" and keep it consistence in the communication to the user.

Copy link
Contributor

@timcharper timcharper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! A few small suggestions, I think. More strongly the mixing of instance being 0-based vs 1-based, but overall looks great :)

Signed-off-by: Cody Cutrer <cody@cutrer.us>
@openhab-bot
Copy link
Collaborator

This pull request has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/homekit-stops-working-after-some-hours-openhab-3-4-0/142502/17

Copy link
Contributor

@yfre yfre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@yfre
Copy link
Contributor

yfre commented Dec 29, 2022

thank you. good refactoring to support improve support for multiple instances

Copy link
Contributor Author

@ccutrer ccutrer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, @timcharper, I guess I didn't get my comments published last week.

@@ -206,4 +254,12 @@ private void printAccessory(String id, Console console) {
}
});
}

private Collection<HomekitAccessory> getIntanceAccessories(@Nullable Integer instance) {
if (instance != null) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really. openHAB requires null-type annotations on everything. HAP-Java doesn't use annotations, but instead uses Optional all over. So the homekit add-on seems to be a free-for-all. Honestly, I lean slightly towards annotations - they make the null-safety-checks seem as if they're more part of the language, than an add-on library. Yes, map and orElse are a bit nicer than nesting a conditional block, but the .get() for direct access when you know it can't possibly be null is slightly more annoying.

Anyhow, I'll add a javadoc to this method more clearly spelling out what a null instance means

logger.warn("cannot resolve the Pv4 address / hostname {}.",
networkAddressService.getPrimaryIpv4HostAddress());
private void startHomekitServer(int instance) throws IOException, InvalidAlgorithmParameterException {
logger.trace("starting HomeKit bridge instance {}", instance + 1);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think we need to standardize on if the variable is index, it's 0-based, but if it's instance it's one-based. I'm not sure I want to be exposing to the user that things are actually 0-based, since for the average user they'll only have 1 instance, and having that instance be "instance 0" would be confusing. The instance number (as 1-based) is also part of the bridge name when you add it in HomeKit (after index 0). @yfre WDYT?

@Override
public void clearHomekitPairings(int instance) {
if (instance < 1 || instance > authInfos.size()) {
return;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I can. It shouldn't happen from the command extension, which is already doing range checking, but if this ever gets called from say a REST API in the future, then it would be good to have.

Signed-off-by: Cody Cutrer <cody@cutrer.us>
@ccutrer
Copy link
Contributor Author

ccutrer commented Dec 29, 2022

@yfre : I can't find when your code comments went on GitHub, I only see them in my email. anyhow, I addressed the interfaceName getting logged. I did not change the temp var for localNetworkInterface - it's required, because networkInterface is @nullable, and the compiler continues to complain if you return it directly since theoretically another thread could change it to null between the non-null check and returning it.

@ccutrer
Copy link
Contributor Author

ccutrer commented Jan 3, 2023

@lolodomo and/or @jlaur : this is good to go from our end, just need a maintainer review. thanks!

Copy link
Contributor

@jlaur jlaur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@jlaur jlaur merged commit 47f5489 into openhab:main Jan 3, 2023
@jlaur jlaur added this to the 4.0 milestone Jan 3, 2023
borazslo pushed a commit to borazslo/openhab-mideaac-addon that referenced this pull request Jan 8, 2023
* [homekit] improve instance management

 * allow addressing individual instances for most console commands
 * don't restart all instances if simply adding/removing instances on
   config change
 * clear stored info when removing instances

* [homekit] reset instance identity when clearing pairings
* [homekit] log the actual interface we looked up

Signed-off-by: Cody Cutrer <cody@cutrer.us>
nemerdaud pushed a commit to nemerdaud/openhab-addons that referenced this pull request Feb 28, 2023
* [homekit] improve instance management

 * allow addressing individual instances for most console commands
 * don't restart all instances if simply adding/removing instances on
   config change
 * clear stored info when removing instances

* [homekit] reset instance identity when clearing pairings
* [homekit] log the actual interface we looked up

Signed-off-by: Cody Cutrer <cody@cutrer.us>
renescherer pushed a commit to renescherer/openhab-addons that referenced this pull request Mar 23, 2023
* [homekit] improve instance management

 * allow addressing individual instances for most console commands
 * don't restart all instances if simply adding/removing instances on
   config change
 * clear stored info when removing instances

* [homekit] reset instance identity when clearing pairings
* [homekit] log the actual interface we looked up

Signed-off-by: Cody Cutrer <cody@cutrer.us>
@ccutrer ccutrer deleted the homekit-instance-management branch November 29, 2023 22:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants