This page last changed on Mar 08, 2016 by mdar.

Hello

I'm working with an Android app programmer to create a Text_To_Speech background app to convert http strings to speech.

When the app is available in Google Play I'll paste the link here.

To prepare, I'm trying to get my head around how a rule should be formatted to trigger from a single source, for example when my phone connects to my home network, then have a command constructed from multiple sources.

For example, the heating mode and number of voice mail messages.

I've mashed this rule together, could someone far better with Drools cast an eye over it and rearrange it to work please?


package org.openremote.controller.protocol;

global org.openremote.controller.statuscache.CommandFacade execute;

import java.util.*;



rule "Heating Mode and Answer machine Text to Speech 01"

when

  $evt0:Event(source matches "Owner_Present", $source0 : source) // This returns PRESENT or ABSENT

   $evt1:Event(source matches "01-52-HeaterMode", $source1 : source) // This returns COMFORT, DAY, NIGHT or SAFE

   $evt2:Event(source matches "Answer_Phone_MSG_Count", $source2 : source) // This returns a number value




then

    String OwnerStatus = $evt0.getValue().toString();
    boolean isActive = buttonStatus != null && buttonStatus.equalsIgnoreCase("PRESENT"); // I only want the rule to operate when the owner returns, I.E. becomes "PRESENT" on the LAN

    System.out.println("Resident is :" + OwnerStatus);



  String Source1Str = $source1.toString();


  String Source2Str = $source2.toString();


  execute.command("memo_text", "Welcome Home, Heating is in"  + Source1Str + " mode: Number of Answer phone messages is" + Source2Str);


  execute.command("TextToSpeech_Android_01", " Welcome Home, Heating is in"  + Source1Str + " mode: Number of Answer phone messages is" + Source2Str);



end

Many thanks in advance.

Stuart

Stuart
May be something missing in your headers? I have asked in the past for an explanation about which ones to include. Never found a satisfying Howto

I ended up adding the whole caboodle I found in other examples
From the little understanding I have, I think you may be missing

import org.openremote.controller.model.event

Pieter

Posted by pz1 at Mar 08, 2016 09:17

If there are no compilations errors then no import is missing. In this case

import org.openremote.controller.model.event

is not necessary because Event() is visible in

package org.openremote.controller.protocol;
Posted by aktur at Mar 08, 2016 11:38

Hi Stuart, the very good rule of thumb is that most logic conditions should be solved in the LHS (between when ... then). On the RHS very simple statements should be used, best one liners.

Anyway, you want trigger a rule WHEN you come home, so you need to detect when "Owner_preset" becomes "PRESENT". Therefore, you need to catch the previous state of "Owner_present" sensor and compare it with the current state. For the previous state you can either use in-memory virtual variable or internal Drools fact. I prefer the second methodology, but the first approach has its advantage too as you can link the variable with a sensor and see it in UI, making debugging a bit easier. Enough intro, lets code

Below are example of rules I would use as a start, not tested by me so you can spot some syntax errors. Nevertheless, the idea should give you some food to think.

package org.openremote.controller.protocol;
global org.openremote.controller.statuscache.CommandFacade execute;

declare OwnerPresent
  state: String
end

rule "Create OwnerPresent internal fact"
// It will be executed only once when controller starts
then
  insert(new OwnerPresent("n/a"));
end

when "Fetch previous present state"
salience -10 // do this after TTS rule fired
when
  Event(source=="Owner_Present", $v: value)
  $op : OwnerPresent(state != $v)
then
  $op.setState($v.toString());
  update($op); // Lets Drools know that the fact has changed
end

rule "Heating Mode and Answer machine Text to Speech 01"
when
  Event(source=="Owner_Present", value=="PRESENT") // This returns PRESENT or ABSENT
  OwnerPresent(state=="ABSENT")
  Event(source matches "01-52-HeaterMode", $v1 : value) // This returns COMFORT, DAY, NIGHT or SAFE
  Event(source matches "Answer_Phone_MSG_Count", $v2 : value) // This returns a number value
then
  execute.command("memo_text", "Welcome Home, Heating is in"  + Sv1.toString() + " mode: Number of Answer phone messages is" + $v2.toString());
  execute.command("TextToSpeech_Android_01", " Welcome Home, Heating is in"  + $v1.toString() + " mode: Number of Answer phone messages is" + $v2.toString());
end
Posted by aktur at Mar 08, 2016 12:04

Thanks Michal,

When we get the android TextToSpeech via IP app finished, I'll post the results on here for all to see.

Cheers,

Stuart

Posted by mdar at Mar 13, 2016 13:44
Document generated by Confluence on Jun 05, 2016 09:33