wjc
2023-02-03 c484347d42f8c14f03f498e689069a14a45abc93
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
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 Newtonsoft.Json.Linq;
using Shared;
 
namespace HDL_ON.UI.Music
{
    public class SendMethod
    {
        private static SendMethod s_Current = null;
        public static SendMethod Current
        {
            get
            {
                if (s_Current == null)
                {
                    s_Current = new SendMethod();
                }
                return s_Current;
            }
 
        }
        /// <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, false, 0);
            })
            { 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 (!this.dataChecking(responsePackNew))
                {
                    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 GetCurrentPalyList(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 (!this.dataChecking(responsePackNew))
                {
                    return;
                }
                //数据返序列化为Function对象
                var str = Newtonsoft.Json.JsonConvert.SerializeObject(responsePackNew.Data);
                var palyLists = Newtonsoft.Json.JsonConvert.DeserializeObject<List<PalyerSongListInfo>>(str);
                if (palyLists == null)
                {
                    palyLists = new List<PalyerSongListInfo>();
                }
                if (palyLists.Count > 0)
                {
                    ///默认拿第一个列表
                    A31MusicModel.Current.CurrentPlayMusicInfoList = palyLists[0].playlist;
                }
            }
            catch
            {
            }
 
        }
        /// <summary>
        /// 获取【多个音乐源】列表名列表
        /// </summary>
        /// <param name="music"></param>
        /// <param name="source">音乐源为null,支持多个源获取</param>
        /// <returns></returns>
        public List<GroupList> GetMultipleSourceListName(Function music, string source)
        {
            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))
                {
                    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>
        /// <returns></returns>
        public List<PalyerSongListInfo> GetMultipleSongListM(Function music, string listName, string source)
        {
            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))
                {
                    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)
        {
            List<PalyerSongListInfo> listInfos = this.GetMultipleSongListM(music, listName, source);
            if (listInfos.Count == 0)
            {
                return new SongList();
            }
            ////默认第一个列表里面第一个音乐列表
            SongList songList = listInfos[0].playlist.Count > 0 ? listInfos[0].playlist[0] : new SongList();
            return songList;
 
        }
        /// <summary>
        /// 检验数据回复成功或者失败
        /// </summary>
        /// <param name="responsePackNew">回复数据对象</param>
        /// <returns></returns>
        private bool dataChecking(ResponsePackNew responsePackNew)
        {
            if (responsePackNew.Code != "0" || responsePackNew.Data == null || responsePackNew.Data.ToString() == "")
            {
                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, string tag, int mTimeout = 5)
        {
            JObject jobject = JObject.Parse(Newtonsoft.Json.JsonConvert.SerializeObject(o));
            return UI2.Intelligence.Automation.Send.Current.RequestServerhomeId(jobject, api_Url, tag, mTimeout);
        }
    }
}