wjc
2022-01-18 e7c4740cb2e4dec599a99c8823af5839acef084c
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
using System;
using System.Collections.Generic;
using System.Net;
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 sMethod=null;    
        public static SendMethod mMethod
        {
            get
            {
                if (sMethod == null)
                {
                    sMethod = new SendMethod();
                }
                return sMethod;
            }
 
        }
        /// <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 GetDeviceStatus(ref A31MusicModel a31Music, List<string> functionIds, string sid)
        {
            try
            {
                //RefreshDeviceStatus(functionIds);
                a31Music.LastDateTime = DateTime.Now;
                ///从缓存里面查找音乐播放器对象<缓存数据收到推送过来的状态会更新缓存数据>
                var allLocalFuntion = FunctionList.List.GetDeviceFunctionList();
                var localFunction = allLocalFuntion.Find((obj) => obj.sid == sid);
                if (localFunction == null)
                {
                    return;
                }
                ///更新的数据
                a31Music.functionMusic = localFunction;
 
            }
            catch { }
        }
 
        /// <summary>
        /// 刷新设备状态
        /// </summary>
        /// <returns></returns>
        public void RefreshDeviceStatus(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" || responsePackNew.Data == null || responsePackNew.Data.ToString() == "")
                {
                    return;
                }
            }
            catch { }
        }
 
 
        /// <summary>
        /// 设备名称修改
        /// </summary>
        public string EditDeviceName(string deviceId, string deviceName)
        {
            Dictionary<string, object> d = new Dictionary<string, object>();
            d.Add("homeId", DB_ResidenceData.Instance.CurrentRegion.id);
            d.Add("deviceId", deviceId);
            d.Add("name", deviceName);
 
            var requestJson = HttpUtil.GetSignRequestJson(d);
            return HttpUtil.RequestHttpsPostFroHome(NewAPI.Api_Post_EditDeviceName, requestJson).Code;
        }
 
        /// <summary>
        /// 获取音乐列表
        /// </summary>
        /// <param name="music"></param>
        /// <returns></returns>
        public void GetPalyList(Function music)
        {
            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() == "")
                {
                    return;
                }
                //数据返序列化为Function对象
                var str = Newtonsoft.Json.JsonConvert.SerializeObject(responsePackNew.Data);
                var palyLists = Newtonsoft.Json.JsonConvert.DeserializeObject<List<PalyListInfo>>(str);
                if (palyLists == null)
                {
                    palyLists =new List<PalyListInfo>();
                }
                if (palyLists.Count > 0)
                {
                    ///默认拿第一个列表
                    A31MusicModel.Current.palyLists = palyLists[0].playlist;
                }
            }
            catch
            {
            }
 
        }
       
        /// <summary>
        ///请求服务器(与住宅有关:例如;homeId) 
        /// </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);
 
        }
    }
}