-
-
Notifications
You must be signed in to change notification settings - Fork 727
DSL Rules: example run a rule #2701
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -955,6 +955,18 @@ then | |||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| ### Run A Rule | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| A rule with UID `r2` can be executed with [RuleManager.runNow()](https://openhab.org/javadoc/latest/org/openhab/core/automation/rulemanager#runNow(java.lang.String)): | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| ```java | ||||||||||||||||||||||||||
| val ruleManagerBundleContext = org.osgi.framework.FrameworkUtil.getBundle(org.openhab.core.automation.RuleManager).bundleContext | ||||||||||||||||||||||||||
| val ruleManagerServiceReference = ruleManagerBundleContext.getServiceReference(org.openhab.core.automation.RuleManager) | ||||||||||||||||||||||||||
| val ruleManager = ruleManagerBundleContext.getService(ruleManagerServiceReference) | ||||||||||||||||||||||||||
| ruleManager.runNow("r2") | ||||||||||||||||||||||||||
|
Comment on lines
+965
to
+966
|
||||||||||||||||||||||||||
| val ruleManager = ruleManagerBundleContext.getService(ruleManagerServiceReference) | |
| ruleManager.runNow("r2") | |
| if (ruleManagerServiceReference === null) { | |
| logWarn("rules-dsl", "RuleManager service reference is unavailable; cannot run rule 'r2'") | |
| } else { | |
| val ruleManager = ruleManagerBundleContext.getService(ruleManagerServiceReference) | |
| if (ruleManager === null) { | |
| logWarn("rules-dsl", "RuleManager service is unavailable; cannot run rule 'r2'") | |
| } else { | |
| ruleManager.runNow("r2") | |
| } | |
| } |
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.
If somebody provides an example/test scenario, where OSGi returns null, or runNow() throws an exception, I will amend the example accordingly.
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.
The sample retrieves an OSGi service via
BundleContext.getService(...)but never releases it. In OSGi this increments the service use count; please wrap usage intry/finallyand callbundleContext.ungetService(ruleManagerServiceReference)(and only after confirming the reference is non-null).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.
I added
ruleManagerBundleContext.ungetService(ruleManagerServiceReference).I am not aware how
RuleManager.runNow(String)can throw an exception, so i am not going to wrap it intry {…} finally {…}.Likewise I cannot think of an example, where the returned values from OSGi are
null. Adding comparison tonullwill make the examples unnecessary complicated.