View unanswered posts | View active topics It is currently Sun May 19, 2013 11:05 pm



Reply to topic  [ 13 posts ] 
 IR receiver option in the UK 
Author Message

Joined: Feb 29, 2012
Posts: 19
Post IR receiver option in the UK
Hi,

Keen to get my Harmony remote to trigger actions and so control my lights. However, I am UK-based so the irlinc doesn't seem to be an option. Anyone found anything else that would do this (and that's not ridiculously expensive)?

LM


Wed Feb 29, 2012 1:06 pm
Profile

Joined: Nov 26, 2009
Posts: 923
Location: Kalispell, MT
Post Re: IR receiver option in the UK
http://www.irtrans.de/en/shop/usb.php

Carl


Thu Mar 01, 2012 12:52 am
Profile

Joined: Feb 29, 2012
Posts: 19
Post Re: IR receiver option in the UK
Carl - Thanks. Still looks expensive for what is really a very simple device - especially the wifi version which is what I would need.

I've had a few alternative thoughts:
1. Use an Arduino. Should be easy enough to assemble the necessary components - IR receiver and wifi transmitter - but does anyone had any experience with this? I've very little coding experience, so not sure how I would make the Arduino talk to Indigo.

2. Use my Squeezebox. It is already very used to receiving IR commands from my remote control, and it's also hooked up to my wireless network. In principle, it ought to be possible for it to receive infrared commands and then trigger Indigo commands. Again, this gets beyond my coding skills but has the great advantage of needing no new hardware.

Unfortunately on 2, I haven't even managed to get Indigo to talk to my Squeezebox properly, so the idea of getting a clever communication in the other direction is a long way off.

LM


Fri Mar 02, 2012 4:36 am
Profile

Joined: Nov 25, 2010
Posts: 172
Location: UK
Post Re: IR receiver option in the UK
Itrans is also good at sending ir. I use it to control tv and window blinds. Easily integrates into indigo, but not a cheap solution.

I'm considering another couple of units for elsewhere in the house


Fri Mar 02, 2012 12:24 pm
Profile

Joined: Nov 26, 2009
Posts: 923
Location: Kalispell, MT
Post Re: IR receiver option in the UK
No doubt for an experienced programmer hacking up some code and hardware to
get an Arduino to do IR would be interesting. I see some examples of it online
but my 2 cents says pop for the IRTrans. It handles virtually any type of IR code,
can even paste in codes from Remote Central etc. That and being able to manage/backup
everything is a huge plus.

I have SB Radio that I have working well with Indigo. I'd be happy to share my setup
with you to get that working....no idea about it receiving/sending IR out to Indigo.

Carl


Fri Mar 02, 2012 1:25 pm
Profile

Joined: Feb 29, 2012
Posts: 19
Post Re: IR receiver option in the UK
Carl

Thanks - I would certainly like to take you up on your offer of help on squeezecentre. Could you post your script?

On the IRTrans I suspect I will end up going that way, but like the idea of some kind of hack.

LM


Fri Mar 02, 2012 1:39 pm
Profile

Joined: Nov 26, 2009
Posts: 923
Location: Kalispell, MT
Post Re: IR receiver option in the UK
Hey LM,

It's pretty straightforward. First, if you haven't already, download and install the Logitech Media Server.

http://www.mysqueezebox.com/download

Then save this script as an attachment, replacing the server IP and MAC address of the player etc.
It's a good idea to have your server setup with a static IP so the script won't have to be revised on reboots.

Code: Select all
-- SlimServer control attachment
--
-- This script provides functions for easily controlling SlimDevices
-- SLIMP3 or Squeezebox from Indigo Execute AppleScript actions.
--
-- Author:   Andrew Turner (software at highearthorbit dot com)
-- Changes:   Paul Roomberg (paul at pasaro dot nl)
--         Nick Sosnicki - 30 Oct 2006 - v1.2
--            -tested on Indigo 2.0b8, SlimServer 6.5.0
--            -added error trapping to shell scripts
--            -set the curl connect timeout to 5 seconds
--               and the curl transaction time limit to
--               10 seconds (these can be increased if needed)
--            - added option to ping server and Squeezebox to
--               see if they are there before sending commands
--            -added support for multiple Squeezeboxes through
--               a nick name/IP address definition; this requires
--               an extra parm for each command
--            -to support multiple Squeezebox status, multiple
--               status variables are used; these will be created
--               automatically if needed
--            -changed slim server display function to allow
--               dynamic control of the time to display
--         Matt Bendiksen - 1 Jan 2007 - v1.2.1
--            -general cleanup, bug fixes, and added to base
--               Indigo 2.0 install
--
-- __Attachment Functions__ Can be called from within Indigo Execute AppleScript actions,
-- or any _tell app "IndigoServer"_ block. If the squeezebox nickname is blank, the first
-- one defined in the map will be used.
--
--   SlimServerDisplay("Line 1", "Line 2", seconds to display, "Squeeze Nickname")
--   SlimServerVolume(volume, "Squeeze Nickname")
--      volume: 0-100
--   SlimServerPlaySong("Song Name", "Squeeze Nickname")
--   SlimServerSkipFwd("Squeeze Nickname")
--   SlimServerSkipRew("Squeeze Nickname")
--   SlimServerPower(On or Off, "Squeeze Nickname")
--      On or Off: 0 is off, 1 is on
--   SlimServerCommand("Command","Squeeze Nickname")
--      available commands: "play", "pause", "stop", "sleep"
--
--   Status variable are named "SlimStatus" and the Squeezebox's nick name
--      for example, "SlimStatusupBath1" and "SlimStatusupBath2"

property slimIP : "192.168.1.xx" -- slimserver IP address
property slimPort : "9000" -- slimserver web port

-- Map of Squeezebox nicknames and their IP addresses. MAC addresses
-- can be used instead, but you must set pingSqueeze to false (the ping
-- test cannot be used with MAC addresses). Add a new pair for every
-- Squeezebox you want to control.
property squeezeMap : {¬
   {"Radio_Name", "00:04:20:26:e4:25"} ¬
      }

property pingSlim : false --if true, ping slimserver before commands
property pingSqueeze : false --if true, ping squeezebox before commands

property slimResponseText : ""

using terms from application "IndigoServer"
   on SlimServerCommand(theCommand, squeezeNick)
      SendToSlimServer(theCommand, 0, 0, 0, squeezeNick)
   end SlimServerCommand
   
   on SlimServerDisplay(displayString1, displayString2, displaySeconds, squeezeNick)
      set lineOne to _searchReplaceText(" ", "%20", displayString1)
      set lineTwo to _searchReplaceText(" ", "%20", displayString2)
      SendToSlimServer("display", lineOne, lineTwo, displaySeconds, squeezeNick)
   end SlimServerDisplay
   
   on SlimServerVolume(volume, squeezeNick)
      SendToSlimServer("mixer", "volume", volume, 0, squeezeNick)
   end SlimServerVolume
   
   on SlimServerPlaySong(songName, squeezeNick)
      SendToSlimServer("playlist", "play", songName, 0, squeezeNick)
      GetSlimStatus(squeezeNick)
   end SlimServerPlaySong
   
   
   on SlimServerPlayFavorite(favorite, squeezeNick)
      SendToSlimServer("favorites", "playlist", "play", "item_id:" & favorite, squeezeNick)
      GetSlimStatus(squeezeNick)
   end SlimServerPlayFavorite
   
   
   on SlimServerSkipFwd(squeezeNick)
      SendToSlimServer("button", "jump_fwd", 1, 0, squeezeNick)
      GetSlimStatus(squeezeNick)
   end SlimServerSkipFwd
   
   on SlimServerSkipRew(squeezeNick)
      SendToSlimServer("button", "jump_rew", 0, 0, squeezeNick)
      GetSlimStatus(squeezeNick)
   end SlimServerSkipRew
   
   on SlimServerPower(onOff, squeezeNick)
      SendToSlimServer("power", onOff, 0, 0, squeezeNick)
      GetSlimStatus(squeezeNick)
   end SlimServerPower
   
   on SlimServerGetStatus(squeezeNick)
      SendToSlimServer("status", 0, 0, 0, squeezeNick)
      GetSlimStatus(squeezeNick)
   end SlimServerGetStatus
   
   on SlimServerLog(logString)
      tell application "IndigoServer"
         log logString using type "SlimServer"
      end tell
   end SlimServerLog
   
   on GetSlimStatus(squeezeNick)
      if squeezeNick is "" then
         set squeezeNick to item 1 of item 1 of squeezeMap
      end if
      tell application "IndigoServer"
         -- if a status variable pair does not exisit for this Squeeze's nick name, create it
         set statusVar1 to "SlimStatus" & squeezeNick & "1"
         set statusVar2 to "SlimStatus" & squeezeNick & "2"
         if not (variable statusVar1 exists) then
            make new variable with properties {name:statusVar1, value:first paragraph of slimResponseText}
         else
            set value of variable statusVar1 to the first paragraph of slimResponseText
         end if
         if not (variable statusVar2 exists) then
            make new variable with properties {name:statusVar2, value:last paragraph of slimResponseText}
         else
            set value of variable statusVar2 to the last paragraph of slimResponseText
         end if
      end tell
   end GetSlimStatus
   
   on SendToSlimServer(p0, p1, p2, p3, squeezeNick)
      -- explicitly set variable
      set squeezeIP to "00:04:20:26:e4:25"
      --lookup IP for squeeze box nickname if provided, else use first entry
      (*      if squeezeNick is "" then
         set squeezeIP to item 2 of item 1 of squeezeMap
      else
         set squeezeIP to ""
         repeat with mapEntry in squeezeMap
            if item 1 of mapEntry is squeezeNick then
               set squeezeIP to item 2 of mapEntry
               exit repeat
            end if
         end repeat
      end if
      if squeezeIP is "" then
         SlimServerLog("Squeezebox nickname " & squeezeNick & " or IP address is not defined")
         return
      end if
*)
      set pingSucess to true
      if pingSlim then --try to ping slimserver
         if not PingAddress(slimIP) then
            set pingSucess to false
            set slimResponseText to "SlimServer at " & slimIP & " not reachable"
            SlimServerLog("SlimServer at " & slimIP & " not reachable")
         end if
      end if
      if pingSqueeze then --try to ping squeezebox
         if not PingAddress(squeezeIP) then
            set pingSucess to false
            set slimResponseText to "Squeezebox at " & squeezeIP & " not reachable"
            SlimServerLog("Squeezebox at " & squeezeIP & " not reachable")
         end if
      end if
      if pingSucess then
         set h to "http://" & slimIP & ":" & slimPort & "/status.txt"
         set d to h & "?p0=" & p0
         if p1 is not equal to 0 then set d to d & "&p1=" & p1
         if p2 is not equal to 0 then set d to d & "&p2=" & p2
         if p3 is not equal to 0 then set d to d & "&p3=" & p3
         set d to d & "&player=" & squeezeIP
         try
            set slimResponseText to DoShellScript("curl --connect-timeout 5 --max-time 10 --get -d --url \"" & d & "\" ")
         on error errStatement number errNum
            set slimResponseText to "Connection error (CURL): " & errNum
            SlimServerLog("Connection error (CURL) with SlimServer at " & slimIP & "; error number: " & errNum)
         end try
      end if
   end SendToSlimServer
end using terms from

-- Pings given address and returns true if there were no errors, false if there were errors
on PingAddress(IPAddress)
   set errTest to true
   try
      set ping_result to DoShellScript("ping -o -q -n -t 3  " & IPAddress)
   on error errStatement number errNum
      set errTest to false
   end try
   return errTest
end PingAddress

-- Glue for shell scripts to ensure correct environmental vars are used
on DoShellScript(scriptStr)
   tell application "Finder" to set returnVal to do shell script scriptStr
   return returnVal
end DoShellScript

(*
   _searchReplaceText(searchTerm, replaceTerm, theText)
   Replaces a string found in a text by another string.
   This handle supports lists of search/replace terms.

   Parameters:
      searchTerm: the text to search for
      replaceTerm: the text to use as replacement
      theText: the text to search/replace

   Examples:
      _searchReplaceText("foo", "bar", "You are a foo") --> "You are a bar"
      _searchReplaceText({"foo", " a "}, {"bar", " one "}, "You are a foo") --> "You are one bar"
*)
to _searchReplaceText(searchTerm, replaceTerm, theText)
   set searchTerm to searchTerm as list
   set replaceTerm to replaceTerm as list
   set theText to theText as text
   
   set oldTID to AppleScript's text item delimiters
   repeat with i from 1 to count searchTerm
      set AppleScript's text item delimiters to searchTerm's item i
      set theText to theText's text items
      set AppleScript's text item delimiters to replaceTerm's item i
      set theText to theText as text
   end repeat
   set AppleScript's text item delimiters to oldTID
   return theText
end _searchReplaceText


Be sure to reload attachments before executing any commands.

Here's a few that I execute as Actions:
(modify paths of course)

This will play any audio file:
Code: Select all
SlimServerPlaySong("file:///Users/TV/Music/iTunes/Indigo/Indigo%20Audio/chime.aif", "Squeezebox_Radio")


For volume:
Code: Select all
SlimServerVolume("40", "Squeeze Nickname")  ---any number between 0-100


On:
Code: Select all
SlimServerPower("1", "Squeezebox_Radio")


Off:
Code: Select all
SlimServerPower("0", "Squeezebox_Radio")


To play a favorite, (easy to set these using the app iPeng):

Code: Select all
SlimServerPlayFavorite("c2c06678.1", "Squeezebox_Radio")


To find the rather cryptic code used in the play favorite command, open a terminal window
and enter: telnet localhost 9090
then: favorites items 1 10 (or however many you want listed, e.g. favorites items 1 15)

It's a bit of trial and error to get just the part of the code listed in the Terminal window
that is used for the command.

Here's what is what I get after entering the favorites items 1 10:

favorites items 1 10 title%3AFavorites id%3Ac2c06678.1 name%3ADavid%20Gray%20Radio type%3Aaudio isaudio%3A1 hasitems%3A0 id%3Ac2c06678.2 name%3AThe%20Beatles%20Radio type%3Aaudio isaudio%3A1 hasitems%3A0 id%3Ac2c06678.3 name%3ADiana%20Krall%20Radio type%3Aaudio isaudio%3A1 hasitems%3A0 id%3Ac2c06678.4 name%3APat%20Metheny%20Radio type%3Aaudio isaudio%3A1 hasitems%3A0 id%3Ac2c06678.5 name%3ALuciano%20Pavarotti%20Radio type%3Aaudio isaudio%3A1 hasitems%3A0 id%3Ac2c06678.6 name%3AThe%20Singers%20Unlimited%20Radio type%3Aaudio isaudio%3A1 hasitems%3A0 id%3Ac2c06678.7 name%3AToots%20Thielemans%20Radio type%3Aaudio isaudio%3A1 hasitems%3A0 id%3Ac2c06678.8 name%3AFrankie%20Laine%20Radio type%3Aaudio isaudio%3A1 hasitems%3A0 id%3Ac2c06678.9 name%3AJazz%20Holidays%20Radio type%3Aaudio isaudio%3A1 hasitems%3A0 id%3Ac2c06678.10 name%3AAbsolute%20Classic%20Rock%20(London%2C%20UK) type%3Aaudio isaudio%3A1 hasitems%3A0 count%3A13

Picking out the part of the code used might require a bit of trial and error, but once
you have the base code just change the last number for other favorites, e.g. c2c06678.1 c2c06678.2 etc.
Hope this will get you started.

Carl


Sat Mar 03, 2012 10:14 am
Profile

Joined: Feb 29, 2012
Posts: 19
Post Re: IR receiver option in the UK
Carl

Many thanks - really appreciate it and hope I have more luck with this than my previous efforts. I'm away for a few days but looking forward to trying this when I get back.

LM


Sat Mar 03, 2012 12:30 pm
Profile

Joined: Feb 29, 2012
Posts: 19
Post Re: IR receiver option in the UK
Carl,

It worked - have it running very well. Only thing I cannot get to work is requesting the status of my squeezeboxes. I think I've seen somewhere in the various threads (either here or on the Logitech forums) that status.txt doesn't work any more. Do you have this working?

LM


Thu Mar 08, 2012 8:54 am
Profile

Joined: Nov 26, 2009
Posts: 923
Location: Kalispell, MT
Post Re: IR receiver option in the UK
I wish I could get that working. I use the SB for announcements
and would like to be able to have it return to whatever state
it was in before I sent the announcement. Gets tiresome to have
to start it playing again each time.

Carl


Thu Mar 08, 2012 11:50 am
Profile

Joined: Feb 29, 2012
Posts: 19
Post Re: IR receiver option in the UK
Carl,

Thanks again for your help. I've now made more progress on the SB integration using the CLI commands rather than some of the older commands. The key is a do shell script command. I'll freely admit that my scripting is rubbish, but this, for example, is an easier way of playing a favourite - it simply plays the nth favourite in your list (where n is specified by the variable "favorite").

Code: Select all
   on SlimServerPlayFavorite(favorite, squeezeNick)
      set theServer to "localhost"
      set thePort to "9090"
      do shell script "printf  \"" & squeezeNick & " favorites playlist play item_id:" & (favorite as integer) & "\\nexit\\n\" | nc " & theServer & space & thePort
      GetSlimStatus(squeezeNick)
   end SlimServerPlayFavorite


I've used a similar technique to get back the album, artist and song title from a squeezebox, although it's quite complex to strip out unwanted characters.

Code: Select all
on GetSlimStatus(squeezeNick)
      if squeezeNick is "" then
         set squeezeNick to item 1 of item 1 of squeezeMap
      end if
      set theServer to "localhost"
      set thePort to "9090"
      
      set Artist to do shell script "printf  \"" & squeezeNick & " artist ?" & "\\nexit\\n\" | nc " & theServer & space & thePort
      set Album to do shell script "printf  \"" & squeezeNick & " album ?" & "\\nexit\\n\" | nc " & theServer & space & thePort
      set Song to do shell script "printf  \"" & squeezeNick & " title ?" & "\\nexit\\n\" | nc " & theServer & space & thePort
      
      set Artist to urldecode(Artist)
      set Artist to rightStringFromRight(Artist, "artist")
      
      set Song to urldecode(Song)
      set Song to rightStringFromRight(Song, "title")
      
      set Album to urldecode(Album)
      set Album to rightStringFromRight(Album, "album")
      
      tell application "IndigoServer"
         -- if a status variable pair does not exisit for this Squeeze's nick name, create it
         set statusVar1 to "SlimStatus" & squeezeNick & "1"
         set statusVar2 to "SlimStatus" & squeezeNick & "2"
         set statusVar3 to "SlimStatus" & squeezeNick & "3"
         set statusVar4 to "SlimStatus" & squeezeNick & "4"
         
         if not (variable statusVar1 exists) then
            make new variable with properties {name:statusVar1, value:Song}
         else
            set value of variable statusVar1 to Song
         end if
         if not (variable statusVar2 exists) then
            make new variable with properties {name:statusVar2, value:Artist}
         else
            set value of variable statusVar2 to Artist
         end if
         if not (variable statusVar3 exists) then
            make new variable with properties {name:statusVar3, value:Album}
         else
            set value of variable statusVar3 to Album
         end if

      end tell
   end GetSlimStatus


on urldecode(theText)
   set sDst to ""
   set sHex to "0123456789ABCDEF"
   set i to 1
   repeat while i ≤ length of theText
      set c to character i of theText
      if c = "+" then
         set sDst to sDst & " "
      else if c = "%" then
         if i > ((length of theText) - 2) then
            display dialog ("Invalid URL Encoded string - missing hex char") buttons {"Crap..."} with icon stop
            return ""
         end if
         set iCVal1 to (offset of (character (i + 1) of theText) in sHex) - 1
         set iCVal2 to (offset of (character (i + 2) of theText) in sHex) - 1
         if iCVal1 = -1 or iCVal2 = -1 then
            display dialog ("Invalid URL Encoded string - not 2 hex chars after % sign") buttons {"Crap..."} with icon stop
            return ""
         end if
         set sDst to sDst & (ASCII character (iCVal1 * 16 + iCVal2))
         set i to i + 2
      else
         set sDst to sDst & c
      end if
      set i to i + 1
   end repeat
   return sDst
end urldecode

on rightStringFromRight(str, del)
   local str, del, oldTIDs
   set oldTIDs to AppleScript's text item delimiters
   try
      set str to str as string
      if str does not contain del then return str
      set AppleScript's text item delimiters to del
      set str to str's last text item
      set AppleScript's text item delimiters to oldTIDs
      return str
   on error eMsg number eNum
      set AppleScript's text item delimiters to oldTIDs
      error "Can't rightStringFromRight: " & eMsg number eNum
   end try
end rightStringFromRight


It's a bit clumsy, but it seems to work!

As I'm sure you know, all of the CLI commands are listed in the technical help section from the usual Squeezecentre web interface. I've found it relatively straightforward to use those to do what I want. Ultimately it would probably make sense to update the entire script to use CLI commands - my current version is a bit of a muddle of the two.

Hope this helps nonetheless.

LM


Sat Mar 17, 2012 6:58 am
Profile

Joined: Nov 26, 2009
Posts: 923
Location: Kalispell, MT
Post Re: IR receiver option in the UK
Thanks LM. Couldn't get either script to work though. No variables are created or populated.
I'll keep experimenting with it.

Carl


Sat Mar 17, 2012 8:15 am
Profile

Joined: Feb 29, 2012
Posts: 19
Post Re: IR receiver option in the UK
Sorry - you need to insert those sections into the main script that you posted before.

Here is my full version:

Code: Select all
-- SlimServer control attachment
--
-- This script provides functions for easily controlling SlimDevices
-- SLIMP3 or Squeezebox from Indigo Execute AppleScript actions.
--
-- Author:   Andrew Turner (software at highearthorbit dot com)
-- Changes:   Paul Roomberg (paul at pasaro dot nl)
--         Nick Sosnicki - 30 Oct 2006 - v1.2
--            -tested on Indigo 2.0b8, SlimServer 6.5.0
--            -added error trapping to shell scripts
--            -set the curl connect timeout to 5 seconds
--               and the curl transaction time limit to
--               10 seconds (these can be increased if needed)
--            - added option to ping server and Squeezebox to
--               see if they are there before sending commands
--            -added support for multiple Squeezeboxes through
--               a nick name/IP address definition; this requires
--               an extra parm for each command
--            -to support multiple Squeezebox status, multiple
--               status variables are used; these will be created
--               automatically if needed
--            -changed slim server display function to allow
--               dynamic control of the time to display
--         Matt Bendiksen - 1 Jan 2007 - v1.2.1
--            -general cleanup, bug fixes, and added to base
--               Indigo 2.0 install
--
-- __Attachment Functions__ Can be called from within Indigo Execute AppleScript actions,
-- or any _tell app "IndigoServer"_ block. If the squeezebox nickname is blank, the first
-- one defined in the map will be used.
--
--   SlimServerDisplay("Line 1", "Line 2", seconds to display, "Squeeze Nickname")
--   SlimServerVolume(volume, "Squeeze Nickname")
--      volume: 0-100
--   SlimServerPlaySong("Song Name", "Squeeze Nickname")
--   SlimServerSkipFwd("Squeeze Nickname")
--   SlimServerSkipRew("Squeeze Nickname")
--   SlimServerPower(On or Off, "Squeeze Nickname")
--      On or Off: 0 is off, 1 is on
--   SlimServerCommand("Command","Squeeze Nickname")
--      available commands: "play", "pause", "stop", "sleep"

-- SlimServerVolumeUp("Squeeze Nickname")
--
--   Status variable are named "SlimStatus" and the Squeezebox's nick name
--      for example, "SlimStatusupBath1" and "SlimStatusupBath2"

property slimIP : "10.0.1.10" -- slimserver IP address
property slimPort : "9000" -- slimserver web port

-- Map of Squeezebox nicknames and their IP addresses. MAC addresses
-- can be used instead, but you must set pingSqueeze to false (the ping
-- test cannot be used with MAC addresses). Add a new pair for every
-- Squeezebox you want to control.
property squeezeMap : {¬
   {"LivingRoom", "00:04:20:05:b8:a9"}, ¬
   {"BedRoom", "00:04:20:16:2d:04"}, ¬
   {"Kitchen", "00:04:20:1f:6d:1d"}, ¬
   {"Study", "00:0d:93:70:ad:b6"} ¬
      }

property pingSlim : false --if true, ping slimserver before commands
property pingSqueeze : false --if true, ping squeezebox before commands

property slimResponseText : ""



using terms from application "IndigoServer"
   
   on SlimServerVolumeDown(squeezeNick)
      set theServer to "localhost"
      set thePort to "9090"
      repeat 5 times
         do shell script "printf  \"" & squeezeNick & " button voldown_front" & "\\nexit\\n\" | nc " & theServer & space & thePort
         if squeezeNick = "LivingRoom" then delay 0.2
      end repeat
      GetSlimStatus(squeezeNick)
   end SlimServerVolumeDown
   
   on SlimServerVolumeUp(squeezeNick)
      set theServer to "localhost"
      set thePort to "9090"
      repeat 5 times
         do shell script "printf  \"" & squeezeNick & " button volup_front" & "\\nexit\\n\" | nc " & theServer & space & thePort
         if squeezeNick = "LivingRoom" then delay 0.2
      end repeat
      GetSlimStatus(squeezeNick)
   end SlimServerVolumeUp
   
   on SlimServerPowerButtonOn(squeezeNick)
      set theServer to "localhost"
      set thePort to "9090"
      do shell script "printf  \"" & squeezeNick & " power_on" & "\\nexit\\n\" | nc " & theServer & space & thePort
      GetSlimStatus(squeezeNick)
      
   end SlimServerPowerButtonOn
   
   
   on SlimServerCommand(theCommand, squeezeNick)
      SendToSlimServer(theCommand, 0, 0, 0, squeezeNick)
      GetSlimStatus(squeezeNick)
   end SlimServerCommand
   
   on SlimServerDisplay(displayString1, displayString2, displaySeconds, squeezeNick)
      set lineOne to _searchReplaceText(" ", "%20", displayString1)
      set lineTwo to _searchReplaceText(" ", "%20", displayString2)
      SendToSlimServer("display", lineOne, lineTwo, displaySeconds, squeezeNick)
   end SlimServerDisplay
   
   on SlimServerVolume(volume, squeezeNick)
      SendToSlimServer("mixer", "volume", volume, 0, squeezeNick)
   end SlimServerVolume
   
   on Volumeup5(squeezeNick)
      GetSlimStatus(squeezeNick)
      set theServer to "localhost"
      set thePort to "9090"
      
      set Vol to do shell script "printf  \"" & squeezeNick & " mixer volume ?" & "\\nexit\\n\" | nc " & theServer & space & thePort
      set Vol to urldecode(Vol)
      set Vol to rightStringFromRight(Vol, "volume ")
      SlimServerLog(squeezeNick & Vol)
      if (Vol as integer) > 95 then Vol = "95"
      do shell script "printf  \"" & squeezeNick & " mixer volume " & (Vol as integer) + 5 & "\\nexit\\n\" | nc " & theServer & space & thePort
      GetSlimStatus(squeezeNick)
   end Volumeup5
   
   on Volumedown5(squeezeNick)
      GetSlimStatus(squeezeNick)
      set theServer to "localhost"
      set thePort to "9090"
      
      set Vol to do shell script "printf  \"" & squeezeNick & " mixer volume ?" & "\\nexit\\n\" | nc " & theServer & space & thePort
      set Vol to urldecode(Vol)
      set Vol to rightStringFromRight(Vol, "volume ")
      SlimServerLog(squeezeNick & Vol)
      if (Vol as integer) < 5 then Vol = "5"
      do shell script "printf  \"" & squeezeNick & " mixer volume " & (Vol as integer) - 5 & "\\nexit\\n\" | nc " & theServer & space & thePort
      GetSlimStatus(squeezeNick)
   end Volumedown5
   
   
   on SlimServerPlaySong(songName, squeezeNick)
      SendToSlimServer("playlist", "play", songName, 0, squeezeNick)
      GetSlimStatus(squeezeNick)
   end SlimServerPlaySong
   
   
   on SlimServerPlayFavorite(favorite, squeezeNick)
      set theServer to "localhost"
      set thePort to "9090"
      do shell script "printf  \"" & squeezeNick & " favorites playlist play item_id:" & (favorite as integer) & "\\nexit\\n\" | nc " & theServer & space & thePort
      GetSlimStatus(squeezeNick)
   end SlimServerPlayFavorite
   
   
   on SlimServerSkipFwd(squeezeNick)
      SendToSlimServer("button", "jump_fwd", 1, 0, squeezeNick)
      GetSlimStatus(squeezeNick)
   end SlimServerSkipFwd
   
   on SlimServerSkipRew(squeezeNick)
      SendToSlimServer("button", "jump_rew", 0, 0, squeezeNick)
      GetSlimStatus(squeezeNick)
   end SlimServerSkipRew
   
   on SlimServerPower(onOff, squeezeNick)
      SendToSlimServer("power", onOff, 0, 0, squeezeNick)
      GetSlimStatus(squeezeNick)
   end SlimServerPower
   
   on SlimServerGetStatus(squeezeNick)
      SendToSlimServer("status", 0, 0, 0, squeezeNick)
      GetSlimStatus(squeezeNick)
   end SlimServerGetStatus
   
   on SlimServerLog(logString)
      tell application "IndigoServer"
         log logString using type "SlimServer"
      end tell
   end SlimServerLog
   
   on GetSlimStatus(squeezeNick)
      if squeezeNick is "" then
         set squeezeNick to item 1 of item 1 of squeezeMap
      end if
      set theServer to "localhost"
      set thePort to "9090"
      
      set Artist to do shell script "printf  \"" & squeezeNick & " artist ?" & "\\nexit\\n\" | nc " & theServer & space & thePort
      set Album to do shell script "printf  \"" & squeezeNick & " album ?" & "\\nexit\\n\" | nc " & theServer & space & thePort
      set Song to do shell script "printf  \"" & squeezeNick & " title ?" & "\\nexit\\n\" | nc " & theServer & space & thePort
      set Vol to do shell script "printf  \"" & squeezeNick & " mixer volume ?" & "\\nexit\\n\" | nc " & theServer & space & thePort
      
      set Artist to urldecode(Artist)
      set Artist to rightStringFromRight(Artist, "artist")
      
      set Song to urldecode(Song)
      set Song to rightStringFromRight(Song, "title")
      
      set Album to urldecode(Album)
      set Album to rightStringFromRight(Album, "album")
      
      set Vol to urldecode(Vol)
      set Vol to rightStringFromRight(Vol, "volume ")
      
      tell application "IndigoServer"
         -- if a status variable pair does not exisit for this Squeeze's nick name, create it
         set statusVar1 to "SlimStatus" & squeezeNick & "1"
         set statusVar2 to "SlimStatus" & squeezeNick & "2"
         set statusVar3 to "SlimStatus" & squeezeNick & "3"
         set statusVar4 to "SlimStatus" & squeezeNick & "4"
         
         if not (variable statusVar1 exists) then
            make new variable with properties {name:statusVar1, value:Song}
         else
            set value of variable statusVar1 to Song
         end if
         if not (variable statusVar2 exists) then
            make new variable with properties {name:statusVar2, value:Artist}
         else
            set value of variable statusVar2 to Artist
         end if
         if not (variable statusVar3 exists) then
            make new variable with properties {name:statusVar3, value:Album}
         else
            set value of variable statusVar3 to Album
         end if
         if not (variable statusVar4 exists) then
            make new variable with properties {name:statusVar4, value:Vol}
         else
            set value of variable statusVar4 to Vol
            
            
         end if
         
      end tell
   end GetSlimStatus
   
   on SendToSlimServer(p0, p1, p2, p3, squeezeNick)
      -- explicitly set variable
      set squeezeIP to "00:04:20:26:e4:25"
      --lookup IP for squeeze box nickname if provided, else use first entry
      (*      if squeezeNick is "" then
         set squeezeIP to item 2 of item 1 of squeezeMap
      else
         set squeezeIP to ""
         repeat with mapEntry in squeezeMap
            if item 1 of mapEntry is squeezeNick then
               set squeezeIP to item 2 of mapEntry
               exit repeat
            end if
         end repeat
      end if
      if squeezeIP is "" then
         SlimServerLog("Squeezebox nickname " & squeezeNick & " or IP address is not defined")
         return
      end if
*)
      set pingSucess to true
      if pingSlim then --try to ping slimserver
         if not PingAddress(slimIP) then
            set pingSucess to false
            set slimResponseText to "SlimServer at " & slimIP & " not reachable"
            SlimServerLog("SlimServer at " & slimIP & " not reachable")
         end if
      end if
      if pingSqueeze then --try to ping squeezebox
         if not PingAddress(squeezeIP) then
            set pingSucess to false
            set slimResponseText to "Squeezebox at " & squeezeIP & " not reachable"
            SlimServerLog("Squeezebox at " & squeezeIP & " not reachable")
         end if
      end if
      if pingSucess then
         set h to "http://" & slimIP & ":" & slimPort & "/status.txt"
         set d to h & "?p0=" & p0
         if p1 is not equal to 0 then set d to d & "&p1=" & p1
         if p2 is not equal to 0 then set d to d & "&p2=" & p2
         if p3 is not equal to 0 then set d to d & "&p3=" & p3
         set d to d & "&player=" & squeezeIP
         try
            set slimResponseText to DoShellScript("curl --connect-timeout 5 --max-time 10 --get -d --url \"" & d & "\" ")
         on error errStatement number errNum
            set slimResponseText to "Connection error (CURL): " & errNum
            SlimServerLog("Connection error (CURL) with SlimServer at " & slimIP & "; error number: " & errNum)
         end try
      end if
   end SendToSlimServer
end using terms from

-- Pings given address and returns true if there were no errors, false if there were errors
on PingAddress(IPAddress)
   set errTest to true
   try
      set ping_result to DoShellScript("ping -o -q -n -t 3  " & IPAddress)
   on error errStatement number errNum
      set errTest to false
   end try
   return errTest
end PingAddress

-- Glue for shell scripts to ensure correct environmental vars are used
on DoShellScript(scriptStr)
   tell application "Finder" to set returnVal to do shell script scriptStr
   return returnVal
end DoShellScript

(*
   _searchReplaceText(searchTerm, replaceTerm, theText)
   Replaces a string found in a text by another string.
   This handle supports lists of search/replace terms.

   Parameters:
      searchTerm: the text to search for
      replaceTerm: the text to use as replacement
      theText: the text to search/replace

   Examples:
      _searchReplaceText("foo", "bar", "You are a foo") --> "You are a bar"
      _searchReplaceText({"foo", " a "}, {"bar", " one "}, "You are a foo") --> "You are one bar"
*)
to _searchReplaceText(searchTerm, replaceTerm, theText)
   set searchTerm to searchTerm as list
   set replaceTerm to replaceTerm as list
   set theText to theText as text
   
   set oldTID to AppleScript's text item delimiters
   repeat with i from 1 to count searchTerm
      set AppleScript's text item delimiters to searchTerm's item i
      set theText to theText's text items
      set AppleScript's text item delimiters to replaceTerm's item i
      set theText to theText as text
   end repeat
   set AppleScript's text item delimiters to oldTID
   return theText
end _searchReplaceText


on urldecode(theText)
   set sDst to ""
   set sHex to "0123456789ABCDEF"
   set i to 1
   repeat while i ≤ length of theText
      set c to character i of theText
      if c = "+" then
         set sDst to sDst & " "
      else if c = "%" then
         if i > ((length of theText) - 2) then
            display dialog ("Invalid URL Encoded string - missing hex char") buttons {"Crap..."} with icon stop
            return ""
         end if
         set iCVal1 to (offset of (character (i + 1) of theText) in sHex) - 1
         set iCVal2 to (offset of (character (i + 2) of theText) in sHex) - 1
         if iCVal1 = -1 or iCVal2 = -1 then
            display dialog ("Invalid URL Encoded string - not 2 hex chars after % sign") buttons {"Crap..."} with icon stop
            return ""
         end if
         set sDst to sDst & (ASCII character (iCVal1 * 16 + iCVal2))
         set i to i + 2
      else
         set sDst to sDst & c
      end if
      set i to i + 1
   end repeat
   return sDst
end urldecode

--c--   rightStringFromRight(str, del)
--d--   Return the text to the right of a delimiter starting from right (full string if not found).
--a--   str : string -- the string to search
--a--   del : string -- the delimiter to use
--r--   string
--x--   rightStringFromRight("ab:ca:bc", ":") --> "bc"
--u--   ljr (http://applescript.bratis-lover.net/library/string/)
on rightStringFromRight(str, del)
   local str, del, oldTIDs
   set oldTIDs to AppleScript's text item delimiters
   try
      set str to str as string
      if str does not contain del then return str
      set AppleScript's text item delimiters to del
      set str to str's last text item
      set AppleScript's text item delimiters to oldTIDs
      return str
   on error eMsg number eNum
      set AppleScript's text item delimiters to oldTIDs
      error "Can't rightStringFromRight: " & eMsg number eNum
   end try
end rightStringFromRight



It may look a bit complicated (it is a bit complicated), particularly the volume bits. Those reflect the fact that my LivingRoom player uses IRblaster to control my amp's volume. That means I needed to do the send button command to control the volume of that player.

Now I would still really like to find a way of getting my Squeezebox to receive an IR command and send it back to the script!

LM


Sat Mar 17, 2012 8:37 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 13 posts ] 

Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.   Template designed by STSoftware.