Subversion Repositories HomeAutomation

Rev

Rev 1954 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  
  3. Enable TCP interface (normally port 9090):
  4. in System/Settings/Network/Services activate
  5. Allow programs on this system to control XBMC for localhost access only
  6. and Allow programs on other systems to control XBMC for access from other computers as well
  7.  
  8. http://wiki.xbmc.org/?title=JSON-RPC_API
  9.  
  10.  
  11. https://groups.google.com/forum/#!msg/commandfusion/eYMgfNSfTuE/sdBuLO5TdeAJ
  12. Quit        jsonrpc?{"jsonrpc":"2.0","method":"Application.Quit","id":1}
  13. Home        jsonrpc?{"jsonrpc":"2.0","method":"Input.Home","id":1}
  14. Update Vid      jsonrpc?{"jsonrpc":"2.0","method":"VideoLibrary.Scan","id":1}
  15. Clean Vid       jsonrpc?{"jsonrpc":"2.0","method":"VideoLibrary.Clean","id":1}
  16. Hibernate       jsonrpc?{"jsonrpc":"2.0","method":"System.Hibernate","id":1}
  17. Reboot      jsonrpc?{"jsonrpc":"2.0","method":"System.Reboot","id":1}
  18. 30Sec Forward       jsonrpc?{"jsonrpc":"2.0","id":1,"method":"Player.Seek","params":{"playerid":1,"value":"smallforward"}}
  19. 30Sec Back      jsonrpc?{"jsonrpc":"2.0","id":1,"method":"Player.Seek","params":{"playerid":1,"value":"smallbackward"}}
  20. 10Min Forward       jsonrpc?{"jsonrpc":"2.0","id":1,"method":"Player.Seek","params":{"playerid":1,"value":"bigforward"}}
  21. 10Min Back      jsonrpc?{"jsonrpc":"2.0","id":1,"method":"Player.Seek","params":{"playerid":1,"value":"bigbackward"}}
  22. Play/Pause      jsonrpc?{"jsonrpc":"2.0","id":1,"method":"Player.PlayPause","params":{"playerid":1}}
  23. Stop        jsonrpc?{"jsonrpc":"2.0","id":1,"method":"Player.Stop","params":{"playerid":1}}
  24. FF      jsonrpc?{"jsonrpc":"2.0","id":1,"method":"Player.SetSpeed","params":{"playerid":1,"speed":"increment"}}
  25. Rew         jsonrpc?{"jsonrpc":"2.0","id":1,"method":"Player.SetSpeed","params":{"playerid":1,"speed":"decrement"}}
  26. Enter       jsonrpc?{"jsonrpc":"2.0","method":"Input.Select","id":1}
  27. Up      jsonrpc?{"jsonrpc":"2.0","method":"Input.Up","id":1}
  28. Down        jsonrpc?{"jsonrpc":"2.0","method":"Input.Down","id":1}
  29. Left        jsonrpc?{"jsonrpc":"2.0","method":"Input.Left","id":1}
  30. Right       jsonrpc?{"jsonrpc":"2.0","method":"Input.Right","id":1}
  31. Back        jsonrpc?{"jsonrpc":"2.0","method":"Input.Back","id":1}
  32. Subtitle Off         jsonrpc?{"jsonrpc":"2.0","id":1,"method":"Player.SetSubtitle","params":{"playerid":1,"subtitle":"off"}}
  33. Subtitle On      jsonrpc?{"jsonrpc":"2.0","id":1,"method":"Player.SetSubtitle","params":{"playerid":1,"subtitle":"on"}}
  34. Select      jsonrpc?{"jsonrpc":"2.0","method":"Input.Select","id":1}
  35. Suspend      jsonrpc?{"jsonrpc":"2.0","method":"System.Suspend","id":1}
  36. Menu        jsonrpc?{"jsonrpc":"2.0","method":"Input.ContextMenu","id":1}
  37. Info         jsonrpc?{"jsonrpc":"2.0","method":"Input.Info","id":1}
  38. Show        OSD jsonrpc?{"jsonrpc":"2.0","method":"Input.ShowOSD","id":1}
  39.  
  40. */
  41.  
  42. var Xbmc_Socket_Id = null;
  43. var Xbmc_Susbscribers = [];
  44. var Xbmc_Sessions = {};
  45. Xmbc_SeekValues      = function() { return [ "30sec", "10min" ]; };
  46.  
  47. function Xbmc_SendCommand(command_name, params, callback)
  48. {
  49.   var new_id = 1;
  50.  
  51.   if (!Xbmc_IsConnected())
  52.   {
  53.     return false;
  54.   }
  55.  
  56.   while (Xbmc_Sessions[new_id])
  57.   {
  58.     new_id++;
  59.   }
  60.  
  61.   Xbmc_Sessions[new_id] = callback;
  62.  
  63.   var command = { "jsonrpc" : "2.0", "method": command_name, "params" : params, "id" : new_id };
  64.  
  65.   Socket_Send(Xbmc_Socket_Id, JSON.stringify(command));
  66.  
  67.   return new_id;
  68. }
  69.  
  70. function Xbmc_SubscribeToConnectionStatus(callback)
  71. {
  72.   Xbmc_Susbscribers.push(callback);
  73. }
  74.  
  75. function Xbmc_IsConnected()
  76. {
  77.   return Xbmc_Socket_Id != null;
  78. }
  79.  
  80. function Xbmc_Connect(host, port)
  81. {
  82.   if (Xbmc_IsConnected())
  83.   {
  84.     Xbmc_Disconnect();
  85.   }
  86.  
  87.   Log("Connecting to XBMC at " + host + ":" + port + "...");
  88.  
  89.   Xbmc_Socket_Id = Socket_Connect(host, port, function(socket_id, data)
  90.   {
  91.     var json_data_list = parse_json_rpc(data)
  92.    
  93.     for (var n = 0; n < json_data_list.length; n++)
  94.     {
  95.       var json_data = json_data_list[n];
  96.    
  97.       if (json_data.id)
  98.       {
  99.         if (Xbmc_Sessions[json_data.id])
  100.         {
  101.           if (json_data.result)
  102.           {
  103.             Xbmc_Sessions[json_data.id]("command_success", socket_id, null, json_data.result);
  104.           }
  105.           else
  106.           {
  107.             Xbmc_Sessions[json_data.id]("command_error", socket_id, null, json_data.error);
  108.           }
  109.          
  110.           delete Xbmc_Sessions[json_data.id];
  111.         }
  112.       }
  113.       else
  114.       {
  115.         for (var n = 0; n < Xbmc_Susbscribers.length; n++)
  116.         {
  117.           Xbmc_Susbscribers[n]("notification", socket_id, json_data.method, json_data.params);
  118.         }
  119.       }
  120.     }
  121.    
  122.   }, function(socket_id, state)
  123.   {
  124.     if (state == 0) // Connected
  125.     {
  126.       Log("Got connect on socket id " + socket_id);
  127.      
  128.       for (var n = 0; n < Xbmc_Susbscribers.length; n++)
  129.       {
  130.         Xbmc_Susbscribers[n]("connected", socket_id);
  131.       }
  132.     }
  133.     else if (state == 1) // Disconnected
  134.     {
  135.       Log("Got disconnect on socket id " + socket_id);
  136.      
  137.       if (socket_id == Xbmc_Socket_Id)
  138.       {
  139.         Xbmc_Socket_Id = null;
  140.       }
  141.      
  142.       for (var n = 0; n < Mbb_Susbscribers.length; n++)
  143.       {
  144.         Xbmc_Susbscribers[n]("disconnected", socket_id);
  145.       }
  146.     }
  147.   });
  148.  
  149.   if (Xbmc_Socket_Id != 0)
  150.   {
  151.     Log("Connected to XBMC at " + host + ":" + port + " successfully!");
  152.     return true;
  153.   }
  154.  
  155.   Log("Failed to connect to XBMC at " + host + ":" + port + "!");
  156.  
  157.   Xbmc_Socket_Id = null;
  158.   return false;
  159. }
  160. Console_RegisterCommand(Xbmc_Connect);
  161.  
  162. function Xbmc_Disconnect()
  163. {
  164.   if (Xbmc_IsConnected())
  165.   {
  166.     Socket_Disconnect(Xbmc_Socket_Id);
  167.   }
  168.  
  169.   return true;
  170. }
  171. Console_RegisterCommand(Xbmc_Disconnect);
  172.  
  173. function Xbmc_Right()
  174. {
  175.   return Xbmc_SendCommand("Input.Right", {});
  176. }
  177. Console_RegisterCommand(Xbmc_Right);
  178.  
  179. function Xbmc_Left()
  180. {
  181.   return Xbmc_SendCommand("Input.Left", {});
  182. }
  183. Console_RegisterCommand(Xbmc_Left);
  184.  
  185. function Xbmc_Up()
  186. {
  187.   return Xbmc_SendCommand("Input.Up", {});
  188. }
  189. Console_RegisterCommand(Xbmc_Up);
  190.  
  191. function Xbmc_Down()
  192. {
  193.   return Xbmc_SendCommand("Input.Down", {});
  194. }
  195. Console_RegisterCommand(Xbmc_Down);
  196.  
  197. function Xbmc_Select()
  198. {
  199.   return Xbmc_SendCommand("Input.Select", {});
  200. }
  201. Console_RegisterCommand(Xbmc_Select);
  202.  
  203. function Xbmc_Back()
  204. {
  205.   return Xbmc_SendCommand("Input.Back", {});
  206. }
  207. Console_RegisterCommand(Xbmc_Back);
  208.  
  209. function Xbmc_PlayPause()
  210. {
  211.   //Xbmc_SendCommand("Player.GetActivePlayers", {});
  212.  
  213.   return Xbmc_SendCommand("Player.PlayPause", { "playerid" : 1 });
  214. }
  215. Console_RegisterCommand(Xbmc_PlayPause);
  216.  
  217. function Xbmc_Stop()
  218. {
  219.   return Xbmc_SendCommand("Player.Stop", { "playerid" : 1 });
  220. }
  221. Console_RegisterCommand(Xbmc_Stop);
  222.  
  223. function Xbmc_Forward(value)
  224. {
  225.   seekValue = "smallforward";
  226.  
  227.   if (value == "30sec")
  228.     seekValue = "smallforward";
  229.   if (value == "10min")
  230.     seekValue = "bigforward";
  231.  
  232.   return Xbmc_SendCommand("Player.Seek", { "playerid" : 1, "value" : seekValue });
  233. }
  234. Console_RegisterCommand(Xbmc_Forward, function(arg_index, args) { return Console_StandardAutocomplete( arg_index, args, Xmbc_SeekValues() ); } );
  235.  
  236. function Xbmc_Backward(value)
  237. {
  238.   seekValue = "smallforward";
  239.  
  240.   if (value == "30sec")
  241.     seekValue = "smallforward";
  242.   if (value == "10min")
  243.     seekValue = "bigforward";
  244.  
  245.   return Xbmc_SendCommand("Player.Seek", { "playerid" : 1, "value" : seekValue });
  246. }
  247. Console_RegisterCommand(Xbmc_Backward, function(arg_index, args) { return Console_StandardAutocomplete( arg_index, args, Xmbc_SeekValues() ); } );
  248.