Call a script for every commands received

Posted on
Wed Feb 16, 2011 8:57 pm
gboudreau offline
User avatar
Posts: 10
Joined: Feb 16, 2011
Location: Montreal, Canada

Call a script for every commands received

Hi,

I'd like to control my INSTEON devices using my own state machines.
To do so, I'd need Indigo to call a script each time it receives a command. That is easy enough.
But I'd also need to know what device sent the command, and what the command is.
Is there an easy way to do this ?

Worst case scenario, I'll enable SQLite logging, and call a script on any command/any device, and the script will look in SQLite for the commands that appeared since it was last called.

Thanks.

- Guillaume Boudreau

Posted on
Thu Feb 17, 2011 10:27 am
jay (support) offline
Site Admin
User avatar
Posts: 18247
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Call a script for every commands received

Check out the "attachment sample.scpt" file in the "Attachments (Disabled)" in this folder:

/Library/Application Support/Perceptive Automation/Indigo 4/Scripts/

If those handlers are defined in any Attachment script file that's enabled (in the "Attachments" folder), they will get called every time a command is received. The address that is returned is an integer so you'll need to look it up to get the right device.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Feb 17, 2011 9:55 pm
gboudreau offline
User avatar
Posts: 10
Joined: Feb 16, 2011
Location: Montreal, Canada

Re: Call a script for every commands received

Thank you. That worked beautifully.
I created a very basic attachment script that simply calls my PHP script that handles my state machines.
I'll be sure to share my code once I'm done, though I'm not sure anyone but programmers would be able / want to use it.

Cheers.

- Guillaume Boudreau

Posted on
Sat Feb 19, 2011 6:15 am
gboudreau offline
User avatar
Posts: 10
Joined: Feb 16, 2011
Location: Montreal, Canada

Re: Call a script for every commands received

Is there an easy way to catch outgoing commands too ?
My state machines work fine now, but if I use Indigo/Indigo Touch to send commands, the machines get de-synced, since it won't receive those signals.

- Guillaume Boudreau

Posted on
Sat Feb 19, 2011 8:22 am
dstrickler offline
User avatar
Posts: 340
Joined: Oct 08, 2010
Location: Boston, MA

Code to convert decimal to hex

The code below works well as a function to convert the Indigo decimal values into their hex equivalent, but being no expert at AppleScript, I'm unsure as to where to put the dot notation commands. Right now the string returns the format "AABBCC" instead of "AA.BB.CC".

Any no, I didn't write it. Shamlessly borrowed from http://macscripter.net/viewtopic.php?pid=71052#p71052

Code: Select all
on numToHex(n, res)
   -- Round the input value to the nearest whole-number. (Just in case it's fractional.)
   set n to n div 0.5 - n div 1
   
   script o
      property hexDigits : missing value
      property hexOut : {}
   end script
   
   if (n > -1) then
      -- The number's positive.
      set o's hexDigits to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
      
      -- Construct a list of the hexadecimal digits, working backwards from the lowest-order one.
      set widthNow to 0
      repeat
         set beginning of o's hexOut to item (n mod 16 + 1) of o's hexDigits
         set n to n div 16
         set widthNow to widthNow + 1
         -- Finish when n is exhausted and the digit count is a multiple of the resolution.
         if (n is 0) and (widthNow mod res is 0) then exit repeat
      end repeat
   else
      -- The number's negative.
      set o's hexDigits to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "0"}
      set negatives to "89ABCDEF"
      
      -- Construct a list of the hexadecimal digits, working backwards from the lowest-order one.
      set widthNow to 0
      repeat
         set c to item (n mod 16 - 1) of o's hexDigits
         set beginning of o's hexOut to c
         set n to (n + 1) div 16 - 1
         set widthNow to widthNow + 1
         -- Finish when n is exhausted, the digit count is a multiple of the resolution,
         -- and the first digit in the list expresses the negative sign bit.
         if (n is -1) and (widthNow mod res is 0) and (c is in negatives) then exit repeat
      end repeat
   end if
   
   -- Coerce the digit list to string.
   set astid to AppleScript's text item delimiters
   set AppleScript's text item delimiters to "" -- this puts a delimiter on each letter (not what we want)
   set hexOut to o's hexOut as string
   set AppleScript's text item delimiters to astid
   
   return hexOut
end numToHex

Dave

Posted on
Sat Feb 19, 2011 10:32 am
gboudreau offline
User avatar
Posts: 10
Joined: Feb 16, 2011
Location: Montreal, Canada

Re: Call a script for every commands received

I have no problem convert HEX to DEC and back. I'm using PHP, not AppleScript, so it's pretty easy to convert those using the hexdec and dechex functions.
The problem I'm having is that I don't know how to trigger AppleScript code when Indigo sends an INSTEON command. I only know how to catch INSTEON commands that Indigo receives.

- Guillaume Boudreau

Posted on
Sat Feb 19, 2011 11:41 am
matt (support) offline
Site Admin
User avatar
Posts: 21422
Joined: Jan 27, 2003
Location: Texas

Re: Call a script for every commands received

Although there is the hook to catch the incoming X10/INSTEON commands from the interface, there isn't currently a hook for the outgoing commands. This is something we plan to address in the plugin architecture of Indigo 5 (still a ways off from beta though).

Current workarounds, which aren't great, are to do something like what the IndigoSqlClient does. It is a python based (source code is in that folder on your Mac) Indigo Server client that catches the device state change messages from the Indigo Server. If you don't just want device state transitions caught, then it also catches the Event Log messages. Another approach altogether is to watch the Event Log file itself. None of these approaches are that straightforward though, which is why we hope to make it much easier in the future.

Image

Posted on
Fri Jan 25, 2013 5:35 am
nexx offline
Posts: 56
Joined: Jun 27, 2010

Re: Call a script for every commands received

matt (support) wrote:
Although there is the hook to catch the incoming X10/INSTEON commands from the interface, there isn't currently a hook for the outgoing commands. This is something we plan to address in the plugin architecture of Indigo 5 (still a ways off from beta though).


Was it addressed in indigo 5?

Posted on
Fri Jan 25, 2013 10:35 am
jay (support) offline
Site Admin
User avatar
Posts: 18247
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Call a script for every commands received

Check out the "Example INSTEON/X10 Listener" plugin in the SDK - it shows how to subscribe to various commands as they come/go.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests