/// <summary> /// 返回发送字符串 /// </summary> /// <param name="jkdid">监控点ID</param> /// <returns></returns> private string GetSendStr(string jkdid) { var senStr = $"{Properties.Settings.Default.SendStr},{jkdid}"; if (Properties.Settings.Default.Crypt) { senStr = DataPacketCodec.Encode(senStr, Properties.Settings.Default.CryptKey) + "#"; return senStr; } return senStr + "#"; }
private void StartMethod() { Parallel.ForEach(SoureJkds, item => { Task.Factory.StartNew(async delegate { var client = new EasyClient(); int retryCount = 0; /*** * 初始化socket连接, 接受返回数据处理 * HxReceiveFilter为自定义的协议 * ***/ client.Initialize(new HxReceiveFilter(), request => { try { item.CURR_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); item.JKD_VALUE = request.Key; if (request.Key.Equals("#")) { retryCount++; } else { retryCount = 0; } if (retryCount <= Settings.RetryCount && retryCount != 0) { item.REDIS_SAVE = bool.FalseString; return; } using (var redis = new RedisClient(Settings.RedisIp, Settings.RedisPort, Settings.RedisPw)) { try { item.REDIS_SAVE = redis.Set(item.JKD_ID, item.JKD_VALUE).ToString(); } catch (Exception e) { item.REDIS_SAVE = bool.FalseString; LogHelper.WriteLog(e.Source, e); } } } catch (Exception ex) { LogHelper.WriteLog(ex.Source, ex); } }); // Connect to the server await client.ConnectAsync(new IPEndPoint(IPAddress.Parse(Settings.RmiIp), Settings.RmiPort)); while (_taskFlag) { try { if (client.IsConnected) { //获取发送字符串 var enStr = DataPacketCodec.Encode($"rij,{item.JKD_ID}", Settings.CryptKey) + "#"; // Send data to the server client.Send(Encoding.UTF8.GetBytes(enStr)); } else { await client.ConnectAsync(new IPEndPoint(IPAddress.Parse(Settings.RmiIp), Settings.RmiPort)); } } catch (Exception ex) { LogHelper.WriteLog(ex.Source, ex); // reconnet await client.ConnectAsync(new IPEndPoint(IPAddress.Parse(Settings.RmiIp), Settings.RmiPort)); } } finally { await Task.Delay(3000); } await client.Close(); });