wxr
2022-01-23 838b275cbd329316d45a125a793f4abd7948b49b
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
using System;
using System.Collections.Generic;
using Shared;
using Shared.IO;
using System.Net;
using HDL_ON.Entity;
using HDL_ON.UI.UI2.FuntionControlView.Music;
 
namespace HDL_ON.UI.Music
{
    public class MusicMain : FrameLayout
    {
        private static MusicMain m_MusicMain = null;
        public static MusicMain mMusicMain
        {
            get
            {
                if (m_MusicMain == null)
                {
                    m_MusicMain = new MusicMain();
                }
                return m_MusicMain;
            }
 
        }
        /// <summary>
        /// MusicMain对象构造函数
        /// </summary>
        public MusicMain()
        {
            Tag = "MusicMain";
        }
        /// <summary>
        /// 重写RemoveFromParent方法
        /// </summary>
        public override void RemoveFromParent()
        {
            base.RemoveFromParent();
            clearA31Threads();
            //进来没有音乐被收藏过,退出有音乐被收藏过
            //A31MusicModel.ReadMusicStates();
        }
        /// <summary>
        /// 创建线程列表
        /// </summary>
        static List<System.Threading.Thread> threadLists = new List<System.Threading.Thread>();
        /// <summary>
        /// 移除线程
        /// </summary>
        static void clearA31Threads()
        {
            var threads = threadLists.FindAll((obj) => { return obj.Name == "A31"; });
            foreach (var thread in threads)
            {
                try
                {
                    threadLists.Remove(thread);
                    if (thread.IsAlive)
                    {
                        ///强制线程抛异常
                        thread.Abort();
                    }
                }
                catch (Exception e)
                {
                    MainPage.Log("clearA31Threads error : " + e.Message);
                }
            }
        }
        /// <summary>
        ///  显示加载界面
        /// </summary>
        public static Loading loading = new Loading();
        /// <summary>
        /// 定义全局对象
        /// </summary>
        VerticalRefreshLayout verticalRefresh;
        /// <summary>
        /// 为了音乐刷新状态定义全局对象
        /// </summary>
        MusicView musicView = new MusicView();
        public void Show()
        {
            #region 界面布局
            this.BackgroundColor = MusicColor.ViewColor;
            var topView = new TopView();
            this.AddChidren(topView.TopFLayoutView());
            topView.topNameBtn.TextID = StringId.a31Music;
            topView.clickBackBtn.MouseUpEventHandler += (sender, e) =>
            {
                RemoveFromParent();
            };
 
            verticalRefresh = new VerticalRefreshLayout
            {
                Y = topView.fLayout.Bottom,
                Height = Application.GetRealHeight(H_W.H - H_W.T_Height),
                Name = "verticalRefresh",
            };
            this.AddChidren(verticalRefresh);
            #endregion
 
            verticalRefresh.BeginHeaderRefreshingAction += () =>
            {
               
                ////发送读取音乐播放器状态线程
                SeachMusic();
                verticalRefresh.EndHeaderRefreshing();
            };
            this.AddChidren(loading);
            SeachMusic();
 
        }
        /// <summary>
        /// 刷新播放器列表
        /// </summary>
        void SeachMusic()
        {
 
            Application.RunOnMainThread(() =>
            {
                verticalRefresh.RemoveAll();
                var musicListFunction = FunctionList.List.GetMusicList();
                for (int i = 0; i < musicListFunction.Count; i++)
                {
                    var function = musicListFunction[i];
                    var music = A31MusicModel.A31MusicModelList.Find((obj) =>
                   (obj.functionMusic.deviceId == function.deviceId && function.spk == "music.standard") || (
                     obj.functionMusic.deviceId == function.deviceId && function.spk == "av.music")
                    );
                    if (music == null)
                    {
                        A31MusicModel.A31MusicModelList.Add(new A31MusicModel { functionMusic = function });
                    }
                    else
                    {
                        music.functionMusic = function;
                    }
                }
 
                for (int i = 0; i < A31MusicModel.A31MusicModelList.Count; i++)
                {
 
                    var a31player = A31MusicModel.A31MusicModelList[i];
                    if (a31player.functionMusic.online == false)
                    {
                        //不在线不显示
                        continue;
                    }
                  
                    Application.RunOnMainThread(() =>
                    {
                        MusicListView(a31player);
                    });
                }
                ///进来读一次音乐播放器状态
                for (int i = 0; i < A31MusicModel.A31MusicModelList.Count; i++)
                {
 
                    var a31player = A31MusicModel.A31MusicModelList[i];
                    if (a31player.functionMusic.online == false)
                    {
                        //不在线不读取
                        continue;
                    }
                    //发送读取音乐播放器状态线程
                    System.Threading.Tasks.Task.Run(() =>
                    {
                        try
                        {
                            SendMethod.mMethod.RefreshDeviceStatus(new List<string> { a31player.functionMusic.deviceId });
                            System.Threading.Thread.Sleep(500);
                        }
                        catch { }
 
                    });
                }
 
            });
 
        }
        /// <summary>
        /// 显示音乐列表的方法
        /// </summary>
        void MusicListView(A31MusicModel player)
        {
            musicView.ViewAddChidren(verticalRefresh);
            musicView.muiscFl.Tag = player.functionMusic;//多个音乐播放器更新状态要用到
            musicView.singerBtn.Text = player.functionMusic.GetAttrState(KeyProperty.song_name);
            musicView.songNameBtn.Text = player.functionMusic.GetAttrState(KeyProperty.song_name);
            musicView.musicNameBtn.Text = player.functionMusic.name;
 
            ///收藏事件
            musicView.collectIconBtn.MouseUpEventHandler += (sender, e) =>
            {
                musicView.collectIconBtn.IsSelected = !musicView.collectIconBtn.IsSelected;
                if (musicView.collectIconBtn.IsSelected)
                {
                    player.functionMusic.collect = true;
                }
                else
                {
                    player.functionMusic.collect = false;
                }
            };
 
 
            ///进入音乐主页事件
            EventHandler<MouseEventArgs> clickPlayView = (sender, e) =>
            {
 
                A31MusicModel.Current = player;
                var a31PlayMusicPage = new A31PlayMusicPage();
                MainPage.BasePageView.AddChidren(a31PlayMusicPage);
                a31PlayMusicPage.Show();
                MainPage.BasePageView.PageIndex = MainPage.BasePageView.ChildrenCount - 1;
            };
            musicView.musicIfonFl.MouseUpEventHandler += clickPlayView;
            musicView.clickBtn.MouseUpEventHandler += clickPlayView;//
            musicView.singerBtn.MouseUpEventHandler += clickPlayView;
            musicView.songNameBtn.MouseUpEventHandler += clickPlayView;//
            musicView.musicNameBtn.MouseUpEventHandler += clickPlayView;
            musicView.regionBtn.MouseUpEventHandler += clickPlayView;//
 
            ///上一曲点击事件
            musicView.prevBtn.MouseDownEventHandler += (sender, e) =>
            {
                musicView.prevBtn.IsSelected = true;
                player.functionMusic.SetAttrState(KeyProperty.song_step, ValueProperty.up);
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic.Add(KeyProperty.song_step, ValueProperty.up);
                SendMethod.mMethod.SendControlCommand(player.functionMusic, dic);
            };
            musicView.prevBtn.MouseUpEventHandler += (sender, e) =>
            {
                musicView.prevBtn.IsSelected = false;
            };
            ///暂停/播放点击事件
            musicView.playBtn.MouseDownEventHandler += (sender, e) =>
            {
                string status = ValueProperty.off;
                if (musicView.playBtn.IsSelected)
                {
                    musicView.playBtn.IsSelected = false;
                    status = ValueProperty.off;
                }
                else
                {
                    musicView.playBtn.IsSelected = true;
                    status = ValueProperty.on;
                }
                player.functionMusic.SetAttrState(KeyProperty.on_off, status);
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic.Add(KeyProperty.on_off, status);
                SendMethod.mMethod.SendControlCommand(player.functionMusic, dic);
            };
            ///下一曲点击事件
            musicView.nextBtn.MouseDownEventHandler += (sender, e) =>
            {
                musicView.nextBtn.IsSelected = true;
                player.functionMusic.SetAttrState(KeyProperty.song_step, ValueProperty.down);
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic.Add(KeyProperty.song_step, ValueProperty.down);
                SendMethod.mMethod.SendControlCommand(player.functionMusic, dic);
            };
            musicView.nextBtn.MouseUpEventHandler += (sender, e) =>
            {
                musicView.nextBtn.IsSelected = false;
            };
            //更新状态线程
            var musicThread = new System.Threading.Thread(() =>
            {
                while (true)
                {
                    if (!player.functionMusic.online)
                    {
                        ///不在线不读状态
                        continue;
                    }
                    //SendMethod.ReadStatus(player);
                    SendMethod.mMethod.GetDeviceStatus(ref player, new List<string> { player.functionMusic.deviceId }, player.functionMusic.sid);
                    System.Threading.Thread.Sleep(1000);
                    Application.RunOnMainThread(() =>
                    {
                        musicView.singerBtn.Text = player.functionMusic.GetAttrState(KeyProperty.song_name);
                        musicView.songNameBtn.Text = player.functionMusic.GetAttrState(KeyProperty.song_name);
                        musicView.musicNameBtn.Text = player.functionMusic.name;
                        if (player.functionMusic.GetAttrState(KeyProperty.on_off) == ValueProperty.on)
                        {
                            musicView.playBtn.IsSelected = true;
                        }
                        else
                        {
                            musicView.playBtn.IsSelected = false;
                        }
                        musicView.regionBtn.Text = player.functionMusic.GetRoomListName();
 
                        if (player.functionMusic.collect)
                        {
                            musicView.collectIconBtn.IsSelected = true;
                        }
                        else
                        {
                            musicView.collectIconBtn.IsSelected = false;
                        }
                    });
                }
            })
            { IsBackground = true, Name = "A31" };
            musicThread.Start();
            threadLists.Add(musicThread);
        }
 
        /// <summary>
        ///指定刷新界面
        /// </summary>
        /// <param name="strView">判断字符</param>
        public void RefreshView(AlinkStatusData alinkStatusData)
        {
            if (alinkStatusData == null || alinkStatusData.status.Count == 0)
            {
                return;
            }
            Application.RunOnMainThread(() =>
            {
                try
                {
                    for (int a =0; a < MainPage.BasePageView.ChildrenCount; a++)
                    {
                        var view = MainPage.BasePageView.GetChildren(a);
                        if (view.GetType() == typeof(MusicMain))
                        {
                            var musicMain = view as MusicMain;
                            if (musicMain != null)
                            {
                                for (int b = 0; b < musicMain.ChildrenCount; b++)
                                {
                                    var view1 = musicMain.GetChildren(b);
                                    if (view1.GetType() == typeof(VerticalRefreshLayout))
                                    {
                                        var vv = view1 as VerticalRefreshLayout;
                                        if (vv != null && vv.Name == "verticalRefresh")
                                        {
                                            for (int c = 0; c < vv.ChildrenCount; c++)
                                            {
                                                var viewfl = vv.GetChildren(c); 
                                                if (viewfl.GetType() == typeof(FrameLayout))
                                                {
                                                    var fl = viewfl as FrameLayout;
                                                    if ((fl.Tag as Function).sid!= alinkStatusData.sid) {
                                                        //不是当前音乐不会更新状态
                                                        continue;
                                                    }
                                                    if (fl != null && fl.Name == "parentfl")
                                                    {
                                                        for (int i = 0; i < fl.ChildrenCount; i++)
                                                        {
                                                            var viewfl1 = fl.GetChildren(i); 
                                                            if (viewfl1.GetType() == typeof(FrameLayout))
                                                            {
                                                                var fl1 = viewfl1 as FrameLayout;
                                                                if (fl1 != null && fl1.Name == "musicparentfl")
                                                                {
                                                                    for (int j = 0; j < fl1.ChildrenCount; j++)
                                                                    {
 
                                                                        if (fl1.GetChildren(j).GetType() == typeof(Button))
                                                                        {
                                                                            var btn = fl1.GetChildren(j) as Button;
                                                                            if (btn == null ||btn.Name==null)
                                                                            {
                                                                                continue;
                                                                            }
                                                                            switch (btn.Name)
                                                                            {
                                                                                case "song":
                                                                                    {
                                                                                        var s = alinkStatusData.status.Find((o) => o.key == KeyProperty.song_name);
                                                                                        if (s != null)
                                                                                        {
                                                                                            btn.Text = s.value;
                                                                                        }
                                                                                    }
                                                                                    break;
                                                                                case "playstatus":
                                                                                    {
 
                                                                                        var s = alinkStatusData.status.Find((o) => o.key == KeyProperty.on_off);
                                                                                        if (s != null)
                                                                                        {
                                                                                            if (s.value == ValueProperty.on)
                                                                                            {
                                                                                                btn.IsSelected = true;
                                                                                            }
                                                                                            else
                                                                                            {
                                                                                                btn.IsSelected = false;
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                    break;
                                                                            }
 
                                                                        }
                                                                    }
                                                                }
                                                            }
 
                                                        }
                                                    }
                                                }
 
                                            }
                                        }
 
                                    }
 
                                }
                            }
 
                        }
                    }
 
                }
                catch { }
            });
        }
    }
 
}