mac
2024-07-25 3f6685c77beeb12baf840733fb890860f4c26e7c
HDL_ON/UI/UI2/FuntionControlView/Music/SendMethod.cs
@@ -6,22 +6,25 @@
using System.Text;
using HDL_ON.DAL.Server;
using HDL_ON.Entity;
using HDL_ON.UI.UI2.FuntionControlView.VideoDoorLock;
using Newtonsoft.Json.Linq;
using Shared;
using static HDL_ON.UI.UI2.FuntionControlView.VideoDoorLock.CommonMethod;
namespace HDL_ON.UI.Music
{
    public class SendMethod
    {
        private static SendMethod sMethod=null;
        public static SendMethod mMethod
        private static SendMethod s_Current = null;
        public static SendMethod Current
        {
            get
            {
                if (sMethod == null)
                if (s_Current == null)
                {
                    sMethod = new SendMethod();
                    s_Current = new SendMethod();
                }
                return sMethod;
                return s_Current;
            }
        }
@@ -44,7 +47,7 @@
        {
            new System.Threading.Thread(() =>
            {
                DriverLayer.Control.Ins.SendWriteCommand(function, dic);
                DriverLayer.Control.Ins.SendWriteCommand(function, dic, false, 0);
            })
            { IsBackground = true }.Start();
        }
@@ -59,7 +62,7 @@
            {
                //RefreshDeviceStatus(functionIds);
                a31Music.LastDateTime = DateTime.Now;
                ///从缓存里面查找音乐播放器对象<缓存数据收到推送过来的状态会更新缓存数据>
                ///从缓存里面查找音乐播放器对象<收到推送过来的状态会更新缓存数据>
                var allLocalFuntion = FunctionList.List.GetDeviceFunctionList();
                var localFunction = allLocalFuntion.Find((obj) => obj.sid == sid);
                if (localFunction == null)
@@ -68,6 +71,7 @@
                }
                ///更新的数据
                a31Music.functionMusic = localFunction;
            }
            catch { }
        }
@@ -75,16 +79,17 @@
        /// <summary>
        /// 刷新设备状态
        /// </summary>
        /// <param name="tipType">是否需要提示,默认提示</param>
        /// <returns></returns>
        public void RefreshDeviceStatus(List<string> functionIds)
        public void RefreshDeviceStatus(List<string> functionIds,TipType tipType = TipType.flicker)
        {
            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" || responsePackNew.Data == null || responsePackNew.Data.ToString() == "")
                var responsePackNew = RequestServerhomeId(d, NewAPI.Api_Post_RefreshDeviceStatus, "刷新设备状态");
                if (!this.DataChecking(responsePackNew, tipType))
                {
                    return;
                }
@@ -108,50 +113,222 @@
        }
        /// <summary>
        /// 获取音乐列表
        /// 获取当前播放音乐列表
        /// </summary>
        /// <param name="music"></param>
        /// <param name="music">音乐播放器</param>
        /// <param name="action">回调</param>
        ///  <param name="tipType">是否需要提示,默认提示</param>
        /// <returns></returns>
        public void GetPalyList(Function music)
        public void GetCurrentPalyList(Function music, Action action, TipType tipType = TipType.flicker)
        {
            try
            {
                Dictionary<string, object> d = new Dictionary<string, object>();
                d.Add("homeId", DB_ResidenceData.Instance.CurrentRegion.id);
                d.Add("deviceIds", new List<string> { music.deviceId });
                var responsePackNew = RequestServerhomeId(d, NewAPI.Api_Post_PlayerList);
                if (responsePackNew.Code != "0"||responsePackNew.Data == null ||responsePackNew.Data.ToString() == "")
                var responsePackNew = RequestServerhomeId(d, NewAPI.Api_Post_PlayerList, "获取音乐列表");
                if (!this.DataChecking(responsePackNew,tipType))
                {
                    return;
                }
                //数据返序列化为Function对象
                var str = Newtonsoft.Json.JsonConvert.SerializeObject(responsePackNew.Data);
                var palyLists = Newtonsoft.Json.JsonConvert.DeserializeObject<List<PalyListInfo>>(str);
                var palyLists = Newtonsoft.Json.JsonConvert.DeserializeObject<List<PalyerSongListInfo>>(str);
                if (palyLists == null)
                {
                    palyLists =new List<PalyListInfo>();
                    palyLists = new List<PalyerSongListInfo>();
                }
                if (palyLists.Count > 0)
                {
                    ///默认拿第一个列表
                    A31MusicModel.Current.palyLists = palyLists[0].playlist;
                    A31MusicModel.Current.CurrentPlayMusicInfoList = palyLists[0].playlist;
                }
            }
            catch
            {
            }
            finally
            {
                Application.RunOnMainThread(() =>
                {
                    action();
                });
            }
        }
        /// <summary>
        /// 获取【多个音乐源】列表名列表
        /// </summary>
        /// <param name="music"></param>
        /// <param name="source">音乐源为null,支持多个源获取</param>
        ///  <param name="tipType">是否需要提示,默认提示</param>
        /// <returns></returns>
        public List<GroupList> GetMultipleSourceListName(Function music, string source,TipType tipType=TipType.flicker)
        {
            try
            {
                Dictionary<string, object> d = new Dictionary<string, object>();
                d.Add("homeId", DB_ResidenceData.Instance.CurrentRegion.id);
                d.Add("gatewayId", DB_ResidenceData.Instance.HomeGateway.gatewayId);
                Dictionary<string, object> deviceIds = new Dictionary<string, object>();
                deviceIds.Add("deviceId", music.deviceId);
                if (!string.IsNullOrEmpty(source))
                {
                    deviceIds.Add("source", source);
                }
                d.Add("deviceInfos", new List<Dictionary<string, object>> { deviceIds });
                var responsePackNew = RequestServerhomeId(d, NewAPI.Api_Post_GroupList, "获取列表名列表");
                if (!this.DataChecking(responsePackNew, tipType))
                {
                    return new List<GroupList>();
                }
                //数据返序列化为Function对象
                var str = Newtonsoft.Json.JsonConvert.SerializeObject(responsePackNew.Data);
                var groupLists = Newtonsoft.Json.JsonConvert.DeserializeObject<List<GroupList>>(str);
                if (groupLists == null)
                {
                    groupLists = new List<GroupList>();
                }
                return groupLists;
            }
            catch
            {
                return new List<GroupList>();
            }
        }
        /// <summary>
        /// 获取【单个音乐源】列表名列表(默认取定列表中一个)
        /// </summary>
        /// <param name="music"></param>
        /// <param name="source">音乐源</param>
        /// <returns></returns>
        public GroupList GetSingleSourceListNameList(Function music, string source)
        {
            List<GroupList> groupList = this.GetMultipleSourceListName(music, source);
            if (groupList.Count == 0)
            {
                return new GroupList();
            }
            ////默认取定列表中一个
            return groupList.Count > 0 ? groupList[0] : new GroupList();
        }
        /// <summary>
        /// 通过列表名获取【多个歌曲列表】
        /// </summary>
        /// <param name="music"></param>
        /// <param name="listName">列表名</param>
        /// <param name="source">音乐源</param>
        /// <param name="tipType">是否需要提示,默认提示</param>
        /// <returns></returns>
        public List<PalyerSongListInfo> GetMultipleSongListM(Function music, string listName, string source, TipType tipType= TipType.flicker)
        {
            try
            {
                Dictionary<string, object> d = new Dictionary<string, object>();
                Dictionary<string, object> d1 = new Dictionary<string, object>();
                Dictionary<string, object> d2 = new Dictionary<string, object>();
                d.Add("homeId", DB_ResidenceData.Instance.CurrentRegion.id);
                d.Add("gatewayId", DB_ResidenceData.Instance.HomeGateway.gatewayId);
                d2.Add("group", listName);
                d1.Add("sid", music.sid);
                if (!string.IsNullOrEmpty(source))
                {
                    d1.Add("source", source);
                }
                d1.Add("groupList", new List<Dictionary<string, object>> { d2 });
                d.Add("sidGroups", new List<Dictionary<string, object>> { d1 });
                var responsePackNew = RequestServerhomeId(d, NewAPI.Api_Post_GroupPlayerList, "通过列表名获取歌曲列表");
                if (!this.DataChecking(responsePackNew, tipType))
                {
                    return new List<PalyerSongListInfo>();
                }
                //数据返序列化为Function对象
                var str = Newtonsoft.Json.JsonConvert.SerializeObject(responsePackNew.Data);
                var palyLists = Newtonsoft.Json.JsonConvert.DeserializeObject<List<PalyerSongListInfo>>(str);
                if (palyLists == null)
                {
                    return new List<PalyerSongListInfo>();
                }
                return palyLists;
            }
            catch
            {
                return new List<PalyerSongListInfo>();
            }
        }
        /// <summary>
        /// 通过列表名获取【单个歌曲列表】(默认取定列表中一个歌曲列表)
        /// </summary>
        /// <param name="music"></param>
        /// <param name="listName">列表名</param>
        /// <param name="source">音乐源</param>
        /// <returns></returns>
        public SongList GetSingleSongList(Function music, string listName, string source, TipType tipType= TipType.flicker)
        {
            List<PalyerSongListInfo> listInfos = this.GetMultipleSongListM(music, listName, source, tipType);
            if (listInfos.Count == 0)
            {
                return new SongList();
            }
            ////默认第一个列表里面第一个音乐列表
            SongList songList = listInfos[0].playlist.Count > 0 ? listInfos[0].playlist[0] : new SongList();
            return songList;
        }
        /// <summary>
        /// <summary>
        /// 检验数据回复成功或者失败
        /// </summary>
        /// <param name="responsePackNew">回复数据对象</param>
        /// <param name="tipType">是否需要提示</param>
        /// <returns></returns>
        private bool DataChecking(ResponsePackNew responsePackNew, TipType tipType)
        {
            if (responsePackNew.Data == null || responsePackNew.Code != "0" || responsePackNew.Data.ToString() == "")
            {
                if (TipType.flicker == tipType)
                {
                    if (responsePackNew == null)
                    {
                        responsePackNew = new ResponsePackNew { message = "没回复,请确认网络是否正常.", Code = "-1", };
                    }
                    Application.RunOnMainThread(() =>
                    {
                        //new Tip()
                        //{
                        //    CloseTime = 1,
                        //    Text = responsePackNew.message + "(" + responsePackNew.Code + ")",
                        //    Direction = AMPopTipDirection.None,
                        //}.Show(MainPage.BasePageView.GetChildren(MainPage.BasePageView.ChildrenCount - 1));
                        CommonMethod.Current.ShowTip(responsePackNew.message + "(" + responsePackNew.Code + ")", 3);
                    });
                }
                return false;
            }
            return true;
        }
        /// <summary>
        ///请求服务器(与住宅有关:例如;homeId) 
        /// </summary>
        /// <param name="o">发送数据</param>
        /// <param name="api_Url">请求地址(不是绝对地址)</param>
        /// <param name="tag">标记->描述接口(自定义)</param>
        /// <returns></returns>
        public  ResponsePackNew RequestServerhomeId(object o, string api_Url, int mTimeout = 5)
        public ResponsePackNew RequestServerhomeId(object o, string api_Url, string tag, int mTimeout = 10)
        {
            var requestJson = HttpUtil.GetSignRequestJson(o);
            return HttpUtil.RequestHttpsPostFroHome(api_Url, requestJson, mTimeout);
            JObject jobject = JObject.Parse(Newtonsoft.Json.JsonConvert.SerializeObject(o));
            return UI2.Intelligence.Automation.Send.Current.RequestServerhomeId(jobject, api_Url, tag, mTimeout);
        }
    }
}