openhab-core icon indicating copy to clipboard operation
openhab-core copied to clipboard

Marketplace: Support multiple templates in one file

Open rkoshak opened this issue 2 years ago • 5 comments

Often times one may have two or more rules that work together to implement a behavior. For example, I've an implementation for an MQTT Event Bus that consists of three rules: publish, subscribe, and online status. It doesn't really make sense to require users to install each of these independently as separate templates.

Using https://www.openhab.org/docs/developer/module-types/#via-json-files as a guide, I attempted to construct a .json file with all three rule templates. Once it was parse though CommunityRuleTemplateAddonHandler complains that multiple templates in one file are not allowed.

2021-10-05 11:36:46.867 [ERROR] [ty.CommunityRuleTemplateAddonHandler] - Rule template from marketplace is invalid: JSON must contain exactly one template!

These rule templates do not really make sense without the others so all three should be installed. Being able to include them all in one marketplace entry will give a nicer user experience since they only have to install one "add-on" to get everything required. It also reduces the effort to create and maintain these templates since there will be only the one entry in the forum to update and maintain.

rkoshak avatar Oct 05 '21 17:10 rkoshak

@ghys Since you created that handler: Is there a special reason why only one template is allowed per file? Since the uid of the add-on is added as tag, and we remove all templates with that that when we uninstall the add-on, I see no good reason for that. Code-wise is looks like removing the one-template-per-file-check and iterating over the templates when adding them to the provider would be everything that is needed.

J-N-K avatar May 19 '22 18:05 J-N-K

@J-N-K, this is somewhat related to the idea of "bundles". Yannick and I had some conversations on this but I don't think we ever opened a specific issue on this 9if one of us did I can't find it at the moment.

In short, why limit it to just one type of thing? What if we could bundle an add-on with some custom UI widgets and some rules, for example? A great example for something like that (minus the binding) is the Scene Control Suite which has a widget and two rules.

Being able to bundle multiple widgets together by itself would be a boon. Then people can make whole unified sets of widgets to do something like theming and the like.

I only bring this up because if there is a way to address the issue in a more comprehensive way that applies to widgets, rule templates, and Blockly libraries(?) it would be a greater improvement than narrowly just addressing the rule templates.

Edit: Forgot to add link to the Scene Control suite on the marketplace. https://community.openhab.org/t/scene-control-suite-scene-control-widget/128259

rkoshak avatar May 19 '22 19:05 rkoshak

@J-N-K that restriction actually comes from here (the Eclipse IoT marketplace implementation in OH2): https://github.com/openhab/openhab2-addons/blob/2d957b7cdab75827c7995c380a16368428167003/bundles/org.openhab.extensionservice.marketplace.automation/src/main/java/org/openhab/extensionservice/marketplace/automation/internal/MarketplaceRuleTemplateProvider.java#L90-L92 I figure since it tried to deserialize as a Set<RuleTemplate> it must mean that supporting installing multiple templates in one go was planned but never implemented.

This is the only exception where an array is needed (JSON rule templates), mostly because there were already a couple entries that I wanted to port over. For the other content types and formats (i.e. YAML) it's deserialized to a simple instance of the target type, it's easier because it mostly allows copy-pasting from the Code tabs in various UI parts. IMHO that should be preserved, but the code could detect if it's an array first and loop over the elements.

"Packages" (I wouldn't call them "bundles" because that's also an OSGi concept, though "packages" is already used as well internally) could be interesting as well, maybe a structure like this would work:

contents: # contents of the package
  - contentType: application/vnd.openhab.ruletemplate
    content:
      uid: ruletemplate1
      .... (RuleTemplate serialization)
  - contentType: application/vnd.openhab.uicomponent;type=blocks
    content:
      uid: blocklibrary1
      .... (RootUIComponent serialization)
  - contentType: application/vnd.openhab.uicomponent;type=widget
    url: (URL ending in .json or .yaml)
  - contentType: application/vnd.openhab.bundle
    url: (URL of a .jar)
  ...
dependencies: # only an idea, tbd, optional dependencies here: addons, existing karaf features (?)...
  addons:
    - binding-mqtt
    - transform-jsonpath
    - marketplace:123456
    - marketplace:234567
staticContent: # also an idea, static files to copy over and made available as static resources to e.g. widgets, like images etc.
  - url: https://example.org/static/pump.svg
    file: static/pump.svg
  ...

They would probably need a new media type + category on the forum too.

ghys avatar May 20 '22 14:05 ghys

This issue has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/openhab-4-0-wishlist/142388/139

openhab-bot avatar Jan 02 '23 14:01 openhab-bot

This issue has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/howto-publish-some-block-library/151645/8

openhab-bot avatar Dec 01 '23 14:12 openhab-bot