Author Topic: XBMC control - LUA code for System.OnQuit, System.Shutdown, etc?  (Read 3319 times)

Offline Aaron

  • Hero Member
  • *****
  • Posts: 1983
  • Karma: +61/-193
Does anyone have LUA code that works to for some basic JSON commands for XBMC? 

I want to create some basic Scenes and hoping someone will share some code.

thanks!

Offline OtisPresley

  • Full Member
  • ***
  • Posts: 144
  • Karma: +2/-1
Re: XBMC control - LUA code for System.OnQuit, System.Shutdown, etc?
« Reply #1 on: August 24, 2013, 02:13:37 pm »
Hi Aaron,

Here is what I do on OpenELEC.  First, I set up and used the XBMC State plugin to detect playback start and end, so I can use these in scenes like controlling lights.

Then, I use the following code in scenes for shutting down XBMC on a 10 second delay; I also have XBMC set to shut down the PC when the shutdown command is issued:
Code: [Select]
function HTTPshutDown(host)
        luup.inet.wget("http://" .. host .. ":8080/jsonrpc?request=%7B%22jsonrpc%22%3A%222.0%22%2C%22method%22%3A%22System.Shutdown%22%2C%22id%22%3A1%7D​")
end

luup.call_delay("HTTPshutDown", 10, "192.168.1.149")

Since Library Watchdog doesn't really work with my OpenELEC (makes it extremely slow and won't shutdown properly), I wrote a shell script that I run on an Ubuntu box to update the library every hour; I suppose you could also convert this to Luup code in a scene and use the Program Logic Timer Switch to kick it off every hour (or whatever you choose).:
Code: [Select]
#!/bin/sh

# Enter the host name and web port for XBMC
xhost="192.168.1.149"
xport="8080"

# Determine whether the host is alive
xping=`ping ${xhost} -c 1 -w 1 | grep "64 bytes from"`

if [ -z "$xping" ]; then
        echo "Host ${xhost} is not alive!"
        exit
fi

# Determine whether XBMC is running on the host
xrunn=`wget -t 1 -T 3 -O - "http://${xhost}:${xport}/jsonrpc?request=%7B%22jsonrpc%22%3A%222.0%22%2C%22method%22%3A%22JSONRPC.Version%22%2C%22id%22%3A1%7D.\")" 2> /dev/null`

if [ -z "$xrunn" ]; then
        echo "XBMC is not running on host ${xhost}!"
        exit
fi

# Determine whether anything is currently playing
xplay=`wget -t 1 -T 3 -O - "http://${xhost}:${xport}/jsonrpc?request=%7B%22jsonrpc%22%3A%222.0%22%2C%22method%22%3A%22Player.GetActivePlayers%22%2C%22id%22%3A1%7D.\")" 2> /dev/null | grep -o playerid`

if [ -n "$xplay" ]; then
        echo "XBMC is busy!"
        exit
fi

# Scan the Music Library
xalib=`wget -t 1 -T 3 -O - "http://${xhost}:${xport}/jsonrpc?request=%7B%22jsonrpc%22%3A%222.0%22%2C%22method%22%3A%22AudioLibrary.Scan%22%2C%22id%22%3A1%7D.\")" 2> /dev/null | grep \"OK\"`

if [ -z "$xalib" ]; then
        echo "Audio Library Scan failed!"
        exit
fi

sleep 3m

# Determine whether anything is currently playing
xplay=`wget -t 1 -T 3 -O - "http://${xhost}:${xport}/jsonrpc?request=%7B%22jsonrpc%22%3A%222.0%22%2C%22method%22%3A%22Player.GetActivePlayers%22%2C%22id%22%3A1%7D.\")" 2> /dev/null | grep -o playerid`

if [ -n "$xplay" ]; then
        echo "XBMC is busy!"
        exit
fi

# Scan the Video Library
xvlib=`wget -t 1 -T 3 -O - "http://${xhost}:${xport}/jsonrpc?request=%7B%22jsonrpc%22%3A%222.0%22%2C%22method%22%3A%22VideoLibrary.Scan%22%2C%22id%22%3A1%7D.\")" 2> /dev/null | grep \"OK\"`

if [ -z "$xvlib" ]; then
        echo "Video Library Scan failed!"
        exit
fi

sleep 1m

# Determine whether anything is currently playing
xplay=`wget -t 1 -T 3 -O - "http://${xhost}:${xport}/jsonrpc?request=%7B%22jsonrpc%22%3A%222.0%22%2C%22method%22%3A%22Player.GetActivePlayers%22%2C%22id%22%3A1%7D.\")" 2> /dev/null | grep -o playerid`

if [ -n "$xplay" ]; then
        echo "XBMC is busy!"
        exit
fi

# Clean the Video Library
xvlib=`wget -t 1 -T 3 -O - "http://${xhost}:${xport}/jsonrpc?request=%7B%22jsonrpc%22%3A%222.0%22%2C%22method%22%3A%22VideoLibrary.Clean%22%2C%22id%22%3A1%7D.\")" 2> /dev/null | grep \"OK\"`

if [ -z "$xvlib" ]; then
        echo "Video Library Clean failed!"
        exit
fi

The entry in Root's crontab looks like this:
Code: [Select]
0 * * * * /etc/scripts/xbmclib.sh
What I did to get the JSON RPC commands right is construct the URL's normally and then feed them through a URL encoder tool (there are plenty of these available online).

I hope this helps!

Does anyone have LUA code that works to for some basic JSON commands for XBMC? 

I want to create some basic Scenes and hoping someone will share some code.

thanks!
« Last Edit: August 24, 2013, 02:45:58 pm by OtisPreslsy »

Offline Aaron

  • Hero Member
  • *****
  • Posts: 1983
  • Karma: +61/-193
Re: XBMC control - LUA code for System.OnQuit, System.Shutdown, etc?
« Reply #2 on: August 25, 2013, 03:30:08 pm »
Otis
thx for the help. I tried your code, and variations like below (OnQuit, OnRestart) - using my internal IP, but and it is not working for me.  I get an error telling me the method is not valid... I'm using frodo, 12.2?

Code: [Select]
function HTTPshutDown(host)
        luup.inet.wget("http://" .. host .. ":8080/jsonrpc?request=%7B%22jsonrpc%22%3A%222.0%22%2C%22method%22%3A%22System.OnQuit%22%2C%22id%22%3A1%7D​")
end

luup.call_delay("HTTPshutDown", 10, "192.168.2.11")

Offline OtisPresley

  • Full Member
  • ***
  • Posts: 144
  • Karma: +2/-1
Re: XBMC control - LUA code for System.OnQuit, System.Shutdown, etc?
« Reply #3 on: August 25, 2013, 04:50:01 pm »
Aaron,

I too am running Frodo 12.2 (OpenELEC 3.0.6).  From the documentation, you can see that System.OnQuit takes in two parameters.  If you click the "show" link on the JSON Schema Description bar, it shows you that both parameters are required.  This is why the command is failing.

I believe the On<something> commands will not work with the HTTP method, because these commands generally tell XBMC to do something when something else happens that would require a notification and HTTP is not supported for Notifications or Direct File Downloads.  I believe you would need to use either TCP or WebSocket for that.

You can also refer to the JSON-RPC API documentation for command formats and support.  There are also some examples.  I use this URL Decoder/Encoder.  You only need to deccode the part after jsonrpc?request= in the URL to see the actual JSON function.

If you just use the System.Shutdown command and have your XBMC set under Settings/System/Power Saving/Shutdown Function to Quit (This option not available in OpenELEC), then I think it will just exit XBMC like you want instead of shutting the PC down.

Here is another Luup snippet I use in a scene that may be of interest.  This one starts Party Mode playback of Music on XBMC:
Code: [Select]
function HTTPparty(host)
        luup.inet.wget("http://" .. host .. ":8080/jsonrpc?request=%7B%22jsonrpc%22%3A%20%222.0%22%2C%20%22method%22%3A%20%22Player.Open%22%2C%20%22params%22%3A%20%7B%20%22item%22%3A%20%7B%20%22partymode%22%3A%20%22music%22%7D%7D%7D")
end

luup.call_delay("HTTPparty", 3, "192.168.1.149")

Otis
thx for the help. I tried your code, and variations like below (OnQuit, OnRestart) - using my internal IP, but and it is not working for me.  I get an error telling me the method is not valid... I'm using frodo, 12.2?

Code: [Select]
function HTTPshutDown(host)
        luup.inet.wget("http://" .. host .. ":8080/jsonrpc?request=%7B%22jsonrpc%22%3A%222.0%22%2C%22method%22%3A%22System.OnQuit%22%2C%22id%22%3A1%7D​")
end

luup.call_delay("HTTPshutDown", 10, "192.168.2.11")
« Last Edit: August 25, 2013, 05:03:29 pm by OtisPreslsy »