|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Data.Linq; |
| 6 | +using System.Xml; |
| 7 | +using System.Net; |
| 8 | + |
| 9 | +namespace HomeMediaCenter |
| 10 | +{ |
| 11 | + public class ItemContainerDreambox : ItemContainerStream |
| 12 | + { |
| 13 | + protected class ServiceParamItemEqualityComparer : IEqualityComparer<object> |
| 14 | + { |
| 15 | + public new bool Equals(object x, object y) |
| 16 | + { |
| 17 | + ServiceParam itemX; |
| 18 | + Item itemY = y as Item; |
| 19 | + |
| 20 | + if (itemY == null) |
| 21 | + { |
| 22 | + itemX = y as ServiceParam; |
| 23 | + itemY = x as Item; |
| 24 | + } |
| 25 | + else |
| 26 | + { |
| 27 | + itemX = x as ServiceParam; |
| 28 | + } |
| 29 | + |
| 30 | + return (itemX.Title == itemY.Title) && (itemX.Path == itemY.Path); |
| 31 | + } |
| 32 | + |
| 33 | + public int GetHashCode(object obj) |
| 34 | + { |
| 35 | + Item itemY = obj as Item; |
| 36 | + |
| 37 | + if (itemY == null) |
| 38 | + { |
| 39 | + ServiceParam itemX = obj as ServiceParam; |
| 40 | + if (itemX == null) |
| 41 | + throw new NullReferenceException(); |
| 42 | + |
| 43 | + return itemX.Title.GetHashCode() ^ itemX.Path.GetHashCode(); |
| 44 | + } |
| 45 | + else |
| 46 | + { |
| 47 | + return itemY.Title.GetHashCode() ^ itemY.Path.GetHashCode(); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + protected class ServiceParam |
| 53 | + { |
| 54 | + public string Title |
| 55 | + { |
| 56 | + get; set; |
| 57 | + } |
| 58 | + |
| 59 | + public string Path |
| 60 | + { |
| 61 | + get; set; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public ItemContainerDreambox() : base() { } |
| 66 | + |
| 67 | + public ItemContainerDreambox(string title, string path, ItemContainer parent) : base(title, path, parent) { } |
| 68 | + |
| 69 | + public override void RefreshMe(DataContext context, ItemManager manager, bool recursive) |
| 70 | + { |
| 71 | + if (manager.UpnpDevice.Stopping) |
| 72 | + return; |
| 73 | + |
| 74 | + foreach (Item item in this.Items) |
| 75 | + item.RefreshMe(context, manager, true); |
| 76 | + } |
| 77 | + |
| 78 | + public void RefreshDreambox(DataContext context, ItemManager manager, Uri basePath, bool isBouquet, ref string pathPrefix) |
| 79 | + { |
| 80 | + Uri servicePath = new Uri(basePath, "/web/getservices" + (isBouquet ? string.Empty : ("?sRef=" + this.Path))); |
| 81 | + |
| 82 | + XmlDocument serviceDoc = new XmlDocument(); |
| 83 | + serviceDoc.Load(servicePath.ToString()); |
| 84 | + |
| 85 | + string pPrefix; |
| 86 | + if (isBouquet) |
| 87 | + pPrefix = string.Empty; |
| 88 | + else if (pathPrefix == null) |
| 89 | + pPrefix = pathPrefix = GetDreamboxStreamAddress(basePath, serviceDoc); |
| 90 | + else |
| 91 | + pPrefix = pathPrefix; |
| 92 | + |
| 93 | + |
| 94 | + //Rozdielova obnova parametrov poloziek v tomto kontajnery |
| 95 | + IEnumerable<ServiceParam> serviceParams = serviceDoc.SelectNodes("/e2servicelist/e2service").Cast<XmlNode>().Select( |
| 96 | + a => new ServiceParam() { Title = a.SelectSingleNode("e2servicename").InnerText, Path = pPrefix + a.SelectSingleNode("e2servicereference").InnerText } |
| 97 | + ).ToArray(); |
| 98 | + |
| 99 | + Item[] toRemove = this.Items.Except(serviceParams, new ServiceParamItemEqualityComparer()).Cast<Item>().ToArray(); |
| 100 | + ServiceParam[] toAdd = serviceParams.Except(this.Items, new ServiceParamItemEqualityComparer()).Cast<ServiceParam>().ToArray(); |
| 101 | + |
| 102 | + RemoveRange(context, manager, toRemove); |
| 103 | + |
| 104 | + foreach (ServiceParam param in toAdd) |
| 105 | + { |
| 106 | + if (isBouquet) |
| 107 | + new ItemContainerDreambox(param.Title, param.Path, this); |
| 108 | + else |
| 109 | + new ItemContainerStream(param.Title, param.Path, this); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private string GetDreamboxStreamAddress(Uri baseUri, XmlDocument serviceDoc) |
| 114 | + { |
| 115 | + try |
| 116 | + { |
| 117 | + //Pokusi sa ziskat stream port na zaklade prveho e2service |
| 118 | + string serviceRef = serviceDoc.SelectSingleNode("/e2servicelist/e2service/e2servicereference").InnerText; |
| 119 | + Uri playlistPath = new Uri(baseUri, "/web/stream.m3u?ref=" + serviceRef); |
| 120 | + |
| 121 | + using (WebClient client = new WebClient()) |
| 122 | + using (System.IO.Stream stream = client.OpenRead(playlistPath)) |
| 123 | + using (System.IO.StreamReader reader = new System.IO.StreamReader(stream)) |
| 124 | + { |
| 125 | + string line; |
| 126 | + while ((line = reader.ReadLine()) != null) |
| 127 | + { |
| 128 | + if (!line.StartsWith("#")) |
| 129 | + break; |
| 130 | + } |
| 131 | + |
| 132 | + Uri streamPath; |
| 133 | + if (Uri.TryCreate(line, UriKind.Absolute, out streamPath)) |
| 134 | + return streamPath.GetLeftPart(UriPartial.Authority) + "/"; |
| 135 | + } |
| 136 | + } |
| 137 | + catch { } |
| 138 | + |
| 139 | + //Ak nie je najdeny odkaz nastav default port |
| 140 | + return string.Format("http://{0}:{1}/", baseUri.Host, 8001); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments