lss
2020-06-10 d0396d3f30d46810f8453bf21f86bd93fe5dbdec
ZigbeeApp/Shared/Phone/UserCenter/SmartSound/Forms/SmartSoundControlForm.cs
@@ -1,216 +1,385 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Shared.Common;
using Shared.Phone.UserCenter.SmartSound.Forms;
using Shared.Phone.UserCenter.SmartSound.Util;
using Shared.Phone.UserCenter.SmartSound.Widget;
using ZigBee.Device;
namespace Shared.Phone.UserCenter.SmartSound
{
    public class SmartSoundControlForm : EditorCommonForm
    {
        private SmartSound mSmartSound;
        public SmartSoundControlForm()
        /// <summary>
        /// 列表控件
        /// </summary>
        private VerticalListControl listView = null;
        private FrameLayout contentLayout = null;
        public int CurrentIndex = 0;
        private MostRightIconControl btnAddDeviceIcon = null;
        /// <summary>
        ///  智能音箱数据
        /// </summary>
        private static SmartSoundInfo.SoundInfo mSoundInfo = null;
        public SmartSoundControlForm(SmartSoundInfo.SoundInfo soundInfo)
        {
            mSoundInfo = soundInfo;
        }
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        public void ShowForm()
        {
            // iniData();//先加载模拟数据
            this.ScrollEnabled = false;
            //设置标题信息
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        public void ShowForm()
        {
            //设置标题信息
            base.SetTitleText("语音控制");
            //右上添加按钮
            var btnAddDeviceIcon = new MostRightIconControl(69, 69);
            btnAddDeviceIcon.UnSelectedImagePath = "Item/Add.png";
            topFrameLayout.AddChidren(btnAddDeviceIcon);
            btnAddDeviceIcon.InitControl();
            btnAddDeviceIcon.ButtonClickEvent += (sender, e) =>
            {
                iniView();
            };
            if (btnAddDeviceIcon == null)
            {
                btnAddDeviceIcon = new MostRightIconControl(69, 69);
                btnAddDeviceIcon.UnSelectedImagePath = "Item/Add.png";
                topFrameLayout.AddChidren(btnAddDeviceIcon);
                btnAddDeviceIcon.InitControl();
                btnAddDeviceIcon.ButtonClickEvent += (sender, e) =>
                {
                    var smartSoundDataAdd = new SmartSoundControlContentForm(mSoundInfo);
                    smartSoundDataAdd.AddForm();
                };
            }
            //初始化中部控件
            //this.InitMiddleFrame();
            this.InitMiddleFrame();
        }
        private void InitMiddleFrame()
        {
            this.ClearBodyFrame();
            HdlThreadLogic.Current.RunThread(async () =>
            {
                this.ShowProgressBar();
                string json = await GetData();//获取数据
                SmartSoundDataInfo dataInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<SmartSoundDataInfo>(json);
                if (dataInfo != null)
                {
                    if (dataInfo.StateCode == "SUCCESS")
                    {
                        SmartSound.getInstantiate().LayerList = dataInfo.ResponseData.LayerList;
                        SmartSound.getInstantiate().TokenID = mSoundInfo.Id;
                        SmartSound.getInstantiate().UserID = mSoundInfo.UserID;
                    }
                }
                HdlThreadLogic.Current.RunMainInThread(() =>
                {
                    if (SmartSound.getInstantiate().LayerList == null || SmartSound.getInstantiate().LayerList.Count < 1)
                    {
                        this.CloseProgressBar();
                        return;
                    }
                    contentLayout = new FrameLayout();
                    this.bodyFrameLayout.AddChidren(contentLayout);
                    contentLayout.RemoveAll();
                    var pullLayout = new MyPullControl(this, SmartSound.getInstantiate().LayerList);
                    contentLayout.AddChidren(pullLayout);
                    pullLayout.InitControl();
                    LoadAllRoomListView();
                    //无数据的时候要做一个提示
                    {
                        bool bol = false;
                        for (int i = 0; i < SmartSound.getInstantiate().LayerList.Count; i++)
                        {
                            if (SmartSound.getInstantiate().LayerList[i].RoomList.Count > 0)
                            {
                                bol = true;
                                break;
                            }
                        }
                        if (!bol)
                        {
                            pullLayout.Visible = false;
                            this.ShowNotDataImage(this.bodyFrameLayout, new string[] { "还没有控制内容哦,", "点击\"+\"添加" });
                        }
                    }
                    this.CloseProgressBar();
                });
            });
        }
        /// <summary>
        /// 将数据转换为智能音箱使用的数据
        /// 加载房间列表
        /// </summary>
        private void iniData(bool isAdd)
        private void LoadAllRoomListView()
        {
            //1.从服务器获取的数据,json反序列化得来
            //2.点击"+"将网关的数据转换为 智能音箱使用的数据
            /*{
                Dictionary<string, string> ss = Config.Instance.Home.FloorDics;//楼层列表
                string currentFloor = Config.Instance.Home.CurrentFloorId;//当前楼层的键
                var listRoom = HdlRoomLogic.Current.GetFloorSortRoom("楼层的主键");//获取当前楼层的所有房间
                var room = listRoom[0];
                for (int i=0;i<room.ListDevice.Count;i++)
                {//设备数据
                    var device = LocalDevice.Current.GetDevice(room.ListDevice[i]);
                    var myType = LocalDevice.Current.GetNotHdlMyDeviceEnumInfo(new List<CommonDevice>() { device });
                    if (myType.BeloneType == DeviceBeloneType.A灯光)
                    {
                        //这里面的就是灯光了
                        var light = device;
                        // light.DeviceAddr;  设备的地址
                        // light.DeviceEpoint;//设备的端口
                    }
                }
                for (int i = 0; i < room.ListSceneId.Count; i++)
                {   //场景数据
                    var device = HdlSceneLogic.Current.GetSceneUIBySceneId(room.ListSceneId[i]);
                    // device.Id;场景 id
                }
            }*/
            if (isAdd)
            try
            {
                mSmartSound = new SmartSound();
                mSmartSound.HomeID = Config.Instance.Home.Id;
                mSmartSound.UserID = Config.Instance.Guid;
                Dictionary<string, string> floorDictionary = Config.Instance.Home.FloorDics;//楼层列表
                if (floorDictionary.Count < 1)
                if (SmartSound.getInstantiate().LayerList == null || SmartSound.getInstantiate().LayerList.Count < 1)
                    return;
                // 加载当前楼层的所有房间 ListView
                if (listView == null)
                {
                    //没有楼层,模拟一个
                    SmartSound.Layer layer = new SmartSound.Layer();
                    layer.LayerID = "0000";
                    layer.LayerName = "一楼";
                    layer.RoomList.Clear();
                    listView = new VerticalListControl();
                    contentLayout.AddChidren(listView);
                    listView.Y = Application.GetRealHeight(173);
                    listView.Height = bodyFrameLayout.Height - Application.GetRealHeight(173);
                    //添加房间
                    SmartSound.Room smartRoom = null;
                    var roomList = HdlRoomLogic.Current.GetAllListRooms();
                    for (int i = 0; i < roomList.Count; i++)
                }
                else if (listView.Parent == null)
                {
                    listView = new VerticalListControl();
                    contentLayout.AddChidren(listView);
                    listView.Y = Application.GetRealHeight(173);
                    listView.Height = bodyFrameLayout.Height - Application.GetRealHeight(173);
                }
                listView.RemoveAll();
                SmartSound.Layer layer = SmartSound.getInstantiate().LayerList[CurrentIndex];
                foreach (SmartSound.Room room in layer.RoomList)
                {
                    var roomRowLayout = new RoomRowLayout(this, room);
                    listView.AddChidren(roomRowLayout);
                    roomRowLayout.InitControl();
                    roomRowLayout.frameTable.ButtonClickEvent += (sender, e) =>
                    {
                        Room room = roomList[i];
                        smartRoom = new SmartSound.Room();
                        layer.RoomList.Add(smartRoom);
                        // 点击房间,进入选择设备界面
                        var deviceChange = new SmartSoundContentForDeviceChange(room);
                        deviceChange.AddForm();
                    };
                        smartRoom.RoomID = room.Id;
                        smartRoom.RoomName = room.Name;
                        smartRoom.DeviceList.Clear();
                        smartRoom.SceneList.Clear();
                    roomRowLayout.btnDelect.ButtonClickEvent += (sender, e) =>
                    {
                        var delBtn = sender as NormalViewControl;
                        DelectRoom(delBtn);
                    };
                }
            }
            catch (Exception e)
            {
                string error = e.Message;
            }
        }
                        //添加设备  // 灯光、窗帘、场景、空调
                        for (int j = 0; j < room.ListDevice.Count; j++)
        private void DelectRoom(NormalViewControl delBtn)
        {
            this.ShowMassage(ShowMsgType.Confirm, "确认删除该房间的控制内容?", async () =>
            {
                HdlThreadLogic.Current.RunThread(async () =>
                {
                    this.ShowProgressBar("正在删除数据....");
                    //删除房间
                    var tag = (SmartSound.Room)delBtn.GetTagByKey("obj");
                    if (SmartSound.getInstantiate().LayerList == null)
                        return;
                    for (int i = 0; i < SmartSound.getInstantiate().LayerList.Count; i++)
                    {
                        SmartSound.Layer _layer = SmartSound.getInstantiate().LayerList[i];
                        for (int j = 0; j < _layer.RoomList.Count; j++)
                        {
                            string key = room.ListDevice[j].ToString();
                            var device = LocalDevice.Current.GetDevice(key);
                            SmartSound.Room _room = _layer.RoomList[j];
                            SmartSound.Device smartDevice = new SmartSound.Device();
                            smartDevice.Id = device.DeviceID.ToString();//设备 Id
                            smartDevice.DeviceAddress = device.DeviceAddr;//设备 MAC
                            smartDevice.Epoint = device.DeviceEpoint;//设备端口
                            smartDevice.ClusterID = 0;
                            smartDevice.DeviceName = device.DeviceName;//设备名称
                            smartDevice.NicksName = "";
                            smartDevice.DeviceType = getDeviceType(device);//设备类型
                            smartDevice.GatewayID = device.CurrentGateWayId;//网关 Id
                            smartDevice.RoomID = room.Id;//房间 Id
                            smartRoom.DeviceList.Add(smartDevice);
                        }
                        var sceneList = HdlSceneLogic.Current.GetRoomSceneList(room);
                        //添加场景
                        for (int j = 0; j < sceneList.Count; j++)
                        {
                            SceneUI scene = sceneList[j];
                            SmartSound.Scene smartScene = new SmartSound.Scene();
                            smartScene.Id = scene.DeviceID.ToString();
                            smartScene.SceneName = scene.Name;
                            smartScene.SceneID = scene.Id;
                            smartScene.DelayTime = scene.DelayTime;
                            smartScene.ClusterID = 0;
                            smartScene.NicksName = "";
                            smartScene.RoomID = room.Id;
                            smartRoom.SceneList.Add(smartScene);
                            if (_room.RoomName == tag.RoomName)
                            {
                                _layer.RoomList.Remove(_room);
                            }
                        }
                    }
                }
            }
                    // 重新上传数据
                    string json = await postBatchEdit();
                    HdlThreadLogic.Current.RunMainInThread(() =>
                    {
                        //this.InitMiddleFrame();
                        this.LoadAllRoomListView();
                    });
                    this.CloseProgressBar();
                });
            }, Language.StringByID(R.MyInternationalizationString.OkMsg));
        }
        /// <summary>
        /// Zigbee设备类型转换为智能音箱使用的类型
        /// </summary>
        /// <returns></returns>
        private int getDeviceType(ZigBee.Device.CommonDevice device)
        private async Task<string> GetData()
        {
            int type = 0;//1=开关灯,2=调光灯,3=RGB 灯,4=窗帘模块,5=开合帘,6=卷帘,7=空调,8=面板,9=新风
            if (device.Type == DeviceType.OnOffOutput && device.DfunctionType == DeviceFunctionType.A灯光)
            {
                type = 1;//这里面的就是继电器灯光了
            }
            else if (device.Type == DeviceType.DimmableLight)
            {
                type = 2;//调光
            }
            else if (device.Type == DeviceType.ColorDimmableLight)
            {
                type = 3;
            }
            else if (device.Type == DeviceType.WindowCoveringDevice)
            {
                //窗帘
                if (((Rollershade)device).WcdType == 4)//开合帘
                {
                    type = 5;
                }
                else if (((Rollershade)device).WcdType == 0)//卷帘
                    type = 6;//卷帘
            }
            else if (device.Type == DeviceType.Thermostat)
            {
                type = 7;
            }
            else if (device.Type == DeviceType.FreshAir)
            {
                type = 9;
            }
            return type;
            // 从服务器获取数据
            string url = string.Format("https://developer.hdlcontrol.com/zigbeespeakerservice/BatchEdit?Platform&UserID={0}&TokenID={1}", mSoundInfo.UserID, mSoundInfo.Id);
            return await MyHttpWebResponse.GetWebRequest(url);
        }
        private void iniView()
        private async Task<string> postBatchEdit()
        {
            //房间列表行{房间列表 textView,楼层下拉窗}
            //显示房间{带选择框的;当选中了房间后需要在底部弹出确认按钮,点击确认跳转到新的界面}
            var frame = new TopRightMenuControl(2, 2);
            //创建楼层
            frame.AddRowMenu(Language.StringByID(R.MyInternationalizationString.uCreatFloor), "Item/CreatFloor.png", "Item/CreatFloorSelected.png", () =>
            {
                //创建楼层
                //this.CreatOrEditorFloor(null, null, string.Empty, string.Empty);
            });
            //创建房间
            frame.AddRowMenu(Language.StringByID(R.MyInternationalizationString.uCreatRoom), "Item/RoomIcon.png", "Item/RoomIconSelected.png", () =>
            {
                /*var form = new AddNewRoomForm();
                form.AddForm(string.Empty);
                form.FinishEditorEvent += () =>
                {
                    //根据指定模式,初始化桌布控件
                    this.InitFrameTableByMode();
                };*/
            });
            // 提交数据至服务器
            string url = "https://developer.hdlcontrol.com/zigbeespeakerservice/BatchEdit";
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(SmartSound.getInstantiate());
            string response_str = await MyHttpWebResponse.PostWebRequest(url, json, Encoding.UTF8);
            return response_str;
        }
        public override int FormActionAgainEvent()
        {
            ShowForm();
            return base.FormActionAgainEvent();
        }
        #region ■ 自定义楼层选择控件_____________________
        /// <summary>
        /// 房间列表行
        /// </summary>
        private class MyPullControl : FrameRowControl
        {
            private SmartSoundControlForm smartSoundControlData = null;
            private List<SmartSound.Layer> Layers = null;
            /// <summary>
            /// 房间列表
            /// </summary>
            private NormalViewControl btnName = null;
            /// <summary>
            /// 房间列表行
            /// </summary>
            public MyPullControl(SmartSoundControlForm _SmartSoundDataAdd, List<SmartSound.Layer> _layers)
            {
                this.smartSoundControlData = _SmartSoundDataAdd;
                this.Layers = _layers;
                this.UseClickStatu = false;
                this.BackgroundColor = UserCenterColor.Current.White;
                this.Height = Application.GetRealHeight(173);
            }
            /// <summary>
            /// 初始化控件
            /// </summary>
            public void InitControl()
            {
                try
                {
                    //显示文本
                    btnName = this.AddLeftCaption(string.Empty, 700);
                    btnName.Height = Application.GetRealHeight(60);
                    btnName.TextSize = 17;
                    btnName.Y = Application.GetRealHeight(57);
                    btnName.Text = "房间列表";
                    var right_icon = this.AddRightArrow();
                    right_icon.SelectedImagePath = "SmartSound/PullDown.png";
                    right_icon.UnSelectedImagePath = "SmartSound/PullDown.png";
                    right_icon.ButtonClickEvent += (sender, e) =>
                    {
                        ShowPullList();
                    };
                    var layout = this.AddMostRightView(Layers[smartSoundControlData.CurrentIndex].LayerName, 300, false);
                    layout.Name = "pullLayout";
                    layout.ButtonClickEvent += (sender, e) =>
                    {
                        ShowPullList();
                    };
                    if (SmartSound.getInstantiate().LayerList.Count < 2)
                    {
                        right_icon.Visible = false;
                        layout.Visible = false;
                    }
                }
                catch(Exception e)
                {
                    string error = e.Message;
                }
            }
            private void ShowPullList()
            {
                //房间列表行{房间列表 textView,楼层下拉窗}
                //显示房间{带选择框的;当选中了房间后需要在底部弹出确认按钮,点击确认跳转到新的界面}
                var pull_frame = new TopRightMenuControl(Layers.Count, 2);
                pull_frame.Y = this.Height;
                for (int i = 0; i < Layers.Count; i++)
                {
                    //创建楼层
                    pull_frame.AddRowMenu(Layers[i].LayerName, "Item/CreatFloor.png", "Item/CreatFloorSelected.png", () =>
                    {
                        smartSoundControlData.CurrentIndex = i;
                        smartSoundControlData.LoadAllRoomListView();
                    });
                }
            }
        }
        #endregion
        #region ■ 自定义智能音箱控件_____________________
        /// <summary>
        /// 语音控制房间行
        /// </summary>
        private class RoomRowLayout : RowLayoutControl
        {
            /// <summary>
            /// 房间名字
            /// </summary>
            private NormalViewControl btnName = null;
            /// <summary>
            /// 删除按钮
            /// </summary>
            public NormalViewControl btnDelect = null;
            private SmartSoundControlForm smartSoundControlForm;
            private SmartSound.Room room;
            /// <summary>
            /// 自定音箱关控件
            /// </summary>
            public RoomRowLayout(SmartSoundControlForm _smartSoundControlForm, SmartSound.Room _room)
            {
                this.smartSoundControlForm = _smartSoundControlForm;
                this.room = _room;
                this.BackgroundColor = UserCenterColor.Current.White;
                this.Height = Application.GetRealHeight(173);
            }
            /// <summary>
            /// 初始化内部控件
            /// </summary>
            public void InitControl()
            {
                //设备
                frameTable.AddLeftCaption(this.room.RoomName, 800);
                frameTable.AddRightArrow();
                //底线
                frameTable.AddBottomLine();
                btnDelect = base.AddDeleteControl();
                btnDelect.Text = "删除";
                btnDelect.AddTag("obj", room);
            }
        }
        #endregion
    }
}
}