Esempio n. 1
0
        public bool UpLoad(byte[] buffer, string deviceid)
        {
            DETECTION_INFO detInfo     = new DETECTION_INFO();//共计866字节
            object         detinfoType = detInfo;
            TastInfo       Tinfo       = new TastInfo();

            Tinfo.ByteArrayToStructureEndian(buffer, ref detinfoType, 0);
            detInfo = (DETECTION_INFO)detinfoType;


            //写入数据库
            string batch_id = System.Text.Encoding.UTF8.GetString(detInfo.batch_id, 0, GetLen(detInfo.batch_id)).Trim();
            string task_id  = System.Text.Encoding.UTF8.GetString(detInfo.task_id, 0, GetLen(detInfo.task_id)).Trim();
            //             UInt64 batch_id = detInfo.batch_id;
            //             UInt32 task_id = detInfo.task_id;
            string p_name          = System.Text.Encoding.UTF8.GetString(detInfo.p_name, 0, GetLen(detInfo.p_name)).Trim();
            string type            = System.Text.Encoding.UTF8.GetString(detInfo.type, 0, GetLen(detInfo.type));
            int    channel         = (int)detInfo.channel;
            string ftest_result    = System.Text.Encoding.UTF8.GetString(detInfo.ftest_result, 0, GetLen(detInfo.ftest_result)).Trim();
            string test_result     = System.Text.Encoding.UTF8.GetString(detInfo.test_result, 0, GetLen(detInfo.test_result)).Trim();
            string result_unit     = System.Text.Encoding.UTF8.GetString(detInfo.result_unit, 0, GetLen(detInfo.result_unit)).Trim();
            string standard        = System.Text.Encoding.UTF8.GetString(detInfo.standard, 0, GetLen(detInfo.standard)).Trim();
            string abs_result      = System.Text.Encoding.UTF8.GetString(detInfo.abs_result, 0, GetLen(detInfo.abs_result)).Trim();
            string sample_no       = System.Text.Encoding.UTF8.GetString(detInfo.sample_no, 0, GetLen(detInfo.sample_no)).Trim();
            string sample_name     = System.Text.Encoding.UTF8.GetString(detInfo.sample_name, 0, GetLen(detInfo.sample_name)).Trim();
            string sites           = System.Text.Encoding.UTF8.GetString(detInfo.sites, 0, GetLen(detInfo.sites)).Trim();
            string submission_unit = System.Text.Encoding.UTF8.GetString(detInfo.submission_unit, 0, GetLen(detInfo.submission_unit)).Trim();
            string test_operator   = System.Text.Encoding.UTF8.GetString(detInfo.test_operator, 0, GetLen(detInfo.test_operator)).Trim();
            string test_unit       = System.Text.Encoding.UTF8.GetString(detInfo.test_unit, 0, GetLen(detInfo.test_unit)).Trim();
            string test_time       = System.Text.Encoding.UTF8.GetString(detInfo.test_time, 0, GetLen(detInfo.test_time)).Trim();
            // DateTime dt = DateTime.ParseExact(test_time, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
            int User_Id = 1234;

            if (deviceid != null)
            {
                User_Id = int.Parse(deviceid);
            }

            string strsql = "Insert Into " +
                            "detectioninfo(batch_id,task_id,p_name,type,channel,Juge_result,Detect_result,result_unit,standard,Absolut_result,sample_no,sample_name,sites,submission_unit,test_operator,test_unit,test_time,Device_ID)"
                            + "values('" + batch_id + "','" + task_id + "','" + p_name + "','" + type + "'," + channel + ",'" + ftest_result + "','" + test_result + "','" + result_unit + "','" + standard + "','" + abs_result + "','" + sample_no + "','" + sample_name + "','" + sites + "','" + submission_unit + "','" + test_operator + "','" + test_unit + "','" + test_time + "'," + User_Id + ")";

            try
            {
                dbMySql.Open(databasename);
                dbMySql.ExcuteNonQuery(databasename, strsql);
                dbMySql.Close(databasename);
                return(true);
            }
            catch (System.Exception ex)
            {
                return(false);
            }
        }
Esempio n. 2
0
        void RecvData(object sender, NetEventArgs e)
        {
            //  string info = string.Format("recv data:{0} from:{1}.", e.Client.Datagram, e.Client);
            //  Console.WriteLine(info);
            TcpSvr svr = (TcpSvr)sender;

            //测试把收到的数据返回给客户端
            // svr.SendText(e.Client, e.Client.Datagram);
            //svr.SendFile(this.textBox1.Text);
            //   Console.Write(">");

            DETECTION_INFO ff = new DETECTION_INFO();
            int            si = Marshal.SizeOf(ff);

            //获取接受到的消息,已经去头,但是包括尾
            // byte[] recivebuffer = System.Text.Encoding.Default.GetBytes(e.Client.Datagram);
            List <Byte> lbyte = new List <Byte>();

            byte[] recivebuffer = e.Client.RvBufer;

            for (int k = 0; k < e.Client.RvBufer.Length - 1; k++)
            {
                byte b = e.Client.RvBufer[k];
                byte c = e.Client.RvBufer[k + 1];
                if (b == 0x5a && c == 0x01)
                {
                    lbyte.Add(0x5b);
                }
                else if (b == 0x5a && c == 0x02)
                {
                    lbyte.Add(0x5a);
                }
                else if (b == 0x5e && c == 0x01)
                {
                    lbyte.Add(0x5d);
                }
                else if (b == 0x5e && c == 0x02)
                {
                    lbyte.Add(0x5e);
                }
                else
                {
                    lbyte.Add(b);
                }
            }
            for (int i = 0; i < lbyte.Count; i++)
            {
                recivebuffer[i] = lbyte[i];
            }

            string batch_id = null;
            string task_id  = null;
            //解析消息头
            MSGHEAD head = new MSGHEAD();

            byte[] headbuffer = new byte[MsgHeadLength];
            Array.Copy(recivebuffer, 1, headbuffer, 0, MsgHeadLength);
            head = Get_MSGHeader(headbuffer);
            // head = Get_MSGHeader(e.Client.Datagram.Substring(0, MsgHeadLength));
            //消息体数据

            int Meg_len = (int)head.Msg_Length - MsgTotalLen;

            byte[] buffer = new byte[Meg_len];//
            Array.Copy(recivebuffer, 15, buffer, 0, Meg_len);

//             for (int i = 0; i < head.Msg_Length; ++i)
//             {
//                 byte t = recivebuffer[i];
//                 Console.Write("{0:X}", t);//这个就是主要,填充
//                 Console.Write(" ");
//
//             }
            // Console.Write("?");
            //接入码s
            UInt32   centercode = head.Msg_GNSSCenter;
            TastInfo Tinfo      = new TastInfo();

            byte[] MesBody = null;
            //空数据
            byte[] nu = new byte[0];
            //判定是不是注册
            REGISTER Is_re = new REGISTER();
            //根据不同消息进行处理

            Session    sendDataSession = svr.FindSession(e.Client.ClientSocket);
            ICloneable copySession     = (ICloneable)sendDataSession;
            Session    clientSession   = (Session)copySession.Clone();

            //    Console.Write("*"+head.Msg_ID);
            switch (head.Msg_ID)
            {
            //中心站只发过一个空包,或者包括简单的配置要求,服务器端组帧。
            //链路登陆请求消息
            case 0x0001:
                Is_re = IsRegest(head, buffer);
                // object structType = Is_re;
                byte[] btArray = new byte[1];
                btArray[0] = Is_re.result;
                //MesBody = BitConverter.GetBytes(Is_re.result);
                svr.SendMessage(e.Client, btArray, 0x0002, centercode);    //登录应答
                if (Is_re.result == 0)
                {
                    foreach (DictionaryEntry de in svr._sessionTable)     //ht为一个Hashtable实例
                    {
                        if (de.Value.Equals(clientSession))
                        {
                            clientSession.DeviceId = Is_re.nameId;
                            SessionId id = clientSession.ID;
                            svr._sessionTable.Remove(clientSession.ID);
                            svr._sessionTable.Add(id, clientSession);
                            break;
                        }
                    }


                    //更新数据库状态
                    string sql = "UPDATE device SET Device_Status = 1,Sockets = '" + clientSession.ClientSocket.RemoteEndPoint + "' WHERE Device_ID = '" + clientSession.DeviceId + "'";
                    database.Open();
                    database.ExcuteNonQuery(sql);
                    database.Close();
                }
                else
                {
                    //Session sendDataSession = svr.FindSession(e.Client.ClientSocket);
                    //更新数据库状态
                    string sql = "UPDATE device SET Device_Status = 0 WHERE Device_ID = '" + clientSession.DeviceId + "'";
                    database.Open();
                    database.ExcuteNonQuery(sql);
                    database.Close();
                }
                break;

            //用于心跳监测
            case 0x0003:

                svr.SendMessage(clientSession, nu, 0x0004, centercode);    //开启视频

                // Console.Write("|" + buffer[1]);
                break;

            //批量任务下发(中心站往终端)
            //TEST NUM	4	Unint32_t	结果个数
            //TEST INFO	804	Octet_string	检测结果信息
            case 0x0005:
                //获取任务个数
                byte[] numbuffer = new byte[4];                //
                Array.Copy(recivebuffer, 14, numbuffer, 0, 4); //获取个数
                //                     Array.Reverse(numbuffer);
                //                     UInt32 num = BitConverter.ToUInt32(numbuffer,0);
                //组帧
                TASK_INFO taskinfo = new TASK_INFO();
                object    structT  = taskinfo;
                MesBody = Tinfo.StructureToByteArrayEndian(structT);
                foreach (Session cliSession in svr._sessionTable.Values)
                {
                    if (cliSession.DeviceId == deviceid)
                    {
                        svr.SendMessage(cliSession, nu, 0x0005, centercode);    //开启视频
                        break;
                    }
                }

                //发送

                break;
            //检测结果上传消息

            //检测结果上传(终端往中心站)
            case 0x0006:
                sendDataSession = svr.FindSession(e.Client.ClientSocket);

                deviceid = sendDataSession.DeviceId;
                UpLoad(buffer, deviceid.ToString());
                break;

            //开启实时监控
            case 07:
                //获取发过来的设备id,根据id找到socket
                deviceid = UInt32.Parse(System.Text.Encoding.UTF8.GetString(buffer));    //提取控制的deviceid

                foreach (Session cliSession in svr._sessionTable.Values)
                {
                    if (cliSession.DeviceId == deviceid)
                    {
                        svr.SendMessage(cliSession, nu, 0x0007, centercode);    //开启视频
                        break;
                    }
                }

                break;

            //中心站往检测仪结束实时视频

            case 08:
                deviceid = UInt32.Parse(System.Text.Encoding.UTF8.GetString(buffer));    //提取控制的deviceid
                foreach (Session cliSession in svr._sessionTable.Values)
                {
                    if (cliSession.DeviceId == deviceid)
                    {
                        svr.SendMessage(cliSession, nu, 0x0008, centercode);    //开启视频
                        break;
                    }
                }

                break;

            case 09:
                //视频上传(检测仪主动请求,中心站返回消息)
                UInt32 res = 0;
                byte[] meg = BitConverter.GetBytes(res);
                Array.Reverse(meg);
                svr.SendMessage(e.Client, meg, 0x000a, centercode);    //录制视频上传应答
                svr.FileBegine = true;
                svr.IsPic      = false;
                break;

            //证件上传请求
            case 0x0000b:
                UInt32 resu = 0;
                byte[] megs = BitConverter.GetBytes(resu);
                Array.Reverse(megs);
                svr.SendMessage(e.Client, nu, 0x000c, centercode);    //上传应答
                svr.FileBegine = true;
                svr.IsPic      = true;
                foreach (Session cliSession in svr._sessionTable.Values)
                {
                    if (cliSession.ClientSocket == e.Client.ClientSocket)
                    {
                        svr.device_id = cliSession.DeviceId.ToString();
                    }
                }
                break;

            //视频调阅请求
            case 0x0000d:
                byte[] dev = new byte[12];
                deviceid = UInt32.Parse(System.Text.Encoding.UTF8.GetString(buffer, 0, 4)); //提取控制的deviceid
                batch_id = System.Text.Encoding.UTF8.GetString(buffer, 4, 8);               //提取控制的deviceid
                task_id  = System.Text.Encoding.UTF8.GetString(buffer, 12, 4);              //提取控制的deviceid
                Array.Copy(buffer, 4, dev, 0, 12);

                foreach (Session cliSession in svr._sessionTable.Values)
                {
                    if (cliSession.DeviceId == deviceid)
                    {
                        svr.SendMessage(cliSession, nu, 0x0008, centercode);    //开启视频
                        break;
                    }
                }
                break;

            //视频调阅
            case 0x000e:
                //将文件存储路径插入数据库
                string filepath = System.Text.Encoding.UTF8.GetString(buffer, 0, GetLen(buffer)).Trim();
                //更新数据库状态
                string sql2 = "UPDATE detectioninfo SET video_path = '" + filepath + "' WHERE batch_id = '" + batch_id + "' AND task_id = '" + task_id + "'";
                database.Open();
                database.ExcuteNonQuery(sql2);
                database.Close();
                break;

            //工作参数
            case 0x0000f:
                break;

            //状态和工作参数
            case 0x0010:

                break;

            //参数配置
            case 0x0011:
                byte[] aa = new byte[4];
                Array.Copy(buffer, 0, aa, 0, 4);
                byte[] Msg = new byte[buffer.Length - 4];
                Array.Copy(buffer, 4, Msg, 0, buffer.Length - 4);
                deviceid = UInt32.Parse(System.Text.Encoding.UTF8.GetString(aa));    //提取控制的deviceid
                foreach (Session cliSession in svr._sessionTable.Values)
                {
                    if (cliSession.DeviceId == deviceid)
                    {
                        svr.SendMessage(cliSession, Msg, 0x0012, centercode);    //开启视频
                        break;
                    }
                }

                break;

            //数据管理
            case 0x0012:

                break;

            //设备管理消息
            case 0x0013:

                break;

            //远程版本升级
            case 0x0014:

                break;

            //版本升级应答
            case 0x0015:

                break;

            //通用消息
            case 0xf000:

                break;

            default:
                break;
            }
        }
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void RecvData(object sender, NetEventArgs e)
        {
            //  string info = string.Format("recv data:{0} from:{1}.", e.Client.Datagram, e.Client);
            TcpSvr svr = (TcpSvr)sender;

            byte[] recivebuffer = System.Text.Encoding.UTF8.GetBytes(e.Client.Datagram);
            byte[] buffer       = new byte[MsgHeadLength];
            Array.Copy(recivebuffer, 1, buffer, 0, MsgHeadLength);
            TastInfo Info     = new TastInfo();
            MSGHEAD  head     = new MSGHEAD();
            object   headType = head;

            Info.ByteArrayToStructureEndian(buffer, ref headType, 0);
            head = (MSGHEAD)headType;
            string centercode = head.Msg_GNSSCenter.ToString();

            switch (head.Msg_ID)
            {
            //用于心跳监测
            case 03:
                svr.SendMessage(e.Client, "", 0x0004, centercode);
                // svr.SendMessage(e.Client, "", 0x0003, centercode);
                break;
            //检测结果上传消息
            //TEST NUM	4	Unint32_t	结果个数
            //TEST INFO	804	Octet_string	检测结果信息

            case 06:
                byte[] test_num = new byte[4];
                Array.Copy(recivebuffer, MsgHeadLength + 1, test_num, 0, 4);
                Array.Reverse(test_num);

                UInt32 num     = (UInt32)BitConverter.ToInt32(test_num, 0);
                byte[] buffer3 = new byte[head.Msg_Length - 18 - 4];
                Array.Copy(recivebuffer, MsgHeadLength + 1 + 4, buffer3, 0, head.Msg_Length - 18 - 4);
                DETECTION_INFO detInfo     = new DETECTION_INFO();
                object         detInfoType = detInfo;

                Info.ByteArrayToStructureEndian(buffer3, ref detInfoType, 0);
                detInfo = (DETECTION_INFO)detInfoType;

//                     DataTable tbInfo = LoadDetectionInfo();
//                     DataSet ds = new DataSet();
//
//                     DataRow r = tbInfo.NewRow();
//                     r["p_name"] = System.Text.Encoding.UTF8.GetString(detInfo.p_name, 0, GetLen(detInfo.p_name)).Trim();
//                     r["sample_name"] = System.Text.Encoding.UTF8.GetString(detInfo.sample_name,0,GetLen(detInfo.sample_name)).Trim();
//                     r["channel"] = detInfo.channel;
//                     r["type"] = System.Text.Encoding.UTF8.GetString(detInfo.type, 0, GetLen(detInfo.type));
//                     r["standard"] = System.Text.Encoding.UTF8.GetString(detInfo.standard, 0, GetLen(detInfo.standard));
//                     r["sample_no"] = System.Text.Encoding.UTF8.GetString(detInfo.sample_no, 0, GetLen(detInfo.sample_no)).Trim();
//                     r["sites"] = System.Text.Encoding.UTF8.GetString(detInfo.sites, 0, GetLen(detInfo.sites)).Trim();
//                     r["submission_unit"] = System.Text.Encoding.UTF8.GetString(detInfo.submission_unit, 0, GetLen(detInfo.submission_unit));
//                     r["test_operator"] = System.Text.Encoding.UTF8.GetString(detInfo.test_operator, 0, GetLen(detInfo.test_operator));
//
//                     r["test_unit"] = System.Text.Encoding.UTF8.GetString(detInfo.test_unit, 0, GetLen(detInfo.test_unit));
//                     r["test_time"] = System.Text.Encoding.UTF8.GetString(detInfo.test_time, 0, GetLen(detInfo.test_time));
//
//                     r["User_Id"] = e.Client.UserId;
//
//                     //并将数据写入到数据库
//                     tbInfo.Rows.Add(r);

                string   p_name          = System.Text.Encoding.UTF8.GetString(detInfo.p_name, 0, GetLen(detInfo.p_name)).Trim();
                string   sample_name     = System.Text.Encoding.UTF8.GetString(detInfo.sample_name, 0, GetLen(detInfo.sample_name)).Trim();
                int      channel         = (int)detInfo.channel;
                string   type            = System.Text.Encoding.UTF8.GetString(detInfo.type, 0, GetLen(detInfo.type));
                string   standard        = System.Text.Encoding.UTF8.GetString(detInfo.standard, 0, GetLen(detInfo.standard));
                string   sample_no       = System.Text.Encoding.UTF8.GetString(detInfo.sample_no, 0, GetLen(detInfo.sample_no)).Trim();
                string   sites           = System.Text.Encoding.UTF8.GetString(detInfo.sites, 0, GetLen(detInfo.sites)).Trim();
                string   submission_unit = System.Text.Encoding.UTF8.GetString(detInfo.submission_unit, 0, GetLen(detInfo.submission_unit));
                string   test_operator   = System.Text.Encoding.UTF8.GetString(detInfo.test_operator, 0, GetLen(detInfo.test_operator));
                string   test_unit       = System.Text.Encoding.UTF8.GetString(detInfo.test_unit, 0, GetLen(detInfo.test_unit));
                string   test_time       = System.Text.Encoding.UTF8.GetString(detInfo.test_time, 0, GetLen(detInfo.test_time));
                DateTime dt      = DateTime.ParseExact(test_time, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
                int      User_Id = (int)e.Client.UserId;
                string   strsql  = "Insert Into " +
                                   "detectioninfo(p_name,sample_name,channel,type,standard,sample_no,sites,submission_unit,test_operator,test_unit,test_time,User_Id)"
                                   + "values('" + p_name + "','" + sample_name + "'," + channel + ",'" + type + "','" + standard + "','" + sample_no + "','" + sites + "','" + submission_unit + "','" + test_operator + "','" + test_unit + "','" + test_time + "'," + User_Id + ")";

                dbMySql.Open(databaseName);
                dbMySql.ExcuteNonQuery(databaseName, strsql);
                dbMySql.Close(databaseName);

                break;

            case 09:
                //视频上传
                svr.SendMessage(e.Client, 0, 0x000a, centercode);
                break;

            case 0x000e:
                //远程状态和工作参数查询应答消息
                //具体参数不明确
                break;


            default:
                break;
            }


            //测试把收到的数据返回给客户端
            //svr.SendText(e.Client, e.Client.Datagram);
            //MessageBox.Show(message.ToString());


            //svr.SendFile(this.textBox1.Text);
        }