using System; using HDL_ON.DAL.Server; using Shared; using System.Collections.Generic; using Newtonsoft.Json.Linq; namespace HDL_ON.UI.UI2.PersonalCenter.PirDevice { public class Method { /// /// 管理位置 /// /// 当前设备 /// 回调函数 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; } /// /// 修改名称 /// /// 提示标题文本 /// 当前存在名称列表 /// 当前名称 /// 回调函数 public void EditControlName(int tipText, List list, string currName, Action action, Action actionCancel, bool tag = false) { new View.TipView().InputBox(tipText, currName, StringId.nameNull, StringId.NameAlreadyExists, list, (text, view ) => { action(text, view); }, () => { actionCancel(); }, tag); } /// /// 添加遥控器的方法 /// /// log图标加载界面 /// 回调函数 public void AddControl(FrameLayout frameLayout, Action action) { View.TipView tipView = new View.TipView(); tipView.InputBox(frameLayout, "", (name, frame) => { if (Pir.currPir != null && Pir.currPir.FunctioList.Count < 10) { Control control = new Control(); control.name = name; control.type = "learn"; control.spk = "ir.learn"; control.deviceId = Pir.currPir.deviceId; ThreadAddControl(control, frame, action); } else { View.TipView tt = new View.TipView(); tt.TipBox(StringId.tip, StringId.bunengchaoguo10); } }, false); } /// /// 发送遥控器命令方法 /// /// 发送参数对象 /// log图标加载界面 /// 回调函数 public void ThreadAddControl(Control control, FrameLayout frame, Action action) { Cloud mqttdate = null; Loading loading = new Loading(); frame.AddChidren(loading); loading.Start(); new System.Threading.Thread(() => { try { //发送添加命令 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 { Application.RunOnMainThread(() => { loading.Hide(); ///这里:监听MTTP推送下来主题; if (mqttdate != null) { //休眠500毫秒,为等待云端创建deviceid; System.Threading.Thread.Sleep(500); //读取添加遥控器deviceID,才知道是否添加成功; 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) => { view.Close(); ThreadAddControl(control, frame, action); }); } }); } else { //监听Mqtt推送下来状态码做提示 View.FailView failView = new View.FailView(); failView.ShouError((view) => { view.Close(); ThreadAddControl(control, frame, action); }); } }); } }) { IsBackground = true }.Start(); } /// /// 获取红外设备列表 /// /// log图标加载界面 /// 回调函数 public static void GetPirDeviceList(FrameLayout frame, Action action) { //清除之前列表; Pir.pirDeviceList.Clear(); //加载log Loading loading = new Loading(); frame.AddChidren(loading); loading.Start(); new System.Threading.Thread(() => { try { 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(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(() => { try { if (Pir.pirDeviceList.Count != 0) { GetControlList(() => { loading.Hide(); action(); }); } else { loading.Hide(); Method method = new Method(); method.ErrorShow(null,"读取红外宝列表失败"); } } catch { } }); } }) { IsBackground = true }.Start(); } /// /// 获取遥控器列表 /// /// 回调函数 public static void GetControlList(Action action) { new System.Threading.Thread(() => { try { for (int i = 0; i < Pir.pirDeviceList.Count; i++) { var pirDevice = Pir.pirDeviceList[i]; 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(str); if (pirJosn != null) { if (null == pirDevice.FunctioList.Find((c) => c.sid == pirJosn.sid)) { pirDevice.FunctioList.Add(pirJosn); } } } } } catch { } } } catch { } finally { Application.RunOnMainThread(() => { action(); }); } }) { IsBackground = true }.Start(); } /// /// 获取设备详情通过(spk,sid) /// /// log图标加载界面 /// 发送参数对象 /// 回调函数 public void GetControl(FrameLayout frame, Control control, Action 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(responsePackNew.Data.ToString()); } } catch { } finally { Application.RunOnMainThread(() => { loading.Hide(); action(function); }); } }) { IsBackground = true }.Start(); } /// /// 发送命令线程 /// /// 发送数据对象 /// 回调函数 /// 判断字符 /// log父控件 public void ThreadSend(Control control, Action action, string str, FrameLayout frame) { //加载log Loading loading = new Loading(); frame.AddChidren(loading); loading.Start(); ResponsePackNew responsePackNew = null; new System.Threading.Thread(() => { try { 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(() => { loading.Hide(); if (responsePackNew != null && responsePackNew.Code == "0" && responsePackNew.Data.ToString() != "") { action(responsePackNew); } else { Method method = new Method(); method.ErrorShow(responsePackNew, ""); } }); } }) { IsBackground = true }.Start(); } /// /// 发送命令线程 /// /// 发送数据对象 /// 回调函数 /// 判断字符 /// log父控件 public void ThreadSend(Control control, Action action, string str,Dialog dialog) { //加载log Loading loading = new Loading(); dialog.AddChidren(loading); ResponsePackNew responsePackNew = null; loading.Start(); new System.Threading.Thread(() => { try { 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(() => { loading.Hide(); if (responsePackNew != null && responsePackNew.Code == "0" && responsePackNew.Data.ToString() != "") { action(responsePackNew); } else { Method method = new Method(); method.ErrorShow(responsePackNew, ""); } }); } }) { IsBackground = true }.Start(); } /// /// MQTT主题推送下来的遥控器数据 /// public static string controldata = ""; /// /// MQTT主题推送下来按键的数据 /// public static string buttondata = ""; /// /// 判断这个主题是否是添加遥控器主题 /// /// 表示不同主题数据 /// 唯一标识 /// 等待时间值 /// public Cloud MqttDate(string text, string sid, int timeValue = 10) { Cloud cloud = null; var dateTime = DateTime.Now; while ((DateTime.Now - dateTime).TotalMilliseconds < timeValue * 1000) { string str = ""; if (text == "遥控器") { str = controldata; } else if (text == "按键") { str = buttondata; } if (!string.IsNullOrEmpty(controldata)) { try { var cloudjson = Newtonsoft.Json.JsonConvert.DeserializeObject(str); for (int i = 0; i < cloudjson.objects.Count; i++) { var objects = cloudjson.objects[i]; if (sid == objects.sid) { cloud = cloudjson; break; } } if (cloud != null) { break; } } catch { } } } return cloud; } /// /// 错误码提示 /// /// /// 自定义错误文本 /// 弹框类型(1=闪烁弹框) public void ErrorShow(ResponsePackNew responsePackNew, string text, int popValue = 1) { string str = ""; if (text == "删除遥控器") { str = Language.StringByID(StringId.delFail); } else if (text == "读取红外宝列表失败") { str = "读取红外宝列表失败"; //Language.StringByID(StringId.delFail); } else { 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; } } } [Serializable] public class Cloud { public string id = ""; public List objects = new List(); public string time_stamp = string.Empty; } [Serializable] public class Objects { public string sid = string.Empty; public string spk = string.Empty; public List attributes = new List(); } [Serializable] public class Attributes { public string key = ""; public string data_type = ""; public List value = new List(); } }