Пример #1
0
        internal void setByLinkInfoMsg(BytesManager bmanager)
        {
            //throw new NotImplementedException();
            //获取carId
            int carId = bmanager.linkInfo.CarID;

            windowResized = true;
            //MessageBox.Show(this.Height.ToString()+" "+this.Width.ToString());
            //Graphics g = this.CreateGraphics();
            //Pen p = new Pen(Color.Red);
            //g.DrawLine(p, 100, 100, 200, 200);
            var windowHeight = this.Size.Height;
            var windowWidth  = this.Size.Width;

            setControlPosition();
            Graphics g = this.CreateGraphics();

            //清除之前绘制的内容
            clearWindow(g);
            //设置car2Server的链路
            car2ServerNum = setCar2ServerInfo(bmanager.linkInfo.ckband);
            uvaNum        = setUvaInfo(bmanager.linkInfo.ttlinkinfo);
            //绘制新的内容
            drawNew(g, windowHeight, windowWidth, car2ServerNum, uvaNum);
        }
Пример #2
0
        /// <summary>
        /// 调度循环,用于接收命令,完成调度。
        /// </summary>
        /// <param name="obj"></param>
        private void dispatchLoop(object obj)
        {
            //throw new NotImplementedException();
            UdpClient    dispatch = obj as UdpClient;
            BytesManager bmanager = null;

#if DEBUG
            textBox_sysLog.Invoke(setSysLogCallBack, "启动成功");
#endif
            while (true)
            {
                //阻塞,只到接收到消息
                Byte[] receiveBytes = null; // dispatch.Receive(ref RemoteIpEndPoint);
                string receiveData  = null; // Encoding.UTF8.GetString(receiveBytes);

                //允许接收任意远端发送的消息
                IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
                try
                {
                    //阻塞,只到接收到消息
                    receiveBytes = dispatch.Receive(ref RemoteIpEndPoint);
                    bmanager     = new BytesManager(receiveBytes);
                    receiveData  = Encoding.UTF8.GetString(receiveBytes);
                }
                catch (Exception e)
                {
                    Trace.WriteLine(e.ToString());
                    Trace.WriteLine(e.Message);
                    continue;
                }

                //输出debug信息
                //Trace.WriteLine("接收到消息:"+receiveData);
#if DEBUG
                textBox_sysLog.Invoke(setSysLogCallBack, ("接收到来自" + RemoteIpEndPoint.ToString() + "消息:" + receiveBytes));
                //textBox_sysLog.Invoke(setSysLogCallBack, (receiveBytes));
#endif
                string[] commands = receiveData.Split(';');
                if (bmanager.msgForm == (int)Global.msgFromType.uva || bmanager.msgForm == (int)Global.msgFromType.helmet)
                {
                    switch (bmanager.uvaMsg.sendType)
                    {
                    case '\u0001':
                    {
#if DEBUG
                        textBox_sysLog.Invoke(setSysLogCallBack, ("解析到 begin 命令"));
#endif
                        //获取无人机id
                        int uvaId = bmanager.uvaMsg.cliNum;
                        //检查是否重复连接
                        if (allUVA.ContainsKey(uvaId))
                        {
                            string sendString = "error;DuplicateConnection;";
                            //Byte[] sendBytes = Encoding.UTF8.GetBytes(sendString);
                            UvaEntity tmpUva    = allUVA[uvaId] as UvaEntity;
                            Byte[]    sendBytes = Command.DuplicateConnection(tmpUva.videoIp, tmpUva.videoPort);
                            try
                            {
                                dispatch.Send(sendBytes, sendBytes.Length, RemoteIpEndPoint);
                                textBox_sysLog.Invoke(setSysLogCallBack, string.Format("向{0}:{1},{2}号无人机发送错误信息{3}", RemoteIpEndPoint.Address, RemoteIpEndPoint.Port, uvaId, sendString));
                            }
                            catch (Exception e)
                            {
#if DEBUG
                                textBox_sysLog.Invoke(setSysLogCallBack, e.ToString());
#endif


                                textBox_sysLog.Invoke(setSysLogCallBack, string.Format("向{0}:{1},{2}号无人机发送错误信息失败", RemoteIpEndPoint.Address, RemoteIpEndPoint.Port, uvaId));
                            }
                            break;
                        }
                        //接收到连接信息,输出日志信息
                        textBox_sysLog.Invoke(setSysLogCallBack, ("接收到来自" + RemoteIpEndPoint + "的连接请求"));
                        //为新的UVA分配接收视频信息的udpClient
                        int restTimes = Global.MAX_RETRY_TIMES;
                        while (true)
                        {
                            if (restTimes > 0)
                            {
#if DEBUG
                                textBox_sysLog.Invoke(setSysLogCallBack, ("正在为" + RemoteIpEndPoint.Address + ":" + RemoteIpEndPoint.Port + "分配视频接收服务器"));
                                textBox_sysLog.Invoke(setSysLogCallBack, (string.Format("第{0}/{1}次尝试", Global.MAX_RETRY_TIMES - restTimes + 1, Global.MAX_RETRY_TIMES)));
#endif
                            }
                            else
                            {
                                textBox_sysLog.Invoke(setSysLogCallBack, (string.Format("为{0}:{1}分配视频接收服务器失败,系统资源不足", RemoteIpEndPoint.Address, RemoteIpEndPoint.Port)));
                                break;
                            }
                            restTimes--;
                            //获取视频接收服务器的ip地址
                            string video_receive_ip = Global.RECEIVE_VIDEO_SERVER;
                            //生成端口,检查是否可用
                            System.Random a       = new Random(System.DateTime.Now.Millisecond);   // use System.DateTime.Now.Millisecond as seed
                            int           RandKey = a.Next(Global.MINPORT, Global.MAXPORT);
#if DEBUG
                            textBox_sysLog.Invoke(setSysLogCallBack, string.Format("为{0}:{1}尝试分配端口{2}", RemoteIpEndPoint.Address, RemoteIpEndPoint.Port, RandKey));
#endif
                            try
                            {
                                //分配端口成功,开启新的线程,接收视频信息
                                UdpClient videoReceiveUDPClient = new UdpClient(new IPEndPoint(IPAddress.Parse("0.0.0.0"), RandKey));
                                videoReceiveUDPClient.Close();
                                //记录无人机
                                UvaEntity tmpUVA = new UvaEntity(RemoteIpEndPoint.Address.ToString(), RemoteIpEndPoint.Port, bmanager.uvaMsg.cliNum, video_receive_ip, RandKey, RemoteIpEndPoint);
                                allUVA.Add(uvaId, tmpUVA);
                                //开启UDP视频接收线程
                                //videoReceiveThread = new Thread(new ParameterizedThreadStart(videoReceiveLoop));
                                //videoReceiveThread.IsBackground = true;
                                //videoReceiveThread.Start(tmpUVA);
                                //暂停0.5秒,等待allUVA更新
                                //System.Threading.Thread.Sleep(500);
                                //分配成功,输出信息
                                textBox_sysLog.Invoke(setSysLogCallBack, (string.Format("为{0}:{1}分配视频接收服务器成功,视频接收地址为{2}:{3}", RemoteIpEndPoint.Address, RemoteIpEndPoint.Port, video_receive_ip, RandKey)));
                                //在全部无人机combbox里面添加一项
                                comboBox_allUVA.Invoke(modifyUVACallBack, tmpUVA, true);
                                break;
                            }
                            catch (Exception e)
                            {
                                Trace.WriteLine(e.StackTrace);
                                textBox_sysLog.Invoke(setSysLogCallBack, e.StackTrace.ToString());
                                textBox_sysLog.Invoke(setSysLogCallBack, e.Message.ToString());
                            }
                        }
                        break;
                    }

                    case '\u0003':
                    case '\u0004':
                    {
#if DEBUG
                        textBox_sysLog.Invoke(setSysLogCallBack, string.Format("接收到{0}信息", bmanager.uvaMsg.sendType == '\u0003'?"end":"ok"));
#endif
                        //获取无人机id
                        //int uvaId = Convert.ToInt32(commands[2]);
                        //获取无人机id
                        int uvaId = bmanager.uvaMsg.cliNum;
                        //检查是否重复注销
                        if (!allUVA.ContainsKey(uvaId))
                        {
                            string sendString = "error;DuplicatedeDisconnect;";
                            //Byte[] sendBytes = Encoding.UTF8.GetBytes(sendString);
                            Byte[] sendBytes = Command.Error(Global.DuplicatedeDisconnect);
                            try
                            {
                                dispatch.Send(sendBytes, sendBytes.Length, RemoteIpEndPoint);
                                textBox_sysLog.Invoke(setSysLogCallBack, string.Format("向{0}:{1},{2}号无人机发送错误信息{3}", RemoteIpEndPoint.Address, RemoteIpEndPoint.Port, uvaId, sendString));
                            }
                            catch (Exception e)
                            {
#if DEBUG
                                textBox_sysLog.Invoke(setSysLogCallBack, e.ToString());
#endif


                                textBox_sysLog.Invoke(setSysLogCallBack, string.Format("向{0}:{1},{2}号无人机发送错误信息失败", RemoteIpEndPoint.Address, RemoteIpEndPoint.Port, uvaId));
                            }
                            break;
                        }
                        //获取无人机实例,进行销毁操作
                        try
                        {
                            UvaEntity tmpUva = allUVA[uvaId] as UvaEntity;
                            comboBox_allUVA.Invoke(modifyUVACallBack, tmpUva, false);
                        }
                        catch (Exception ee)
                        {
                            Trace.WriteLine(ee.Message);
                        }

                        break;
                    }

                    //收到心跳信息
                    case '\u0002':
                    {
                        int    id   = -1;
                        string type = null;
                        string info = null;
                        try
                        {
                            //获取设备的id号
                            //id = Convert.ToInt32(commands[2]);
                            id = bmanager.uvaMsg.cliNum;
                            //获取设备类型
                            type = bmanager.uvaMsg.cliType == '\u0001'?"uva": "helmet";
                            //type = commands[1];
                            //info = commands[3];
                            Trace.WriteLine("心跳命令解析完毕" + type + id.ToString());
                            //string[] infos = info.Split('&');
                            if (!allUVA.Contains(id))
                            {
                                Trace.WriteLine(string.Format("{0} {1}已经下线", type, id.ToString()));
                                break;
                            }
                            string x, y, heartTime;
                            try
                            {
                                //x = infos[0];
                                //y = infos[1];
                                //heartTime = infos[2];
                                x         = string.Format("{0}_{1}_{2}", bmanager.uvaMsg.latDeg, bmanager.uvaMsg.latMin, bmanager.uvaMsg.latSec);
                                y         = string.Format("{0}_{1}_{2}", bmanager.uvaMsg.lonDeg, bmanager.uvaMsg.lonMin, bmanager.uvaMsg.lonSec);
                                heartTime = string.Format("{0}_{1}_{2}", bmanager.uvaMsg.hour, bmanager.uvaMsg.minute, bmanager.uvaMsg.second);
                                UvaEntity tmpUVA = allUVA[id] as UvaEntity;
                                //tmpUVA.receiveHeartAsync(x, y, heartTime);
                                byte[] sendBytes = Command.HeratResponse(tmpUVA);
                                string gpsString = string.Format("{0}{1}{2}{3},{4}{5}{6}{7}",
                                                                 bmanager.uvaMsg.latDeg, bmanager.uvaMsg.latMin, bmanager.uvaMsg.latSec, bmanager.uvaMsg.latDir,
                                                                 bmanager.uvaMsg.lonDeg, bmanager.uvaMsg.lonMin, bmanager.uvaMsg.lonSec, bmanager.uvaMsg.lonDir);
                                dispatch.Send(sendBytes, sendBytes.Length, RemoteIpEndPoint);
#if DEBUG
                                Trace.WriteLine(string.Format("Terminal {8} GPS Info:\r\n{0}{1}{2}{3},{4}{5}{6}{7}\r\n",
                                                              bmanager.uvaMsg.latDeg, bmanager.uvaMsg.latMin, bmanager.uvaMsg.latSec, bmanager.uvaMsg.latDir,
                                                              bmanager.uvaMsg.lonDeg, bmanager.uvaMsg.lonMin, bmanager.uvaMsg.lonSec, bmanager.uvaMsg.lonDir,
                                                              id));
                                textBox_sysLog.Invoke(setSysLogCallBack, string.Format("解析到心跳命令\r\n" +
                                                                                       "终端 {0} GPS:{1}\r\n" +
                                                                                       "时间 {2}", id, gpsString, heartTime));
#endif
                                if ((allUVA[id] as UvaEntity).panelName == Global.MAIN_PANEL)
                                {
                                    labelGPS.Invoke(updateGPSCAllBack, string.Format("{0}{1}{2}{3},{4}{5}{6}{7}",
                                                                                     bmanager.uvaMsg.latDeg, bmanager.uvaMsg.latMin, bmanager.uvaMsg.latSec, bmanager.uvaMsg.latDir,
                                                                                     bmanager.uvaMsg.lonDeg, bmanager.uvaMsg.lonMin, bmanager.uvaMsg.lonSec, bmanager.uvaMsg.lonDir));
                                }
                            }
                            catch (Exception e)
                            {
                                Trace.WriteLine("info解析失败");
                                Trace.WriteLine(e.StackTrace);
                                Trace.WriteLine(e.Message);
#if DEBUG
                                textBox_sysLog.Invoke(setSysLogCallBack, e.Message);
#endif
                                //throw;
                            }
                        }
                        catch (Exception e)
                        {
                            Trace.WriteLine(e.StackTrace);
                            Trace.WriteLine(e.Message);

                            //throw;
                        }
                        break;
                    }

                    default:
                        break;
                    }
                }
                else if (bmanager.msgForm == (int)Global.msgFromType.linkInfo)
                {
                    linkInfoBoard.setByLinkInfoMsg(bmanager);
                }
            }
        }