Kodi Community Forum

Full Version: Getting started with json-control via bash
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

Sorry if this is a repost, seems I should not be the first one to have this issue, but I've been unable to find the info I need.

I do some basic setup via bash before and after running xbmc (setting screen resolution, changing system volume, stopping spotify).

Now I'm trying to implement a new feature in that script.
Part of that requires me to send a quit signal to xbmc from an external source (bash).

My first thought was that maybe I could just send 'stop' to the xbmc binary and it would do the trick.
xbmc.bin does not take many arguments however, so I kept looking for another solution.

Found this page: http://wiki.xbmc.org/index.php?title=JSON_RPC#System

Seems to be precisely what I want, but I don't know how to start using it.

I've activated the xbmc web server, but I do not understand how it works.

I tried browsing the following:
Code:
http://localhost:8080/xbmcCmds/xbmcHttp?command=XBMC.Quit
Got this as an answer:
Code:
[LIST]Error:Unknown command[/LIST]

I tried using telnet from my shell, but clearly I do not understand the syntax:
Code:
$ [b]telnet localhost 9090[/b]
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
[b]{ "XBMC.Quit", ControlPower, "Quit xbmc" }[/b]
{
   "error" : {
      "code" : -32700,
      "message" : "Parse error."
   },
   "id" : 0,
   "jsonrpc" : "2.0"
}
[b]{ "XBMC.Quit" }[/b]
{
   "error" : {
      "code" : -32700,
      "message" : "Parse error."
   },
   "id" : 0,
   "jsonrpc" : "2.0"
}

Seems to be alot of documentation of available calls once you got the hang of how to communicate with the json api, but I've been unable to find a guide to get starting with that.
Is there an introduction somewhere to help me over the initial learning bump?

Just a few lines of bash showing how to quit xbmc via tcp would be excellent to get me going.
I have no idea about what you want to do or know of anything about it even, but, you can go over to IRC freeenode #xbmc-linux and ask there.
try
wget "http://localhost:8080/xbmcCmds/xbmcHttp?command=XBMC.Quit"
or
wget "http://xbmc:xbmc@localhost:8080/xbmcCmds/xbmcHttp?command=XBMC.Quit"
atari800 Wrote:try
wget "http://localhost:8080/xbmcCmds/xbmcHttp?command=XBMC.Quit"
or
wget "http://xbmc:xbmc@localhost:8080/xbmcCmds/xbmcHttp?command=XBMC.Quit"

Same result:
Code:
[b]$ wget "http://xbmc:xbmc@localhost:8080/xbmcCmds/xbmcHttp?command=XBMC.Quit"[/b]
--2011-08-20 15:47:40--  http://xbmc:*password*@localhost:8080/xbmcCmds/xbmcHttp?command=XBMC.Quit
Resolving localhost... ::1, 127.0.0.1
Connecting to localhost|::1|:8080... failed: Connection refused.
Connecting to localhost|127.0.0.1|:8080... connected.
HTTP request sent, awaiting response... 401 Unauthorized
Connecting to localhost|127.0.0.1|:8080... connected.
HTTP request sent, awaiting response... 200 OK
Length: 41
Saving to: `xbmcHttp?command=XBMC.Quit'

100%[======================================>] 41          --.-K/s   in 0s      

2011-08-20 15:47:40 (5,03 MB/s) - `xbmcHttp?command=XBMC.Quit' saved [41/41]

[b]$ cat xbmcHttp\?command\=XBMC.Quit [/b]
<html>
<li>Error:Unknown command
</html>
Read the wikipage! The actions are based upon the json-rpc 2.0 spec which is clearly specified here (which wiki has link to) https://www.google.com/accounts/ServiceL...-0?pli%3D1

One example of a command is
Code:
{ "jsonrpc": "2.0", "method": "JSONRPC.Ping", "id": 1 }
topfs2 Wrote:Read the wikipage! The actions are based upon the json-rpc 2.0 spec which is clearly specified here (which wiki has link to) https://www.google.com/accounts/ServiceL...-0?pli%3D1

I followed the link from the wiki, but knowing nothing, like I do, that page is quite daunting.

The example however worked like a charm! Thank you!

Code:
[b]$ telnet localhost 9090[/b]
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
[b]{ "jsonrpc": "2.0", "method": "JSONRPC.Ping", "id": 1 }[/b]
{
   "id" : 1,
   "jsonrpc" : "2.0",
   "result" : "pong"
}
[b]{ "jsonrpc": "2.0", "method": "XBMC.Quit", "id": 1 } [/b]      
{
   "id" : 1,
   "jsonrpc" : "2.0",
   "result" : "OK"
}
{
   "jsonrpc" : "2.0",
   "method" : "Announcement",
   "params" : {
      "message" : "ApplicationStop",
      "sender" : "xbmc"
   }
}
Connection closed by foreign host.
The link in the top has a bunch of examples. Thats the reason we choose json-rpc as its a well specified protocol we don't have to explain. All you need to know is method name and its parameters (which is what we have in wiki), how to format it should be explained in the formal specification

Anyways, glad to hear you got it working.
Here is a really simple example of how one could (there is quite likely alot of alot better ways to do it) use this in bash.

First, create a function:
Code:
function json
{
         echo { \"jsonrpc\": \"2.0\", \"method\": \"$1\", \"id\": 1 } | telnet localhost 9090 &> /dev/null
}

Then you can just do the following to make xbmc quit:
Code:
json XBMC.quit

One obvious downside with my approach is that telnet will always exit with 1 as the exit code, so you won't know if the operation succeeded or not.