public HaiKangYongChuanModel Analyze(string codeStr)
        {
            List <string>         codelist = codeStr.ToArray().Select(m => m.ToString()).ToList <string>();
            HaiKangYongChuanModel model    = new HaiKangYongChuanModel();

            model.StartCode   = new string[] { codelist.TakeCode(1 * baseCount), codelist.TakeCode(1 * baseCount) };
            model.LiuShui     = new string[] { codelist.TakeCode(1 * baseCount), codelist.TakeCode(1 * baseCount) };
            model.XieYiBanBen = new string[] { codelist.TakeCode(1 * baseCount), codelist.TakeCode(1 * baseCount) };
            model.ShiJian     = new string[] { codelist.TakeCode(1 * baseCount), codelist.TakeCode(1 * baseCount), codelist.TakeCode(1 * baseCount), codelist.TakeCode(1 * baseCount), codelist.TakeCode(1 * baseCount), codelist.TakeCode(1 * baseCount) };
            model.YuanDiZhi   = new string[] { codelist.TakeCode(1 * baseCount), codelist.TakeCode(1 * baseCount), codelist.TakeCode(1 * baseCount), codelist.TakeCode(1 * baseCount), codelist.TakeCode(1 * baseCount), codelist.TakeCode(1 * baseCount) };
            model.MuDiDiZhi   = new string[] { codelist.TakeCode(1 * baseCount), codelist.TakeCode(1 * baseCount), codelist.TakeCode(1 * baseCount), codelist.TakeCode(1 * baseCount), codelist.TakeCode(1 * baseCount), codelist.TakeCode(1 * baseCount) };
            string low   = codelist.TakeCode(1 * baseCount);
            string heigh = codelist.TakeCode(1 * baseCount);

            model.DataLength    = int.Parse(StringHelper.Convert16To10(heigh + low));//
            model.MingLingZiJie = new string[] { codelist.TakeCode(1 * baseCount) };
            model.DataContent   = codelist.TakeCode(model.DataLength * baseCount);
            model.CRC_16        = new string[] { codelist.TakeCode(1 * baseCount) };
            model.EndCode       = new string[] { codelist.TakeCode(1 * baseCount), codelist.TakeCode(1 * baseCount) };

            AnalyzeDataContent(model);
            return(model);
        }
        public void AnalyzeDataContent(HaiKangYongChuanModel model)
        {
            string code = model.DataContent;

            if (code.Length > 0)
            {
                List <string> codelist = code.ToArray().Select(m => m.ToString()).ToList <string>();
                //类型标识
                string LeiXingBiaoZhi = StringHelper.Convert16To10(codelist.TakeCode(1 * baseCount));
                //信息对象数目
                int bodyCount = int.Parse(StringHelper.Convert16To10(codelist.TakeCode(1 * baseCount)));
                model.Content_Information.LeiXingBiaoZhi = LeiXingBiaoZhi;
                model.Content_Information.DuiXiangShuMu  = bodyCount;
                switch (model.Content_Information.LeiXingBiaoZhi)
                {
                case "1":
                {
                    //上传建筑消防设施系统状态
                    for (int i = 0; i < bodyCount; i++)
                    {
                        Content_Object contentObject = new Content_Object();
                        contentObject.Content_Body    = AnalyzeDataContent_1(codelist.TakeCode(4 * baseCount));
                        contentObject.ShiJianBiaoQian = codelist.TakeCode(6 * baseCount);
                        model.Content_Object.Add(contentObject);
                    }
                }
                break;

                case "2":
                {
                    //上传 建筑消防设施 部件 运行状态
                    for (int i = 0; i < bodyCount; i++)
                    {
                        Content_Object contentObject = new Content_Object();
                        contentObject.Content_Body    = AnalyzeDataContent_2(codelist.TakeCode(40 * baseCount));
                        contentObject.ShiJianBiaoQian = codelist.TakeCode(6 * baseCount);
                        model.Content_Object.Add(contentObject);
                    }
                }
                break;

                case "21":
                {
                    //上传用户信息传输装置 运行状态
                    for (int i = 0; i < bodyCount; i++)
                    {
                        Content_Object contentObject = new Content_Object();
                        contentObject.Content_Body    = AnalyzeDataContent_21(codelist.TakeCode(1 * baseCount));
                        contentObject.ShiJianBiaoQian = codelist.TakeCode(6 * baseCount);
                        model.Content_Object.Add(contentObject);
                    }
                }
                break;

                case "24":
                {
                    //上传用户信息传输装置操作信息
                    for (int i = 0; i < bodyCount; i++)
                    {
                        Content_Object contentObject = new Content_Object();
                        contentObject.Content_Body    = AnalyzeDataContent_24(codelist.TakeCode(2 * baseCount));
                        contentObject.ShiJianBiaoQian = codelist.TakeCode(6 * baseCount);
                        model.Content_Object.Add(contentObject);
                    }
                }
                break;

                case "25":
                {
                    //上传用户信息传输装置软件版本
                    for (int i = 0; i < bodyCount; i++)
                    {
                        Content_Object contentObject = new Content_Object();
                        contentObject.Content_Body = AnalyzeDataContent_25(codelist.TakeCode(2 * baseCount));
                        //contentObject.ShiJianBiaoQian = codelist.TakeCode(6 * baseCount);
                        model.Content_Object.Add(contentObject);
                    }
                }
                break;

                case "90":
                {
                    //同步 用户 信息传输 装置时钟
                }
                break;

                case "91":
                {
                    //查岗命令
                }
                break;

                default: break;
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 响应接收的消息
        /// </summary>
        /// <param name="reciveClient"></param>
        private void ReceiveMsgFromClient(object reciveClient)
        {
            TcpClient client = reciveClient as TcpClient;

            if (client == null)
            {
                Invoke(myD_ShowMessage, $"client error");
                return;
            }
            while (true)
            {
                try
                {
                    NetworkStream stream = client.GetStream();
                    int           num    = stream.Read(bytes, 0, bytes.Length); //将数据读到result中,并返回字符长度                  
                    if (num != 0)
                    {
                        string str = StringHelper.byteToHexStr(bytes, num);

                        FileHelper.OpenWrite($@"{Application.StartupPath}//log//{DateTime.Now.ToString("yyyyMMddHH")}.txt", $@"收到报文{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:{str}");
                        //string str = Encoding.UTF8.GetString(bytes, 0, num);//把字节数组中流存储的数据以字符串方式赋值给str
                        //在服务器显示收到的数据
                        Invoke(myD_ShowMessage, "From: " + client.Client.RemoteEndPoint.ToString() + " : " + str);
                        //解析收到的数据
                        HaiKangYongChuanServer server   = new HaiKangYongChuanServer();
                        HaiKangYongChuanModel  reqModel = server.Analyze(str);
                        //显示内容
                        StringBuilder showStr = new StringBuilder();
                        showStr.Append($"\r\n");
                        showStr.Append($"流水号:{string.Join("",reqModel.LiuShui)}\r\n");
                        showStr.Append($"协议版本号:{string.Join("", reqModel.XieYiBanBen)}\r\n");
                        showStr.Append($"时间:{HaiKangYongChuanServer.CodeToTime(reqModel.ShiJian)}\r\n");
                        showStr.Append($"源地址:{string.Join("", reqModel.YuanDiZhi)}\r\n");
                        showStr.Append($"目的地址:{string.Join("", reqModel.MuDiDiZhi)}\r\n");
                        string mingLingZiJie = reqModel.MingLingZiJie[0].Convert16To10();
                        showStr.Append($"命令:{db.Keywords.FirstOrDefault(m=>m.KeyType== "控制单元命令"&&m.KeyCode== mingLingZiJie)?.KeyContent?? mingLingZiJie}\r\n");
                        //要处理命令为3的情况
                        showStr.Append($"数据单元类型:{db.Keywords.FirstOrDefault(m => m.KeyType == "数据单元标识符_类型标志" && m.KeyCode == reqModel.Content_Information.LeiXingBiaoZhi)?.KeyContent ?? reqModel.Content_Information.LeiXingBiaoZhi}\r\n");
                        showStr.Append($"接收到{reqModel.Content_Object.Count}个信息对象\r\n");
                        foreach (var item in reqModel.Content_Object)
                        {
                            //上传 建筑消防设施 部件 运行状态
                            showStr.Append($"---------------------\r\n");
                            //showStr.Append($"信息类型:{db.Keywords.FirstOrDefault(m => m.KeyType == "数据单元标识符_类型标志" && m.KeyCode == reqModel. )}\n");
                            string xiTongLeiXing = item.Content_Body.XiTongLeiXing?.TrimStart('0');
                            showStr.Append($"系统类型:{db.Keywords.FirstOrDefault(m => m.KeyType == "系统类型" && m.KeyCode == xiTongLeiXing)?.KeyContent ?? item.Content_Body.XiTongLeiXing}\r\n");
                            showStr.Append($"系统地址:{item.Content_Body.XiTongDiZhi}\r\n");
                            showStr.Append($"系统状态:{new HaiKangYongChuanServer().AnalyzeBitCode(item.Content_Body.XiTongZhuangTai, "建筑消防设施系统状态")}\r\n");
                            string buJianLeiXing = item.Content_Body.BuJianLeiXing?.TrimStart('0');
                            showStr.Append($"部件类型:{db.Keywords.FirstOrDefault(m => m.KeyType == "部件类型" && m.KeyCode == buJianLeiXing)?.KeyContent ?? item.Content_Body.BuJianLeiXing}\r\n");
                            showStr.Append($"部件地址:{item.Content_Body.BuJianDiZhi}\r\n");
                            showStr.Append($"部件状态:{new HaiKangYongChuanServer().AnalyzeBuJianZhuangTai(item.Content_Body.BuJianZhuangTai)}\r\n");

                            showStr.Append($"部件说明:{item.Content_Body.BuJianShuoMing}\r\n");
                            //说明解码
                            //Encoding gb18030 = Encoding.GetEncoding("GB18030");
                            //byte[] smbytes = StringHelper.strToToHexByte(item.Content_Body.BuJianShuoMing);
                            //string smStr = gb18030.GetString(smbytes);

                            //上传用户信息传输装置 运行状态
                            showStr.Append($"用户信息传输装置运行状态:{new HaiKangYongChuanServer().AnalyzeBitCode(item.Content_Body.YongChuanYunXingZhuangTai, "用户信息传输装置运行状态")}\r\n");
                            //上传用户信息传输装置软件版本
                            showStr.Append($"用户信息传输装置软件版本:{item.Content_Body.YongChuanRuanJianBanBen}\r\n");
                            showStr.Append($"操作信息:{new HaiKangYongChuanServer().AnalyzeBitCode(item.Content_Body.CaoZuoXinXi, "用户信息传输装置操作标志")}\r\n");
                            showStr.Append($"操作员编号:{item.Content_Body.CaoZuoYuanBianHao}\r\n");
                            showStr.Append($"时间标签:{HaiKangYongChuanServer.CodeToTime(item.ShiJianBiaoQian)}\r\n");
                            showStr.Append($"\r\n");
                        }
                        Invoke(myD_ShowMessage, showStr.ToString());


                        if (mingLingZiJie == ((int)Enums.KongZhiDanYuanEnum.确认).ToString())
                        {
                            continue;
                        }


                        #region 答复
                        StringBuilder returnStr = new StringBuilder();
                        returnStr.Append(reqModel.StartCode[0]);
                        returnStr.Append(reqModel.StartCode[1]);
                        returnStr.Append(reqModel.LiuShui[0]);
                        returnStr.Append(reqModel.LiuShui[1]);
                        returnStr.Append(reqModel.XieYiBanBen[0]);
                        returnStr.Append(reqModel.XieYiBanBen[1]);
                        returnStr.Append(HaiKangYongChuanServer.TimeToCode(DateTime.Now)); ////new DateTime(2019,1,1,1,1,1)
                        returnStr.Append(reqModel.YuanDiZhi[0]);                           //回发时把原地址和目的地址调转
                        returnStr.Append(reqModel.YuanDiZhi[1]);
                        returnStr.Append(reqModel.YuanDiZhi[2]);
                        returnStr.Append(reqModel.YuanDiZhi[3]);
                        returnStr.Append(reqModel.YuanDiZhi[4]);
                        returnStr.Append(reqModel.YuanDiZhi[5]);
                        returnStr.Append(reqModel.MuDiDiZhi[0]);//回发时把原地址和目的地址调转
                        returnStr.Append(reqModel.MuDiDiZhi[1]);
                        returnStr.Append(reqModel.MuDiDiZhi[2]);
                        returnStr.Append(reqModel.MuDiDiZhi[3]);
                        returnStr.Append(reqModel.MuDiDiZhi[4]);
                        returnStr.Append(reqModel.MuDiDiZhi[5]);

                        string data       = "";
                        string dataLength = "0000";
                        string mingLing   = "03";
                        if (reqModel.Content_Information.LeiXingBiaoZhi == ((int)Enums.ShuJuDanYuanLeiXing.用户信息传输装置系统时间).ToString())
                        {
                            data       = "90".Convert10To16() + "01".Convert10To16() + HaiKangYongChuanServer.TimeToCode(new DateTime(2022, 1, 1, 1, 1, 1));
                            dataLength = "00080000";
                            mingLing   = "01";
                        }

                        returnStr.Append(dataLength); //应用数据单元长度为0
                        returnStr.Append(mingLing);   //命令字节:确认
                        returnStr.Append(data);
                        //crc校验
                        int    checksum = 0;
                        string checkStr = returnStr.ToString().Substring(4, returnStr.ToString().Length - 4);
                        byte[] c        = StringHelper.strToToHexByte(checkStr);
                        for (int i = 0; i < c.Length; i++)
                        {
                            checksum += c[i];
                        }
                        var crc64 = (byte)(checksum & 0xFF);
                        returnStr.Append(crc64.ToString().Convert10To16());
                        returnStr.Append(reqModel.EndCode[0]);
                        returnStr.Append(reqModel.EndCode[1]);
                        string msg = returnStr.ToString();
                        FileHelper.OpenWrite($@"{Application.StartupPath}//log//{DateTime.Now.ToString("yyyyMMddHH")}.txt", $@"答复报文{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:{returnStr}");
                        Invoke(myD_ShowMessage, "Return message success: " + msg);
                        bool result = TCPHelper.SendToClient(client, msg, out msg);
                        if (!result)
                        {
                            Invoke(myD_ShowMessage, "Return message faild: " + msg);
                        }
                        #endregion
                    }
                    else
                    {
                        //这里需要注意 当num=0时表明客户端已经断开连接,需要结束循环,不然会死循环一直卡住
                        Invoke(myD_ShowMessage, $"Client closed");
                        break;
                    }
                }
                catch (Exception e)
                {
                    //链接失败 从集合中移除出错客户端
                    if (client?.Client.Connected == true)
                    {
                        clients.Remove(clients.FirstOrDefault(m => m.RemoteEndPoint == client?.Client?.RemoteEndPoint?.ToString()));
                        Invoke(myD_ShowMessage, "error:" + e.ToString());
                    }


                    break;
                }
            }
        }