Subversion Repositories HomeAutomation

Rev

Rev 1954 | 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.     Log(state);
  84.    
  85.     if (state == 0) // Connected
  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.       for (var n = 0; n < Mbb_Susbscribers.length; n++)
  95.       {
  96.         Xbmc_Susbscribers[n]("disconnected", socket_id);
  97.        
  98.         if (socket_id == Xbmc_Socket_Id)
  99.         {
  100.           Xbmc_Socket_Id = null;
  101.         }
  102.       }
  103.     }
  104.   });
  105.  
  106.   if (Xbmc_Socket_Id != 0)
  107.   {
  108.     Log("Connected to XBMC at " + host + ":" + port + " successfully!");
  109.     return true;
  110.   }
  111.  
  112.   Xbmc_Socket_Id = null;
  113.   return false;
  114. }
  115. Console_RegisterCommand(Xbmc_Connect);
  116.  
  117. function Xbmc_Disconnect()
  118. {
  119.   if (Xbmc_IsConnected())
  120.   {
  121.     Socket_Disconnect(Xbmc_Socket_Id);
  122.   }
  123.  
  124.   return true;
  125. }
  126. Console_RegisterCommand(Xbmc_Disconnect);
  127.  
  128. function Xbmc_Right()
  129. {
  130.   return Xbmc_SendCommand("Input.Right", {});
  131. }
  132. Console_RegisterCommand(Xbmc_Right);
  133.  
  134. function Xbmc_Left()
  135. {
  136.   return Xbmc_SendCommand("Input.Left", {});
  137. }
  138. Console_RegisterCommand(Xbmc_Left);
  139.  
  140. function Xbmc_Up()
  141. {
  142.   return Xbmc_SendCommand("Input.Up", {});
  143. }
  144. Console_RegisterCommand(Xbmc_Up);
  145.  
  146. function Xbmc_Down()
  147. {
  148.   return Xbmc_SendCommand("Input.Down", {});
  149. }
  150. Console_RegisterCommand(Xbmc_Down);
  151.  
  152. function Xbmc_Select()
  153. {
  154.   return Xbmc_SendCommand("Input.Select", {});
  155. }
  156. Console_RegisterCommand(Xbmc_Select);
  157.  
  158. function Xbmc_Back()
  159. {
  160.   return Xbmc_SendCommand("Input.Back", {});
  161. }
  162. Console_RegisterCommand(Xbmc_Back);
  163.  
  164. function Xbmc_PlayPause()
  165. {
  166.   //Xbmc_SendCommand("Player.GetActivePlayers", {});
  167.  
  168.   return Xbmc_SendCommand("Player.PlayPause", { "playerid" : 1 });
  169. }
  170. Console_RegisterCommand(Xbmc_PlayPause);
  171.  
  172. function Xbmc_Stop()
  173. {
  174.   return Xbmc_SendCommand("Player.Stop", { "playerid" : 1 });
  175. }
  176. Console_RegisterCommand(Xbmc_Stop);
  177.  
  178.