Skip to content

Commit

Permalink
[rules] Remove unused rule providers (#183)
Browse files Browse the repository at this point in the history
* [rules] Remove unused rule providers
Removes methods to select/register another provider for rules.
Those are removed, because they go far beyond the use case of most
users. As they were not documented, it is not expected that anyone is
using them.

* Update CHANGELOG

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
  • Loading branch information
florian-h05 authored Dec 2, 2022
1 parent 4831c2b commit 9e470a3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 54 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
| Type | Namespace | Description | Reference | Breaking |
|-------------|-----------|---------------------------------------------------------------------|--------------------------------------------------------|----------|
| Enhancement | `items` | ItemHistory: Change return types of min/max between/since to number | [#175](https://github.com/openhab/openhab-js/pull/175) | Yes |
| Cleanup | `rules` | Remove unused rule providers | [#183](https://github.com/openhab/openhab-js/pull/183) | Yes |

Also see the [Release Milestone](https://github.com/openhab/openhab-js/milestone/8).

Expand Down
57 changes: 9 additions & 48 deletions rules/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,11 @@ const triggers = require('../triggers');
const { automationManager, ruleRegistry } = require('@runtime/RuleSupport');

const RuleManager = osgi.getService('org.openhab.core.automation.RuleManager');
const factory = require('@runtime/rules').factory;

/**
* Generates an item name given it's configuration.
*
* @memberOf rules
* @memberof rules
* @private
* @param {object} ruleConfig The rule config
* @param {string} userInfo.name The name of the rule.
Expand All @@ -72,7 +71,7 @@ const itemNameForRule = function (ruleConfig) {
/**
* Links an item to a rule. When the item is switched on or off, so will the rule be.
*
* @memberOf rules
* @memberof rules
* @private
* @param {HostRule} rule The rule to link to the item.
* @param {items.Item} item the item to link to the rule.
Expand Down Expand Up @@ -100,7 +99,7 @@ const linkItemToRule = function (rule, item) {
/**
* Gets the groups that an rule-toggling-item should be a member of. Will create the group item if necessary.
*
* @memberOf rules
* @memberof rules
* @private
* @param {RuleConfig} ruleConfig The rule config describing the rule
* @returns {String[]} the group names to put the item in
Expand All @@ -126,7 +125,7 @@ const getGroupsForItem = function (ruleConfig) {
* Check whether a rule exists.
* Only works for rules created in the same file.
*
* @memberOf rules
* @memberof rules
* @private
* @param {string} uid the UID of the rule
* @returns {boolean} whether the rule exists
Expand All @@ -139,7 +138,7 @@ const ruleExists = function (uid) {
* Remove a rule when it exists. The rule will be immediately removed.
* Only works for rules created in the same file.
*
* @memberOf rules
* @memberof rules
* @param {string} uid the UID of the rule
* @returns {boolean} whether the rule was actually removed
*/
Expand All @@ -157,7 +156,7 @@ const removeRule = function (uid) {
* Runs the rule with the given UID. Throws errors when the rule doesn't exist
* or is unable to run (e.g. it's disabled).
*
* @memberOf rules
* @memberof rules
* @param {string} uid the UID of the rule to run
* @param {Map<Object>} [args={}] args optional dict of data to pass to the called rule
* @param {boolean} [cond=true] when true, the called rule will only run if it's conditions are met
Expand Down Expand Up @@ -220,7 +219,7 @@ const setEnabled = function (uid, isEnabled) {
* execute: (event) => { // do stuff }
* });
*
* @memberOf rules
* @memberof rules
* @param {RuleConfig} ruleConfig The rule config describing the rule
* @returns {HostRule} the created rule
* @throws Will throw an error if the rule with the passed in uid already exists
Expand Down Expand Up @@ -280,54 +279,17 @@ const JSRule = function (ruleConfig) {
// Register rule here
if (triggers && triggers.length > 0) {
rule.setTriggers(triggers);
rule = registerRule(rule);
rule = automationManager.addRule(rule);
}

return rule;
};

let currentProvider = automationManager;

const withNewRuleProvider = function (fn) {
const cachedRules = [];
currentProvider = {
addRule: r => {
// r = factory.processRule(r);
// r.setConfigurationDescriptions(null);
// r.setConfiguration(null);
cachedRules.push(factory.processRule(r));
}
};

try {
fn();
const provider = factory.newRuleProvider(cachedRules);
osgi.registerService(provider, Java.type('org.openhab.core.automation.RuleProvider').class.getName());
} finally {
currentProvider = automationManager;
}
};

const withManagedProvider = function (fn) { // eslint-disable-line no-unused-vars
const previousProvider = currentProvider;
currentProvider = automationManager;

try {
fn();
} finally {
currentProvider = previousProvider;
}
};

const registerRule = function (rule) {
return currentProvider.addRule(rule);
};

/**
* Creates a rule, with an associated SwitchItem that can be used to toggle the rule's enabled state.
* The rule will be created and immediately available.
*
* @memberOf rules
* @memberof rules
* @param {RuleConfig} ruleConfig The rule config describing the rule
* @returns {HostRule} the created rule
* @throws Will throw an error is a rule with the given UID already exists.
Expand Down Expand Up @@ -484,7 +446,6 @@ const getTriggeredData = function (input) {
};

module.exports = {
withNewRuleProvider,
removeRule,
runRule,
isEnabled,
Expand Down
9 changes: 4 additions & 5 deletions types/rules/rules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,11 @@ export type RuleConfig = {
*/
overwrite?: boolean;
};
export function withNewRuleProvider(fn: any): void;
/**
* Remove a rule when it exists. The rule will be immediately removed.
* Only works for rules created in the same file.
*
* @memberOf rules
* @memberof rules
* @param {string} uid the UID of the rule
* @returns {boolean} whether the rule was actually removed
*/
Expand All @@ -114,7 +113,7 @@ export function removeRule(uid: string): boolean;
* Runs the rule with the given UID. Throws errors when the rule doesn't exist
* or is unable to run (e.g. it's disabled).
*
* @memberOf rules
* @memberof rules
* @param {string} uid the UID of the rule to run
* @param {Map<Object>} [args={}] args optional dict of data to pass to the called rule
* @param {boolean} [cond=true] when true, the called rule will only run if it's conditions are met
Expand Down Expand Up @@ -154,7 +153,7 @@ export function setEnabled(uid: string, isEnabled: boolean): void;
* execute: (event) => { // do stuff }
* });
*
* @memberOf rules
* @memberof rules
* @param {RuleConfig} ruleConfig The rule config describing the rule
* @returns {HostRule} the created rule
* @throws Will throw an error if the rule with the passed in uid already exists
Expand All @@ -164,7 +163,7 @@ export function JSRule(ruleConfig: RuleConfig): HostRule;
* Creates a rule, with an associated SwitchItem that can be used to toggle the rule's enabled state.
* The rule will be created and immediately available.
*
* @memberOf rules
* @memberof rules
* @param {RuleConfig} ruleConfig The rule config describing the rule
* @returns {HostRule} the created rule
* @throws Will throw an error is a rule with the given UID already exists.
Expand Down
2 changes: 1 addition & 1 deletion types/rules/rules.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9e470a3

Please sign in to comment.