tzy
2021-03-23 132ef8524e38d06da8cf74292a3ac1eff065bb1b
HDL_ON/UI/UI2/4-PersonalCenter/PirDevice/Method.cs
@@ -9,8 +9,68 @@
    public class Method
    {
        /// <summary>
        /// 程序主入口
        /// </summary>
        /// <param name="frame"></param>
        /// <param name="function"></param>
        public void MainView(FrameLayout frame, Entity.Function function,Action action)
        {
            Pir pirDevice = new Pir();
            if (function != null)
            {//数据转换
                pirDevice.name = function.name;
                pirDevice.deviceId = function.deviceId;
                pirDevice.sid = function.sid;
                pirDevice.online = function.online;
            }
            Pir.currPir = pirDevice;
            GetControlList(frame, () =>
            {
                Application.RunOnMainThread(() =>
                {
                    var page = new PirMain();
                    MainPage.BasePageView.AddChidren(page);
                    page.Show();
                    MainPage.BasePageView.PageIndex = MainPage.BasePageView.ChildrenCount - 1;
                    page.BackAction += () => {
                        action?.Invoke();
                    };
                });
            }, Pir.currPir);
        }
        /// <summary>
        /// 管理位置
        /// </summary>
        /// <param name="control">当前设备</param>
        /// <param name="action">回调函数</param>
        public void ManagementPosition(Entity.Function control, Action action)
        {
            var view = new ChooseRoomPage(control, action);
            MainPage.BasePageView.AddChidren(view);
            view.LoadPage();
            MainPage.BasePageView.PageIndex = MainPage.BasePageView.ChildrenCount - 1;
        }
        /// <summary>
        /// 修改名称
        /// </summary>
        /// <param name="tipText">提示标题文本</param>
        /// <param name="list">当前存在名称列表</param>
        /// <param name="currName">当前名称</param>
        /// <param name="action">回调函数</param>
        public void EditControlName(int tipText, List<string> list, string currName, Action<string, Dialog> action, Action actionCancel, bool tag = false)
        {
            new View.TipView().InputBox(tipText, currName, StringId.nameNull, StringId.NameAlreadyExists, list, (text, view
                ) =>
            {
                action(text, view);
            }, () => { actionCancel(); }, tag);
        }
        /// <summary>
        /// 添加遥控器的方法
        /// </summary>
        /// <param name="frameLayout">log图标加载界面</param>
        /// <param name="action">回调函数</param>
        public void AddControl(FrameLayout frameLayout, Action<Control> action)
        {
            View.TipView tipView = new View.TipView();
@@ -36,11 +96,12 @@
        /// <summary>
        /// 发送遥控器命令方法
        /// </summary>
        /// <param name="control"></param>
        /// <param name="frame"></param>
        /// <param name="control">发送参数对象</param>
        /// <param name="frame">log图标加载界面</param>
        /// <param name="action">回调函数</param>
        public void ThreadAddControl(Control control, FrameLayout frame, Action<Control> action)
        {
            DAL.Server.ResponsePackNew responsePackNew = null;
            Cloud mqttdate = null;
            Loading loading = new Loading();
            frame.AddChidren(loading);
            loading.Start();
@@ -48,7 +109,19 @@
            {
                try
                {
                    responsePackNew = PirSend.Add(control);
                    //发送添加命令
                    var responsePackNew = PirSend.Add(control);
                    if (responsePackNew != null && responsePackNew.Code == "0" && responsePackNew.Data.ToString() != "")
                    {
                        string sid = responsePackNew.Data.ToString();
                        controldata = "";
                        mqttdate = MqttDate("遥控器", sid);
                        if (mqttdate != null)
                        {
                            control.sid = sid;
                        }
                    }
                }
                catch { }
                finally
@@ -56,52 +129,58 @@
                    Application.RunOnMainThread(() =>
                    {
                        loading.Hide();
                        if (responsePackNew != null && responsePackNew.Code == "0" && responsePackNew.Data.ToString() != "")
                        ///这里:监听MTTP推送下来主题;
                        if (mqttdate != null)
                        {
                            string sid = responsePackNew.Data.ToString();
                            var mqttdate = MqttDate(sid);
                            if (mqttdate != null)
                            //休眠500毫秒,为等待云端创建deviceid;
                            System.Threading.Thread.Sleep(500);
                            //读取添加遥控器deviceID,才知道是否添加成功;
                            GetControl(frame, control, (device) =>
                            {
                                ///这里:监听MTTP推送下来主题,才知道是否添加成功
                                control.sid = sid;
                                //休眠500毫秒,为等待云端创建deviceid;
                                System.Threading.Thread.Sleep(500);
                                GetControl(frame, control,(device)=> {
                                    if (device != null)
                                    {
                                        control.deviceId = device.deviceId;
                                        frame.RemoveFromParent();//添加成功关闭弹窗
                                        action(control);
                                    }
                                });
                            }
                            else
                            {
                                //监听Mqtt推送下来状态码做提示
                                View.FailView failView = new View.FailView();
                                failView.ShouError((view) =>
                                if (device != null)
                                {
                                    view.Close();
                                    ThreadAddControl(control, frame, action);
                                });
                            }
                                    control.deviceId = device.deviceId;
                                    frame.RemoveFromParent();//添加成功关闭弹窗
                                    action(control);
                                }
                                else
                                {
                                    //监听Mqtt推送下来状态码做提示
                                    View.FailView failView = new View.FailView();
                                    failView.ShouError((view) =>
                                    {
                                        view.Close();
                                        ThreadAddControl(control, frame, action);
                                    });
                                }
                            });
                        }
                        else
                        {
                            ErrorShow(responsePackNew);
                            //监听Mqtt推送下来状态码做提示
                            View.FailView failView = new View.FailView();
                            failView.ShouError((view) =>
                            {
                                view.Close();
                                ThreadAddControl(control, frame, action);
                            });
                        }
                    });
                }
            })
            { IsBackground = true }.Start();
        }
        /// <summary>
        /// 获取红外设备列表
        /// </summary>
        /// <param name="frame">log图标加载界面</param>
        /// <param name="action">回调函数</param>
        public static void GetPirDeviceList(FrameLayout frame, Action action)
        {
            //清除之前列表;
@@ -109,56 +188,77 @@
            //加载log
            Loading loading = new Loading();
            frame.AddChidren(loading);
            HDL_ON.DAL.Server.ResponsePackNew responsePackNew = null;
            loading.Start();
            new System.Threading.Thread(() =>
            {
                try
                {
                    responsePackNew = PirSend.GetDeviceList("ir.module");
                    var responsePackNew = PirSend.GetDeviceList("ir.module");
                    if (responsePackNew != null && responsePackNew.Code == "0" && responsePackNew.Data.ToString() != "")
                    {
                        var jobject = Newtonsoft.Json.Linq.JObject.Parse(responsePackNew.Data.ToString());
                        string list = jobject["list"].ToString();
                        var jArray = Newtonsoft.Json.Linq.JArray.Parse(list);
                        for (int a = 0; a < jArray.Count; a++)
                        {
                            var jay = jArray[a];
                            string spk = jay["spk"].ToString();
                            if (spk == "ir.module")
                            {
                                //数据返序列化为Logic对象
                                var str = Newtonsoft.Json.JsonConvert.SerializeObject(jay);
                                var pirJosn = Newtonsoft.Json.JsonConvert.DeserializeObject<Pir>(str);
                                if (pirJosn != null)
                                {
/* 项目“HDL-ON_iOS”的未合并的更改
在此之前:
                                    if (null == Pir.pirDeviceList.Find((c) => c.deviceId == pirJosn.deviceId))
在此之后:
                                    if (null == list.pirDeviceList.Find((c) => c.deviceId == pirJosn.deviceId))
*/
                                    if (null == PirDevice.Pir.pirDeviceList.Find((c) => c.deviceId == pirJosn.deviceId))
                                    {
/* 项目“HDL-ON_iOS”的未合并的更改
在此之前:
                                        Pir.pirDeviceList.Add(pirJosn);
在此之后:
                                        list.pirDeviceList.Add(pirJosn);
*/
                                        PirDevice.Pir.pirDeviceList.Add(pirJosn);
                                    }
                                }
                            }
                        }
                    }
                }
                catch { }
                finally
                {
                    Application.RunOnMainThread(() =>
                    {
                        loading.Hide();
                        try
                        {
                            if (responsePackNew != null && responsePackNew.Code == "0" && responsePackNew.Data.ToString() != "")
                            if (Pir.pirDeviceList.Count != 0)
                            {
                                var jobject = Newtonsoft.Json.Linq.JObject.Parse(responsePackNew.Data.ToString());
                                string list = jobject["list"].ToString();
                                var jArray = Newtonsoft.Json.Linq.JArray.Parse(list);
                                for (int a = 0; a < jArray.Count; a++)
                                GetControlList(() =>
                                {
                                    var jay = jArray[a];
                                    string spk = jay["spk"].ToString();
                                    if (spk == "ir.module")
                                    {
                                        //数据返序列化为Logic对象
                                        var str = Newtonsoft.Json.JsonConvert.SerializeObject(jay);
                                        var pirJosn = Newtonsoft.Json.JsonConvert.DeserializeObject<Pir>(str);
                                        if (pirJosn != null)
                                        {
                                            if (null == Pir.pirDeviceList.Find((c) => c.deviceId == pirJosn.deviceId))
                                            {
                                    loading.Hide();
                                    action();
                                                Pir.pirDeviceList.Add(pirJosn);
                                            }
                                        }
                                    }
                                }
                                GetControlList(frame, action);
                                });
                            }
                            else
                            {
                                loading.Hide();
                                Method method = new Method();
                                method.ErrorShow(responsePackNew);
                                method.ErrorShow(null,"读取红外宝列表失败");
                            }
                        }
                        catch { }
@@ -171,15 +271,12 @@
        }
        /// <summary>
        /// 获取遥控器列表
        /// 获取所有红外宝遥控器列表
        /// </summary>
        public static void GetControlList(FrameLayout frame, Action action)
        /// <param name="action">回调函数</param>
        public static void GetControlList(Action action)
        {
            //加载log
            Loading loading = new Loading();
            frame.AddChidren(loading);
            HDL_ON.DAL.Server.ResponsePackNew responsePackNew = null;
            loading.Start();
            new System.Threading.Thread(() =>
            {
                try
@@ -189,45 +286,127 @@
                        var pirDevice = Pir.pirDeviceList[i];
                        try
                        {
                            responsePackNew = PirSend.ControlList(pirDevice);
                            var responsePackNew = PirSend.ControlList(pirDevice);
                            if (responsePackNew != null && responsePackNew.Code == "0" && responsePackNew.Data.ToString() != "")
                            {
                                var jArray = JArray.Parse(responsePackNew.Data.ToString());
                                for (int a = 0; a < jArray.Count; a++)
                                {
                                    var jay = jArray[a];
                                    //数据返序列化为Logic对象
                                    var str = Newtonsoft.Json.JsonConvert.SerializeObject(jay);
                                    var pirJosn = Newtonsoft.Json.JsonConvert.DeserializeObject<Entity.Function>(str);
                                    if (pirJosn != null)
                                    {
                                        if (null == pirDevice.FunctioList.Find((c) => c.sid == pirJosn.sid))
                                        {
                                            pirDevice.FunctioList.Add(pirJosn);
                                        }
                                    }
                                }
                            }
                        }
                        catch { }
                        finally
                        {
                            Application.RunOnMainThread(() =>
                            {
                                try
                                {
                                    if (responsePackNew != null && responsePackNew.Code == "0" && responsePackNew.Data.ToString() != "")
                                    {
                                        var jArray = Newtonsoft.Json.Linq.JArray.Parse(responsePackNew.Data.ToString());
                                        for (int a = 0; a < jArray.Count; a++)
                                        {
                                            var jay = jArray[a];
                                            //数据返序列化为Logic对象
                                            var str = Newtonsoft.Json.JsonConvert.SerializeObject(jay);
                                            var pirJosn = Newtonsoft.Json.JsonConvert.DeserializeObject<Entity.Function>(str);
                                            if (pirJosn != null)
                                            {
                                                if (null == pirDevice.FunctioList.Find((c) => c.sid == pirJosn.sid))
                                                {
                                                    pirDevice.FunctioList.Add(pirJosn);
                                                }
                                            }
                                        }
                                    }
                                    else
                    }
                }
                catch { }
                finally
                {
                    Application.RunOnMainThread(() =>
                    {
                        action();
                    });
                }
            })
            { IsBackground = true }.Start();
        }
        /// <summary>
        /// 获取单个红外宝遥控器列表
        /// </summary>
        /// <param name="action">回调函数</param>
        public static void GetControlList(FrameLayout frame ,Action action, Pir pirDevice)
        {
            Loading loading = new Loading();
            frame.AddChidren(loading);
            loading.Start();
            new System.Threading.Thread(() =>
            {
                try
                {
                    try
                    {
                        var responsePackNew = PirSend.ControlList(pirDevice);
                        if (responsePackNew != null && responsePackNew.Code == "0" && responsePackNew.Data.ToString() != "")
                        {
                            var jArray = JArray.Parse(responsePackNew.Data.ToString());
                            for (int a = 0; a < jArray.Count; a++)
                            {
                                var jay = jArray[a];
                                //数据返序列化为Logic对象
                                var str = Newtonsoft.Json.JsonConvert.SerializeObject(jay);
                                var pirJosn = Newtonsoft.Json.JsonConvert.DeserializeObject<Entity.Function>(str);
                                if (pirJosn != null)
                                {
                                    if (null == pirDevice.FunctioList.Find((c) => c.sid == pirJosn.sid))
                                    {
                                        //Method method = new Method();
                                        //method.ErrorShow(responsePackNew);
                                        pirDevice.FunctioList.Add(pirJosn);
                                    }
                                }
                                catch { }
                            });
                            }
                        }
                    }
                    catch { }
                }
                catch { }
                finally
                {
                    Application.RunOnMainThread(() =>
                    {
                        loading.Hide();
                        action();
                    });
                }
            })
            { IsBackground = true }.Start();
        }
        /// <summary>
        /// 获取设备详情通过(spk,sid)
        /// </summary>
        /// <param name="frame">log图标加载界面</param>
        /// <param name="control">发送参数对象</param>
        /// <param name="action">回调函数</param>
        public void GetControl(FrameLayout frame, Control control, Action<Entity.Function> action)
        {
            Entity.Function function = null;
            //加载log
            Loading loading = new Loading();
            frame.AddChidren(loading);
            loading.Start();
            new System.Threading.Thread(() =>
            {
                try
                {
                    // 获取设备详情通过(spk,sid)
                    var responsePackNew = PirSend.GetinfoBySid(control);
                    if (responsePackNew != null && responsePackNew.Code == "0" && responsePackNew.Data.ToString() != "")
                    {
                        //var str = Newtonsoft.Json.JsonConvert.SerializeObject(responsePackNew.Data.ToString());
                        function = Newtonsoft.Json.JsonConvert.DeserializeObject<Entity.Function>(responsePackNew.Data.ToString());
                    }
                }
                catch { }
@@ -236,57 +415,69 @@
                    Application.RunOnMainThread(() =>
                    {
                        loading.Hide();
                        action();
                        action(function);
                    });
                }
            })
            { IsBackground = true }.Start();
        }
        /// <summary>
        /// 获取遥控器详细
        /// 发送命令线程
        /// </summary>
        public void GetControl(FrameLayout frame, Control control, Action<Entity.Function> action)
        /// <param name="control">发送数据对象</param>
        /// <param name="action">回调函数</param>
        /// <param name="str">判断字符</param>
        /// <param name="view">判断log父控件</param>
        /// <param name="frame">log父控件</param>
        /// <param name="dialog">log父控件</param>
        public static void ThreadSend(Control control, Action<ResponsePackNew> action, string str,string view, FrameLayout frame, Dialog dialog)
        {
            Entity.Function function = null;
            //加载log
            Loading loading = new Loading();
            frame.AddChidren(loading);
            HDL_ON.DAL.Server.ResponsePackNew responsePackNew = null;
            if (view == "dialog")
            {
                dialog.AddChidren(loading);
            }
            else
            {
                frame.AddChidren(loading);
            }
            loading.Start();
            ResponsePackNew responsePackNew = null;
            new System.Threading.Thread(() =>
            {
                try
                {
                    responsePackNew = PirSend.GetinfoBySid(control);
                    if (str == "删除")
                    {
                        responsePackNew = PirSend.DeleteDevice(control.deviceId);
                    }
                    else if (str == "修改名称")
                    {
                        responsePackNew = PirSend.DeviceRename(control.deviceId, control.name);
                    }
                    else if (str == "删除按键") {
                       // responsePackNew = PirSend.CodeRemove(control.deviceId, control.name);
                    }
                }
                catch { }
                finally
                {
                    Application.RunOnMainThread(() =>
                    {
                        try
                        loading.Hide();
                        if (responsePackNew != null && responsePackNew.Code == "0" && responsePackNew.Data.ToString() != "")
                        {
                            if (responsePackNew != null && responsePackNew.Code == "0" && responsePackNew.Data.ToString() != "")
                            {
                                var str = Newtonsoft.Json.JsonConvert.SerializeObject(responsePackNew.Data.ToString());
                                function = Newtonsoft.Json.JsonConvert.DeserializeObject<Entity.Function>(str);
                            }
                            else
                            {
                                Method method = new Method();
                                method.ErrorShow(responsePackNew);
                            }
                            action(responsePackNew);
                        }
                        catch { }
                        finally
                        else
                        {
                            Application.RunOnMainThread(() =>
                            {
                                loading.Hide();
                                action(function);
                            });
                            Method method = new Method();
                            method.ErrorShow(responsePackNew, "");
                        }
                    });
@@ -294,62 +485,126 @@
            })
            { IsBackground = true }.Start();
        }
        /// <summary>
        /// MQTT主题推送下来的数据(遥控器添加)
        /// MQTT主题推送下来的遥控器数据
        /// </summary>
        public static string addcontrondata = "";
        public static string controldata = "";
        /// <summary>
        /// MQTT主题推送下来的数据(学习按键)
        /// MQTT主题推送下来按键的数据
        /// </summary>
        public static string studybtndata = "";
        public static string buttondata = "";
        /// <summary>
        /// 判断这个主题是否是添加遥控器主题
        /// </summary>
        /// <param name="text">表示不同主题数据</param>
        /// <param name="sid">唯一标识</param>
        /// <param name="timeValue">等待时间值</param>
        /// <returns></returns>
        public Cloud MqttDate(string sid,int timeValue=10)
        public Cloud MqttDate(string text, string sid, int timeValue = 10)
        {
            Cloud cloud=null;
            Cloud cloud = null;
            var dateTime = DateTime.Now;
            while ((DateTime.Now - dateTime).TotalMilliseconds < timeValue * 1000)
            {
                if (!string.IsNullOrEmpty(addcontrondata))
                string str = "";
                if (text == "遥控器")
                {
                   var cloudjson = Newtonsoft.Json.JsonConvert.DeserializeObject<Cloud>(addcontrondata);
                    for (int i = 0; i < cloudjson.objects.Count; i++)
                    str = controldata;
                }
                else if (text == "按键")
                {
                    str = buttondata;
                }
                if (!string.IsNullOrEmpty(controldata))
                {
                    try
                    {
                        var objects = cloudjson.objects[i];
                        if (sid == objects.sid)
                        var cloudjson = Newtonsoft.Json.JsonConvert.DeserializeObject<Cloud>(str);
                        for (int i = 0; i < cloudjson.objects.Count; i++)
                        {
                            cloud = cloudjson;
                            var objects = cloudjson.objects[i];
                            if (sid == objects.sid)
                            {
                                cloud = cloudjson;
                                break;
                            }
                        }
                        if (cloud != null)
                        {
                            break;
                        }
                    }
                    break;
                    catch { }
                }
            }
            return cloud;
        }
        /// <summary>
        /// 错误码提示
        /// </summary>
        /// <param name="responsePackNew"></param>
        public void ErrorShow(ResponsePackNew responsePackNew)
        /// <param name="text">自定义错误文本</param>
        /// <param name="popValue">弹框类型(1=闪烁弹框)</param>
        public void ErrorShow(ResponsePackNew responsePackNew, string text, int popValue = 1)
        {
            if (responsePackNew != null && responsePackNew.Code == "14005")
            string str = "";
            if (text == "删除遥控器")
            {
                new Intelligence.Automation.LogicView.TipPopView().FlashingBox(Language.StringByID(StringId.gatewayNotOnline));
                str = Language.StringByID(StringId.delFail);
            }
            else if (text == "读取红外宝列表失败")
            {
                str = "读取红外宝列表失败"; //Language.StringByID(StringId.delFail);
            }
            else
            {
                new Intelligence.Automation.LogicView.TipPopView().FlashingBox(Language.StringByID(StringId.saveFail));
                if (responsePackNew != null)
                {
                    switch (responsePackNew.Code)
                    {
                        case "14005":
                            {
                                str = Language.StringByID(StringId.gatewayNotOnline);
                            }
                            break;
                        case "10807":
                            {
                                str = "红外宝下遥控器超过最大(10个)数量限制";// Language.StringByID(StringId.gatewayNotOnline);
                            }
                            break;
                        case "2":
                            {
                                str = "系统维护中~请稍后再试~";
                            }
                            break;
                        default:
                            {
                                str ="获取数据失败" ;// Language.StringByID(StringId.saveFail);
                            }
                            break;
                    }
                }
            }
            switch (popValue)
            {
                case 1:
                    {
                        new Intelligence.Automation.LogicView.TipPopView().FlashingBox(str);
                    }
                    break;
                case 2: { } break;
                case 3: { } break;
            }
        }
@@ -357,9 +612,6 @@
    [Serializable]
    public class Cloud
    {
        /// <summary>
        /// 设备id
        /// </summary>
        public string id = "";
        public List<Objects> objects = new List<Objects>();
        public string time_stamp = string.Empty;