When aplescript hangs indigo is slow - rainbow wheel

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
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
User avatar
kw123
Posts: 8705
Joined: Sun May 12, 2013 4:44 pm
Location: Dallas, TX
Contact:

When aplescript hangs indigo is slow - rainbow wheel

Post by kw123 »

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..
nsheldon
Posts: 2469
Joined: Mon Aug 09, 2010 2:12 pm
Location: CA
Contact:

Re: When aplescript hangs indigo is slow - rainbow wheel

Post by nsheldon »

How are you executing the AppleScript from within Indigo? Which version of Indigo are you using?
User avatar
kw123
Posts: 8705
Joined: Sun May 12, 2013 4:44 pm
Location: Dallas, TX
Contact:

Re: When aplescript hangs indigo is slow - rainbow wheel

Post by kw123 »

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"
nsheldon
Posts: 2469
Joined: Mon Aug 09, 2010 2:12 pm
Location: CA
Contact:

Re: When aplescript hangs indigo is slow - rainbow wheel

Post by nsheldon »

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.
Locked

Return to “Extending Indigo with Plugins and Python”