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 3 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>
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
70 changes: 70 additions & 0 deletions bundles/org.openhab.misc.addonsuggestionfinder/README.md
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.
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-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>
```
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,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

View workflow job for this annotation

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

The method getDiscoveryMethods() is undefined for the type org.openhab.core.addon.AddonInfo
.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

View workflow job for this annotation

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

The import org.openhab.core.addon.AddonDiscoveryMethod cannot be resolved
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

View workflow job for this annotation

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

The import org.openhab.core.addon.AddonMatchProperty cannot be resolved

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);

Check failure on line 45 in bundles/org.openhab.misc.addonsuggestionfinder/src/main/java/org/openhab/misc/addonsuggestionfinder/internal/AddonListSerializer.java

View workflow job for this annotation

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

AddonDiscoveryMethod cannot be resolved to a type

xstream.alias("discovery-method", AddonDiscoveryMethod.class);

Check failure on line 47 in bundles/org.openhab.misc.addonsuggestionfinder/src/main/java/org/openhab/misc/addonsuggestionfinder/internal/AddonListSerializer.java

View workflow job for this annotation

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

AddonDiscoveryMethod cannot be resolved to a type
xstream.aliasField("service-type", AddonDiscoveryMethod.class, "serviceType");

Check failure on line 48 in bundles/org.openhab.misc.addonsuggestionfinder/src/main/java/org/openhab/misc/addonsuggestionfinder/internal/AddonListSerializer.java

View workflow job for this annotation

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

AddonDiscoveryMethod cannot be resolved to a type
xstream.aliasField("mdns-service-type", AddonDiscoveryMethod.class, "mdnsServiceType");

Check failure on line 49 in bundles/org.openhab.misc.addonsuggestionfinder/src/main/java/org/openhab/misc/addonsuggestionfinder/internal/AddonListSerializer.java

View workflow job for this annotation

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

AddonDiscoveryMethod cannot be resolved to a type
xstream.addImplicitCollection(AddonDiscoveryMethod.class, "matchProperties", "match-property",

Check failure on line 50 in bundles/org.openhab.misc.addonsuggestionfinder/src/main/java/org/openhab/misc/addonsuggestionfinder/internal/AddonListSerializer.java

View workflow job for this annotation

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

AddonDiscoveryMethod cannot be resolved to a type
AddonMatchProperty.class);

Check failure on line 51 in bundles/org.openhab.misc.addonsuggestionfinder/src/main/java/org/openhab/misc/addonsuggestionfinder/internal/AddonListSerializer.java

View workflow job for this annotation

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

AddonMatchProperty cannot be resolved to a type
}

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>
Loading
Loading