Subversion Repositories HomeAutomation

Rev

Rev 2294 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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