Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Feature/triggers #1936

Merged
merged 26 commits into from
Sep 24, 2016
Merged

Feature/triggers #1936

merged 26 commits into from
Sep 24, 2016

Conversation

phxql
Copy link
Contributor

@phxql phxql commented Jul 28, 2016

This PR implements a PoC for triggers.

Sample XML snippet:

    <channel-type id="trigger_1" advanced="false">
        <trigger-type>String</trigger-type>
        <label>Trigger 1</label>
        <description>Description of trigger 1</description>
        <event>
            <options>
                <option value="PRESSED">pressed</option>
                <option value="RELEASED">released</option>
                <option value="DOUBLE_PRESSED">double pressed</option>
            </options>
        </event>
    </channel-type>

In the handler:

emitEvent(TRIGGER_1, new StringType("PRESSED"));

In the rule engine DSL:

rule "Test the trigger"
when
  Channel 'triggertest:sample:9c9a498d:trigger_1' triggered PRESSED
then
  DemoString.postUpdate("Triggered: " + receivedEvent.event)
end

* @throws IllegalArgumentException if the UID or the item type is null or empty,
* or the the meta information is null
*/
public ChannelType(ChannelTypeUID uid, boolean advanced, String itemType, String triggerType, String label,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say this new constructor should NOT have itemType and state params, so that it is the one used for creating instances of trigger channels.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ChannelType has two constructors: The first one for use without triggerType, the second for use withTriggerType (itemType is then null). I can't add a constructor only for triggerType, as itemType and triggerType are both String and then it would clash with the itemType constructor.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, ok.

@kaikreuzer
Copy link
Contributor

Thanks @phxql Looks in general good to me as a starting point - and it isn't that much code change after all.

What seems to be still completely missing is the adaption of the Channel class itself, right? To fully see the implications, we would need the triggers to be considered there as well as in the REST API and the thing file definitions.

@phxql
Copy link
Contributor Author

phxql commented Aug 2, 2016

What do you mean with thing file definitions? The thing-types.xml?

@kaikreuzer
Copy link
Contributor

@phxql
Copy link
Contributor Author

phxql commented Aug 2, 2016

Ah, the DSL. I'll start with the adaption of the channel class and the REST interface, as i know that already.

@phxql
Copy link
Contributor Author

phxql commented Aug 23, 2016

I updated the PR:

  • Items are only created for channels which have an item type
  • The ChannelDTO for the REST API now includes a triggerType
  • Declared ThingEventTrigger in the JSON for the ruleengine
  • Added TRIGGER rules to the rule engine and updated the DSL.
rule "Test the trigger"
when
  Channel 'triggertest:sample:9c9a498d:trigger_1' triggered PRESSED
then
  DemoString.postUpdate("Triggered: " + receivedEvent.event)
end

@phxql
Copy link
Contributor Author

phxql commented Aug 23, 2016

Open issues:

  • RuleEngineImpl#executeRules(java.lang.Iterable<Rule>, Type): Add the trigger event to the DSL context.

@sjsf
Copy link
Contributor

sjsf commented Aug 24, 2016

Having had a quick glance, the approach still looks good so far.

However, I'm missing a change in Thing.xtext and GenericThingProvider so that it becomes possible to define the channels in the Thing DSL.

@phxql
Copy link
Contributor Author

phxql commented Aug 24, 2016

Proposal:

Thing channel with item type String

Thing yahooweather:weather:losangeles [ location="2442047", unit="us", refresh=120 ] {
    Channels:
        String : customChannel1 []
}

That's the syntax right now.

Thing channel with item type String

Thing yahooweather:weather:losangeles [ location="2442047", unit="us", refresh=120 ] {
    Channels:
        Item String : customChannel1 []
}

Thats an alternative syntax for the same usecase.

Thing channel with trigger type String

Thing yahooweather:weather:losangeles [ location="2442047", unit="us", refresh=120 ] {
    Channels:
        Trigger String : customChannel1 []
}

Thats the syntax for adding a trigger channel.

@phxql
Copy link
Contributor Author

phxql commented Aug 24, 2016

Wrote a gist containing the changes: https://gist.github.com/phxql/e75b287ab8601d43d63d553175e6b877

@@ -92,6 +92,7 @@ public void receive(Event event) {
if (callback != null) {
logger.trace("Received Event: Source: " + event.getSource() + " Topic: " + event.getTopic() + " Type: "
+ event.getType() + " Payload: " + event.getPayload());
// TODO MKA: Why is the topic checked against the source and not event.getSource()?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simply because for some event types the event source is not the item (or even not set...)

@sjsf
Copy link
Contributor

sjsf commented Aug 25, 2016

@maggu2810, @kaikreuzer could you please have a look?

@maggu2810
Copy link
Contributor

@SJKA Sure, it is already placed somewhere in my PriorityQueue 😉

@@ -93,7 +93,7 @@ public void receive(Event event) {
if (callback != null) {
logger.trace("Received Event: Source: " + event.getSource() + " Topic: " + event.getTopic() + " Type: "
+ event.getType() + " Payload: " + event.getPayload());

// Check the topic for the source as the event source is not set for the most events.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty misleading - the event.source is something completely different than the "source" configuration property in this handler (which means "object that this event refers to", but not "sender of the event").

@phxql
Copy link
Contributor Author

phxql commented Sep 8, 2016

Added system triggers.

@@ -0,0 +1,21 @@
package org.eclipse.smarthome.core.thing;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

license header missing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks :-)

Signed-off-by: Moritz Kammerer <moritz.kammerer@qaware.de>
@marcelrv
Copy link
Contributor

marcelrv commented Sep 11, 2016

@kaikreuzer

"no feedback" from my person should be interpret as "still no time to review the trigger stuff" 😉

Here as well, bit challenged in time... will take a look shortly

Copy link
Contributor

@maggu2810 maggu2810 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't tested the code, but code review done with some minor comments.

*
* @param uid the unique identifier which identifies this Channel type within
* the overall system (must neither be null, nor empty)
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you add an empty line after every @param tag?
Please remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@@ -29,112 +31,121 @@ import org.slf4j.LoggerFactory
*/
class RulesJvmModelInferrer extends ScriptJvmModelInferrer {

static private final Logger logger = LoggerFactory.getLogger(RulesJvmModelInferrer)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know anything about Xtend / Xtext, but does we need to use a static variable here?

But you should use the common modifier order: private static final (or drop static).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Signed-off-by: Moritz Kammerer <moritz.kammerer@qaware.de>
Signed-off-by: Moritz Kammerer <moritz.kammerer@qaware.de>
Signed-off-by: Moritz Kammerer <moritz.kammerer@qaware.de>
@kaikreuzer
Copy link
Contributor

Thanks for the review @maggu2810.
I think we are ready to merge then (@marcelrv & others, if you have comments/change requests, we can still address those in follow-up PRs). I'll wait for another day in case anyone still wants to veto.

@kaikreuzer
Copy link
Contributor

I just noticed that this PR has >1000 lines, so we anyhow cannot merge immediately - I have created CQ 12006 for it.
@phxql Please DO NOT add any further commits to your branch; we need to keep this exact state for the CQ to be valid. Any further changes would then need to go into new PRs.

@kaikreuzer kaikreuzer added CQ and removed CQ labels Sep 21, 2016
@kaikreuzer
Copy link
Contributor

CQ has been approved!

@digitaldan
Copy link
Contributor

I am looking at this for the HarmonyHub binding now. I can see their use in rules, but how does a thing handler respond to a trigger channel?

To give a little background, The HarmonyHub has a thing type called "device" , think of this as your TV or Stereo. Each "device" has a channel called "buttonPress", think of this as pressing a button on your remote control. This channels sends the name of the button to press as a string, so "Power On" or "ENTER" for example. This value are dynamically created for the channel as State Options. So for example:

DeviceThing -> ButtonPressChannel -> ButtonName {PowerOn, PowerOff, Enter, 1, 2, 3, 4 ...}

I can see how I could use trigger events to send the button names, but how would the ThingHandler get these events. Also how would a user define a trigger channel in say a sitemap? Should I rather be looking on how to compliment my current button channel just for rules or is this meant as a replacement for this type of "Send Only" channel.

@kaikreuzer
Copy link
Contributor

It seems I misinterpreted the "buttonPress" channel in the HarmonyHub binding when I suggested you to look at the trigger channels. Trigger channels are ONLY meant for events coming from the remote device, i.e. you get hold of an external button press on some remote control.
In the Harmony case, you actually want to trigger a button press yourself - for this, you will have to use "regular" channels as you do. What doesn't fit well is to use "state options", because in effect, what you need would be "command options" instead.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants