Possible to use transform in a rule?

I am trying to use the Design Pattern to clean a rules files I have. My most complicated rule involves using my door sensors and cameras to alert me by sending a text message with a picture if a door is opened and the alarm is “armed”. So, here is a example of a rule for one of my doors:

rule "front door opened"
when
    Item Front_Door changed from CLOSED to OPEN
then
	if(Alarm.state==ON){
	var FrontdoorcamO = "frontdoorO.jpg"
	var frontdoormessageO = "Front door opened at "+ now.toString ("HH:mm dd-MM-yyyy")
		executeCommandLine("wget http://X.X.X.X:XXXX/picture/3/current -O /tmp/" + FrontdoorcamO)
        Thread::sleep(5000)
       		sendMail("XXXXXXXXXX@vzwpix.com", "Contact", frontdoormessageO, "file:///tmp/" +FrontdoorcamO)
			Fault.postUpdate("ALERT - Front Door Has Been Opened")}
				else if(Alarm.state==OFF){
					Fault.postUpdate("Front door has been opened")}
end

I have to replicate this rule 5 more times for the other external doors. Right now, I have cut and pasted this rule for the others, changing the door names.
I started to re-write this rule using gDoors group, and I think I have that part resolved, but the problem I am running in to is the :

executeCommandLine("wget http://X.X.X.X:XXXX/picture/3/current -O /tmp/" + FrontdoorcamO)

command. The “3” part of this command is specific to each camera (different number for each camera). So to consolidate, and have one rule for all six doors, I need to assign the camera number that is the closest to each triggering item and do something like:

executeCommandLine("wget http://X.X.X.X:XXXX/picture/"+triggeringItem.name"/current -O /tmp" +Camera)

but have the triggeringItem.name transformed to the camera number that has a view on the door. What would be the best way to go about this? I attempted to write the rule as:

rule "a door is opened"
when
	Member of gDoors changed from CLOSED to OPEN
then
	if(Alarm.state==ON)
	val Front_Door = "3"
	val Alcove_Door = "7"
	val GarageSide_Door = "5"
	val Bulkhead_Door = "6"
	val Kitchen_Door = "7"
    var String name = triggeringItem.name.toString.split("_").get(0)
	var Camera = name+".jpg"
	var MessageO = (name + "opened at "+ now.toString  ("HH:mm dd-MM-yyyy")	
    var Command = ("wget http://X.X.X.X:XXXX/picture/")+triggeringItem.name("/current -O /tmp/" +Camera)
		executeCommandLine(Command)
		Thread::sleep(5000)
		sendMail("XXXXXXXXXX@vzwpix.com". "Contact", MessageO, "file:///tmp/" +Camera)
    else if(Alarm.state==OFF){
				Fault.postUpdate(name + "has been opened")}
end

I am getting no errors showing in VS Code, but this shows up in the log:

2018-07-25 13:10:35.425 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'Doors-Test.rules' has errors, therefore ignoring it: [7,2]: no viable alternative at input 'val'
[15,5]: missing ')' at 'var'
[18,37]: no viable alternative at input '"Contact"'
[18,46]: mismatched input ',' expecting 'end'

I am clearly missing something… any help would be appreciated!

~John

Use ms Code tomorrow write your coded. It will be highlight the problem.

Why not put the camera name/number in door name and parse it out?

Your error…

Sorry, see you are

I forgot the other “)” at the end… I just corrected that… Looking at your suggestion to parse out a camera number by including it in the name now.

This is demonstrated in Design Pattern: Encoding and Accessing Values in Rules Approach 3.

1 Like

@rlkoshak,
That pointed me in the right direction!

Here is the working version of the rule:

rule "a door is opened"

when
	Member of gDoors changed from CLOSED to OPEN
then
    if(Alarm.state==ON)	{
        var String name = triggeringItem.name.toString.split("_").get(0)
        var String Viewpoint = transform("MAP", "camera.map", name)
        var Camera = name+".jpg"
        var MessageO = (name + " door opened at "+ now.toString  ("HH:mm dd-MM-yyyy"))	
        var Command = ("wget http://X.X.X.X:XXXX/picture/"+Viewpoint)+("/current -O /tmp/" +Camera)
		    executeCommandLine(Command)
		    createTimer(now.plusSeconds(1), [ |
		    sendMail("XXXXXXXXXX@vzwpix.com", "Contact", MessageO, "file:///tmp/" +Camera)])}
    else if(Alarm.state==OFF){
	    Fault.postUpdate(name+" has been opened")}
end