예제 #1
0
        /// <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
        }
예제 #2
0
        /// <summary>
        ///     发送至客户端
        /// </summary>
        private void SendToClient(object ob)
        {
            object[]       obs    = ob as object[];
            string         type   = "";
            M_MT_TagDefine info   = null;
            string         strIds = "";

            if (obs == null)
            {
                return;
            }

            if (obs.Length == 1)
            {
                type = obs[0].ToString();
            }
            if (obs.Length == 2)
            {
                type = obs[0].ToString();
                info = obs[1] as M_MT_TagDefine;
            }
            lock (BasePublic.lockerConfig)
            {
                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.GetNewList(type, info);

                    if (lst == null)
                    {
                        return;
                    }
                    if (type.ToString() != "0")
                    {
                        //lst=(from li in lst where li.TagCode=="Epi" select li).ToList();
                    }
                }
                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;
                }

                DataTable dt = GetSendTable(lst, type);

                #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 (type != "0")
                {
                    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
            }
        }