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

Modified finder discovery schema to make future finders easier to create #3924

Merged
merged 6 commits into from
Dec 16, 2023

Conversation

mherwege
Copy link
Contributor

@mherwege mherwege commented Dec 14, 2023

In the add-on schema, we currently have a special field for mDNS discovery: mdns-service-type.

As noted in #3920, new add-on finders may also require special parameters. Rather than extending the schema each time, this PR changes the schema to include a parameter section and adapts the mDNS finder to read the mdnsServiceType from a parameter.

While not strictly necessary to make this change for the 4.1 release, it would be good to do it now to avoid future changes in the schema.

The proposed schema section then becomes:

<discovery-method>
 	<service-type>mdns</service-type>
 	<discovery-parameters>
 	 	<discovery-parameter>
 	 	 	<name>mdnsServiceType</name>
 	 	 	<value>...</value>
 	 	</discovery-parameter>
  	</discovery-parameters>
 	<match-properties>
		<match-property>
 			<name>...</name>
 			<regex>...</regex>
 		</match-property>
 	</match-properties>
</discovery-method>

A follow-up PR will be created to change the addon.xml files in the addons repository.

This PR is code complete but requires a full build for testing.

EDIT: Full build done and tested. All is still behaving as expected. Tests run through fine.

Signed-off-by: Mark Herwege <[email protected]>
@mherwege mherwege requested a review from a team as a code owner December 14, 2023 09:11
@mherwege
Copy link
Contributor Author

@andrewfg @J-N-K @kaikreuzer Could you please have a look at this?

Signed-off-by: Mark Herwege <[email protected]>
Copy link
Contributor

@andrewfg andrewfg left a comment

Choose a reason for hiding this comment

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

Generally it looks good.

But I think the string "mdnsServiceType" should be a constant.

Also I have not actually tested yet on a full build.

@@ -148,6 +149,12 @@ && propertyMatches(matchProperties, APPLICATION, service.getApplication())
return result;
}

private String getMdnsServiceType(AddonDiscoveryMethod method) {
String param = method.getParameters().stream().filter(p -> "mdnsServiceType".equals(p.getName()))
Copy link
Contributor

Choose a reason for hiding this comment

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

"mdnsServiceType" should be a constant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -103,12 +104,12 @@ private void setupAddonInfos() {
AddonDiscoveryMethod hp = new AddonDiscoveryMethod().setServiceType(AddonFinderConstants.SERVICE_TYPE_MDNS)
.setMatchProperties(
List.of(new AddonMatchProperty("rp", ".*"), new AddonMatchProperty("ty", "hp (.*)")))
.setMdnsServiceType("_printer._tcp.local.");
.setParameters(List.of(new AddonParameter("mdnsServiceType", "_printer._tcp.local.")));
Copy link
Contributor

Choose a reason for hiding this comment

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

as above

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

addonInfos.add(AddonInfo.builder("hpprinter", "binding").withName("HP").withDescription("HP Printer")
.withDiscoveryMethods(List.of(hp)).build());

AddonDiscoveryMethod hue = new AddonDiscoveryMethod().setServiceType(AddonFinderConstants.SERVICE_TYPE_MDNS)
.setMdnsServiceType("_hue._tcp.local.");
.setParameters(List.of(new AddonParameter("mdnsServiceType", "_hue._tcp.local.")));
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -125,13 +126,13 @@ private void setupMockAddonInfoProvider() {
AddonDiscoveryMethod hp = new AddonDiscoveryMethod().setServiceType(AddonFinderConstants.SERVICE_TYPE_MDNS)
.setMatchProperties(
List.of(new AddonMatchProperty("rp", ".*"), new AddonMatchProperty("ty", "hp (.*)")))
.setMdnsServiceType("_printer._tcp.local.");
.setParameters(List.of(new AddonParameter("mdnsServiceType", "_printer._tcp.local.")));
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, but with local constant. Otherwise an import of MDNSAddonFinder is required. This would create a circular import.


AddonDiscoveryMethod hue1 = new AddonDiscoveryMethod().setServiceType(AddonFinderConstants.SERVICE_TYPE_UPNP)
.setMatchProperties(List.of(new AddonMatchProperty("modelName", "Philips hue bridge")));

AddonDiscoveryMethod hue2 = new AddonDiscoveryMethod().setServiceType(AddonFinderConstants.SERVICE_TYPE_MDNS)
.setMdnsServiceType("_hue._tcp.local.");
.setParameters(List.of(new AddonParameter("mdnsServiceType", "_hue._tcp.local.")));
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

Copy link
Contributor Author

@mherwege mherwege Dec 14, 2023

Choose a reason for hiding this comment

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

Done, but with local constant. Otherwise an import of MDNSAddonFinder is required. This would create a circular import.

assertEquals(1, parameters.size());
AddonParameter parameter = parameters.get(0);
assertNotNull(parameter);
assertEquals("mdnsServiceType", parameter.getName());
Copy link
Contributor

Choose a reason for hiding this comment

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

"mdnsServiceType" should be a constant

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Left as is, as this would also require a local constant and the exact name of the parameter is not relevant for the test.

@@ -67,7 +60,7 @@ private AddonInfoProvider createAddonInfoProvider0() {

private AddonInfoProvider createAddonInfoProvider1() {
AddonDiscoveryMethod discoveryMethod = new AddonDiscoveryMethod().setServiceType("mdns")
.setMdnsServiceType("_hue._tcp.local.");
.setParameters(List.of(new AddonParameter("mdnsServiceType", "_hue._tcp.local.")));
Copy link
Contributor

Choose a reason for hiding this comment

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

"mdnsServiceType" should be a constant

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Left as is, as this would also require a local constant and the exact name of the parameter is not relevant for the test.

Signed-off-by: Mark Herwege <[email protected]>
@mherwege mherwege changed the title [WIP] Modified finder discovery schema to make future finders easier to create Modified finder discovery schema to make future finders easier to create Dec 14, 2023
Signed-off-by: Mark Herwege <[email protected]>
Copy link
Contributor

@andrewfg andrewfg left a comment

Choose a reason for hiding this comment

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

LGTM

@J-N-K
Copy link
Member

J-N-K commented Dec 14, 2023

I have just one question: would it make sense to also have a more generic expression than regex?

@andrewfg
Copy link
Contributor

a more generic expression

@J-N-K what is your use case? what do you mean?

@J-N-K
Copy link
Member

J-N-K commented Dec 14, 2023

I have nothing special in mind. I was just wondering if some new service could use something different than a regex (e.g. glob-like matching) and pattern would be a more generic fit.

@andrewfg
Copy link
Contributor

wondering if some new service could use something different than a regex

IMHO a regex would cover all potential use cases.

@mherwege
Copy link
Contributor Author

I have nothing special in mind. I was just wondering if some new service could use something different than a regex (e.g. glob-like matching) and pattern would be a more generic fit.

Regex is self explaining. Calling it pattern will require external documentation and I don’t see anything that cannot be covered by a regex. So I vote for keeping it as is.

@mherwege
Copy link
Contributor Author

I tested this locally with a full build (including this PR and the add-on PR). I receive the same add-on suggestions as before, so this is working as expected.

holgerfriedrich added a commit to holgerfriedrich/openhab-addons that referenced this pull request Dec 16, 2023
Signed-off-by: Holger Friedrich <[email protected]>
holgerfriedrich added a commit to holgerfriedrich/openhab-core that referenced this pull request Dec 16, 2023
Signed-off-by: Holger Friedrich <[email protected]>
@kaikreuzer kaikreuzer merged commit fe242f8 into openhab:main Dec 16, 2023
3 checks passed
kaikreuzer added a commit to openhab/website that referenced this pull request Dec 16, 2023
@kaikreuzer
Copy link
Member

FTR: xsd has been updated on the website with openhab/website@f3d354d.

holgerfriedrich added a commit to holgerfriedrich/openhab-core that referenced this pull request Dec 16, 2023
Signed-off-by: Holger Friedrich <[email protected]>
@mherwege mherwege deleted the finder_discovery branch December 16, 2023 11:22
holgerfriedrich added a commit to holgerfriedrich/openhab-addons that referenced this pull request Dec 17, 2023
Signed-off-by: Holger Friedrich <[email protected]>
@holgerfriedrich holgerfriedrich added this to the 4.1 milestone May 15, 2024
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.

5 participants