From efad979b6fae76fb37a4de7e94e6bac0a85cb72c Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期二, 30 十一月 2021 13:14:28 +0800
Subject: [PATCH] 2021-11-30 1.修复乐橙Android,没调用拒接接口问题。

---
 HDL_ON/UI/UI2/FuntionControlView/Music/SendMethod.cs |  285 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 274 insertions(+), 11 deletions(-)

diff --git a/HDL_ON/UI/UI2/FuntionControlView/Music/SendMethod.cs b/HDL_ON/UI/UI2/FuntionControlView/Music/SendMethod.cs
index 60ec371..0a3fc25 100644
--- a/HDL_ON/UI/UI2/FuntionControlView/Music/SendMethod.cs
+++ b/HDL_ON/UI/UI2/FuntionControlView/Music/SendMethod.cs
@@ -4,12 +4,28 @@
 using System.Net.Sockets;
 using System.Security;
 using System.Text;
+using HDL_ON.DAL.Server;
+using HDL_ON.Entity;
 using Shared;
 
 namespace HDL_ON.UI.Music
 {
     public class SendMethod
     {
+        private static SendMethod m_sendMethod=null;
+        public static SendMethod sendMethod
+        {
+            get
+            {
+                if (m_sendMethod == null)
+                {
+                    m_sendMethod = new SendMethod();
+                }
+                return m_sendMethod;
+            }
+
+        }
+
         /// <summary>
         ///鎼滅储A31闊充箰鎾斁鍣�
         /// </summary>
@@ -152,7 +168,6 @@
         {
             try
             {
-
                 if (a31MusicModel.ServerClientType == 1)
                 {
                     var result = OpenWeb("http://" + a31MusicModel.IPAddress + "/httpapi.asp?command=multiroom:getSlaveList");
@@ -210,12 +225,27 @@
                 a31MusicModel.A31PlayStatus.loop = se.SearchForTextOfTag("LoopMode");
                 a31MusicModel.A31PlayStatus.Source = se.SearchForTextOfTag("PlayMedium");
                 a31MusicModel.A31PlayStatus.playSource = se.SearchForTextOfTag("TrackSource");
-                a31MusicModel.A31PlayStatus.TrackURL = se.SearchForTextOfTag("TrackURI");
+                a31MusicModel.A31PlayStatus.TrackURL = se.SearchForTextOfTag("TrackURI").Replace("&", "&amp;amp;");
 
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.Message);
+            }
+            finally
+            {
+                #region 鏇存柊鍏朵粬鐣岄潰闊充箰鐘舵��
+                Application.RunOnMainThread(() =>
+                {
+                    a31MusicModel.trait_on_off.curValue = a31MusicModel.A31PlayStatus.status == "play" ? "on" : "off";
+                    //Console.WriteLine($"music : {a31MusicModel.sid} : {a31MusicModel.on_off}");
+                    HomePage.UpdataFunctionStates(a31MusicModel);
+                    foreach (var e in A31MusicModel.A31MusicModelList)
+                    {
+                        RoomPage.UpdataStates(a31MusicModel);
+                    }
+                });
+                #endregion
             }
         }
 
@@ -449,7 +479,8 @@
         /// <param name="volume">Volume.</param>
         public static void ControlVolume(int volume, A31MusicModel a31player)
         {
-            System.Threading.Tasks.Task.Run(() => {
+            System.Threading.Tasks.Task.Run(() =>
+            {
                 A31MusicModel.ProgressDateTime = DateTime.Now;
                 try
                 {
@@ -509,14 +540,14 @@
             foreach (SecurityElement track in SecurityElement.FromString(se.Text).SearchForChildByTag("Tracks").Children)
             {
                 MusicInfo musicInfo = new MusicInfo();
-                musicInfo.URL = track.SearchForTextOfTag("URL");
+                musicInfo.URL = track.SearchForTextOfTag("URL").Replace("&", "&amp;amp;");
                 var metadata = track.SearchForTextOfTag("Metadata").Replace("&", "&amp;");
                 var item = SecurityElement.FromString(metadata).SearchForChildByTag("item");
-                musicInfo.Title = item.SearchForTextOfTag("dc:title");
-                musicInfo.Artist = item.SearchForTextOfTag("upnp:artist");
-                musicInfo.Album = item.SearchForTextOfTag("upnp:album");
+                musicInfo.Title = item.SearchForTextOfTag("dc:title").Replace("&", "&amp;amp;");
+                musicInfo.Artist = item.SearchForTextOfTag("upnp:artist").Replace("&", "&amp;amp;");
+                musicInfo.Album = item.SearchForTextOfTag("upnp:album").Replace("&", "&amp;amp;");
                 musicInfo.Duration = item.SearchForTextOfTag("res");
-                musicInfo.AlbumId = item.SearchForTextOfTag("song:albumid");
+                musicInfo.AlbumId = item.SearchForTextOfTag("song:albumid").Replace("&", "&amp;amp;");
                 musicInfoList.Add(musicInfo);
             }
             return musicInfoList;
@@ -556,7 +587,6 @@
         /// <returns>璇诲彇鍒扮殑鐢靛彴缁勫垪琛ㄤ俊鎭紝璇诲彇涓嶅埌鍙嶉涓簄ull</returns>
         public static string ReadRadioList(string url)
         {
-
             WebClient webClient = new WebClient();
             webClient.Proxy = null;
             webClient.Headers.Add("Content-type", "plain/text; charset=UTF-8");
@@ -584,7 +614,7 @@
         /// <summary>
         /// 鑾峰彇褰撳墠鎾斁鐨勫垪琛�
         /// </summary>
-        public static  string GetCurrentPlayList(A31MusicModel a31player)
+        public static string GetCurrentPlayList(A31MusicModel a31player)
         {
             System.Text.StringBuilder getPlayList = new System.Text.StringBuilder();
             getPlayList.AppendLine("<?xml version=\"1.0\"encoding=\"utf-8\"?>");
@@ -608,7 +638,7 @@
             return null;
         }
         /// <summary>
-        /// 鎾斁闊充箰鍛戒护
+        /// 鎾斁USB闊充箰鍛戒护
         /// </summary>
         /// <param name="listName">鍒楄〃鍚嶇О</param>
         /// <param name="number">鎾斁绱㈠紩</param>
@@ -634,6 +664,172 @@
             }
             catch { }
         }
+
+
+
+        /// <summary>
+        /// 鎺ㄩ�佹挱鏀惧懡浠�
+        /// </summary>
+        /// <param name="musicInfo">鎾斁闊充箰</param>
+        /// <param name="listName">鍒楄〃鍚嶇О</param>
+        /// <param name="musicList">鎺ㄩ�佸垪琛�(榛樿鎺�50棣�)</param>
+        public static void PushList(MusicInfo musicInfo, string listName, List<MusicInfo> musicList, A31MusicModel a31player, string musicSource)
+        {
+
+            //鏈�澶у彂閫佺殑鏉℃暟
+            int maxCount = 50;
+            //鎵惧嚭褰撳墠鐐瑰嚮闊充箰鐨勭储寮�
+            int statIndex = musicList.FindIndex((obj) => { return obj == musicInfo; });
+            //鐐瑰嚮鍚庨潰杩樻湁澶氬皯鏉¢煶涔�
+            int count = musicList.Count - statIndex;
+            int endIndex = 0;
+            if (maxCount < count)
+            {
+                //濡傛灉鍚庨潰鐨勯煶涔愬ぇ浜庢渶澶ф暟锛岃缃悗闈㈡暟鎹殑鏈�澶х储寮� 
+                endIndex = statIndex + maxCount - 1;
+            }
+            else
+            {
+                //濡傛灉鍚庨潰鏁版嵁涓嶅锛屽氨鐢ㄦ�绘暟鍑忓幓鏈�澶ф潯鏁帮紝寰楀埌寮�濮嬬储寮�
+                statIndex = musicList.Count - maxCount - 1;
+                endIndex = musicList.Count - 1;
+            }
+            //濡傛灉寮�濮嬬储寮曞皬浜�0锛岄偅灏辫缃负绗竴鏉¢煶涔�
+            if (statIndex < 0)
+            {
+                statIndex = 0;
+            }
+
+            StringBuilder sb = new StringBuilder();
+            sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
+            sb.AppendLine("<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
+            sb.AppendLine("<s:Body>");
+            sb.AppendLine("<u:CreateQueue xmlns:u=\"urn:schemas-wiimu-com:service:PlayQueue:1\">");
+            sb.AppendLine("<QueueContext>&lt;?xml version=\"1.0\"?&gt;");
+            sb.AppendLine("&lt;PlayList&gt;");
+            sb.AppendLine("&lt;ListName&gt;DLNA" + listName + "&lt;/ListName&gt;");
+            sb.AppendLine("&lt;ListInfo&gt;");
+            sb.AppendLine("&lt;Radio&gt;0&lt;/Radio&gt;");//
+            sb.AppendLine("&lt;SourceName&gt;UPnPServer&lt;/SourceName&gt;");//          
+            sb.AppendLine("&lt;MarkSearch&gt;0&lt;/MarkSearch&gt;");
+            sb.AppendLine("&lt;TrackNumber&gt;" + (endIndex - statIndex + 1) + "&lt;/TrackNumber&gt;");
+            sb.AppendLine("&lt;Quality&gt;0&lt;/Quality&gt;");
+            sb.AppendLine("&lt;UpdateTime&gt;5367&lt;/UpdateTime&gt;");
+            sb.AppendLine("&lt;LastPlayIndex&gt;2&lt;/LastPlayIndex&gt;");
+            sb.AppendLine("&lt;SwitchPageMode&gt;0&lt;/SwitchPageMode&gt;");
+            sb.AppendLine("&lt;CurrentPage&gt;0&lt;/CurrentPage&gt;");
+            sb.AppendLine("&lt;TotalPages&gt;0&lt;/TotalPages&gt;");
+            sb.AppendLine("&lt;/ListInfo&gt;");
+            sb.AppendLine("&lt;Tracks&gt;");
+
+            int number = 1;
+
+            for (int i = statIndex, Tracknumber = 1; i <= endIndex; i++, Tracknumber++)
+            {
+                var tempMusicInfo = musicList[i];
+
+                if (musicInfo == tempMusicInfo)
+                {
+                    number = Tracknumber;
+                }
+
+                if (tempMusicInfo.SourceType == "Local")
+                {
+                    tempMusicInfo.URL = "http://" + new Shared.Net.NetWiFi().IpAddress + ":" + com.hdl.on.Server.Port + "/" + tempMusicInfo.ID;
+                }
+
+                sb.AppendLine("&lt;Track" + Tracknumber + "&gt;");
+                sb.AppendLine("&lt;URL&gt;" + tempMusicInfo.URL + "&lt;/URL&gt;");
+                sb.AppendLine("&lt;Metadata&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;");
+                sb.AppendLine("&amp;lt;DIDL-Lite xmlns:dc=&amp;quot;http://purl.org/dc/elements/1.1/&amp;quot; xmlns:upnp=&amp;quot;urn:schemas-upnp-org:metadata-1-0/upnp/&amp;quot; xmlns:song=&amp;quot;www.wiimu.com/song/&amp;quot; xmlns=&amp;quot;urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/&amp;quot;&amp;gt;");
+                sb.AppendLine("&amp;lt;upnp:class&amp;gt;object.item.audioItem.musicTrack&amp;lt;/upnp:class&amp;gt;");
+                sb.AppendLine("&amp;lt;item id=&amp;quot;0&amp;quot;&amp;gt;");
+                sb.AppendLine("&amp;lt;song:subid&amp;gt;&amp;lt;/song:subid&amp;gt;");
+                sb.AppendLine("&amp;lt;song:description&amp;gt;unknown&amp;lt;/song:description&amp;gt;");
+                sb.AppendLine("&amp;lt;song:skiplimit&amp;gt;6&amp;lt;/song:skiplimit&amp;gt;");
+                sb.AppendLine("&amp;lt;song:id&amp;gt;" + tempMusicInfo.ID + "&amp;lt;/song:id&amp;gt;");
+                sb.AppendLine("&amp;lt;song:like&amp;gt;0&amp;lt;/song:like&amp;gt;");
+                sb.AppendLine("&amp;lt;song:singerid&amp;gt;0&amp;lt;/song:singerid&amp;gt;");
+                sb.AppendLine("&amp;lt;song:albumid&amp;gt;" + tempMusicInfo.AlbumId + "&amp;lt;/song:albumid&amp;gt;");
+
+                sb.AppendLine("&amp;lt;res protocolInfo=&amp;quot;http-get:*:audio/mpeg:DLNA.ORG_PN=MP3;DLNA.ORG_OP=01;&amp;quot; duration=&amp;quot;" + "0:0:0.0" + "&amp;quot;&amp;gt;" + tempMusicInfo.URL + "&amp;lt;/res&amp;gt;");
+
+                sb.AppendLine("&amp;lt;dc:title&amp;gt;" + tempMusicInfo.Title + "&amp;lt;/dc:title&amp;gt;");
+                sb.AppendLine("&amp;lt;dc:creator&amp;gt;DJ Sanny J&amp;lt;/dc:creator&amp;gt;");
+                if (musicSource == "鎴戠殑鍒楄〃")
+                {
+                    sb.AppendLine("&amp;lt;upnp:artist&amp;gt;" + listName + "&amp;lt;/upnp:artist&amp;gt;");
+                }
+                else
+                {
+                    sb.AppendLine("&amp;lt;upnp:artist&amp;gt;" + tempMusicInfo.Artist + "&amp;lt;/upnp:artist&amp;gt;");
+                }
+                sb.AppendLine("&amp;lt;upnp:album&amp;gt;" + tempMusicInfo.Album + "&amp;lt;/upnp:album&amp;gt;");
+                sb.AppendLine("&amp;lt;upnp:albumArtURI&amp;gt;unknown&amp;lt;/upnp:albumArtURI&amp;gt;");
+                sb.AppendLine("&amp;lt;/item&amp;gt;");
+                sb.AppendLine("&amp;lt;/DIDL-Lite&amp;gt;");
+                sb.AppendLine("&lt;/Metadata&gt;");
+                sb.AppendLine("&lt;Id&gt;" + tempMusicInfo.ID + "&lt;/Id&gt;");
+                sb.AppendLine("&lt;Source&gt;STATION-NETWORK&lt;/Source&gt;");//UPnPServer
+                sb.AppendLine("&lt;Key&gt;" + tempMusicInfo.URL + "&lt;/Key&gt;");
+                sb.AppendLine("&lt;/Track" + Tracknumber + "&gt;");
+            }
+            sb.AppendLine("&lt;/Tracks&gt;");
+            sb.AppendLine("&lt;/PlayList&gt;");
+            sb.AppendLine("</QueueContext>");
+            sb.AppendLine("</u:CreateQueue>");
+            sb.AppendLine("</s:Body>");
+            sb.AppendLine("</s:Envelope>");
+
+            SendMusicLists(a31player.IPAddress, a31player.Port, "CreateQueue", sb.ToString());
+
+            StringBuilder playString = new StringBuilder();
+            playString.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
+            playString.AppendLine("<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
+            playString.AppendLine("<s:Body>");
+            playString.AppendLine("<u:PlayQueueWithIndex xmlns:u=\"urn:schemas-wiimu-com:service:PlayQueue:1\">");
+            playString.AppendLine("<QueueName>DLNA" + listName + "</QueueName>");
+            playString.AppendLine("<Index>" + number + "</Index>");
+            playString.AppendLine("</u:PlayQueueWithIndex>");
+            playString.AppendLine("</s:Body>");
+            playString.AppendLine("</s:Envelope>");
+
+            Play(a31player.IPAddress, a31player.Port, playString.ToString());
+        }
+
+        static void SendMusicLists(string ip, int port, string soapAction, string listInfo)
+        {
+            System.Net.WebClient webClient = new System.Net.WebClient();
+            webClient.Headers.Add("SOAPACTION", "\"urn:schemas-wiimu-com:service:PlayQueue:1#" + soapAction + "\"");
+            webClient.Headers.Add("CONTENT-TYPE", "text/xml; charset=\"utf-8\"");
+            try
+            {
+                byte[] recevieBytes = webClient.UploadData(new Uri("http://" + ip + ":" + port + "/upnp/control/PlayQueue1"), "POST", System.Text.Encoding.UTF8.GetBytes(listInfo));
+                var s = System.Text.Encoding.UTF8.GetString(recevieBytes, 0, recevieBytes.Length);
+            }
+            catch (Exception e)
+            {
+                var d = e.Message;
+            }
+        }
+
+        static void Play(string ip, int port, string info)
+        {
+            System.Net.WebClient webClient = new System.Net.WebClient();
+            webClient.Headers.Add("SOAPACTION", "\"urn:schemas-wiimu-com:service:PlayQueue:1#PlayQueueWithIndex\"");
+            webClient.Headers.Add("CONTENT-TYPE", "text/xml; charset=\"utf-8\"");
+            try
+            {
+                byte[] recevieBytes = webClient.UploadData(new Uri("http://" + ip + ":" + port + "/upnp/control/PlayQueue1"), "POST", System.Text.Encoding.UTF8.GetBytes(info));
+                var s = System.Text.Encoding.UTF8.GetString(recevieBytes, 0, recevieBytes.Length);
+            }
+            catch
+            {
+
+            }
+        }
+
+
 
         static System.Threading.Thread thread;
         /// <summary>
@@ -705,5 +901,72 @@
             public string creator = string.Empty;
         }
 
+        /// <summary>
+        /// 鑾峰彇闊充箰鍒楄〃
+        /// </summary>
+        public List<Function> GetMusicList
+        {
+            get
+            {
+                return FunctionList.List.GetMusicList();
+            }
+        }
+        /// <summary>
+        /// 鍙戦�佹帶鍒跺懡浠�
+        /// </summary>
+        /// <param name="function">褰撳墠璁惧</param>
+        /// <param name="dic">鍙戦�佹帶鍒舵暟鎹�</param>
+        public void SendControlCommand(Function function, Dictionary<string, string> dic)
+        {
+            new System.Threading.Thread(() =>
+            {
+                DriverLayer.Control.Ins.SendWriteCommand(function, dic);
+            })
+            { IsBackground = true }.Start();
+        }
+
+        /// <summary>
+        /// 鍒锋柊璁惧鐘舵��
+        /// </summary>
+        /// <returns></returns>
+        public void RefreshDeviceStatus(Function music, List<string> functionIds)
+        {
+            try
+            {
+                Dictionary<string, object> d = new Dictionary<string, object>();
+                d.Add("homeId", DB_ResidenceData.Instance.CurrentRegion.id);
+                d.Add("deviceIds", functionIds);
+                var responsePackNew = RequestServerhomeId(d, NewAPI.Api_Post_RefreshDeviceStatus);
+                if (responsePackNew.Code != "0")
+                {
+                    return;
+                }
+                //鏁版嵁杩斿簭鍒楀寲涓篎unction瀵硅薄
+                var str = Newtonsoft.Json.JsonConvert.SerializeObject(responsePackNew.Data.ToString());
+                var function = Newtonsoft.Json.JsonConvert.DeserializeObject<Function>(str);
+                if (function == null)
+                {
+                    return;
+                }
+                music.name = function.name;
+                music.sid = function.sid;
+                music.spk = function.spk;
+                music.status = function.status;
+                music.roomIds = function.roomIds;
+                music.attributes = function.attributes;
+            }
+            catch { }
+        }
+
+        /// <summary>
+        ///璇锋眰鏈嶅姟鍣紙涓庝綇瀹呮湁鍏�:渚嬪锛沨omeId锛� 
+        /// </summary>
+        /// <returns></returns>
+        public  ResponsePackNew RequestServerhomeId(object o, string api_Url, int mTimeout = 5)
+        {
+            var requestJson = HttpUtil.GetSignRequestJson(o);
+            return HttpUtil.RequestHttpsPostFroHome(api_Url, requestJson, mTimeout);
+
+        }
     }
 }

--
Gitblit v1.8.0