When aplescript hangs indigo is slow - rainbow wheel

Forum rules

This is a legacy forum which is locked for new topics. New topics should be started in one of the other forums under Extending Indigo

Posted on
Thu May 23, 2013 5:31 pm
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

When aplescript hangs indigo is slow - rainbow wheel

The script sometimes hangs on the curl statement - guess the wifi shield is not that stable.

When I have scheduled it in Cron indigo is fine - it does not know of the script, I guess.
When I have scheduled it from within Indigo, Indigo becomes unresponsive.

Any switches I should use and is there a way to timeout curl in less than 20 seconds (it normally takes 8 secs to run, 7 in curl)
And is there a way to find out if curl comes back successfully

Thanks

Karl


-- read data from wifi shield on-top of arduino Uno reading the alarm voltages of the inhouse alarm system of the doors and windows.
-- set variables in indigo, then send out emails in case "away" and (door is "open" or was "opened" )

set wdsData to ""
try -- read data from arduino alarm reader
set wdsData to (do shell script "curl 192.168.1.xxx -d curl=1") --- xxx is the local IP of the wifi shield
end try

set alarm_flag to (word 3 of paragraph 2 of wdsData) as text
set changed_flag to (word 4 of paragraph 2 of wdsData) as text
tell application "IndigoServer"
set value of variable "alarm_study_door" to alarm_flag
set value of variable "alarm_study_door_changed" to changed_flag
end tell

set alarm_flag to (word 3 of paragraph 3 of wdsData) as text
set changed_flag to (word 4 of paragraph 3 of wdsData) as text
tell application "IndigoServer"
set value of variable "alarm_motion_detection" to alarm_flag
set value of variable "alarm_motion_detection_changed" to changed_flag
end tell

-- etc..

Posted on
Thu May 23, 2013 5:42 pm
nsheldon offline
Posts: 2469
Joined: Aug 09, 2010
Location: CA

Re: When aplescript hangs indigo is slow - rainbow wheel

How are you executing the AppleScript from within Indigo? Which version of Indigo are you using?

Posted on
Thu May 23, 2013 6:09 pm
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: When aplescript hangs indigo is slow - rainbow wheel

as stated:
- when scheduled in crontab indigo is not impacted,
- when I schedule it with Indigo 6.0.0 b8 in / schedule / Actions / embedded Applescript
and the script hangs, Indigo becomes unresponsive





added some check at beginning of apple script to avoid running it twice:

tell application "Finder"
if POSIX file "$HOME/Documents/alarm_script_running" exists then
set hanging to "1"
else
set hanging to "0"
end if
end tell

if hanging = "1" then
tell application "IndigoServer"
set value of variable "applescript_read_alarm_hanging" to "1"
end tell
else

tell application "IndigoServer"
set value of variable "applescript_read_alarm_hanging" to "0"
end tell

do shell script "date > $HOME/Documents/alarm_script_running"

set wdsData to ""
try -- read data from arduino alarm reader
set wdsData to (do shell script "curl 192.168.1.xxx -d curl=1")
end try

set alarm_flag to (word 3 of paragraph 2 of wdsData) as text
set changed_flag to (word 4 of paragraph 2 of wdsData) as text
tell application "IndigoServer"
set value of variable "alarm_study_door" to alarm_flag
set value of variable "alarm_study_door_changed" to changed_flag
end tell
...
tell application "IndigoServer"
set value of variable "applescript_read_alarm_hanging" to "0"
end tell
end if
do shell script "rm $HOME/Documents/alarm_script_running"

Posted on
Fri May 24, 2013 12:05 am
nsheldon offline
Posts: 2469
Joined: Aug 09, 2010
Location: CA

Re: When aplescript hangs indigo is slow - rainbow wheel

kw123 wrote:
- when I schedule it with Indigo 6.0.0 b8 in / schedule / Actions / embedded Applescript...

That was the information I was looking for.

When executing AppleScript as an embedded script, it's executed in the same thread as IndigoServer itself, so if the AppleScript hangs, so does the entire Indigo system. The best way to avoid server delays due to AppleScript delays is to put the AppleScript into an external AppleScript file and execute the external script rather than an embedded one. This results in a multi-threaded script execution.
kw123 wrote:
Any switches I should use and is there a way to timeout curl in less than 20 seconds (it normally takes 8 secs to run, 7 in curl)

Yes. Use
Code: Select all
curl -m 20 192.168.1.xxx -d curl=1
The "-m 20" sets the maximum number of seconds curl is allowed to take for the entire operation before aborting.

kw123 wrote:
And is there a way to find out if curl comes back successfully

Yes. Modify your "try" statement by adding an "on error" handler. The "do shell script" AppleScript verb will throw an error if curl returns with a non-zero exit status and you can catch both the error message (what you'd see in the terminal that's output to Standard Error) as well as the numerical exit status.
Code: Select all
set wdsData to ""
set errorNumber to 0
set errorMessage to ""
try -- read data from arduino alarm reader
    set wdsData to (do shell script "curl 192.168.1.xxx -d curl=1") -- xxx is the local IP of the wifi shield
on error aMessage number anError
    set errorNumber to anError
    set errorMessage to aMessage
end try
You can then check the "errorNumber" AppleScript variable to see if it's non-zero. If it is, curl exited badly. The output of the error (and the standard curl progress bar data) will be in the "errorMessage" variable.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest