-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Conversation
* 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>
9e56da0
to
21b6d58
Compare
@@ -206,4 +254,12 @@ private void printAccessory(String id, Console console) { | |||
} | |||
}); | |||
} | |||
|
|||
private Collection<HomekitAccessory> getIntanceAccessories(@Nullable Integer instance) { | |||
if (instance != null) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* 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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this 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>
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
thank you. good refactoring to support improve support for multiple instances |
There was a problem hiding this 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) { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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>
@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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thanks!
* [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>
* [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>
* [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>
No description provided.