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

[addonsinfoprovider] Addon for providing addon-info of other addons #15780

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bom/openhab-addons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2141,6 +2141,11 @@
<artifactId>org.openhab.voice.watsonstt</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.misc.addonsuggestionfinder</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,21 @@
<description>Binding for HP Printers with Embedded Web Servers</description>
<connection>local</connection>

<discovery-methods>

Check failure on line 11 in bundles/org.openhab.binding.hpprinter/src/main/resources/OH-INF/addon/addon.xml

View workflow job for this annotation

GitHub Actions / Build (Java 17, ubuntu-22.04)

Invalid content was found starting with element discovery-methods. One of {countries, service-id, config-description, config-description-ref} is expected.
<discovery-method>
<service-type>mdns</service-type>
<mdns-service-type>_printer._tcp.local.</mdns-service-type>
<match-properties>
<match-property>
<name>rp</name>
<regex>.*</regex>
</match-property>
<match-property>
<name>ty</name>
<regex>hp (.*)</regex>
</match-property>
</match-properties>
</discovery-method>
</discovery-methods>

</addon:addon>
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,20 @@
<description>The Hue Binding integrates the Philips Hue system. It allows to control Hue bulbs.</description>
<connection>hybrid</connection>

<discovery-methods>
<discovery-method>
<service-type>mdns</service-type>
<mdns-service-type>_hue._tcp.local.</mdns-service-type>
</discovery-method>
<discovery-method>
<service-type>upnp</service-type>
<match-properties>
<match-property>
<name>modelName</name>
<regex>Philips hue bridge.*</regex>
</match-property>
</match-properties>
</discovery-method>
</discovery-methods>

</addon:addon>
1 change: 1 addition & 0 deletions bundles/org.openhab.misc.addonsuggestionfinder/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/src/main/resources/addons.xml
13 changes: 13 additions & 0 deletions bundles/org.openhab.misc.addonsuggestionfinder/NOTICE
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
80 changes: 80 additions & 0 deletions bundles/org.openhab.misc.addonsuggestionfinder/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# 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.
Copy link
Contributor

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.

Copy link
Contributor Author

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.


| XML Element Name | Description | Instances |
|---------------------|-------------------------------------------------------------------------------|------------------------------------------------|
| `discovery-methods` | Wrapper for `discovery-method` elements (see below). | Zero or one instances per file. |
| `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-properties` | Wrapper for `match-property` elements (see below). | Zero or one instances per `discovery-method`. |
| `match-property` | A property name and regular expression used for matching discovery findings. | Zero or more instances 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="hpprinter" 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-methods>
<discovery-method>
<service-type>mdns</service-type>
<mdns-service-type>_printer._tcp.local.</mdns-service-type>
<match-properties>
<match-property>
<name>rp</name>
<regex>.*</regex>
</match-property>
<match-property>
<name>ty</name>
<regex>hp (.*)</regex>
</match-property>
</match-properties>
</discovery-method>
</discovery-methods>
</addon:addon>
```

The following is an example for the discovery XML description for the Philips Hue bridge.

```xml
<addon:addon id="hue" 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>Philips Hue</name>
<description>Philips Hue Binding</description>
<connection>local</connection>
<discovery-methods>
<discovery-method>
<serviceType>mdns</serviceType>
<mdnsServiceType>_hue._tcp.local.</mdnsServiceType>
</discovery-method>
<discovery-method>
<service-type>upnp</service-type>
</match-properties>
<match-property>
<name>modelName</name>
<regex>Philips hue bridge</regex>
</match-property>
</match-properties>
</discovery-method>
</discovery-methods>
</addon:addon>
```
53 changes: 53 additions & 0 deletions bundles/org.openhab.misc.addonsuggestionfinder/pom.xml
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="&lt;\?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,89 @@
/**
* 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.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.regex.PatternSyntaxException;
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.AddonInfoList;
import org.openhab.core.addon.AddonInfoListReader;
import org.openhab.core.addon.AddonInfoProvider;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* 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 Logger logger = LoggerFactory.getLogger(AddonSuggestionInfoProvider.class);
private final Set<AddonInfo> candidateAddonInfos = new HashSet<>();

@Activate
public AddonSuggestionInfoProvider() {
setCandidates(getResourceXml());
}

@Override
public @Nullable AddonInfo getAddonInfo(@Nullable String id, @Nullable Locale locale) {
return candidateAddonInfos.stream().filter(a -> a.getId().equals(id)).findFirst().orElse(null);
andrewfg marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public Set<AddonInfo> getAddonInfos(@Nullable Locale locale) {
return candidateAddonInfos;
}

private String getResourceXml() {
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) {
}
}
}
throw new IllegalStateException("Error loading 'addons.xml' resource");
}

public void setCandidates(String xml) {
candidateAddonInfos.clear();
AddonInfoListReader reader = new AddonInfoListReader();
try {
AddonInfoList addonInfoList = reader.readFromXML(xml);
if (addonInfoList != null) {
candidateAddonInfos.addAll(addonInfoList.getAddons().stream().collect(Collectors.toSet()));
}
} catch (PatternSyntaxException e) {
logger.warn("PatternSyntaxException: message:{}, description:{}, pattern:{}, index:{}", e.getMessage(),
e.getDescription(), e.getPattern(), e.getIndex(), e);
}
}
}
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,3 @@

</addons>
</addon-info-list>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?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
-->

<addon-info-list>
<addons>
Loading
Loading