/// <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="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> /// 从回复队列中回复数据(未使用) /// </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> /// 将回复数据放入队列(每个测站同一命令只允许有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); } }
//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); } }