界面:
代码:`// ***************************************************
//
//文件名称(File Name):SetDeviceAddrPowerPanelForm.cs
//
//功能描述(Description):设置设备地址
//
//数据表(Tables): 无
//
//作者(Author):徐文文
//
//日期(Create Date):2021.07.07
//
// ***************************************************
using ControlFor.Common;
using ControlFor.DataModel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ControlFor.Devices;
namespace ControlFor.Media
{
public partial class SetDeviceAddrPowerPanelForm : Form
{
///
/// 设备类型uuid
///
string m_deviceType_uuid = “”;
///
/// 设备品牌uuid
///
string m_brandType_uuid = “”;
///
/// 插卡取电窗体
///
AddEditPowerPanelForm m_powerPanelForm;
///
///
///
///
delegate void SetTextBoxCallBack(string my_str);
///
/// 返回的地址
///
public string m_return_addr = “”;
public SetDeviceAddrPowerPanelForm()
{
InitializeComponent();
}
public SetDeviceAddrPowerPanelForm(string my_deviceType_uuid, string my_brandType_uuid, AddEditPowerPanelForm my_powerPanelForm)
{
m_deviceType_uuid = my_deviceType_uuid;
m_brandType_uuid = my_brandType_uuid;
m_powerPanelForm = my_powerPanelForm;
InitializeComponent();
CommonClass.SetFormIcon(this);
initComboxDataFunc();
}
///
/// 初始化下拉框数据
///
public void initComboxDataFunc()
{
try
{
#region 控制类型
List my_sourceList = new List()
{
};
ComBoboxSource my_source1 = new ComBoboxSource();
my_source1.text = "lora控制";
my_source1.value = "lora";
ComBoboxSource my_source2 = new ComBoboxSource();
my_source2.text = "网口控制";
my_source2.value = "network";
my_sourceList.Add(my_source1);
my_sourceList.Add(my_source2);
this.cmb_controlType.DataSource = my_sourceList;
this.cmb_controlType.DisplayMember = "text";
this.cmb_controlType.ValueMember = "value";
this.cmb_controlType.SelectedIndex = 1;
this.cmb_controlType.SelectedIndex = 0;
#endregion
#region 控制网关
//控制网关数据由控制类型数据决定
#endregion
}
catch (Exception ex)
{
LogHelper.WriteLog(ex, "SetDeviceAddrPowerPanelForm->initComboxDataFunc");
}
}
private void SetDeviceAddrPowerPanelForm_Load(object sender, EventArgs e)
{
}
private void cmb_controlType_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
if (this.cmb_controlType.SelectedIndex >= 0)
{
LoadBindDeviceListFunc(cmb_controlType.SelectedValue.ToString(), m_deviceType_uuid, m_brandType_uuid);
}
else
{
//绑定设备
List<ComBoboxSource> my_sourceList_binding_device = new List<ComBoboxSource>()
{
new ComBoboxSource()
{
text = "请选择设备",
value = "-1"
}
};
this.cmb_control_gatway.DataSource = my_sourceList_binding_device;
this.cmb_control_gatway.DisplayMember = "text";
this.cmb_control_gatway.ValueMember = "value";
this.cmb_control_gatway.SelectedIndex = 0;
}
}
catch (Exception ex)
{
LogHelper.WriteLog(ex, "SetDeviceAddrPowerPanelForm->cmb_controlType_SelectedIndexChanged");
}
}
// <summary>
/// 初始化绑定设备下拉框
/// </summary>
/// <param name="my_control_type">控制类型</param>
/// <param name="my_device_type_uuid">设备类型uuid</param>
/// <param name="my_device_brand_uuid">品牌类型uuid</param>
public void LoadBindDeviceListFunc(string my_control_type, string my_device_type_uuid, string my_device_brand_uuid)
{
try
{
if (my_control_type == "ControlFor.DataModel.ComBoboxSource" ||
my_device_type_uuid == "" ||
my_device_brand_uuid == "ControlFor.DataModel.ComBoboxSource" ||
my_device_brand_uuid == "-1")
{
return;
}
Dictionary<string, string> my_param_submit = new Dictionary<string, string>();
my_param_submit.Add("control_type", my_control_type);
my_param_submit.Add("device_type_uuid", my_device_type_uuid);
my_param_submit.Add("device_brand_uuid", my_device_brand_uuid);
string my_str_gstr = CommonClass.http.NewPost(InfoClass.api + @"/ToolRest/getControlMaster", my_param_submit);
dynamic my_data = JsonConvert.DeserializeObject<dynamic>(my_str_gstr);
if (my_data.code > 0 && !string.IsNullOrEmpty(my_data.re.ToString()))
{
List<dynamic> my_lst = CommonClass.GetList_dynamic(my_data.re.ToString());
List<ComBoboxSource> my_datasorce = new List<ComBoboxSource>()
{
new ComBoboxSource()
{
text = "请选择绑定设备",
value = "-1"
}
};
if (my_lst != null)
{
for (int i = 0; i < my_lst.Count; i++)
{
my_datasorce.Add(new ComBoboxSource()
{
text = my_lst[i].master_name != null ? my_lst[i].master_name.ToString() : "",
value = my_lst[i].master_id != null ? my_lst[i].master_id.ToString() : ""
});
}
}
if (my_lst.Count > 0)
{
this.cmb_control_gatway.DataSource = my_datasorce;
this.cmb_control_gatway.DisplayMember = "text";
this.cmb_control_gatway.ValueMember = "value";
this.cmb_control_gatway.SelectedIndex = 1;
}
else
{
this.cmb_control_gatway.DataSource = my_datasorce;
this.cmb_control_gatway.DisplayMember = "text";
this.cmb_control_gatway.ValueMember = "value";
this.cmb_control_gatway.SelectedIndex = 0;
}
}
else
{
MessageBox.Show("网络传输数据异常。");
}
}
catch (Exception ex)
{
LogHelper.WriteLog(ex, "SetDeviceAddrPowerPanelForm->LoadBindDeviceListFunc");
}
}
private void btn_submit_Click(object sender, EventArgs e)
{
try
{
try
{
SocketClass.SocketReceiveDataPushOutEvent -= new Lora_Connect_DisConnect_EventHandler(showSocketReturnDataFunc);
SocketClass.SocketReceiveDataPushOutEvent += new Lora_Connect_DisConnect_EventHandler(showSocketReturnDataFunc);
}
catch (Exception ex)
{
LogHelper.WriteLog(ex, "尝试解绑/绑定插卡取电面板中的函数失败");
return;
}
Dictionary<string, object> my_param_submit = new Dictionary<string, object>();
my_param_submit.Add("device_brand_uuid", m_brandType_uuid);
my_param_submit.Add("device_type_uuid", m_deviceType_uuid);
if (string.IsNullOrEmpty(txt_addr.Text.Trim()))
{
MessageBox.Show("请输入设备地址");
return;
}
if (txt_addr.Text.Trim().Length != 2)
{
MessageBox.Show("设备地址位两位");
return;
}
my_param_submit.Add("device_address", txt_addr.Text.Trim());
my_param_submit.Add("control_type", cmb_controlType.SelectedValue.ToString());
if (cmb_control_gatway.SelectedIndex <= 0)
{
MessageBox.Show("请选择控制网关");
return;
}
my_param_submit.Add("master_id", cmb_control_gatway.SelectedValue.ToString());
string my_str_gstr = CommonClass.http.NewPost(InfoClass.api + @"/ToolRest/getPowerPanelAddress", my_param_submit);
dynamic my_data = JsonConvert.DeserializeObject<dynamic>(my_str_gstr);
if (my_data.code < 0)
{
MessageBox.Show("参数或网络问题,请求失败");
}
}
catch (Exception ex)
{
LogHelper.WriteLog(ex, "SetDeviceAddrPowerPanelForm->btn_submit_Click");
}
}
/// <summary>
/// 插卡取电面板获取地址,处理Socket返回的数据
/// 作者:徐文文
/// </summary>
/// <param name="my_str"></param>
public void showSocketReturnDataFunc(string my_str)
{
try
{
//threadupdra.Abort();
SocketClass.SocketReceiveDataPushOutEvent -= new Lora_Connect_DisConnect_EventHandler(showSocketReturnDataFunc);
string ret = my_str.Replace("$_S_H", "");
dynamic data = JsonConvert.DeserializeObject<dynamic>(ret);
// dynamic my_msg = JsonConvert.DeserializeObject<dynamic>(data.msg.ToString());
string my_msg = data.msg != null ? data.msg.ToString() : "";
if (my_msg.Contains("success")) //返回成功
{
SocketClass.result = "";
string my_operation_type = data.operation_type != null ? data.operation_type.ToString() : "";
if (my_operation_type == "POWER_PANEL_ADDRESS") //插卡取电面板获取设备地址
{
string my_address = data.re.address != null ? data.re.address.ToString() : "";
showDataFunc(my_address);
}
}
else
{
SocketClass.result = "";
MessageBox.Show("返回数据数据异常");
}
}
catch (Exception ex)
{
LogHelper.WriteLog(ex, "SetDeviceAddrPowerPanelForm->showSocketReturnDataFunc");
}
}
/// <summary>
/// 更新返回的数据地址
/// 作者:徐文文
/// </summary>
/// <param name="my_str"></param>
public void showDataFunc(string my_str)
{
if (!string.IsNullOrEmpty(my_str))
{
if (txt_return_addr.InvokeRequired)
{
SetTextBoxCallBack labDele = new SetTextBoxCallBack(showDataFunc);
this.Invoke(labDele, new object[] { my_str });
}
else
{
txt_return_addr.Text = my_str;
}
}
}
private void SetDeviceAddrPowerPanelForm_FormClosed(object sender, FormClosedEventArgs e)
{
//关闭窗体发生取消
this.DialogResult = DialogResult.Cancel;
}
private void txt_return_addr_TextChanged(object sender, EventArgs e)
{
try
{
m_return_addr = txt_return_addr.Text.Trim();
if (txt_return_addr.Text.Trim() != txt_addr.Text.Trim())
{
MessageBox.Show("更改设备地址失败。");
}
else
{
MessageBox.Show("更改设备地址成功。");
}
}
catch (Exception ex)
{
LogHelper.WriteLog(ex);
}
}
}
}
`
注意事项,这个是测试代码,严重客户端连接是否在线,最优化的办法就是看心跳是否收到:
定义一个全局变量num,假设我们心跳每15s发送一次,有一个线程定时发送,每发送一次心跳num++,每收到一次心跳num–,如果num大于2,说明至少两次没有收到心跳,重连,如果重连成功,num置0