Subversion Repositories HomeAutomation

Rev

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

  1.  
  2. var Xbmc_Socket_Id = null;
  3. var Xbmc_Susbscribers = [];
  4. var Xbmc_Sessions = {};
  5.  
  6. function Xbmc_SendCommand(command_name, params, callback)
  7. {
  8.   var new_id = 1;
  9.  
  10.   if (!Xbmc_IsConnected())
  11.   {
  12.     return false;
  13.   }
  14.  
  15.   while (Xbmc_Sessions[new_id])
  16.   {
  17.     new_id++;
  18.   }
  19.  
  20.   Xbmc_Sessions[new_id] = callback;
  21.  
  22.   var command = { "jsonrpc" : "2.0", "method": command_name, "params" : params, "id" : new_id };
  23.  
  24.   Socket_Send(Xbmc_Socket_Id, JSON.stringify(command));
  25.  
  26.   return new_id;
  27. }
  28.  
  29. function Xbmc_SubscribeToConnectionStatus(callback)
  30. {
  31.   Xbmc_Susbscribers.push(callback);
  32. }
  33.  
  34. function Xbmc_IsConnected()
  35. {
  36.   return Xbmc_Socket_Id != null;
  37. }
  38.  
  39. function Xbmc_Connect(host, port)
  40. {
  41.   if (Xbmc_IsConnected())
  42.   {
  43.     Xbmc_Disconnect();
  44.   }
  45.  
  46.   Log("Connecting to XBMC at " + host + ":" + port + "...");
  47.  
  48.   Xbmc_Socket_Id = Socket_Connect(host, port, function(socket_id, data)
  49.   {
  50.     var json_data_list = parse_json_rpc(data)
  51.    
  52.     for (var n = 0; n < json_data_list.length; n++)
  53.     {
  54.       var json_data = json_data_list[n];
  55.    
  56.       if (json_data.id)
  57.       {
  58.         if (Xbmc_Sessions[json_data.id])
  59.         {
  60.           if (json_data.result)
  61.           {
  62.             Xbmc_Sessions[json_data.id]("command_success", socket_id, null, json_data.result);
  63.           }
  64.           else
  65.           {
  66.             Xbmc_Sessions[json_data.id]("command_error", socket_id, null, json_data.error);
  67.           }
  68.          
  69.           delete Xbmc_Sessions[json_data.id];
  70.         }
  71.       }
  72.       else
  73.       {
  74.         for (var n = 0; n < Xbmc_Susbscribers.length; n++)
  75.         {
  76.           Xbmc_Susbscribers[n]("notification", socket_id, json_data.method, json_data.params);
  77.         }
  78.       }
  79.     }
  80.    
  81.   }, function(socket_id, state)
  82.   {
  83.     if (state == 0) // Connected
  84.     {
  85.       Log("Got connect on socket id " + socket_id);
  86.      
  87.       for (var n = 0; n < Xbmc_Susbscribers.length; n++)
  88.       {
  89.         Xbmc_Susbscribers[n]("connected", socket_id);
  90.       }
  91.     }
  92.     else if (state == 1) // Disconnected
  93.     {
  94.       Log("Got disconnect on socket id " + socket_id);
  95.      
  96.       if (socket_id == Xbmc_Socket_Id)
  97.       {
  98.         Xbmc_Socket_Id = null;
  99.       }
  100.      
  101.       for (var n = 0; n < Mbb_Susbscribers.length; n++)
  102.       {
  103.         Xbmc_Susbscribers[n]("disconnected", socket_id);
  104.       }
  105.     }
  106.   });
  107.  
  108.   if (Xbmc_Socket_Id != 0)
  109.   {
  110.     Log("Connected to XBMC at " + host + ":" + port + " successfully!");
  111.     return true;
  112.   }
  113.  
  114.   Log("Failed to connect to XBMC at " + host + ":" + port + "!");
  115.  
  116.   Xbmc_Socket_Id = null;
  117.   return false;
  118. }
  119. Console_RegisterCommand(Xbmc_Connect);
  120.  
  121. function Xbmc_Disconnect()
  122. {
  123.   if (Xbmc_IsConnected())
  124.   {
  125.     Socket_Disconnect(Xbmc_Socket_Id);
  126.   }
  127.  
  128.   return true;
  129. }
  130. Console_RegisterCommand(Xbmc_Disconnect);
  131.  
  132. function Xbmc_Right()
  133. {
  134.   return Xbmc_SendCommand("Input.Right", {});
  135. }
  136. Console_RegisterCommand(Xbmc_Right);
  137.  
  138. function Xbmc_Left()
  139. {
  140.   return Xbmc_SendCommand("Input.Left", {});
  141. }
  142. Console_RegisterCommand(Xbmc_Left);
  143.  
  144. function Xbmc_Up()
  145. {
  146.   return Xbmc_SendCommand("Input.Up", {});
  147. }
  148. Console_RegisterCommand(Xbmc_Up);
  149.  
  150. function Xbmc_Down()
  151. {
  152.   return Xbmc_SendCommand("Input.Down", {});
  153. }
  154. Console_RegisterCommand(Xbmc_Down);
  155.  
  156. function Xbmc_Select()
  157. {
  158.   return Xbmc_SendCommand("Input.Select", {});
  159. }
  160. Console_RegisterCommand(Xbmc_Select);
  161.  
  162. function Xbmc_Back()
  163. {
  164.   return Xbmc_SendCommand("Input.Back", {});
  165. }
  166. Console_RegisterCommand(Xbmc_Back);
  167.  
  168. function Xbmc_PlayPause()
  169. {
  170.   //Xbmc_SendCommand("Player.GetActivePlayers", {});
  171.  
  172.   return Xbmc_SendCommand("Player.PlayPause", { "playerid" : 1 });
  173. }
  174. Console_RegisterCommand(Xbmc_PlayPause);
  175.  
  176. function Xbmc_Stop()
  177. {
  178.   return Xbmc_SendCommand("Player.Stop", { "playerid" : 1 });
  179. }
  180. Console_RegisterCommand(Xbmc_Stop);
  181.  
  182.