eclipse-archived / smarthome

Eclipse SmartHome™ project
https://www.eclipse.org/smarthome/
Eclipse Public License 2.0
862 stars 786 forks source link

[Automation] [JSR223] Proposal for a tiny extension of class SimpleRule #6242

Open lewie opened 5 years ago

lewie commented 5 years ago

When a rule (jsr223 with javascript) is executed, in logfiles the rule uid is logged like:

2018-09-20 11:45:10.003 [.event.RuleStatusInfoEvent] - 84eeb321-5f12-45dc-b11d-677f7c77eb1d updated: RUNNING
2018-09-20 11:45:10.005 [.event.RuleStatusInfoEvent] - 84eeb321-5f12-45dc-b11d-677f7c77eb1d updated: IDLE

I woud like to have a uid like "84eeb321-5f12-45dc-b11d-677f7c77eb1d-[ruleName or other String]"

For actions or triggers etc. from javascripts i can set their uid by ModuleBuilder.withId(uidStr) etc..

But for rules most time SimpleRule class is used, which misses a setUID function, so uid stays unset (null). Therefore uid is automatically set to a cryptic string later by RuleBuilder.create().

Is it possible to implement setUID into SimpleRule without risking anything else?

If yes, better by constructor:

public SimpleRule(String ruleUID) {
    uid = ruleUID;
    //or to get real unique:  uid = UUID.randomUUID().toString() + "-" + ruleUID;
}

or better add:

    /**
     * This method is used to specify the {@link Rule}'s uid.
     *
     * @param ruleUID the {@link Rule}'s unique identification uid, or {@code null}.
     */
    public void setUID(@Nullable String ruleUID) {
        uid = ruleUID;
    }
illnesse commented 5 years ago

Sweet, i've been wondering about that too. Hope it's not a big deal.

lewie commented 5 years ago

Nearly same time others wondered... ;-) https://community.openhab.org/t/port-jsr223-bundle-to-openhab-2/2633/167

kaikreuzer commented 5 years ago

Is this in any way related to https://github.com/eclipse/smarthome/issues/3580?

lewie commented 5 years ago

Not directly,

Rule rule = element.getUID() == null ? new Rule(generateUID()) : new Rule(element.getUID());

If Rule class is used directly, I would have the ability to set my own UID. But if I use SimpleRule, I can not specify my own UID.

It would be very convenient to be allowed to set the UID itself.

5iver commented 5 years ago

@lewie , out of curiousity, what use do you have in mind for a custom rule UID... would just adding the rule ID to the logging suffice? I was planning to do this, but it hasn't bugged me enough.

2018-09-26 05:51:54.062 [DEBUG] [org.eclipse.smarthome.automation.core.internal.RuleEngineImpl] - The rule 'c9e72d23-5b18-4c92-9c52-8832b743f1bd' is executed.
2018-09-26 05:51:57.034 [DEBUG] [org.eclipse.smarthome.automation.core.internal.RuleEngineImpl] - The trigger 'Weather_SolarRadiation-received-update' of rule 'aae0a2ac-1610-4eb2-87ab-990ebc4305ec' is triggered.
2018-09-26 05:51:57.045 [DEBUG] [org.eclipse.smarthome.automation.core.internal.RuleEngineImpl] - The rule 'aae0a2ac-1610-4eb2-87ab-990ebc4305ec' is executed.
2018-09-26 05:52:15.010 [DEBUG] [org.eclipse.smarthome.automation.core.internal.RuleEngineImpl] - The trigger 'Cron-checkMythTVStatus-af186e11c16d11e8947c001bb952f560' of rule '7ccae4dd-0dcf-41e0-9e32-e49f327b8d54' is triggered.
2018-09-26 05:52:15.067 [DEBUG] [org.eclipse.smarthome.automation.core.internal.RuleEngineImpl] - The rule '7ccae4dd-0dcf-41e0-9e32-e49f327b8d54' is executed.
2018-09-26 05:53:52.316 [DEBUG] [org.eclipse.smarthome.automation.core.internal.RuleEngineImpl] - The trigger 'Outlet5_Voltage-changed' of rule 'f6570818-d41b-4f3b-9409-fbb235361a3e' is triggered.
2018-09-26 05:53:52.327 [DEBUG] [org.eclipse.smarthome.automation.core.internal.RuleEngineImpl] - The rule 'f6570818-d41b-4f3b-9409-fbb235361a3e' is executed.
2018-09-26 05:54:05.013 [DEBUG] [org.eclipse.smarthome.automation.core.internal.RuleEngineImpl] - The trigger 'Cron-getBloomskyData-af11421ec16d11e8a7a1001bb952f560' of rule '0d7a6786-bdb6-4b54-ae74-7597cf8c174e' is triggered.
2018-09-26 05:54:05.458 [DEBUG] [org.eclipse.smarthome.automation.core.internal.RuleEngineImpl] - The rule '0d7a6786-bdb6-4b54-ae74-7597cf8c174e' is executed.
2018-09-26 05:54:45.680 [DEBUG] [org.eclipse.smarthome.automation.core.internal.RuleEngineImpl] - The trigger 'DS_MasterBedroom_Siren_Battery-changed' of rule 'ace7da59-0cfb-43f8-b82f-ff13fc1fbae8' is triggered.
2018-09-26 05:54:45.680 [DEBUG] [org.eclipse.smarthome.automation.core.internal.RuleEngineImpl] - The rule 'ace7da59-0cfb-43f8-b82f-ff13fc1fbae8' is executed.
2018-09-26 05:56:10.006 [DEBUG] [org.eclipse.smarthome.automation.core.internal.RuleEngineImpl] - The trigger 'Cron-checkForWeatherAlert-af15d600c16d11e8af0f001bb952f560' of rule '1a4c1c0b-1653-4631-a8d8-4a8f247934a0' is triggered.
lewie commented 5 years ago

@openhab-5iver, in some openHAB installations I have dozens of script files (using jsr223 JavaScript or other languages). In Paper-UI they are shown like: grafik At the moment, for Rule.name, I set the filename the Rule.description is set to the Rule's name and line number.

To identify the script file and a running rule from log files, currently it has to be searched over the cryptic UID shown in log files. The meaningless UID has to be compared via the Paper UI to find a rule. This does not make sense for me and many others. Me it buggers enough now. ;-)

2018-09-24 17:30:00.008 [.event.RuleStatusInfoEvent] - 286a0517-42ca-46ff-87ae-91e1f290b938 updated: RUNNING
2018-09-24 17:30:00.009 [.event.RuleStatusInfoEvent] - 286a0517-42ca-46ff-87ae-91e1f290b938 updated: IDLE

would be cool to get Rule.name in log files like: 286a0517-42ca-46ff-87ae-91e1f290b938.[Rule.name] This could simplest done for me, by the ability to set UID in SimpleRule class by Setter or Constructor.

5iver commented 5 years ago

At the moment, for Rule.name, I set the filename the Rule.description is set to the Rule's name and line number.

Have you tried using the rule name in addition to the description? Paper UI displays mine like this (prefixed with the script file name). Before I modified the Jython triggers.py module, they all had the name of 'triggers'.

image

Changing the logging should be a really easy and non-intrusive update... I'll look at it this weekend if you don't get to it first. I'm polishing up a major overhaul of triggers.py to use a natural language trigger decorator (like in the Rules DSL) that will accept all of the available trigger types, and a rule decorator that includes the rule ID. Everything is working except for startup and shutdown triggers, but I haven't even gotten these working yet through the raw ESH API. Rule definition...

@rule("Alert: Astronomy event")
@when("Channel astro:sun:local:eclipse#event triggered START")
@when("Channel astro:moon:local:eclipse#event triggered START")
def astronomyEvent(event):
    #do stuff

The module also puts the rule name in log entries when there are errors...

2018-09-27 05:48:20.018 [ERROR] [org.eclipse.smarthome.automation.rules.Regression Test: Test decorator Cron] - Traceback (most recent call last):
  File "/opt/openhab2/conf/lib/python/openhab/triggers.py", line 174, in execute
    self.callback(inputs.get('event'))
  File "<script>", line 81, in testDecoratorCronRegression
NameError: global name 'blah' is not defined