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
|
{
|
/// <summary>
|
/// 添加遥控器的方法
|
/// </summary>
|
public void AddControl(FrameLayout frameLayout, Action<Control> 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);
|
|
}
|
/// <summary>
|
/// 发送遥控器命令方法
|
/// </summary>
|
/// <param name="control"></param>
|
/// <param name="frame"></param>
|
public void ThreadAddControl(Control control, FrameLayout frame, Action<Control> action)
|
{
|
DAL.Server.ResponsePackNew responsePackNew = null;
|
Loading loading = new Loading();
|
frame.AddChidren(loading);
|
loading.Start();
|
new System.Threading.Thread(() =>
|
{
|
try
|
{
|
responsePackNew = PirSend.Add(control);
|
}
|
catch { }
|
finally
|
{
|
Application.RunOnMainThread(() =>
|
{
|
loading.Hide();
|
if (responsePackNew != null && responsePackNew.Code == "0" && responsePackNew.Data.ToString() != "")
|
{
|
string sid = responsePackNew.Data.ToString();
|
var mqttdate = MqttDate(sid);
|
if (mqttdate != null)
|
{
|
///这里:监听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) =>
|
{
|
view.Close();
|
ThreadAddControl(control, frame, action);
|
});
|
}
|
|
|
}
|
else
|
{
|
ErrorShow(responsePackNew);
|
}
|
});
|
}
|
})
|
{ IsBackground = true }.Start();
|
|
}
|
|
/// <summary>
|
/// 获取红外设备列表
|
/// </summary>
|
public static void GetPirDeviceList(FrameLayout frame, Action action)
|
{
|
//清除之前列表;
|
Pir.pirDeviceList.Clear();
|
//加载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");
|
}
|
catch { }
|
finally
|
{
|
Application.RunOnMainThread(() =>
|
{
|
loading.Hide();
|
try
|
{
|
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)
|
{
|
if (null == Pir.pirDeviceList.Find((c) => c.deviceId == pirJosn.deviceId))
|
{
|
|
Pir.pirDeviceList.Add(pirJosn);
|
|
}
|
}
|
}
|
|
}
|
GetControlList(frame, action);
|
|
}
|
else
|
{
|
Method method = new Method();
|
method.ErrorShow(responsePackNew);
|
}
|
}
|
catch { }
|
|
});
|
}
|
|
})
|
{ IsBackground = true }.Start();
|
|
}
|
/// <summary>
|
/// 获取遥控器列表
|
/// </summary>
|
public static void GetControlList(FrameLayout frame, Action action)
|
{
|
//加载log
|
Loading loading = new Loading();
|
frame.AddChidren(loading);
|
HDL_ON.DAL.Server.ResponsePackNew responsePackNew = null;
|
loading.Start();
|
new System.Threading.Thread(() =>
|
{
|
try
|
{
|
for (int i = 0; i < Pir.pirDeviceList.Count; i++)
|
{
|
var pirDevice = Pir.pirDeviceList[i];
|
try
|
{
|
responsePackNew = PirSend.ControlList(pirDevice);
|
|
}
|
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
|
{
|
//Method method = new Method();
|
//method.ErrorShow(responsePackNew);
|
}
|
}
|
catch { }
|
|
});
|
}
|
}
|
}
|
catch { }
|
finally
|
{
|
Application.RunOnMainThread(() =>
|
{
|
loading.Hide();
|
action();
|
});
|
}
|
})
|
{ IsBackground = true }.Start();
|
|
}
|
|
/// <summary>
|
/// 获取遥控器详细
|
/// </summary>
|
public void GetControl(FrameLayout frame, Control control, Action<Entity.Function> action)
|
{
|
Entity.Function function = null;
|
//加载log
|
Loading loading = new Loading();
|
frame.AddChidren(loading);
|
HDL_ON.DAL.Server.ResponsePackNew responsePackNew = null;
|
loading.Start();
|
new System.Threading.Thread(() =>
|
{
|
try
|
{
|
responsePackNew = PirSend.GetinfoBySid(control);
|
}
|
catch { }
|
finally
|
{
|
Application.RunOnMainThread(() =>
|
{
|
try
|
{
|
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);
|
}
|
}
|
catch { }
|
finally
|
{
|
Application.RunOnMainThread(() =>
|
{
|
loading.Hide();
|
action(function);
|
});
|
}
|
|
});
|
}
|
|
})
|
{ IsBackground = true }.Start();
|
}
|
|
/// <summary>
|
/// MQTT主题推送下来的数据(遥控器添加)
|
/// </summary>
|
public static string addcontrondata = "";
|
/// <summary>
|
/// MQTT主题推送下来的数据(学习按键)
|
/// </summary>
|
public static string studybtndata = "";
|
|
/// <summary>
|
/// 判断这个主题是否是添加遥控器主题
|
/// </summary>
|
/// <returns></returns>
|
public Cloud MqttDate(string sid,int timeValue=10)
|
{
|
Cloud cloud=null;
|
var dateTime = DateTime.Now;
|
while ((DateTime.Now - dateTime).TotalMilliseconds < timeValue * 1000)
|
{
|
if (!string.IsNullOrEmpty(addcontrondata))
|
{
|
var cloudjson = Newtonsoft.Json.JsonConvert.DeserializeObject<Cloud>(addcontrondata);
|
for (int i = 0; i < cloudjson.objects.Count; i++)
|
{
|
var objects = cloudjson.objects[i];
|
if (sid == objects.sid)
|
{
|
cloud = cloudjson;
|
break;
|
}
|
}
|
break;
|
}
|
|
}
|
return cloud;
|
}
|
|
/// <summary>
|
/// 错误码提示
|
/// </summary>
|
/// <param name="responsePackNew"></param>
|
public void ErrorShow(ResponsePackNew responsePackNew)
|
{
|
|
if (responsePackNew != null && responsePackNew.Code == "14005")
|
{
|
new Intelligence.Automation.LogicView.TipPopView().FlashingBox(Language.StringByID(StringId.gatewayNotOnline));
|
|
}
|
else
|
{
|
new Intelligence.Automation.LogicView.TipPopView().FlashingBox(Language.StringByID(StringId.saveFail));
|
|
}
|
}
|
|
}
|
[Serializable]
|
public class Cloud
|
{
|
/// <summary>
|
/// 设备id
|
/// </summary>
|
public string id = "";
|
public List<Objects> objects = new List<Objects>();
|
public string time_stamp = string.Empty;
|
|
|
}
|
[Serializable]
|
public class Objects
|
{
|
|
public string sid = string.Empty;
|
public string spk = string.Empty;
|
public List<Attributes> attributes = new List<Attributes>();
|
}
|
[Serializable]
|
public class Attributes
|
{
|
public string key = "";
|
public string data_type = "";
|
public List<string> value = new List<string>();
|
|
}
|
}
|