Sending an HTTP Request

Whenever my entrance door is opened, I want to send a 30 seconds video to my email.

In order to accomplish that, I have a batch file that that checks what is the latest video file my camera captured, waits for 30 seconds (for the recording to end) and email me that file.

In order to execute that batch file, I have created a PHP page on my computer’s local web server, that simply executes the file.

Having read the issue about 5 threads, and the recommendation to use a timer, whenever the entrance door is opened, I execute the following:

createTimer(now.plusSeconds(1),  [ |
   sendHttpGetRequest("http://192.168.1.111/SendMailOpenHAB.php")
])

While I don’t care about the HTTP response, I get an error that it takes more than 5000ms.

How can I overcome this error?
Is there a better way to achieve what I want?

Add a timeout parameter so it will wait longer than 5 seconds for the call to that php to return.

sendHttpGetRequest("http://192.168.1.111/SendMailOpenHAB.php", 10000)
1 Like

Reading many of your other posts and recommendations, about making sure rules run fast, not use sendHttp*Request, etc, and since the batch file is running for ~40 seconds (waiting for the 30 seconds video to finish recording), I am using the following to call the PHP file:

createTimer(now.plusMillis(1),  [ |
        logInfo("EntranceDoorOpened", "BEGINNING: Running email batch file - Home Empty")
        sendHttpGetRequest("http://192.168.1.111/SendMailOpenHAB.php", 40000)
        logInfo("EntranceDoorOpened", "END: Running email batch file - Home Empty")   
])

I understand this means that the rule does not wait for the HTTP call, as the call is done in a different thread.

1. Does the long wait for the call (40 seconds) can still cause any issues (slowness, high CPU, etc.)?

I read in many posts you wrote, that there is a limit of 5 rule threads that can run at the same time.
2. Is there a limit on the number of “createTimer” threads that can run at the same time?

Since I don’t really care what is the response the HTTP call gets (the PHP file executes the batch file that sends the video) -
3. would you recommend modifying the HTTP call, so it will not wait that long?

Thanks in advance.

You’ve shifted the problem from hogging a rules thread to hogging a timer thread.
That can mean that other timers becoming due cannot start executing.

I think you can use HTTP binding for fire-and-forget, because it uses a different timeout mechanism.

I suppose you could use HTTP action with a very short timeout, inside a try-catch that suppresses the error message.

What is the limit on the number of timers?

I don’t mind the error appearing in the log.

Do you see a problem reducing the HTTP timeout to 1 sec, thus making the HTTP call and receiving the error after 1 second, but still the PHP would run my batch file?

createTimer(now.plusMillis(1),  [ |
        logInfo("EntranceDoorOpened", "BEGINNING: Running email batch file - Home Empty")
      sendHttpGetRequest("http://192.168.1.111/SendMailOpenHAB.php", 1000)
        logInfo("EntranceDoorOpened", "END: Running email batch file - Home Empty")   
])

Well, using the 1mS createTimer here is pointless really.

You’re only firing off an HTTP request, whether that triggers a script or makes a bank payment makes no difference to openHAB. It’s only going to wait as long as the timeout to see if anything comes back.
Have you tried 0 out of interest? Not clear from docs if it might work.

Re Timers; you can have as many as you wish awaiting their moment, but I think only two executing their code. Hence the advice not to hog there.

I’d be inclined to do the small work to suppress an expected error message - wood/trees for the next issue.

As rossko57 indicates, this only moves the problem. There are two thread pools, one for Rules that has 5 by default and another for Timers that has 2 by default. You are likely to have fewer Timer actually executing at the same time than Rules so this might be better. Or it could be worse if you have lots of timers. NOTE that some bindings use threads out of the Timer thread pool too like Astro. cron triggered Rules use these threads too.

Yes, 2. Note that they are only using a thread when they are actively running. When they are awaiting their trigger time they do not consume and threads.

The best solution would be if you can modify the php script to not block until the batch file finishes. If the php script can kick off the script and immediately return and do all that extra work in the background it would be far better over all.

Using the HTTP binding like rossko57 recommends indeed would probably be a better approach, though there would be no way to ever suppress the error from the logs short of configuring the logger to not log any errors from the HTTP binding I think.

Thank you @rossko57 and @rlkoshak for spending your time, assisting me and sharing all the information.

That is a great idea, and I made this change in the PHP script.
I have also modified the HTTP call to wait only 1000ms:

sendHttpGetRequest("http://192.168.1.111/SendMailOpenHAB.php", 1000)

This will still allow me to see if there is an error whenever the called server is not responding.

Would you two still recommend the HTTP binding approach also now, when the PHP automatically provides a response?

Please note that even with the HTTP binding I will still have to use the timers, since the same rule makes several HTTP calls, which should be in a specific order along 15 seconds.

This is the full picture:

rule EntranceDoor Opened
when
   Item iEntranceDoorSensor changed to OPEN
then
        createTimer(now.plusMillis(1),  [ |
                sendHttpGetRequest(". . . . .", 1000)  // Move camera to face door
        ])
        createTimer(now.plusSeconds(1),  [ |
                sendHttpGetRequest(". . . . .", 1000)  // Start Video recording
        ])
        createTimer(now.plusSeconds(1),  [ |
                sendHttpGetRequest(". . . . .", 1000)  // Run PHP file that checks what is the latest file NOW but sends it in 30 seconds
        ])
        createTimer(now.plusSeconds(15),  [ |
                sendHttpGetRequest(". . . . .", 1000)  // Stop Video recording
        ])
end

Thanks again for your assistance and tips.

My personal Rule of thumb is if I can have fewer lines of code in Rules it’s better. I’m not sure it saves you much in this case. I wouldn’t change the working Rule.

However I have some observations.

I’d get rid of the first timer (now.plusMillis(1)) and just call the HTTP command.

The next two timers go off at basically the same time, so why not combine them and just use one Timer that calls both?

For me to understand & learn:
Wouldn’t this mean that if it takes the camera 1000ms to respond to the move request, it will make the rule wait for a whole second?
If my understanding is correct, why would you prefer the rule to wait for 1 second over using a timer thread? Because there are only 2 timer threads and 5 rule threads?

You are correct.
The reason for this is that the “Start Video recording” and “Stop video recording” are at the same part of the code, while the “Run PHP file” is in a later part of the code, which calls the PHP file with a parameter, based on an “if” clause.

Based on reading your Design Pattern: How to Structure a Rule, I assume I should build the HTTP string in the “if” clause, and only then create a single timer with the 2 HTTP calls:
One “static” to Start Video recording
Second “dynamic” (which was built in the “if” clause) to run the PHP file.

Am I correct?

EDIT:

While waiting for your answer, I tried modifying the rule, but I receive an error in VS.

The error is:

Cannot refer to the non-final variable httpPHPStr inside a lambda expression

The modified rule:

rule EntranceDoor Opened
when
   Item iEntranceDoorSensor changed to OPEN
then
        createTimer(now.plusMillis(1),  [ |
                sendHttpGetRequest(". . . . .", 1000)  // Move camera to face door
        ])

        var httpPHPStr = "http://192.168.1.111/SendMailOpenHAB.php?id=3"   // Parents at home

        if (ParentsNotAtHome) {
             if (KidsAtHome) {
                  httpPHPStr = "http://192.168.1.111/SendMailOpenHAB.php?id=1"  // Kids Home Alone
             } else {
                  httpPHPStr = "http://192.168.1.111/SendMailOpenHAB.php?id=2"  // Home is Empty
            }
       }

        createTimer(now.plusSeconds(1),  [ |
                sendHttpGetRequest(". . . . .", 1000)  // Start Video recording
                sendHttpGetRequest(httpPHPStr, 1000)  // Run PHP file that checks what is the latest file NOW but sends it in 30 seconds
        ])
        createTimer(now.plusSeconds(15),  [ |
                sendHttpGetRequest(". . . . .", 1000)  // Stop Video recording
        ])
end

I assume this is because the httpPHPStr variable is not defined within the createTimer.
How should I tackle this?
Or does it mean I cannot use your DP in this case?

Rick can give you the “why” about lambdas and context, but the overall effect is that you cannot pass vars into timer lambdas - the code between [ ]. This is the “non-final” thing it’s whinging about.

You can pass vals - but have to observe the limitation that you cannot modify a val after creation, it’s like a constant.

Unrelated consideration; do you want to evaluate conditions at the time you create the timer, or at the moment it executes? Doesn’t matter a lot when it’s just a few seconds, but it can do, and then you’d need to structure as required.

This should work, note use of val and var

             // only one place to set URL, easier to maintain
        val httpPHPStr = "http://192.168.1.111/SendMailOpenHAB.php"

        createTimer(now.plusSeconds(1),  [ |
                sendHttpGetRequest(". . . . .", 1000)  // Start Video recording
                var httpPHPStrParam = httpPHPStr + "?id=3"   // Parents at home
                if (ParentsNotAtHome) {
                    if (KidsAtHome) {
                       httpPHPStrParam = httpPHPStr + "?id=1"  // Kids Home Alone
                    } else {
                       httpPHPStrParam = httpPHPStr + "?id=2"  // Home is Empty
                    }
                }
                sendHttpGetRequest(httpPHPStrParam, 1000)  // Run PHP file that checks what is the latest file NOW but sends it in 30 seconds
        ])

I’m a bit confused with the overall task - if you have control of Start and Stop recording, why not do the grab-and-post part after recording has stopped?