/// <summary> /// Sets all items to "Not available". /// Should be called when there's no connection to the server. /// </summary> void SetAllNotAvailable() { foreach (Player p in Player.GetAllPlayers()) { p.Available = false; } lock (artists) foreach (MusicItem a in artists) { a.Available = false; } lock (albums) foreach (MusicItem a in albums) { a.Available = false; } lock (radios) foreach (RadioSuperItem r in radios) { r.Available = false; } }
void ParseResponse(string response) { string head = response.Substring(0, Math.Max(0, response.IndexOf(' '))); if (string.Equals(head, "players")) { // parse players foreach (QueryResponseItem itm in ParseQueryResponse("playerindex", response, new string[] { "playerid", "name", "model", "connected", "canpoweroff" })) { Player.CreatePlayer(itm.Values[0], itm.Values[1], itm.Values[2], itm.Values[3] == "1", false, itm.Values[4] == "1"); } // get status for all players foreach (Player p in Player.GetAllPlayers()) { commandQueue.Enqueue(p.Id + " status - 0 subscribe:0"); } playersLoaded = true; // now load radios, artists and albums commandQueue.Enqueue("radios - 100000"); commandQueue.Enqueue("artists 0 1000000"); commandQueue.Enqueue("albums 0 1000000 tags:lyja"); } else if (string.Equals(head, "artists")) { artistnameToArtistMap.Clear(); lock (artists) { artists.Clear(); ArtistMusicItem artist; foreach (QueryResponseItem itm in ParseQueryResponse("id", response, new string[] { "id", "artist" })) { artist = new ArtistMusicItem(int.Parse(itm.Values[0]), itm.Values[1]); artists.Add(artist); artistnameToArtistMap.Add(artist.Artist, artist); } } #if VERBOSE_OUTPUT Console.WriteLine("SQC: Artists loaded"); #endif } else if (string.Equals(head, "albums")) { lock (albums) { albums.Clear(); ArtistMusicItem artist; int firstSongId; foreach (QueryResponseItem itm in ParseQueryResponse("id", response, new string[] { "id", "album", "year", "artwork_track_id", "artist" })) { if (!artistnameToArtistMap.TryGetValue(itm.Values[4], out artist)) { artist = null; } if (!int.TryParse(itm.Values[3], out firstSongId)) { firstSongId = -1; } albums.Add(new AlbumMusicItem(int.Parse(itm.Values[0]), itm.Values[1], artist, itm.Values[2], firstSongId)); } } artistAndAlbumsLoaded = true; #if VERBOSE_OUTPUT Console.WriteLine("SQC: Albums loaded"); #endif } else if (string.Equals(head, "radios")) { lock (radios) { radios.Clear(); foreach (QueryResponseItem itm in ParseQueryResponse("cmd", response, new string[] { "cmd", "name", "type" })) { // only xmlbrowser types if (itm.Values[2] == "xmlbrowser" && radiosToLoad.Contains(itm.Values[1].ToLower())) { RadioSuperItem radio = new RadioSuperItem(itm.Values[0], itm.Values[1]); radios.Add(radio); // request radio items commandQueue.Enqueue(radio.Command + " items 0 100000"); } } } radiosLoaded = true; } else if (string.Equals(head, "rescan") && string.Equals(response, "rescan done")) { commandQueue.Enqueue("artists 0 1000000"); commandQueue.Enqueue("albums 0 1000000 tags:lyja"); } else { // check if it's a player Player player = Player.GetFromId(Util.UriDecode(head)); if (player != null && response.Length > head.Length + 1) { response = response.Substring(head.Length + 1); int i = response.IndexOf(' '); if (i < 0) { i = response.Length; } string command = response.Substring(0, i); #if VERBOSE_OUTPUT Console.WriteLine("SQC: Player command response: " + command); #endif if (string.Equals(command, "status")) { var parsedResponse = ParseQueryResponse("player_name", response, new string[] { "player_connected", "power", "sync_master", "sync_slaves", "mode" }); foreach (QueryResponseItem itm in parsedResponse) { // find status bool isConnected = itm.Values[0] == "1"; bool isPoweredOn = itm.Values[1] == "1"; string mode = itm.Values[4]; PlayerStatus status; if (!isConnected) { status = PlayerStatus.Disconnected; } else { if (!isPoweredOn) { status = PlayerStatus.TurnedOff; } else { // parse mode switch (mode) { case "play": status = PlayerStatus.Playing; break; case "pause": status = PlayerStatus.Paused; break; default: status = PlayerStatus.Stopped; break; } } } player.Status = status; // get players that are synced with this player List <Player> syncedPlayers = new List <Player> (); if (itm.Values[2] != null && itm.Values[3] != null) { string playersInSyncGroup = string.Format("{0},{1}", itm.Values[2], itm.Values[3]); foreach (string s in playersInSyncGroup.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { Player p = Player.GetFromId(s.Trim()); if (p != null && p != player) { syncedPlayers.Add(p); } } } // due to bug http://bugs.slimdevices.com/show_bug.cgi?id=7990 // we need to get the status of all players that are now unsynced // BEGIN workaround foreach (Player p in player.SyncedPlayers) { if (!syncedPlayers.Contains(p)) { commandQueue.Enqueue(p.Id + " status - 0"); } } // END workaround player.SetSynchedPlayers(syncedPlayers); } } else { // check if it's a radio RadioSuperItem radio = null; lock (radios) foreach (RadioSuperItem r in radios) { if (response.StartsWith(r.Command + " items ")) { radio = r; break; } } if (radio != null) { // get position of content start int pos = response.IndexOf("item_id%3A"); if (pos < 0) { pos = response.IndexOf("title%3A"); } if (pos > 0) { string ids; int id; List <RadioSubItem> children; RadioSubItem child; // find parent RadioItem parent = radio; // is there a item_id filter? if (response.Substring(pos).StartsWith("item_id%3A")) { ids = response.Substring(pos + 10, response.IndexOf(" ", pos + 10) - pos - 10); foreach (string subId in ids.Split(new char[] { '.' })) { foreach (RadioSubItem rmi in parent.Children) { if (rmi.Id.ToString() == subId) { parent = rmi; break; } } } } //Console.WriteLine (response); children = new List <RadioSubItem> (); foreach (QueryResponseItem itm in ParseQueryResponse("id", response, new string[] { "id", "hasitems", "name" })) { ids = itm.Values[0]; if (parent is RadioSubItem && ids == (parent as RadioSubItem).IdPath) { continue; } if (ids.Contains(".")) { ids = ids.Substring(ids.LastIndexOf(".") + 1); } if (int.TryParse(ids, out id) && itm.Values[2] != null) { child = new RadioSubItem(parent, id, itm.Values[2], itm.Values[1] != null && itm.Values[1] != "0"); children.Add(child); // request children if (child.HasItems) { commandQueue.Enqueue(string.Format("{0} items 0 10000 item_id:{1}", child.GetSuper().Command, child.IdPath)); } } } parent.Children = children.ToArray(); } } } } } }