-
-
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
[addonsinfoprovider] Addon for providing addon-info of other addons #15780
Changes from 3 commits
f9429a0
86335c9
82f22db
4d86b85
80ba2fa
0434443
fd25845
48d8df3
615c693
81e0d30
1de4eea
d5785e9
d2f8199
9a4769b
9e837a7
4e52ccb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/src/main/resources/addons.xml |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
This content is produced and maintained by the openHAB project. | ||
|
||
* Project home: https://www.openhab.org | ||
|
||
== Declared Project Licenses | ||
|
||
This program and the accompanying materials are made available under the terms | ||
of the Eclipse Public License 2.0 which is available at | ||
https://www.eclipse.org/legal/epl-2.0/. | ||
|
||
== Source Code | ||
|
||
https://github.com/openhab/openhab-addons |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# AddonSuggestionFinder Addon | ||
|
||
This is a special addon that implements an `AddonInfoProvider` service containing information about suggested Addons that could potentially be installed. | ||
It allows developers to include information in their own addons so that the system can scan the user's network to discover potential addons that can automatically be installed. | ||
|
||
## Addon Developer Notes | ||
|
||
If you want to your addon to scan the user's system then you need to include additional fields in your `src/main/resources/OH-INF/addon.xml` file. | ||
|
||
| XML Element Name | Description | Instances | | ||
|---------------------|-------------------------------------------------------------------------------|-----------------------------------------------| | ||
| `discovery-method` | Complex XML element describing an addon discovery method. | Zero or more instances per file. | | ||
| `service-type` | The type of discovery method. May be `upnp` or `mdns`. | Mandatory one per `discovery-method`. | | ||
| `mdns-service-type` | If `service-type` is `mdns`, contains the MDNS discovery service type. | Optional one per `discovery-method`. | | ||
| `match-property` | A property name and regular expression used for matching discovery findings. | Zero or more instance per `discovery-method`. | | ||
| `name` | A property name to search for. | Mandatory one instance per `match-property`. | | ||
| `regex` | A regular expression (or plain string) that needs to match the property name. | Mandatory one instance per `match-property`. | | ||
|
||
## Example `addon.xml` File | ||
|
||
The following is an example for the discovery XML description for HP Printers. | ||
|
||
```xml | ||
<addon:addon id="groovyscripting" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:addon="https://openhab.org/schemas/addon/v1.0.0" | ||
xsi:schemaLocation="https://openhab.org/schemas/addon/v1.0.0 https://openhab.org/schemas/addon-1.0.0.xsd"> | ||
|
||
<type>binding</type> | ||
<name>HP Printer</name> | ||
<description>HP Printer Binding</description> | ||
<connection>local</connection> | ||
<discovery-method> | ||
<service-type>mdns</service-type> | ||
<match-property> | ||
<name>rp</name> | ||
<regex>.*</regex> | ||
</match-property> | ||
<match-property> | ||
<name>ty</name> | ||
<regex>hp (.*)</regex> | ||
</match-property> | ||
<mdns-service-type>_printer._tcp.local.</mdns-service-type> | ||
</discovery-method> | ||
</addon:addon> | ||
``` | ||
|
||
The following is an example for the discovery XML description for the Philips Hue bridge. | ||
|
||
```xml | ||
<addon:addon id="groovyscripting" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
andrewfg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
xmlns:addon="https://openhab.org/schemas/addon/v1.0.0" | ||
xsi:schemaLocation="https://openhab.org/schemas/addon/v1.0.0 https://openhab.org/schemas/addon-1.0.0.xsd"> | ||
|
||
<type>binding</type> | ||
<name>Philips Hue</name> | ||
<description>Philips Hue Binding</description> | ||
<connection>local</connection> | ||
<discovery-method> | ||
<serviceType>mdns</serviceType> | ||
<mdnsServiceType>_hue._tcp.local.</mdnsServiceType> | ||
</discovery-method> | ||
<discovery-method> | ||
<service-type>upnp</service-type> | ||
<match-property> | ||
<name>modelName</name> | ||
<regex>Philips hue bridge</regex> | ||
</match-property> | ||
</discovery-method> | ||
</addon:addon> | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.openhab.addons.bundles</groupId> | ||
<artifactId>org.openhab.addons.reactor.bundles</artifactId> | ||
<version>4.1.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>org.openhab.misc.addonsuggestionfinder</artifactId> | ||
|
||
<name>openHAB Add-ons :: Bundles :: Addon Suggestion Finder</name> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-antrun-plugin</artifactId> | ||
<version>3.1.0</version> | ||
<inherited>false</inherited> | ||
<executions> | ||
<execution> | ||
<id>create-addonsinfo</id> | ||
<goals> | ||
<goal>run</goal> | ||
</goals> | ||
<phase>generate-sources</phase> | ||
<configuration> | ||
<target> | ||
<concat destfile="src/main/resources/addons.xml"> | ||
<header file="src/main/resources/header.xml" filtering="no"/> | ||
<fileset dir="${basedirRoot}/bundles"> | ||
<include name="*/src/main/resources/OH-INF/addon/addon.xml"/> | ||
</fileset> | ||
<filterchain> | ||
<linecontainsRegExp negate="true"> | ||
<regexp pattern="<\?xml"/> | ||
</linecontainsRegExp> | ||
</filterchain> | ||
<footer file="src/main/resources/footer.xml" filtering="no"/> | ||
</concat> | ||
</target> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<features name="org.openhab.misc.addonsuggestionfinder-${project.version}" xmlns="http://karaf.apache.org/xmlns/features/v1.4.0"> | ||
<repository>mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/${ohc.version}/xml/features</repository> | ||
|
||
<feature name="openhab-misc-addonsuggestionfinder" description="Addon Suggestion Finder" version="${project.version}"> | ||
<feature>openhab-runtime-base</feature> | ||
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.misc.addonsuggestionfinder/${project.version}</bundle> | ||
</feature> | ||
</features> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* Copyright (c) 2010-2023 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.misc.addonsuggestionfinder; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Locale; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.core.addon.AddonInfo; | ||
import org.openhab.core.addon.AddonInfoProvider; | ||
import org.openhab.misc.addonsuggestionfinder.internal.AddonListSerializer; | ||
import org.osgi.service.component.annotations.Activate; | ||
import org.osgi.service.component.annotations.Component; | ||
|
||
/** | ||
* The {@link AddonSuggestionInfoProvider} provides a list of candidate suggested addons to be installed. | ||
* | ||
* @author Andrew Fiddian-Green - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
@Component(name = "addon-suggestion-info-provider", service = AddonInfoProvider.class) | ||
public class AddonSuggestionInfoProvider implements AddonInfoProvider { | ||
|
||
private final Set<AddonInfo> addonInfos; | ||
|
||
@Activate | ||
public AddonSuggestionInfoProvider() { | ||
AddonListSerializer serializer = new AddonListSerializer(); | ||
String xml = loadAddonXmlResource(); | ||
addonInfos = serializer.fromXML(xml).getAddonInfos().stream().filter(a -> !a.getDiscoveryMethods().isEmpty()) | ||
Check failure on line 45 in bundles/org.openhab.misc.addonsuggestionfinder/src/main/java/org/openhab/misc/addonsuggestionfinder/AddonSuggestionInfoProvider.java GitHub Actions / Build (Java 17, ubuntu-22.04)
|
||
.collect(Collectors.toSet()); | ||
} | ||
|
||
@Override | ||
public @Nullable AddonInfo getAddonInfo(@Nullable String id, @Nullable Locale locale) { | ||
return addonInfos.stream().filter(a -> a.getId().equals(id)).findFirst().orElse(null); | ||
} | ||
|
||
@Override | ||
public Set<AddonInfo> getAddonInfos(@Nullable Locale locale) { | ||
return addonInfos; | ||
} | ||
|
||
private String loadAddonXmlResource() { | ||
ClassLoader loader = getClass().getClassLoader(); | ||
if (loader != null) { | ||
InputStream stream = loader.getResourceAsStream("addons.xml"); | ||
if (stream != null) { | ||
try { | ||
return new String(stream.readAllBytes(), StandardCharsets.UTF_8); | ||
} catch (IOException e) { | ||
// fall through | ||
} | ||
} | ||
} | ||
return ""; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Copyright (c) 2010-2023 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.misc.addonsuggestionfinder.internal; | ||
|
||
import java.util.List; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.core.addon.AddonInfo; | ||
|
||
/** | ||
* DTO for a list of {@link AddonInfo}. | ||
* | ||
* @author Andrew Fiddian-Green - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class AddonInfoList { | ||
public @Nullable List<AddonInfo> addonInfos; | ||
|
||
public List<AddonInfo> getAddonInfos() { | ||
List<AddonInfo> addonInfos = this.addonInfos; | ||
return addonInfos != null ? addonInfos : List.of(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Copyright (c) 2010-2023 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.misc.addonsuggestionfinder.internal; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.openhab.core.addon.AddonDiscoveryMethod; | ||
Check failure on line 16 in bundles/org.openhab.misc.addonsuggestionfinder/src/main/java/org/openhab/misc/addonsuggestionfinder/internal/AddonListSerializer.java GitHub Actions / Build (Java 17, ubuntu-22.04)
|
||
import org.openhab.core.addon.AddonInfo; | ||
import org.openhab.core.addon.AddonMatchProperty; | ||
Check failure on line 18 in bundles/org.openhab.misc.addonsuggestionfinder/src/main/java/org/openhab/misc/addonsuggestionfinder/internal/AddonListSerializer.java GitHub Actions / Build (Java 17, ubuntu-22.04)
|
||
|
||
import com.thoughtworks.xstream.XStream; | ||
import com.thoughtworks.xstream.io.xml.StaxDriver; | ||
|
||
/** | ||
* Serializer/deserializer for addon suggestion finder. | ||
* | ||
* @author Andrew Fiddian-Green - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class AddonListSerializer { | ||
|
||
private final XStream xstream; | ||
|
||
public AddonListSerializer() { | ||
xstream = new XStream(new StaxDriver()); | ||
|
||
xstream.ignoreUnknownElements(); | ||
xstream.allowTypesByWildcard(new String[] { "org.openhab.**" }); | ||
|
||
xstream.alias("addons", AddonInfoList.class); | ||
xstream.addImplicitCollection(AddonInfoList.class, "addonInfos", "addon", AddonInfo.class); | ||
|
||
xstream.alias("addon", AddonInfo.class); | ||
xstream.useAttributeFor(AddonInfo.class, "id"); | ||
xstream.addImplicitCollection(AddonInfo.class, "discoveryMethods", "discovery-method", | ||
AddonDiscoveryMethod.class); | ||
|
||
xstream.alias("discovery-method", AddonDiscoveryMethod.class); | ||
xstream.aliasField("service-type", AddonDiscoveryMethod.class, "serviceType"); | ||
xstream.aliasField("mdns-service-type", AddonDiscoveryMethod.class, "mdnsServiceType"); | ||
xstream.addImplicitCollection(AddonDiscoveryMethod.class, "matchProperties", "match-property", | ||
AddonMatchProperty.class); | ||
} | ||
|
||
public AddonInfoList fromXML(String xml) { | ||
return (AddonInfoList) xstream.fromXML(xml); | ||
} | ||
|
||
public String toXML(AddonInfoList addons) { | ||
return xstream.toXML(addons); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<addon:addon id="addonsuggestionfinder" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:addon="https://openhab.org/schemas/addon/v1.0.0" | ||
xsi:schemaLocation="https://openhab.org/schemas/addon/v1.0.0 https://openhab.org/schemas/addon-1.0.0.xsd"> | ||
|
||
<type>misc</type> | ||
<name>Addon Suggestion Finder</name> | ||
<description>This is a special addon for finding suggested addons to be installed on setup.</description> | ||
|
||
</addon:addon> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
</addons> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright (c) 2010-2023 Contributors to the openHAB project | ||
|
||
See the NOTICE file(s) distributed with this work for additional | ||
information. | ||
|
||
This program and the accompanying materials are made available under the | ||
terms of the Eclipse Public License 2.0 which is available at | ||
http://www.eclipse.org/legal/epl-2.0 | ||
|
||
SPDX-License-Identifier: EPL-2.0 | ||
--> | ||
|
||
<addons> |
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.
This should be documented here, i.e. through the openhab-docs repository:
https://www.openhab.org/docs/developer/addons/addon.html#xml-structure-for-add-on-definitions
A link could be provided here, but probably the contents should be removed to avoid double maintenance.
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.
Agreed. But let us keep it here for now, until we make more progress on the total package.