-
Notifications
You must be signed in to change notification settings - Fork 0
/
MopidyClient.cs
209 lines (173 loc) · 7.28 KB
/
MopidyClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Windows.Data.Json;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
namespace BackgroundApplication1
{
class MopidyClient : IDisposable
{
private MessageWebSocket socket;
private DataWriter messageWriter;
private Dictionary<double, Action<JsonObject>> callbacks = new Dictionary<double, Action<JsonObject>>();
private int currentId = 0;
public MopidyClient()
{
socket = new MessageWebSocket();
socket.Control.MessageType = SocketMessageType.Utf8;
socket.Closed += (senderSocket, args) =>
{
// DO SOMETHING
};
socket.MessageReceived += (sender, args) =>
{
using (var reader = args.GetDataReader())
{
reader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
string resultString = reader.ReadString(reader.UnconsumedBufferLength);
JsonObject message;
if (JsonObject.TryParse(resultString, out message))
{
IJsonValue idValue;
if (message.TryGetValue("id", out idValue))
{
double id = idValue.GetNumber();
Action<JsonObject> callback;
if (callbacks.TryGetValue(id, out callback))
{
callback(message);
}
}
}
}
};
messageWriter = new DataWriter(socket.OutputStream);
}
public void Dispose()
{
messageWriter?.Dispose();
messageWriter = null;
socket?.Dispose();
socket = null;
}
public async Task Open()
{
await socket.ConnectAsync(new Uri("ws://192.168.1.111:6680/mopidy/ws"));
}
private void Socket_MessageReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args)
{
}
public async Task<string> Search(string search)
{
using (AutoResetEvent handle = new AutoResetEvent(false))
{
JsonObject message = new JsonObject();
message.Add("method", JsonValue.CreateStringValue("core.library.search"));
JsonObject queryObject = new JsonObject();
queryObject.Add("track_name", JsonValue.CreateStringValue(search));
JsonArray urisArray = new JsonArray();
urisArray.Add(JsonValue.CreateStringValue("spotify:"));
JsonObject paramsObject = new JsonObject();
paramsObject.Add("query", queryObject);
paramsObject.Add("uris", urisArray);
message.Add("params", paramsObject);
string result = null;
await Send(message, searchResult =>
{
JsonArray tracks = searchResult.GetNamedArray("result")[0].GetObject().GetNamedArray("tracks");
result = tracks.First().GetObject().GetNamedString("uri");
handle.Set();
});
handle.WaitOne(TimeSpan.FromSeconds(30));
return result;
}
}
public async Task<string> SearchArtist(string artistName)
{
using (AutoResetEvent handle = new AutoResetEvent(false))
{
JsonObject message = new JsonObject();
message.Add("method", JsonValue.CreateStringValue("core.library.search"));
JsonObject queryObject = new JsonObject();
queryObject.Add("artist", JsonValue.CreateStringValue(artistName));
JsonArray urisArray = new JsonArray();
urisArray.Add(JsonValue.CreateStringValue("spotify:"));
JsonObject paramsObject = new JsonObject();
paramsObject.Add("query", queryObject);
paramsObject.Add("uris", urisArray);
message.Add("params", paramsObject);
string result = null;
await Send(message, searchResult =>
{
JsonArray resultArray = searchResult.GetNamedArray("result");
JsonArray artists = resultArray.FirstOrDefault()?.GetObject().GetNamedArray("artists");
if (artists != null)
{
result = artists.FirstOrDefault()?.GetObject().GetNamedString("uri");
}
handle.Set();
});
handle.WaitOne(TimeSpan.FromSeconds(30));
return result;
}
}
private async Task Send(JsonObject message, Action<JsonObject> callback = null)
{
double id = currentId++;
message.SetNamedValue("jsonrpc", JsonValue.CreateStringValue("2.0"));
message.SetNamedValue("id", JsonValue.CreateNumberValue(id));
if (callback != null)
{
callbacks[id] = callback;
}
messageWriter.WriteString(message.Stringify());
await messageWriter.StoreAsync();
}
public async Task Play(string trackUri)
{
// Buffer any data we want to send.
messageWriter.WriteString(@"[
{""jsonrpc"": ""2.0"", ""id"": 1, ""method"": ""core.tracklist.clear"" },
{ ""jsonrpc"": ""2.0"", ""id"": 1, ""method"": ""core.tracklist.add"", ""params"": { ""uri"": """ + trackUri + @"""} },
{ ""jsonrpc"": ""2.0"", ""id"": 1, ""method"": ""core.playback.play""}
]");
// Send the data as one complete message.
await messageWriter.StoreAsync();
}
public async Task Stop()
{
JsonObject message = new JsonObject();
message.Add("method", JsonValue.CreateStringValue("core.tracklist.clear"));
await Send(message);
}
public async Task<int> GetVolume()
{
JsonObject message = new JsonObject();
message.Add("method", JsonValue.CreateStringValue("core.playback.get_volume"));
using (AutoResetEvent handle = new AutoResetEvent(false))
{
int result = -1;
await Send(message, volumeResult =>
{
double volumeDouble = volumeResult.GetNamedNumber("result");
result = (int)volumeDouble;
handle.Set();
});
handle.WaitOne(TimeSpan.FromSeconds(30));
return result;
}
}
public async Task SetVolume(int volume)
{
JsonObject message = new JsonObject();
message.Add("method", JsonValue.CreateStringValue("core.playback.set_volume"));
JsonArray paramsArray = new JsonArray();
paramsArray.Add(JsonValue.CreateNumberValue(volume));
message.Add("params", paramsArray);
await Send(message);
}
}
}