using HDL_ON.Stan;
using Shared;
using System;
using System.Collections.Generic;
using System.Text;
namespace HDL_ON.UI
{
///
/// 添加Evoyo的Mini智能遥控器步骤4界面
///
public class AddMiniRemoteControlDirection4Page : EditorCommonForm
{
#region ■ 变量声明___________________________
///
/// wifi名字
///
private string wifiName = string.Empty;
///
/// wifi密码
///
private string wifiPsw = string.Empty;
///
/// 新设备的sid(用来匹配哪一个设备是新的)
///
private string newDeviceSid = string.Empty;
///
/// 超时线程是否运行
///
private bool timeoutThreadActivity = false;
///
/// 上传到云的图标
///
private IconViewControl btnClound = null;
///
/// 上传到云
///
private NormalViewControl btnCloundView = null;
///
/// 连接成功的图标
///
private IconViewControl btnConnect = null;
///
/// 连接成功
///
private NormalViewControl btnConnetView = null;
#endregion
#region ■ 初始化_____________________________
///
/// 画面显示(底层会固定调用此方法,借以完成画面创建)
///
/// wifi名字
/// wifi密码
public void ShowForm(string i_wifiName, string i_wifiPsw)
{
this.wifiName = i_wifiName;
this.wifiPsw = i_wifiPsw;
//设置头部信息
base.SetTitleText(Language.StringByID(StringId.AddInfraredRemoteControl));
//这个界面的背景需要白色
bodyFrameLayout.BackgroundColor = UI.CSS.CSS_Color.MainBackgroundColor;
//添加接收蓝牙反馈的事件
HdlBluetoothLogic.Current.AddReceiveEvent(this.BluetoothReceiveEvent);
//添加云端反馈事件
HdlCloudReceiveLogic.Current.AddCloudReceiveEvent("AddMiniRemoteControlDirection4Page", this.CloudReceiveEvent);
//初始化中部信息
this.InitMiddleFrame();
}
///
/// 初始化中部信息
///
private void InitMiddleFrame()
{
//清空body
this.ClearBodyFrame();
//图片
var btnPic = new PicViewControl(282, 121);
btnPic.Y = Application.GetRealHeight(122);
btnPic.Gravity = Gravity.CenterHorizontal;
btnPic.UnSelectedImagePath = "PersonalCenter/AddDevice/WifiAndPhoneConnect.png";
bodyFrameLayout.AddChidren(btnPic);
//正在连接中...
var btnSearch = new NormalViewControl(bodyFrameLayout.Width, Application.GetRealHeight(20), false);
btnSearch.Y = btnPic.Bottom + Application.GetRealHeight(40);
btnSearch.TextColor = CSS.CSS_Color.FirstLevelTitleColor;
btnSearch.TextID = StringId.NowConnectting;
btnSearch.TextAlignment = TextAlignment.Center;
bodyFrameLayout.AddChidren(btnSearch);
//请让红外遥控尽量接近WIFI路由器
var strMsg = Language.StringByID(StringId.AddInfraredRemoteControlMsg5);
this.AddListMsgControls(bodyFrameLayout, strMsg, CSS.CSS_FontSize.TextFontSize,
CSS.CSS_Color.PromptingColor1, Application.GetRealHeight(20), btnSearch.Bottom + Application.GetRealHeight(4));
//网络连接中
var btnNetIcon = new IconViewControl(20);
btnNetIcon.X = Application.GetRealWidth(60);
btnNetIcon.Y = btnPic.Bottom + Application.GetRealHeight(265);
btnNetIcon.UnSelectedImagePath = "PersonalCenter/AddDevice/NetConnectSelect.png";
bodyFrameLayout.AddChidren(btnNetIcon);
var btnNetView = new NormalViewControl(140, 18, true);//左右间距40再加文本60
btnNetView.Y = btnNetIcon.Bottom + Application.GetRealHeight(16);
btnNetView.TextSize = CSS.CSS_FontSize.PromptFontSize_FirstLevel;
btnNetView.TextColor = CSS.CSS_Color.FirstLevelTitleColor;
btnNetView.TextAlignment = TextAlignment.Center;
btnNetView.TextID = StringId.InTheNetworkLink;
bodyFrameLayout.AddChidren(btnNetView);
//一条横线
var btnLine1 = new NormalViewControl(Application.GetRealWidth(92), 2, false);
btnLine1.X = btnNetIcon.Right + Application.GetRealWidth(3);
btnLine1.Y = btnNetIcon.Y + (btnNetIcon.Height + 2) / 2;
btnLine1.BackgroundColor = CSS.CSS_Color.PromptingColor1;
bodyFrameLayout.AddChidren(btnLine1);
//上传到云
this.btnClound = new IconViewControl(20);
btnClound.Gravity = Gravity.CenterHorizontal;
btnClound.Y = btnNetIcon.Y;
btnClound.UnSelectedImagePath = "PersonalCenter/AddDevice/UploadToClound.png";
btnClound.SelectedImagePath = "PersonalCenter/AddDevice/UploadToCloundSelect.png";
bodyFrameLayout.AddChidren(btnClound);
this.btnCloundView = new NormalViewControl(btnNetView.Width, btnNetView.Height, false);
btnCloundView.Y = btnNetView.Y;
btnCloundView.Gravity = Gravity.CenterHorizontal;
btnCloundView.TextSize = CSS.CSS_FontSize.PromptFontSize_FirstLevel;
btnCloundView.TextColor = CSS.CSS_Color.PromptingColor1;
btnCloundView.TextAlignment = TextAlignment.Center;
btnCloundView.TextID = StringId.UploadToCloud;
bodyFrameLayout.AddChidren(btnCloundView);
//一条横线
var btnLine2 = new NormalViewControl(btnLine1.Width, btnLine1.Height, false);
btnLine2.X = btnClound.Right + Application.GetRealWidth(3);
btnLine2.Y = btnLine1.Y;
btnLine2.BackgroundColor = CSS.CSS_Color.PromptingColor1;
bodyFrameLayout.AddChidren(btnLine2);
//连接成功
this.btnConnect = new IconViewControl(20);
btnConnect.X = bodyFrameLayout.Width - btnNetIcon.X - btnConnect.IconSize;
btnConnect.Y = btnNetIcon.Y;
btnConnect.UnSelectedImagePath = "PersonalCenter/AddDevice/ConnectSuccess.png";
btnConnect.SelectedImagePath = "PersonalCenter/AddDevice/ConnectSuccessSelect.png";
bodyFrameLayout.AddChidren(btnConnect);
this.btnConnetView = new NormalViewControl(btnNetView.Width, btnNetView.Height, false);
btnConnetView.X = bodyFrameLayout.Width - btnNetView.Width - btnNetView.X;
btnConnetView.Y = btnNetView.Y;
btnConnetView.TextSize = CSS.CSS_FontSize.PromptFontSize_FirstLevel;
btnConnetView.TextColor = CSS.CSS_Color.PromptingColor1;
btnConnetView.TextAlignment = TextAlignment.Center;
btnConnetView.TextID = StringId.ConnectSuccess;
bodyFrameLayout.AddChidren(btnConnetView);
HdlThreadLogic.Current.RunThread(() =>
{
System.Threading.Thread.Sleep(2000);
HdlThreadLogic.Current.RunMain(() =>
{
//发送账号和密码给蓝牙
this.SendAccountAndPswToBluetooth();
});
//启动超时线程
this.StartTimeoutThread();
});
}
#endregion
#region ■ 显示成功界面_______________________
///
/// 显示成功界面
///
private void ShowSuccessView(Entity.Function newDevice)
{
//清空body
this.ClearBodyFrame();
//图片
var btnPic = new PicViewControl(180, 180);
btnPic.Y = Application.GetRealHeight(48);
btnPic.Gravity = Gravity.CenterHorizontal;
btnPic.UnSelectedImagePath = "Public/TipIcon_Successfully.png";
bodyFrameLayout.AddChidren(btnPic);
//添加成功
var btnFail = new NormalViewControl(bodyFrameLayout.Width, Application.GetRealHeight(22), false);
btnFail.Y = btnPic.Bottom + Application.GetRealHeight(16);
btnFail.TextSize = CSS.CSS_FontSize.SubheadingFontSize;
btnFail.TextColor = CSS.CSS_Color.MainColor;
btnFail.TextAlignment = TextAlignment.Center;
btnFail.TextID = StringId.AddSuccess;
bodyFrameLayout.AddChidren(btnFail);
//可以开始使用红外遥控功能!
var strMsg = Language.StringByID(StringId.AddInfraredRemoteControlMsg7);
int yy = this.AddListMsgControls(bodyFrameLayout, strMsg, CSS.CSS_FontSize.TextFontSize, CSS.CSS_Color.TextualColor,
Application.GetRealHeight(20), btnFail.Bottom + Application.GetRealHeight(8));
//开始使用
var btnUse = new BottomClickButton(220);
btnUse.Y = yy + Application.GetRealHeight(60);
btnUse.TextID = StringId.StartUse;
bodyFrameLayout.AddChidren(btnUse);
btnUse.ButtonClickEvent += (sender, e) =>
{
if (newDevice == null)
{
//关闭掉全部的界面,应该不会发生
HdlFormLogic.Current.CloseAllOpenForm();
return;
}
var form = HdlFormLogic.Current.GetFormByName("AddMiniRemoteControlDirection1Page") as AddMiniRemoteControlDirection1Page;
if (form.AddDeviceEvent != null)
{
//代表这个是由温总那边调用的,直接回调函数
form.AddDeviceEvent.Invoke(newDevice);
//关闭掉这个界面
this.CloseForm();
//再把AddMiniRemoteControlDirection1Page界面关了
HdlFormLogic.Current.CloseFormByFormName("AddMiniRemoteControlDirection1Page");
}
else
{
//代表这并不是由温总的界面调用的,则关闭掉全部的界面
HdlFormLogic.Current.CloseAllOpenForm();
//然后再把温总的界面new起来
new UI2.PersonalCenter.PirDevice.Method().MainView(this, newDevice,()=> { });
}
};
}
#endregion
#region ■ 显示失败界面_______________________
///
/// 显示失败界面
///
private void ShowFailView()
{
//清空body
this.ClearBodyFrame();
//图片
var btnPic = new PicViewControl(180, 180);
btnPic.Y = Application.GetRealHeight(48);
btnPic.Gravity = Gravity.CenterHorizontal;
btnPic.UnSelectedImagePath = "Public/TipIcon_Failed.png";
bodyFrameLayout.AddChidren(btnPic);
//添加失败
var btnFail = new NormalViewControl(bodyFrameLayout.Width, Application.GetRealHeight(22), false);
btnFail.Y = btnPic.Bottom + Application.GetRealHeight(16);
btnFail.TextSize = CSS.CSS_FontSize.SubheadingFontSize;
btnFail.TextColor = CSS.CSS_Color.AuxiliaryColor2;
btnFail.TextAlignment = TextAlignment.Center;
btnFail.TextID = StringId.AddFail;
bodyFrameLayout.AddChidren(btnFail);
//1、请检查设备是否正常通电
//2、请开启蓝牙功能
//3、并长按按钮10s,指示灯常亮
var strMsg = Language.StringByID(StringId.AddInfraredRemoteControlMsg6);
this.AddListMsgControls(bodyFrameLayout, strMsg, CSS.CSS_FontSize.TextFontSize, CSS.CSS_Color.TextualColor,
Application.GetRealHeight(20), btnFail.Bottom + Application.GetRealHeight(8),
TextAlignment.Center, true);
//重试
var btnReDo = this.AddBottomClickButton(Language.StringByID(StringId.Retry));
btnReDo.ButtonClickEvent += (sender, e) =>
{
//重新初始化中部信息
this.CloseForm();
var form = new AddMiniRemoteControlDirection2Page();
form.AddForm(this.wifiName, this.wifiPsw);
};
}
#endregion
#region ■ 发送账号和密码给蓝牙_______________
///
/// 发送账号和密码给蓝牙
///
private void SendAccountAndPswToBluetooth()
{
//获取发送到蓝牙的数据
var sendData = this.GetSendToBluetoothData();
//不等待
HdlBluetoothLogic.Current.SendData(sendData);
}
#endregion
#region ■ 红外宝相关方法_____________________
///
/// 检测是否是新的红外宝
///
/// 推送枚举
/// 推送的数据
///
private bool CheckIsNewMiniRemoteControl(CloudPushEnum pushEnum, string i_data)
{
if (pushEnum != CloudPushEnum.A新设备上报) { return false; }
try
{
var strSpk = string.Empty;
//转为json
var json = Newtonsoft.Json.Linq.JObject.Parse(i_data);
var linqArry = json["objects"] as Newtonsoft.Json.Linq.JArray;
if (linqArry != null)
{
//数组类型
if (linqArry.Count == 0) { return false; }
strSpk = linqArry[0]["spk"].ToString();
this.newDeviceSid = linqArry[0]["sid"].ToString();
}
else
{
//非数组类型
strSpk = json["objects"]["spk"].ToString();
this.newDeviceSid = json["objects"]["sid"].ToString();
}
if (strSpk != Entity.SPK.IrModule)
{
//如果不是红外宝的话
return false;
}
return true;
}
catch { return false; }
}
///
/// 刷新红外宝信息
///
/// 成功的回调事件
private void RefreshMiniRemoteControlInfo(Action successEvent)
{
var pra = new Dictionary();
pra.Add("homeId", Entity.DB_ResidenceData.Instance.CurrentRegion.RegionID);
pra.Add("spk", Entity.SPK.IrModule);
var requestJson = DAL.Server.HttpUtil.GetSignRequestJson(pra);
HdlThreadLogic.Current.RunThread(() =>
{
int count = 0;
while (this.Parent != null)
{
try
{
//超过5次都不成功,真不处理了
count++;
if (count > 5) { return; }
//获取全部红外宝列表
var result = DAL.Server.HttpUtil.RequestHttpsPostFroHome(DAL.Server.NewAPI.Api_Post_GetDevcieList, requestJson);
if (result == null || result.Code != DAL.Server.StateCode.SUCCESS)
{
System.Threading.Thread.Sleep(3000);
continue;
}
var listDevice = Newtonsoft.Json.JsonConvert.DeserializeObject(result.Data.ToString());
Entity.Function newDevice = null;
foreach (var newFunction in listDevice.list)
{
//添加到缓存当中
newFunction.SaveFunctionFile();
Entity.FunctionList.List.IniFunctionList(newFunction.savePath, true);
if (newFunction.sid == this.newDeviceSid)
{
//这个设备是新追加的
newDevice = newFunction;
}
}
//调用回调函数
successEvent?.Invoke(newDevice);
return;
}
catch
{
System.Threading.Thread.Sleep(3000);
}
}
});
}
#endregion
#region ■ 云端反馈___________________________
///
/// 云端反馈
///
/// 枚举
/// 推送的内容
private void CloudReceiveEvent(CloudPushEnum pushEnum, string i_data)
{
//检测是否是新的红外宝
if (this.CheckIsNewMiniRemoteControl(pushEnum, i_data) == false)
{
return;
}
//刷新红外宝信息
this.RefreshMiniRemoteControlInfo((Entity.Function newDevice) =>
{
//移除这个事件
HdlCloudReceiveLogic.Current.RemoveCloudReceiveEvent("AddMiniRemoteControlDirection4Page");
//停止线程
this.timeoutThreadActivity = false;
//添加设备成功之后,处理一些东西
this.DoSomethingAfterAddDeviceSuccess(newDevice);
});
}
///
/// 添加设备成功之后,处理一些东西
///
private void DoSomethingAfterAddDeviceSuccess(Entity.Function newDevice)
{
HdlThreadLogic.Current.RunThread(() =>
{
//整点特效而已
for (int i = 1; i <= 6; i++)
{
System.Threading.Thread.Sleep(1000);
if (i == 1)
{
HdlThreadLogic.Current.RunMain(() =>
{
//整点特效而已
this.btnClound.IsSelected = true;
this.btnCloundView.TextColor = CSS.CSS_Color.FirstLevelTitleColor;
});
}
else if (i == 4)
{
HdlThreadLogic.Current.RunMain(() =>
{
//整点特效而已
this.btnConnect.IsSelected = true;
this.btnConnetView.TextColor = CSS.CSS_Color.FirstLevelTitleColor;
});
}
else if (i == 6)
{
HdlThreadLogic.Current.RunMain(() =>
{
//显示成功界面
this.ShowSuccessView(newDevice);
});
}
}
});
}
#endregion
#region ■ 蓝牙反馈___________________________
///
/// 蓝牙反馈
///
/// 蓝牙反馈的数据
private void BluetoothReceiveEvent(string i_receviceData)
{
#if DEBUG
System.Console.WriteLine("蓝牙返回:" + i_receviceData);
#endif
}
#endregion
#region ■ 超时线程___________________________
///
/// 启动超时线程
///
private void StartTimeoutThread()
{
HdlThreadLogic.Current.RunThread(() =>
{
//360秒超时
int timeout = 360;
while (this.timeoutThreadActivity == false && this.Parent != null)
{
System.Threading.Thread.Sleep(1000);
timeout--;
if (timeout == 0)
{
break;
}
}
if (timeout <= 0)
{
//显示失败界面
HdlThreadLogic.Current.RunMain(() =>
{
this.ShowFailView();
});
}
});
}
#endregion
#region ■ 界面关闭___________________________
///
/// 界面关闭
///
public override void CloseFormBefore()
{
//摧毁蓝牙
HdlBluetoothLogic.Current.Dispone();
HdlCloudReceiveLogic.Current.RemoveCloudReceiveEvent("AddMiniRemoteControlDirection4Page");
base.CloseFormBefore();
}
#endregion
#region ■ 一般方法___________________________
///
/// 获取发送到蓝牙的数据
///
///
private string GetSendToBluetoothData()
{
var pra = new { id = "id010203", ssid = this.wifiName, password = this.wifiPsw };
var praData = Newtonsoft.Json.JsonConvert.SerializeObject(pra);
var sendData = "Topic:/user/id/custom/wifi/set\r\n";
sendData += "Length:" + praData.Length + "\r\n\r\n";
sendData += praData;
return sendData;
}
#endregion
}
}