public void SendCommand(UdpService.UdpServer US) { //lock (US.Us) foreach (var udp in US.Us)//在线 { //查询命令集中 站号、功能码 符合条件的记录 var command = from c in US.UQ.Qusd where c.STCD == udp.STCD select c; List <UdpSendData> Command = command.ToList <UdpSendData>(); if (Command.Count() > 0) { if (Command.First().STATE == 0) //0为从未发给rtu { System.Threading.Thread thread = null; try { System.Threading.Thread.Sleep(800); thread = new System.Threading.Thread(() => StartCommand(thread, US, Command.First(), Command.First().COMMANDCODE)); thread.Start(); } catch { Service.ServiceControl.log.Error(DateTime.Now + "UDP信道召测线程启动异常!"); } } } } }
/// <summary> /// 清空召测命令集中的命令 /// </summary> /// <param name="US">udp服务</param> public static void ClearUsdQ(UdpServer US) { lock (US.UQ.Qusd) { US.UQ.Qusd = new ConcurrentQueue <UdpSendData>(); } }
/// <summary> /// 从列表删除Udp对象 /// </summary> /// <param name="US">udp服务</param> /// <param name="Second">秒</param> public static void DelClosSocket(UdpServer US, int Second) { string ServiceID = US.ServiceID; List <UdpSocket> Us = US.Us; var temp = from u in Us where u.CONNECTTIME != null select u; ArrayList al = new ArrayList(); foreach (var item in temp) { DateTime dt1 = item.DATATIME.Value.AddSeconds(Second); DateTime dt2 = DateTime.Now; if (DateTime.Compare(dt1, dt2) < 0) { al.Add(item); } } foreach (UdpSocket item in al) { UdpBussiness.UdpDisconnected(US, item.IpEndPoint); item.IpEndPoint = null; item.CONNECTTIME = null; item.DATATIME = null; } al = null; }
/// <summary> /// 上线通知(数据解析时得到站号时调用) /// </summary> /// <param name="US"></param> /// <param name="STCD"></param> public static void UdpConnected(UdpServer US, string STCD) { string ServiceId = US.ServiceID; ServiceBussiness.WriteQUIM("UDP", ServiceId, STCD, "上线!", new byte[] { }, Service.ServiceEnum.EnCoderType.HEX, Service.ServiceEnum.DataType.Text); ServiceBussiness.WriteQUIM("", "", "", "stcd|" + STCD + ":udp", new byte[] { }, Service.ServiceEnum.EnCoderType.HEX, Service.ServiceEnum.DataType.State); }
public UdpThread(UdpServer Udp) { udp = Udp; //udp服务没有在线概念,所以记录 timer_SocketManager = new Timer(new TimerCallback(SocketManager), null, 5000, 5000); timer_SendData = new Timer(new TimerCallback(SendData), null, 100, 100); }
/// <summary> /// 删除列表中超时连接 /// </summary> /// <param name="US">udp服务</param> /// <param name="IpEndPoint">Udp标识</param> public static void DelSocket(UdpServer US, IPEndPoint IpEndPoint) { List <UdpSocket> Us = US.Us; var temp = from u in Us where u.IpEndPoint == IpEndPoint select u; if (temp.Count() > 0) { temp.First().IpEndPoint = null; temp.First().CONNECTTIME = null; temp.First().DATATIME = null; } }
/// <summary> /// 将回复数据放入队列 /// </summary> /// <param name="US">udp服务</param> /// <param name="STCD">测站编号</param> /// <param name="bt">数据</param> /// <param name="CommandCode">命令码</param> public static void WriteUsdQ(UdpServer US, string STCD, byte[] bt, string CommandCode) { ConcurrentQueue <UdpSendData> Qusd = US.UQ.Qusd; UdpSendData usd = new UdpSendData(); usd.Data = bt; usd.STCD = STCD; usd.COMMANDCODE = CommandCode; usd.STATE = 0; lock (Qusd) { Qusd.Enqueue(usd); } }
/// <summary> /// 将收到数据放入数据队列 /// </summary> /// <param name="US">udp服务</param> /// <param name="IpEndPoint">Udp标识</param> /// <param name="bt">数据</param> public static void WriteUrdQ(UdpServer US, IPEndPoint IpEndPoint, byte[] bt) { ConcurrentQueue <UdpReceivedData> Qurd = US.UQ.Qurd; UdpReceivedData urd = new UdpReceivedData(); urd.IpEndPoint = IpEndPoint; urd.Data = bt; if (bt.Length > 0) { lock (Qurd) { Qurd.Enqueue(urd); } } }
/// <summary> /// 删除列表中超时连接 /// </summary> /// <param name="US">udp服务</param> /// <param name="IpEndPoint">Udp标识</param> public static void DelSocket(UdpServer US, IPEndPoint IpEndPoint) { List <UdpSocket> Us = US.Us; lock (Us) { var temp = from u in Us where u.IpEndPoint == IpEndPoint select u; foreach (var item in temp) { Us.Remove(item); break; } } }
/// <summary> /// 移除召测命令集中的命令 /// </summary> /// <param name="US">udp服务</param> /// <param name="STCD">站号</param> /// <param name="CommandCode">命令码</param> public static void RemoveUsdQ(UdpServer US, string STCD, string CommandCode) { ConcurrentQueue <UdpSendData> Qusd = US.UQ.Qusd; lock (Qusd) { UdpSendData usd = null; if (Qusd.TryDequeue(out usd)) { if (usd.STCD != STCD || usd.COMMANDCODE != CommandCode) { Qusd.Enqueue(usd); } } } }
/// <summary> /// 移除召测命令集中的命令 /// </summary> /// <param name="US">udp服务</param> /// <param name="STCD">站号</param> /// <param name="CommandCode">命令码</param> public static void RemoveUsdQ(UdpServer US, string STCD, string CommandCode) { lock (US.UQ.Qusd) { for (int i = 0; i < US.UQ.Qusd.Count; i++) { UdpSendData usd = null; if (US.UQ.Qusd.TryDequeue(out usd)) { if (usd.STCD != STCD || usd.COMMANDCODE != CommandCode) { US.UQ.Qusd.Enqueue(usd); } } } } }
/// <summary> /// 添加Udp对象 /// </summary> /// <param name="US">udp服务</param> /// <param name="IpEndPoint">Udp标识</param> public static void AddSocket(UdpServer US, IPEndPoint IpEndPoint) { List <UdpSocket> Us = US.Us; lock (Us) { var temp = from u in Us where u.IpEndPoint.Address.ToString() == IpEndPoint.Address.ToString() && u.IpEndPoint.Port == IpEndPoint.Port select u; int count = temp.Count <UdpSocket>(); if (count == 0) { //添加 UdpSocket us = new UdpSocket(); us.CONNECTTIME = DateTime.Now; us.IpEndPoint = IpEndPoint; Us.Add(us); } } }
/// <summary> /// 掉线通知 /// </summary> /// <param name="US">udp服务</param> /// <param name="IpEndPoint">Udp标识</param> public static void UdpDisconnected(UdpServer US, IPEndPoint IpEndPoint) { string ServiceId = US.ServiceID; List <UdpSocket> Us = US.Us; var temp = from u in Us where u.IpEndPoint == IpEndPoint select u; if (temp.Count() > 0) { if (temp.First().STCD != null && temp.First().STCD != "") { ServiceBussiness.WriteQUIM("UDP", ServiceId, temp.First().STCD, "下线!", new byte[] { }, ServiceBussiness.EnCoderType.HEX, ServiceBussiness.DataType.Text); } else { ServiceBussiness.WriteQUIM("UDP", ServiceId, IpEndPoint.Address.ToString() + ":" + IpEndPoint.Port, "下线!", new byte[] { }, ServiceBussiness.EnCoderType.HEX, ServiceBussiness.DataType.Text); } } }
/// <summary> /// 从回复队列中回复数据(未使用) /// </summary> /// <param name="US">udp服务</param> public static void SendData(UdpServer US) { string ServiceId = US.ServiceID; ConcurrentQueue <UdpSendData> Qusd = US.UQ.Qusd; List <UdpSocket> Us = US.Us; UdpClient udpclient; lock (Qusd) { int k = Qusd.Count; while (Qusd.Count > 0) { UdpSendData us = null; Qusd.TryDequeue(out us); if (us != null) { lock (Us) { var temp = from u in Us where us.STCD == u.STCD select u; if (temp.Count() > 0) { udpclient = new UdpClient(US.PORT); //()绑定指定端口 udpclient.Connect(temp.First().IpEndPoint); udpclient.Send(us.Data, us.Data.Length); udpclient.Close(); //ServiceBussiness.WriteQSM(ServiceId, temp.First().STCD, "回复数据", us.Data); } else { Qusd.Enqueue(us); } } k--; if (k <= 0) { return; } } } } }
/// <summary> /// 添加Udp对象 /// </summary> /// <param name="US">udp服务</param> /// <param name="IpEndPoint">Udp标识</param> //public static void AddSocket(UdpServer US, IPEndPoint IpEndPoint) //{ // List<UdpSocket> Us = US.Us; // lock (Us) // { // var temp = from u in Us where u.IpEndPoint.Address.ToString() == IpEndPoint.Address.ToString() && u.IpEndPoint.Port ==IpEndPoint.Port select u; // int count = temp.Count<UdpSocket>(); // if (count == 0) // { // //添加 // UdpSocket us = new UdpSocket(); // us.CONNECTTIME = DateTime.Now; // us.IpEndPoint = IpEndPoint; // Us.Add(us); // } // } //} /// <summary> /// 更新Udp对象 /// </summary> /// <param name="US">udp服务</param> /// <param name="IpEndPoint">Udp标识</param> /// <param name="STCD">测站编号</param> /// <param name="B">上线标识</param> //public static void UpdSocket(UdpServer US, IPEndPoint IpEndPoint, string STCD, out bool B) //{ // List<UdpSocket> Us = US.Us; // B = false; // lock (Us) // { // var temp = from u in Us where u.STCD == STCD select u; // List<UdpSocket> TEMP = temp.ToList<UdpSocket>(); // int count = temp.Count<UdpSocket>(); // if (count == 0) // { // temp = from u in Us where u.IpEndPoint.Address.ToString() == IpEndPoint.Address.ToString() && u.IpEndPoint.Port ==IpEndPoint.Port select u; // TEMP = temp.ToList<UdpSocket>(); // count = TEMP.Count<UdpSocket>(); // if (count == 0) // { AddSocket(US, IpEndPoint); } // else // { // if (STCD != null) // { // TEMP.First().STCD = STCD; // B = true; // } // TEMP.First().DATATIME = DateTime.Now; // TEMP.First().IpEndPoint = IpEndPoint; // } // } // else // { // //更新 // TEMP.First().DATATIME = DateTime.Now; // TEMP.First().IpEndPoint = IpEndPoint; // temp = from u in Us where u.STCD == null && u.IpEndPoint.Address.ToString() == IpEndPoint.Address.ToString() && u.IpEndPoint.Port == IpEndPoint.Port select u; // TEMP = temp.ToList<UdpSocket>(); // count = TEMP.Count<UdpSocket>(); // if (count > 0) // Us.Remove(TEMP.First()); // } // } //} public static void UpdSocket(UdpServer US, IPEndPoint IpEndPoint, string STCD, out bool B) { List <UdpSocket> Us = US.Us; B = false; var temp = from u in Us where u.STCD == STCD select u; if (temp.Count() > 0) { if (temp.First().CONNECTTIME != null) { B = true; } else { temp.First().CONNECTTIME = DateTime.Now; } temp.First().IpEndPoint = IpEndPoint; temp.First().DATATIME = DateTime.Now; } }
/// <summary> /// 掉线通知 /// </summary> /// <param name="US">udp服务</param> /// <param name="IpEndPoint">Udp标识</param> public static void UdpDisconnected(UdpServer US, IPEndPoint IpEndPoint) { string ServiceId = US.ServiceID; List <UdpSocket> Us = US.Us; var temp = from u in Us where u.IpEndPoint == IpEndPoint select u; List <UdpSocket> TEMP = temp.ToList <UdpSocket>(); if (TEMP.Count() > 0) { if (TEMP.First().STCD != null && TEMP.First().STCD != "") { ServiceBussiness.WriteQUIM("UDP", ServiceId, TEMP.First().STCD, "下线!", new byte[] { }, Service.ServiceEnum.EnCoderType.HEX, Service.ServiceEnum.DataType.Text); ServiceBussiness.WriteQUIM("", "", "", "stcd|" + TEMP.First().STCD + ":udp:", new byte[] { }, Service.ServiceEnum.EnCoderType.HEX, Service.ServiceEnum.DataType.State); } else { ServiceBussiness.WriteQUIM("UDP", ServiceId, IpEndPoint.Address.ToString() + ":" + IpEndPoint.Port, "断开连接!", new byte[] { }, Service.ServiceEnum.EnCoderType.HEX, Service.ServiceEnum.DataType.Text); } } }
/// <summary> /// 从列表删除Udp对象 /// </summary> /// <param name="US">udp服务</param> /// <param name="Minute">分钟</param> public static void DelClosSocket(UdpServer US, int Minute) { string ServiceID = US.ServiceID; List <UdpSocket> Us = US.Us; lock (Us) { ArrayList al = new ArrayList(); foreach (UdpSocket item in Us) { if (item.DATATIME.ToString("yyyy-MM-dd HH:mm:ss") != "0001-01-01 00:00:00") { DateTime dt1 = item.DATATIME.AddSeconds(Minute); DateTime dt2 = DateTime.Now; if (DateTime.Compare(dt1, dt2) < 0) { al.Add(item); } } else { DateTime dt1 = item.CONNECTTIME.AddSeconds(Minute); DateTime dt2 = DateTime.Now; if (DateTime.Compare(dt1, dt2) < 0) { al.Add(item); } } } foreach (UdpSocket item in al) { UdpBussiness.UdpDisconnected(US, item.IpEndPoint); Us.Remove(item); } al = null; } }
/// <summary> /// 将回复数据放入队列(每个测站同一命令只允许有1条,默认为最后一条) /// </summary> /// <param name="US">udp服务</param> /// <param name="STCD">测站编号</param> /// <param name="bt">数据</param> /// <param name="CommandCode">命令码</param> public static void WriteUsdQ(UdpServer US, string STCD, byte[] bt, string CommandCode) { ConcurrentQueue <UdpSendData> Qusd = US.UQ.Qusd; var qusd = from u in Qusd where u.STCD == STCD && u.COMMANDCODE == CommandCode select u; List <UdpSendData> QUSD = qusd.ToList <UdpSendData>(); lock (Qusd) if (QUSD.Count() > 0) { QUSD.First().Data = bt; QUSD.First().STATE = 0; } else { UdpSendData usd = new UdpSendData(); usd.Data = bt; usd.STCD = STCD; usd.COMMANDCODE = CommandCode; usd.STATE = 0; Qusd.Enqueue(usd); } }
/// <summary> /// 更新Udp对象 /// </summary> /// <param name="US">udp服务</param> /// <param name="IpEndPoint">Udp标识</param> /// <param name="STCD">测站编号</param> /// <param name="B">上线标识</param> public static void UpdSocket(UdpServer US, IPEndPoint IpEndPoint, string STCD, out bool B) { List <UdpSocket> Us = US.Us; B = false; lock (Us) { var temp = from u in Us where u.STCD == STCD select u; int count = temp.Count <UdpSocket>(); if (count == 0) { temp = from u in Us where u.IpEndPoint.Address.ToString() == IpEndPoint.Address.ToString() && u.IpEndPoint.Port == IpEndPoint.Port select u; count = temp.Count <UdpSocket>(); if (count == 0) { AddSocket(US, IpEndPoint); } else { if (STCD != null) { temp.First().STCD = STCD; B = true; } temp.First().DATATIME = DateTime.Now; temp.First().IpEndPoint = IpEndPoint; } } else { //更新 temp.First().DATATIME = DateTime.Now; temp.First().IpEndPoint = IpEndPoint; } } }
/// <summary> /// 接收数据时收到未设置测站信息,自动将测站信息入库并在列表中添加 /// </summary> /// <param name="STCD">站号</param> /// <param name="NFOINDEX">信道类型</param> /// <param name="Server">服务</param> public void InsertNewSTCD(string STCD, Service.ServiceEnum.NFOINDEX NFOINDEX, object Server) { if (STCD.Length == 0) { return; } var rtu = from r in Service.ServiceBussiness.RtuList where r.STCD == STCD select r; if (rtu.Count() == 0) { Service.Model.YY_RTU_Basic model = new Service.Model.YY_RTU_Basic(); model.STCD = STCD; model.PassWord = "******"; model.NiceName = STCD; bool b = PublicBD.db.AddRTU(model); //添加 if (b) { Service.ServiceBussiness.RtuList.Add(new Service.Model.YY_RTU_Basic() { STCD = STCD, NiceName = STCD, PassWord = "******" }); } } if (NFOINDEX == Service.ServiceEnum.NFOINDEX.UDP) { UdpService.UdpServer US = Server as UdpService.UdpServer; List <UdpService.UdpSocket> Us = US.Us; var udps = from u in Us where u.STCD == STCD select u; if (udps.Count() == 0) { UdpSocket us = new UdpSocket() { STCD = STCD }; Us.Add(us); } } else if (NFOINDEX == Service.ServiceEnum.NFOINDEX.TCP) { TcpService.TcpServer TS = Server as TcpService.TcpServer; List <TcpService.TcpSocket> Ts = TS.Ts; var tcps = from t in Ts where t.STCD == STCD select t; if (tcps.Count() == 0) { TcpSocket ts = new TcpSocket() { STCD = STCD }; Ts.Add(ts); } } else if (NFOINDEX == Service.ServiceEnum.NFOINDEX.GSM) { if (STCD != null || STCD != "") { GsmService.GsmServer GS = Server as GsmService.GsmServer; List <GsmService.GsmMobile> Gs = GS.Gs; var gsms = from g in Gs where g.STCD == STCD select g; if (gsms.Count() == 0) { GsmMobile gs = new GsmMobile() { STCD = STCD }; Gs.Add(gs); } } } //else if (NFOINDEX == Service.ServiceEnum.NFOINDEX.COM) //{ // ComService.ComServer CS = Server as ComService.ComServer; // List<ComService.ComSatellite> Cs = CS.Cs; // var coms = from g in Cs where g.STCD == STCD select g; // if (coms.Count() == 0) // { // } //} }
public void PacketArrived(UdpService.UdpServer US) { //throw new NotImplementedException(); }
//udp发送命令逻辑执行 void StartCommand(System.Threading.Thread thread, UdpService.UdpServer US, UdpService.UdpSendData usd, string COMMANDCODE) { try { #region int count = 2; while (count < 3) { DateTime datetime = DateTime.Now; //判断命令列表里面有没有命令 var command = from com in US.UQ.Qusd where com.STCD == usd.STCD && COMMANDCODE == com.COMMANDCODE select com; List <UdpSendData> Command = command.ToList <UdpSendData>(); //判断有没有在线设备 var udp = from u in US.Us where u.STCD == usd.STCD && u.IpEndPoint != null select u; List <UdpSocket> UDP = udp.ToList <UdpSocket>(); if (Command.Count() > 0 && UDP.Count() > 0) //是否有 { string STCD = Command.First().STCD; byte[] Data = Command.First().Data; //Data ASC string data = Encoding.ASCII.GetString(Data); Command.First().STATE = count + 1; UDP.First().DATATIME = datetime;//更新为当前时间 US.UDPClient.Send(Command.First().Data, Command.First().Data.Length, UDP.First().IpEndPoint); //通知界面 ServiceBussiness.WriteQUIM(Service.ServiceEnum.NFOINDEX.UDP.ToString(), US.ServiceID, STCD, "发送召测命令", Data, Service.ServiceEnum.EnCoderType.ASCII, Service.ServiceEnum.DataType.Text); //写入本地命令列表 ServiceBussiness.WriteListCommand(STCD, Service.ServiceEnum.NFOINDEX.UDP, COMMANDCODE, data, datetime, count + 1); //单条命令状态发送到界面程序 ServiceBussiness.CommandWriteQUIM(STCD, Service.ServiceEnum.NFOINDEX.UDP.ToString(), COMMANDCODE, data, datetime, count + 1); System.Threading.Thread.Sleep(60 * 1000); //可设置 if (count >= 2) //三次后超时 { datetime = DateTime.Now; //再判断一次是否列表中还存在(有回复时,会从列表中将命令删除) command = from com in US.UQ.Qusd where com.STCD == usd.STCD && usd.COMMANDCODE == com.COMMANDCODE select com; Command = command.ToList <UdpSendData>(); if (Command.Count() > 0) { foreach (var item in ServiceControl.udp) { UdpService.UdpBussiness.RemoveUsdQ(item, STCD, COMMANDCODE); } //写入命令列表 状态-1超时 ServiceBussiness.WriteListCommand(STCD, Service.ServiceEnum.NFOINDEX.UDP, COMMANDCODE, data, datetime, -1); //单条命令状态发送到界面程序 ServiceBussiness.CommandWriteQUIM(STCD, Service.ServiceEnum.NFOINDEX.UDP.ToString(), COMMANDCODE, data, datetime, -1); //System.Threading.Thread.Sleep(30 * 1000); //datetime = DateTime.Now; //命令本地列表中删除超时记录 List <Command> lc = ServiceBussiness.RemoveListCommand(STCD, Service.ServiceEnum.NFOINDEX.UDP, COMMANDCODE, -1); foreach (var item in lc) { PublicBD.db.AddDataCommand(item.STCD, item.CommandID, item.DATETIME, datetime, data, (int)Service.ServiceEnum.NFOINDEX.UDP, -1); } } } } else { return; } count++; } #endregion } catch (Exception ex) { Service.ServiceControl.log.Error(DateTime.Now + "udp发送命令逻辑执行异常!", ex); } }
/// <summary> /// 解析数据包 /// </summary> /// <param name="US">udp服务</param> public static void ResolvePacket(UdpServer US) { Reflection.PacketArrived(US); }
/// <summary> /// 发送命令方法含试发三次的业务逻辑 /// </summary> /// <param name="US">udp服务</param> public static void SendCommand(UdpServer US) { Reflection.SendCommand(US); }
public void SendCommand(UdpService.UdpServer US) { //throw new NotImplementedException(); }
public void PacketArrived(UdpService.UdpServer US) { string ServiceId = US.ServiceID; ConcurrentQueue <UdpReceivedData> Qurd = US.UQ.Qurd; List <UdpSocket> Us = US.Us; ConcurrentQueue <UdpSendData> Qusd = US.UQ.Qusd; while (Qurd.Count > 0) { UdpReceivedData urd = null; Qurd.TryDequeue(out urd); if (urd != null) { try { //注册&透传 Service.ServiceBussiness.RemoteCommand(urd.Data); //Service.ServiceBussiness.Registered30(urd.Data); //ASCII To String string STR = Encoding.ASCII.GetString(urd.Data); List <string> list = pd.SubPackage(STR); if (list.Count > 0) { bool PkV = true; foreach (var item in list) { PkV = pd.PacketValidate(item); if (PkV) { string STCD = pd.GetCode(item); InsertNewSTCD(STCD, Service.ServiceEnum.NFOINDEX.UDP, US); bool B = false; //更新socket列表的stcd、socket UdpBussiness.UpdSocket(US, urd.IpEndPoint, STCD, out B); if (!B) { //上线 UdpBussiness.UdpConnected(US, STCD); } //通知界面 ServiceBussiness.WriteQUIM("UDP", ServiceId, STCD, "接收数据", urd.Data, Service.ServiceEnum.EnCoderType.ASCII, Service.ServiceEnum.DataType.Text); PacketArrived(item, ServiceEnum.NFOINDEX.UDP, US); } } } else { ServiceBussiness.WriteQUIM("UDP", ServiceId, "", "接收异常数据", urd.Data, Service.ServiceEnum.EnCoderType.ASCII, Service.ServiceEnum.DataType.Text); } } catch (Exception ex) { //通知界面 ServiceBussiness.WriteQUIM("UDP", ServiceId, "", "接收异常数据", urd.Data, Service.ServiceEnum.EnCoderType.ASCII, Service.ServiceEnum.DataType.Text); log.Error(DateTime.Now + "包处理操作异常" + ex.ToString()); } } } }
/// <summary> /// 上线通知 /// </summary> /// <param name="US">udp服务</param> /// <param name="IpEndPoint">Udp标识</param> public static void UdpConnected(UdpServer US, IPEndPoint IpEndPoint) { string ServiceId = US.ServiceID; ServiceBussiness.WriteQUIM("UDP", ServiceId, IpEndPoint.Address.ToString() + ":" + IpEndPoint.Port, "上线!", new byte[] { }, ServiceBussiness.EnCoderType.HEX, ServiceBussiness.DataType.Text); }