Applescript snippit using curl to set variables in Indigo

Posted on
Tue Oct 12, 2010 10:18 am
baadpuppy offline
User avatar
Posts: 16
Joined: Oct 01, 2010
Location: Virginia

Applescript snippit using curl to set variables in Indigo

As part of my home automation, I'm using Phone Amego, with a pair of scripts (EnterProximity.scpt and LeaveProximity.scpt). I have this running on both my home Indigo server and my work desktop. Due to a bit of network trickery, the work machine can talk to the home machine via the remote interface. I'm using curl within applescript to set a variable to the hostname of the computer. A trigger on changes to that variable looks to see if it is a "home" computer or not, and sets the atHome variable based on that.

Here's the snippet that sets an Indigo variable via applescript:
Code: Select all
on setIndigoVariable(theVariable, theValue)
   set theProtocol to "http" -- vs https
   set theServer to "127.0.0.1" -- dns name will work if it is resolvable
   set thePort to "8176" -- the Indigo default, change this to match your settings
   set theUserPass to "myUsername:myPassword" -- your Indigo remote access username/password
   set theURL to theProtocol & "://" & theServer & ":" & thePort & "/variables/" & theVariable
   do shell script "/usr/bin/curl --digest -u " & theUserPass & " -X PUT -d value=" & theValue & " " & theURL
   return ""
end setIndigoVariable


the rest of my EnterProximity.scpt file looks like this:

Code: Select all
set PROX to "Here: "

set {computer name:compName, home directory:userHome} to system info

set currTime to current date

set {LF, CR} to {ASCII character 10, ASCII character 13}

set theFilePath to (userHome as string) & "Dropbox:personal:proximity:" & compName & ".txt"
set theFileReference to open for access theFilePath with write permission
set theText to PROX & (currTime as string) & LF
write theText to theFileReference starting at eof
close access theFileReference

setIndigoVariable("jimIphoneLocation", compName)


Hopefully some of this will be useful to others.

jim

Posted on
Tue Oct 12, 2010 10:46 am
jay (support) offline
Site Admin
User avatar
Posts: 18245
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Applescript snippit using curl to set variables in Indig

So, you're using the RESTful API to set the variable because then you can use the same script on both the local Indigo server and your remote Mac (with minor changes)? On the remote Mac you change the "theServer" variable to the host name of your Indigo server machine (if you're using prism you could use that as the host as well).

Very interesting - thanks for the contribution.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Oct 12, 2010 11:44 am
baadpuppy offline
User avatar
Posts: 16
Joined: Oct 01, 2010
Location: Virginia

Re: Applescript snippit using curl to set variables in Indig

jay wrote:
So, you're using the RESTful API to set the variable because then you can use the same script on both the local Indigo server and your remote Mac (with minor changes)? On the remote Mac you change the "theServer" variable to the host name of your Indigo server machine (if you're using prism you could use that as the host as well).

Very interesting - thanks for the contribution.


I actually cheat a little bit... my firewall at home is m0n0wall. It lets you set any name to IP mapping you want internally, and all the internal machines trust its answers. So, for the hostname I use, internal machines get the internal IP. The external machines get a CNAME to a dyndns name. The script lives in Dropbox, so there's only 1 version of it. Changes propagate fairly rapidly.

I am planning to do the apache https proxy in front of it that I read about on this forum in the near future. When I do that, I can just update the http to https, and make sure the username and password are correct, and the port number, and within a few minutes all the machines will be updated.

By using a real domain name, and short circuiting the path on the internal home network, I don't need any vpn software or anything to make it work. I still use a vpn connection into my m0n0wall for the actual indigo client.

jim

Posted on
Tue Oct 12, 2010 3:52 pm
jay (support) offline
Site Admin
User avatar
Posts: 18245
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Applescript snippit using curl to set variables in Indig

Mrrr?

Now I'm really confused: if the script is local to the IndigoServer machine, why are you using the RESTful API? Wouldn't it be easier to just:

Code: Select all
on setIndigoVariable(theVariable, theValue)
    tell application "IndigoServer"
        set value of variable theVariable to theValue
    end tell
end setIndigoVariable

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Oct 12, 2010 5:44 pm
baadpuppy offline
User avatar
Posts: 16
Joined: Oct 01, 2010
Location: Virginia

Re: Applescript snippit using curl to set variables in Indig

ah... I guess I wasn't too clear.

The script lives within my dropbox. Damia, which is currently running the Indigo server, and Aimee, the mac mini I use at work are the two machines running the proximity script. Both run the identical script.

I have a DNS name for my indigo server. For the sake of argument (and to try to keep details private), let's say it is homeautomation.example.com. That name is a CNAME pointing to a dyndns name. The dyndns name of course matches the public IP of the home network, which has a NAT rule allowing inbound traffic to Indigo. Everything is configured to talk to homeautomation.example.com, and it just works, whether I'm home or out and about.

Inside the home network, homeautomation.example.com resolves to a 192.168.x.x address (damia's IP to be precise). Outside, it goes through the CNAME to the dyndns name, and gets there.

In the future, damia will not be the server, but it will continue to be the bluetooth proximity detector. I have a mac mini I'm prepping to become the new Indigo server, and it will live outside of bluetooth range.

Using the RESTful API for a local script doesn't make much sense. Using it for a location agnostic script makes more sense.

Originally, the proximity script only ran at work, and I added the Indigo stuff in later. I wrote it to make it easier to fill in my timesheet, so I'd know my actual presence hours without having to write it down all the tie.

Also, without an instance of IndigoServer on the machine editing the applescript, the applescript editor butchers the scripts if you do it the local way.

I like having a single script that behaves as expected no matter where I run it.

jim

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 5 guests