Ejemplo n.º 1
0
        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            if (_gpsdService != null && _gpsdService.IsRunning)
            {
                return;
            }

            var info = new GpsdInfo()
            {
                Address = "***.* **.* **.***",
                //Default
                //Port = 2947,
                //IsProxyEnabled = true,
                //ProxyAddress = "proxy",
                //ProxyPort = 80,
                //ProxyCredentials = new ProxyCredentials("*****", "*****")
            };

            _gpsdService = new GpsService(info);
            _writer      = new StreamWriter("testFile1.nmea");

            _gpsdService.RegisterRawDataEvent(GpsdServiceOnRawLocationChanged);
            _gpsdService.RegisterDataEvent(GpsdServiceOnLocationChanged);

            var task = Task.Run(() =>
            {
                Retry.Do(_gpsdService.Connect, TimeSpan.FromSeconds(1));
            });

            task.ContinueWith(t =>
            {
                MessageBox.Show(t.Exception?.ToString(), "Exception", MessageBoxButton.OK);
            }, CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.FromCurrentSynchronizationContext());
        }
Ejemplo n.º 2
0
        public static TcpClient GetTcpClient(GpsdInfo data)
        {
            var uriBuilder = new UriBuilder
            {
                Scheme = Uri.UriSchemeHttp,
                Host   = data.ProxyAddress,
                Port   = data.ProxyPort
            };

            var proxyUri = uriBuilder.Uri;
            var request  = WebRequest.Create("http://" + data.Address + ":" + data.Port);
            var webProxy = new WebProxy(proxyUri);

            request.Proxy  = webProxy;
            request.Method = "CONNECT";

            if (data.ProxyCredentials != null)
            {
                var credentials = data.ProxyCredentials;
                if (credentials.GetType() == typeof(ProxyCredentials))
                {
                    webProxy.Credentials = new NetworkCredential(credentials.ProxyUsername, ((ProxyCredentials)credentials).ProxyPassword);
                }
                else if (credentials.GetType() == typeof(SecureProxyCredentials))
                {
                    webProxy.Credentials = new NetworkCredential(data.ProxyCredentials.ProxyUsername, ((SecureProxyCredentials)credentials).ProxyPassword);
                }
            }
            else
            {
                webProxy.UseDefaultCredentials = true;
            }

            var response       = request.GetResponse();
            var responseStream = response.GetResponseStream();

            Debug.Assert(responseStream != null);

            const BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance;

            var rsType             = responseStream.GetType();
            var connectionProperty = rsType.GetProperty("Connection", flags);

            var connection            = connectionProperty.GetValue(responseStream, null);
            var connectionType        = connection.GetType();
            var networkStreamProperty = connectionType.GetProperty("NetworkStream", flags);

            var networkStream  = networkStreamProperty.GetValue(connection, null);
            var nsType         = networkStream.GetType();
            var socketProperty = nsType.GetProperty("Socket", flags);
            var socket         = (Socket)socketProperty.GetValue(networkStream, null);

            return(new TcpClient {
                Client = socket
            });
        }
Ejemplo n.º 3
0
        public void StartGpsReading(GpsdInfo data)
        {
            if (_streamReader == null || !_client.Connected)
            {
                throw new NotConnectedException();
            }

            _retryReadCount = data.RetryRead;
            IsRunning       = true;
            while (IsRunning)
            {
                if (!_client.Connected)
                {
                    throw new ConnectionLostException();
                }

                try
                {
                    var gpsData = _streamReader.ReadLine();
                    OnRawGpsDataReceived(gpsData);
                    if (gpsData == null)
                    {
                        if (_retryReadCount == 0)
                        {
                            Disconnect();
                            throw new ConnectionLostException();
                        }
                        _retryReadCount--;
                        continue;
                    }

                    var message     = _gpsdDataParser.GetGpsData(gpsData);
                    var gpsLocation = message as GpsLocation;
                    if (gpsLocation == null || _previousReadTime != null && data.ReadFrequenty != 0 &&
                        gpsLocation.Time.Subtract(new TimeSpan(0, 0, 0, 0, data.ReadFrequenty)) <= _previousReadTime)
                    {
                        continue;
                    }
                    OnGpsDataReceived(new GpsDataEventArgs(gpsLocation));
                    _previousReadTime = gpsLocation.Time;
                }
                catch (IOException)
                {
                    Disconnect();
                    throw;
                }
            }
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            _eventHandler += ExitHandler;
            SetConsoleCtrlHandler(_eventHandler, true);

            var info = new GpsdInfo()
            {
                Address = "***.* **.* **.***",
                //Default
                //Port = 2947,
                //IsProxyEnabled = true,
                //ProxyAddress = "proxy",
                //ProxyPort = 80,
                //ProxyCredentials = new ProxyCredentials("*****", "*****")
            };

            _gpsService = new GpsService(info);

            _gpsService.RegisterDataEvent(GpsdServiceOnLocationChanged);
            _gpsService.Connect();

            Console.WriteLine("Press enter to continue...");
            Console.ReadKey();
        }
Ejemplo n.º 5
0
 public GpsdGpsClient(GpsdInfo connectionData) : base(GpsType.Gpsd, connectionData)
 {
 }