/// <summary>发送告警</summary> /// <param name="groupName">告警组</param> /// <param name="webhook">告警Url</param> /// <param name="title">告警标题</param> /// <param name="message">告警消息</param> public static void SendAlarm(String groupName, String webhook, String title, String message) { XTrace.WriteLine(message); var group = AlarmGroup.FindByName(groupName); var hi = new AlarmHistory { GroupId = group?.Id ?? 0, Name = groupName, Success = true, Action = title, Content = message, Creator = Environment.MachineName, }; try { if (webhook.IsNullOrEmpty() && group != null && group.Enable) { webhook = group?.WebHook; } if (webhook.IsNullOrEmpty()) { throw new InvalidOperationException("未设置或未启用告警地址!"); } if (webhook.Contains("qyapi.weixin")) { hi.Category = "QyWeixin"; var weixin = new WeiXinClient { Url = webhook }; using var span = DefaultTracer.Instance?.NewSpan("SendWeixin", message); weixin.SendMarkDown(message); } else if (webhook.Contains("dingtalk")) { hi.Category = "DingTalk"; var dingTalk = new DingTalkClient { Url = webhook }; using var span = DefaultTracer.Instance?.NewSpan("SendDingTalk", message); dingTalk.SendMarkDown(title, message, null); } } catch (Exception ex) { hi.Success = false; hi.Content += Environment.NewLine + ex.ToString(); } hi.SaveAsync(); }
private AuthorizeState WebAppRequestAuthentication() { var state = Guid.NewGuid().ToString("N"); Session["nop.externalauth.weixin.authentication.web"] = state; var authUrl = WeiXinClient.GenerateWebLoginRequestUrl(_weiXinExternalAuthSettings.WebAppId, state, GenerateLocalCallbackUri().AbsoluteUri).AbsoluteUri; return(new AuthorizeState("", OpenAuthenticationStatus.RequiresRedirect) { Result = new RedirectResult(authUrl) }); }
public void RemoveClient(TcpClient tcpclt) { try { WeiXinClient tmpwx = new WeiXinClient(); foreach (WeiXinClient wx in LstWeiXin) { if (wx.myclient.Equals(tcpclt)) { tmpwx = wx; } } if (LstWeiXin.Contains(tmpwx)) { string endpoint = tmpwx.GetStrRemoteEndPoint; tmpwx.myclient.ShutDownAndClose(); LstWeiXin.Remove(tmpwx); //LogHelper.WriteLog("微信端" + DateTime.Now.ToString("HH:mm:ss") + endpoint + "-->断开连接"); } DeviceClient tmpdev = new DeviceClient(); foreach (DeviceClient dev in LstDev) { if (dev.myclient.Equals(tcpclt)) { tmpdev = dev; } } if (LstDev.Contains(tmpdev)) { //再此判断,满足条件再移除,无论在哪调用都要判断 TimeSpan tspan = new TimeSpan(); tspan = DateTime.Now - tmpdev.LastTime; if (tspan.TotalSeconds > 600) { string endpoint = tmpwx.GetStrRemoteEndPoint; //LogHelper.WriteLog("MAC地址为" + tmpdev.StrMac + "的设备于" + DateTime.Now.ToString("HH:mm:ss") + endpoint + "-->断开连接"); //tmpdev.SendOffLineMsg();//检测到断开连接,发送离线 tmpdev.myclient.ShutDownAndClose(); LstDev.Remove(tmpdev); } } } catch (Exception e) { LogHelper.WriteLog("程序错误" + e.Message + ";" + e.StackTrace); } }
/// <summary> /// 传入mac与类型,得到链接信息.传说中的索引器 /// </summary> /// <param name="strmac"></param> /// <param name="type">1代表微信端,2代表设备</param> /// <returns></returns> public ClientInfo this[string strmac, int type] { get { if (type == 1) { WeiXinClient tmpwx = null; foreach (WeiXinClient wx in LstWeiXin) { if (wx.StrMac.Equals(strmac)) { tmpwx = wx; break; } else { continue; } } return(tmpwx); } else { DeviceClient tmpwx = null; foreach (DeviceClient wx in LstDev) { if (wx.StrMac.Equals(strmac)) { tmpwx = wx; break; } else { continue; } } return(tmpwx); } } }
public bool AddClientWithOutCmd(ClientInfo cltinfo) { bool isAdd = false; if (cltinfo is WeiXinClient) { WeiXinClient cltwx = (WeiXinClient)cltinfo; bool exist = false; foreach (WeiXinClient wx in LstWeiXin) { try { bool isneedupdate = false; if (wx.StrMac.Equals(cltwx.StrMac)) { if (wx.myclient == null || !wx.myclient.Connected) { isneedupdate = true; } if (!wx.GetStrRemoteEndPoint.Equals(cltwx.GetStrRemoteEndPoint)) { isneedupdate = true; } if (isneedupdate) { wx.myclient = cltwx.myclient; //LogHelper.WriteLog("Client Update [TCP-MAC],Now is " + wx.StrMac + "--" + wx.GetStrRemoteEndPoint); } exist = true; } } catch (Exception ex) { string messages = string.Empty; messages = "捕获异常" + ex.Message + "错误发生位置为" + ex.StackTrace; //LogHelper.WriteLog(messages); } } if (!exist) { //应该在服务器第一次收到命令时加入 isAdd = true; //LogHelper.WriteLog("Client ADD [TCP-MAC],Now is " + cltwx.StrMac + "--" + cltwx.GetStrRemoteEndPoint); LstWeiXin.Add(cltwx); } } else { if (cltinfo is DeviceClient) { DeviceClient cltdc = (DeviceClient)cltinfo;//可以转换的 bool exist = false; foreach (DeviceClient dc in LstDev) { try { bool isneedupdate = false; if (dc.StrMac.Equals(cltdc.StrMac)) { if (dc.myclient == null || !dc.myclient.Connected) { isneedupdate = true; } if (!dc.GetStrRemoteEndPoint.Equals(cltdc.GetStrRemoteEndPoint)) { isneedupdate = true; } if (isneedupdate) { dc.myclient = cltdc.myclient; dc.LastBeat = cltdc.LastBeat; dc.LastTime = cltdc.LastTime; dc.LastCommanResponse = cltdc.LastCommanResponse; dc.BoxState = cltdc.BoxState; dc.SendONLineMsg(); //LogHelper.WriteLog("Device Update [TCP-MAC],Now is " + dc.StrMac + "--" + dc.GetStrRemoteEndPoint); } exist = true; } } catch (Exception ex) { string messages = string.Empty; messages = "捕获异常" + ex.Message + "错误发生位置为" + ex.StackTrace; //LogHelper.WriteLog(messages); } } if (!exist) { isAdd = true; LstDev.Add(cltdc); LogHelper.WriteLog("Device ADD [TCP-MAC],Now is " + cltdc.StrMac + "--" + cltdc.GetStrRemoteEndPoint); cltdc.SendONLineMsg(); //通知所有客户端有设备连接上来 //try //{ // server.SendAll(cltdc.StrMac + ";" + cltdc.GetStrRemoteEndPoint); //} //catch (Exception ex) //{ // string messages = string.Empty; // messages = "捕获异常" + ex.Message + "错误发生位置为" + ex.StackTrace; // //LogHelper.WriteLog(messages); //} } } } return(isAdd); }
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 //{ //} } }