/// <summary> /// 发送能源数据到客户端 /// </summary> private void SendToClient_Energy(object type) { //Thread.Sleep(SendPeriod); if (server.ClientAll != null && server.ClientAll.Count == 0) { var msg = "服务端无连接,不发送"; Console.WriteLine(msg); } var lst = new List <M_BS_Realdata_Spt>(); try { lst = BLLFactory <B_BS_Realdata_Spt> .Instance.GetEnergyRecordList(DateTime.Now); if (lst == null) { return; } } catch (Exception e) { var msg = string.Format("{0} {1} {2}", DateTime.Now.ToString(BasePublic.strTimeFormat), "查询实时数据spt出错", e.Message); Console.WriteLine(string.Format(msg)); BasePublic.ExceptionDeal(BaseEnum.Socket, msg); return; } var dt = BaseFunction.CreateTable("TagID,TagValue,TagName,TagStatus,RealValue,RealTime"); DataRow dr = null; double value = 0; var status = 0; var str = ""; foreach (var item in lst) { var model_Tag = (from li in BasePublic.lstTagsEnergy where li.TagCode == item.TagID select li).FirstOrDefault(); //查找该位号的缓存数据 var mtRow = (from li in mtTable where li.Key == item.TagID select li).FirstOrDefault(); if (model_Tag == null) { continue; } //if (mtRow.Count == 0) //{ // mtRow.StartTime = DateTime.Now; //} //mtRow.Count += 1; //mtRow.NoDataCount = 0; //if (mtRow.Count >= BasePublic.OneDataCount) //{ // mtRow.Value = BasePublic.OneDataCount; // mtRow.Count = 0; // //mtRow.AlarmCount = 0; //} dr = dt.NewRow(); dr["RealTime"] = DateTime.Now.ToString(BasePublic.strTimeFormat); dr["RealValue"] = item.RealValue; dr["TagID"] = item.TagID; dr["TagName"] = model_Tag.TagName; str = ""; status = 0; var IsAlarm = false; if (model_Tag.IsAlarm) { // M_BS_TagDefine model_PreTags = // (from li in BasePublic.lstTagsAll where li.ID == model_Tag.PreTags select li).FirstOrDefault(); // if (model_PreTags == null || model_PreTags.PreTags.Trim().Length == 0) // { // IsAlarm = true; // } // else // { // var model_PreValue = // (from li in lst where li.TagID == model_PreTags.ID select li).FirstOrDefault(); // if (model_PreValue.RealValue > 0) // { // IsAlarm = true; // } // } if (IsAlarm) { if (item.RealValue > model_Tag.TagHH) { status = 1; str = string.Format("参数值({1})过高,正常值{2}({3}至{4})", model_Tag.TagName, item.RealValue, model_Tag.TagVal, model_Tag.TagLL, model_Tag.TagHH); } if (item.RealValue < model_Tag.TagLL) { status = 1; str = string.Format("参数值({1})过低,正常值{2}({3}至{4})", model_Tag.TagName, item.RealValue, model_Tag.TagVal, model_Tag.TagLL, model_Tag.TagHH); } mtRow.AlarmCount += 1; mtRow.Status = status; mtRow.Msg = str; if (mtRow.AlarmCount >= 10) { try { } catch (Exception e) { var msg = string.Format("{0} {1} {2}", DateTime.Now.ToString(BasePublic.strTimeFormat), "转化实时数据出错", e.Message); Console.WriteLine(string.Format(msg)); BasePublic.ExceptionDeal(BaseEnum.Socket, msg); } } } } //dr["TagStatus"] = status; //dr["TagValue"] = str; //dt.Rows.Add(dr); dt.Rows.Add(dr); } #region 发送到客户端 //string strJson = BaseFunction.DataTable2Json(dt); var strJson = BaseFunction.DataTableToJson(dt); var byteLen = Encoding.Default.GetByteCount(strJson); var strMsg = string.Format("\n\n{0}向端口发送【能源】数据:" + "*****************************", DateTime.Now.ToString(BasePublic.strTimeFormat)); if (strJson == null || strJson.Trim().Length == 0) { strMsg += string.Format("\n{0}字符串长度为0,数据不发送", DateTime.Now.ToString(BasePublic.strTimeFormat)); return; } if (server.ClientAll == null) { strMsg += string.Format("\n{0}", "无客户端连接,数据未发送"); Console.WriteLine(strMsg); return; } strMsg += string.Format("\n允许最大在线人数:{0}\n当前连接数为:{1}", server.ClientMax, server.ClientAll.Count); foreach (var item in server.ClientAll) { if (!server.clientCheck(item)) { strMsg += string.Format("\n客户端{0}{1}", item.Address + ":" + item.Port, "不在线"); break; } Console.WriteLine(strMsg); var sendData = Encoding.UTF8.GetBytes(strJson); var aaa = sendData[sendData.Length - 1]; if (strJson == null || strJson.Trim(']').Length < 1) { strMsg += string.Format("\n{0}字符串长度为0,客户端{1}{2}", DateTime.Now.ToString(BasePublic.strTimeFormat), item.Address + ":" + item.Port, "发送信息不成功"); return; } strMsg += string.Format("\n{0}客户端{1}{2}", DateTime.Now.ToString(BasePublic.strTimeFormat), item.Address + ":" + item.Port, "发送信息成功"); Console.WriteLine(strMsg); server.sendMessage(item, strJson); } #endregion }
/// <summary> /// 将实时数据进行处理 /// </summary> /// <param name="lst"></param> /// <returns></returns> private DataTable GetSendTable(List <M_BS_Realdata_Spt> lst, string type = "0") { var dt = BaseFunction.CreateTable("TagID,TagValue,TagName,TagStatus,RealValue,RealTime"); DataRow dr = null; double value = 0; var status = 0; var str = ""; foreach (var item in lst) { M_BS_TagDefine model_Tag = null; MTKeyValue mtRow = null; if (type == "0") { model_Tag = (from li in BasePublic.lstTagsAll where li.ID == item.TagID select li).FirstOrDefault(); //查找该位号的缓存数据 mtRow = (from li in mtTable where li.Key == item.TagID select li).FirstOrDefault(); } else { model_Tag = (from li in BasePublic.lstTagsAllNQ where li.ID == item.TagID select li).FirstOrDefault(); //查找该位号的缓存数据 mtRow = (from li in mtTableNQ where li.Key == item.TagID select li).FirstOrDefault(); } if (mtRow == null || model_Tag == null) { continue; } if (mtRow.Count == 0) { mtRow.StartTime = DateTime.Now; } mtRow.Count += 1; mtRow.NoDataCount = 0; if (mtRow.Count >= BasePublic.OneDataCount) { mtRow.Value = BasePublic.OneDataCount; mtRow.Count = 0; //mtRow.AlarmCount = 0; } dr = dt.NewRow(); dr["RealTime"] = DateTime.Now.ToString(BasePublic.strTimeFormat); dr["RealValue"] = item.RealValue; dr["TagID"] = item.TagID; dr["TagName"] = model_Tag.TagName; str = ""; status = 0; var IsAlarm = false; if (model_Tag.IsAlarm) { var model_PreTags = (from li in BasePublic.lstTagsAll where li.ID == model_Tag.PreTags select li).FirstOrDefault(); if (model_PreTags == null || model_PreTags.PreTags.Trim().Length == 0) { IsAlarm = true; } else { var model_PreValue = (from li in lst where li.TagID == model_PreTags.ID select li).FirstOrDefault(); if (model_PreValue.RealValue > 0) { IsAlarm = true; } } if (IsAlarm) { if (item.RealValue > model_Tag.TagHH) { status = 1; str = string.Format("参数值({1})过高,正常值{2}({3}至{4})", model_Tag.TagName, item.RealValue, model_Tag.TagVal, model_Tag.TagLL, model_Tag.TagHH); } if (item.RealValue < model_Tag.TagLL) { status = 1; str = string.Format("参数值({1})过低,正常值{2}({3}至{4})", model_Tag.TagName, item.RealValue, model_Tag.TagVal, model_Tag.TagLL, model_Tag.TagHH); } mtRow.AlarmCount += 1; mtRow.Status = status; mtRow.Msg = str; if (mtRow.AlarmCount >= 10) { try { } catch (Exception e) { var msg = string.Format("{0} {1} {2}", DateTime.Now.ToString(BasePublic.strTimeFormat), "转化实时数据出错", e.Message); Console.WriteLine(string.Format(msg)); BasePublic.ExceptionDeal(BaseEnum.Socket, msg); continue; } } } } dr["TagStatus"] = status; dr["TagValue"] = str; dt.Rows.Add(dr); } return(dt); }