void ExecuReceive(object data) { lock (CurrentClient) { AutoResetEvent exitEvent; exitEvent = new AutoResetEvent(false); //List<byte[]> mac_list = new List<byte[]>(); //bool open = true; TcpDatagramReceivedEventArgs <byte[]> e = (TcpDatagramReceivedEventArgs <byte[]>)data; LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "服务器接收的基础数据:" + data); LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + ";服务器收到来自设备的数据:" + Converts.GetTPandMac(e.Datagram) + ";"); int lengths = e.Datagram.Length;//接受客户端握手信息 string str_mac = ""; string str_boxdate = ""; //byte[] boxdate = new byte[6]; //byte[] mac = new byte[12]; byte[] boxdate = new byte[12]; byte[] mac = new byte[15]; //byte[] macnew = new byte[13]; LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "服务器命令头:" + e.Datagram[0]); switch (e.Datagram[0]) { //第一个字节是EF case 239: //主机返回的数据 { #region 发送数据到客户端 //Array.Copy(e.Datagram, 10, mac, 0, 12); //Array.Copy(e.Datagram, 22, boxdate, 0, 6); try { Array.Copy(e.Datagram, 10, mac, 0, 15); Array.Copy(e.Datagram, 25, boxdate, 0, 12); str_mac = Converts.GetTPandMac(mac); str_boxdate = Converts.GetTPandMac(boxdate); DeviceClient dev = new DeviceClient(); dev.BoxState = str_boxdate; dev.myclient = e.TcpClient; //请求端 dev.OnLine = false; dev.StrMac = str_mac; dev.typeid = 2; //byte[] LastRes = HexStringToByte(GetLastStrToEnd("EF02", Converts.GetTPandMac(e.Datagram))); byte[] LastRes = HexStringToByte(GetLastStrToEnd("EF03", Converts.GetTPandMac(e.Datagram))); LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + ";服务器收到来自设备" + str_mac + "的数据:" + Converts.GetTPandMac(e.Datagram) + ";"); //showLog(DateTime.Now.ToString("HH:mm:ss") + ";服务器收到来自设备" + str_mac + "的数据:" + Converts.GetTPandMac(e.Datagram) + ";"); if (!CheckDataGram(LastRes)) { //byte[] box_number = new byte[] { 0, 0, 0, 0, 0, 0 }; byte[] box_number = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; byte[] byteCommand = GetSendMulty(mac, box_number, 0x06); string sss = ByteToHexString(byteCommand); //暂时注释掉,回复指令 //int j = 0; //while (j < 3) //{ // try // { // Thread.Sleep(750); // LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "向目标:" + dev.GetStrRemoteEndPoint + ";向MAC为" + str_mac + "发送重发要求" + sss); // dev.SendMsg(byteCommand); // break; // } // catch { j++; } //} } else { if (e.Datagram[9] == 0x03) //开箱指令响应 { string strLastCommanResponse = ByteToHexString(LastRes); WeiXinClient wxclient = CurrentClient[str_mac, 1] as WeiXinClient; if (wxclient != null) { wxclient.receivenum += GetStrAppearTimes("EF03", Converts.GetTPandMac(e.Datagram)); LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "接收到来自MAC为" + str_mac + "的响应,响应的内容为:" + Converts.GetTPandMac(e.Datagram)); //LogHelper.showLog(txtLog, (DateTime.Now.ToString("HH:mm:ss") + "接收到来自MAC为" + str_mac + "的响应,响应的内容为:" + Converts.GetTPandMac(e.Datagram))); try { if (wxclient.SendMsg(LastRes)) //收到的报文回送 { LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "向微信端回送成功:" + wxclient.GetStrRemoteEndPoint + strLastCommanResponse); //LogHelper.showLog(txtLog, (DateTime.Now.ToString("HH:mm:ss") + "向微信端回送成功:" + wxclient.GetStrRemoteEndPoint + strLastCommanResponse)); CurrentClient.RemoveClient(wxclient.myclient); } else { LogHelper.showLog(txtLog, (DateTime.Now.ToString("HH:mm:ss") + "向微信端" + wxclient.GetStrRemoteEndPoint + "回送失败:" + strLastCommanResponse)); //LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "向微信端" + wxclient.GetStrRemoteEndPoint + "回送失败:" + strLastCommanResponse); CurrentClient.RemoveClient(wxclient.myclient); } } catch { CurrentClient.RemoveClient(wxclient.myclient); //LogHelper.showLog(txtLog, (DateTime.Now.ToString("HH:mm:ss") + "向微信端" + wxclient.GetStrRemoteEndPoint + "回送失败:" + strLastCommanResponse)); LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "向微信端" + wxclient.GetStrRemoteEndPoint + "回送失败:" + strLastCommanResponse); } } dev.LastCommanResponse = strLastCommanResponse; bool isAdded = CurrentClient.AddClient(dev); CurrentClient.MultiSendOffLine(); } else if (e.Datagram[9] == 0x01) //心跳 { #region 将心跳信息存入数据库 byte[] byte_list = HexStringToByte(str_mac); byte[] new_byte_list = new byte[byte_list.Length]; var new_mac = string.Empty; for (int i = 0; i < byte_list.Length; i++) { byte new_byte = (byte)(byte_list[i] - 0x30); new_mac += new_byte.ToString(); } var commend = Converts.GetTPandMac(e.Datagram); LogHelper.SaveHeart(new_mac, commend); var sql = string.Format(@" declare @cou int select @cou =count(id) from WP_设备心跳记录表 where mac='{0}' if @cou =0 begin insert into WP_设备心跳记录表(mac,command) values('{0}','{1}') end else begin update WP_设备心跳记录表 set command = '{1}',createtime = getdate() where mac = '{0}' end", new_mac, commend); try { var b = DbHelperSQL.ExecuteSql(sql); } catch (Exception ex2) { LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + ex2.Message); } #endregion /* * dev.LastBeat = ByteToHexString(e.Datagram); //服务器回应心跳包 * //LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "来自设备:" + dev.GetStrRemoteEndPoint + ";收到心跳" + dev.LastBeat); * byte[] responsebeats; * string strres; * responsebeats = GetRightBeatsResponse(mac, true); * strres = ByteToHexString(responsebeats); * //LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "会送命令:" + responsebeats.ToString() + ";收到心跳" + dev.LastBeat); * if (CheckDataGram(LastRes)) * { * //LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "验证通过情况:" + responsebeats.ToString() + ";收到心跳" + dev.LastBeat); * //dev.SendMsg(responsebeats); * //LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "向" + str_mac + "发送响应心跳:" + strres); * } */ bool isAdded = CurrentClient.AddClientWithOutCmd(dev); CurrentClient.MultiSendOffLine(); } //bool isAdded = CurrentClient.AddClient(dev); //CurrentClient.MultiSendOffLine(); } } catch (Exception e2) { LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + ";" + e2.Message); } #endregion } break; //微信过来的,发命令 case 02: { //mac_list.Add(e.Datagram); //if (open) //{ //} try { #region 客户端发送的控制数据 //open = false; LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "客户端:" + e.TcpClient.Client.RemoteEndPoint.ToString() + "的原始命令:" + ByteToHexString(e.Datagram)); LogHelper.showLog(txtLog, (DateTime.Now.ToString("HH:mm:ss") + "客户端:" + e.TcpClient.Client.RemoteEndPoint.ToString() + "的原始命令:" + ByteToHexString(e.Datagram))); Array.Copy(e.Datagram, 1, mac, 0, 15); //mac_list.RemoveAt(0); str_mac = Converts.GetTPandMac(mac); LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + "MAC:" + str_mac); WeiXinClient wxclt = new WeiXinClient(); wxclt.sendnum = 0; wxclt.receivenum = 0; wxclt.myclient = e.TcpClient; wxclt.StrMac = str_mac; wxclt.command = ByteToHexString(e.Datagram); wxclt.typeid = 1; CurrentClient.AddClient(wxclt); DeviceClient dev = CurrentClient[str_mac, 2] as DeviceClient; LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + " MAC:" + str_mac); if (dev != null) { //拆命令发送 LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + " dev不为空:" + str_mac); LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + " 命令长度:" + e.Datagram.Length); byte[] box_number = new byte[e.Datagram.Length - 16]; Array.Copy(e.Datagram, 16, box_number, 0, e.Datagram.Length - 16); LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + " boxnumber:" + Converts.GetTPandMac(box_number)); int sendnum = 0; #region 一次性 byte[] byteCommand = GetSendMulty(mac, box_number); string sss = ByteToHexString(byteCommand); LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + " sss:" + sss); try { Thread.Sleep(750); LogHelper.WriteLog(DateTime.Now.ToString("HH:mm:ss") + " byteCommand:" + byteCommand); dev.SendMsg(byteCommand); sendnum++; break; } catch (Exception err) { LogHelper.WriteLog("发送指令异常:" + err.Message + "错误发生位置为" + err.StackTrace); LogHelper.showLog(txtLog, ("发送指令异常:" + err.Message + "错误发生位置为" + err.StackTrace)); } #endregion wxclt.sendnum = sendnum; CurrentClient.AddClient(wxclt); } #endregion } catch (Exception ex) { LogHelper.WriteLog("发送指令异常:" + ex.Message + "错误发生位置为" + ex.StackTrace); } } break; default: break; } Control.CheckForIllegalCrossThreadCalls = false; label1.Text = CurrentClient.GetDevCount().ToString(); label3.Text = CurrentClient.GetWeiXinNumber().ToString(); //try //{ //} //catch (Exception ex) //{ // string messages = string.Empty; // messages = "捕获异常" + ex.Message + "错误发生位置为" + ex.StackTrace; // //LogHelper.WriteLog(messages); // //LogHelper.showLog(txtLog, messages); //} //finally //{ //} } }