Пример #1
0
        /// <summary>
        /// Gets the type of the GPS tracker that connected
        /// </summary>
        /// <param name="message"></param>
        /// <param name="bytesRead"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public static IGPSTracker GetGPSTracker(byte[] message, int bytesRead, object client)
        {
            Utilities.writeLine("Debug 6: Processing GPS tracker type");

            IGPSTracker _tracker = null;
            char        _1stChar = Convert.ToChar(message[0]);

            // Trim leading nulls from byte array .... some versions of the firmware needs this!
            if (_1stChar == '\0')
            {
                byte[] theNew = new byte[message.Length];

                Buffer.BlockCopy(message, 1, theNew, 0, message.Length - 1);

                message = theNew;
            }


            ASCIIEncoding encoder = new ASCIIEncoding();

            Utilities.writeLine("Debug 6.2: " + encoder.GetString(message, 0, bytesRead));

            _1stChar = Convert.ToChar(message[0]);
            char _2ndChar  = Convert.ToChar(message[1]);
            char _3rdChar  = Convert.ToChar(message[2]);
            bool isInRange = _3rdChar >= 'A' && _3rdChar <= 'z';// range given in the spec for VT340

            Utilities.writeLine("Debug 6.5: 1st char: " + _1stChar + " 2nd char: " + _2ndChar + " 3rd char: " + _3rdChar);

            if (_1stChar == '(')
            {
                Utilities.writeLine("Debug 7: Type is GPS518");
                _tracker = new GPS518();
            }
            else
            if (_1stChar == '$' && _2ndChar == '$' && !isInRange)
            {
                Utilities.writeLine("Debug 8: Type is VT300");
                _tracker = new VT300(client);
            }
            else
            if (_1stChar == '$' && _2ndChar == '$' && isInRange)
            {
                Utilities.writeLine("Debug 9: Type is VT340");
                _tracker = new VT340(client);
            }
            else
            if (_1stChar == '$')
            {
                Utilities.writeLine("Debug 8: Type is VT300 - one of the weird ones");
                _tracker = new VT300(client);
            }
            else
            {
                Utilities.writeLine("Debug 9.5: 1st char: " + _1stChar + " 2nd char: " + _2ndChar + " 3rd char: " + _3rdChar);
                Utilities.writeLine("Debug 10: Type of tracker is unidentified");
            }

            return(_tracker);
        }
Пример #2
0
        private void HandleClientComm(object client)
        {
            TcpClient     tcpClient    = (TcpClient)client;
            NetworkStream clientStream = tcpClient.GetStream();


            byte[] message = new byte[4096];
            int    bytesRead;

            while (true)
            {
                bytesRead = 0;
                IGPSTracker _tracker = null;

                try
                {
                    //blocks until a client sends a message
                    bytesRead = clientStream.Read(message, 0, 4096);
                    Utilities.writeLine("Debug 3: Time: " + DateTime.Now + " data read: " + bytesRead);
                }
                catch (Exception e)
                {
                    //a socket error has occured
                    Utilities.writeLine("Error 1: " + e.Message);
                    Utilities.writeLine("Error 2: " + e.StackTrace);
                    break;
                }

                if (bytesRead == 0)
                {
                    Utilities.writeLine("Dubug 4: Sorry, no data read, disconnecting");
                    //the client has disconnected from the server
                    break;
                }

                //message has successfully been received
                ASCIIEncoding encoder = new ASCIIEncoding();
                Utilities.writeLine("Debug 5: " + encoder.GetString(message, 0, bytesRead));

                try
                {
                    if (_tracker == null)
                    {
                        _tracker = Management.GetGPSTracker(message, bytesRead, client);
                    }
                    _tracker.RecievedMessage(message, bytesRead);
                    _tracker.SendMessages(tcpClient);
                }
                catch (Exception e)
                {
                    Utilities.writeLine("Error 3: " + e.Message);
                    Utilities.writeLine("Error 4: " + e.StackTrace);
                    Utilities.writeLine("Error data: " + encoder.GetString(message, 0, bytesRead));
                }
            }

            Utilities.writeLine("Debug 100: Closing TCP Client");
            tcpClient.Close();
        }
Пример #3
0
        private void HandleClientComm(object client)
        {
            TcpClient     tcpClient    = (TcpClient)client;
            NetworkStream clientStream = tcpClient.GetStream();


            byte[] message = new byte[4096];
            int    bytesRead;

            while (true)
            {
                bytesRead = 0;
                IGPSTracker _tracker = null;

                try
                {
                    //blocks until a client sends a message
                    bytesRead = clientStream.Read(message, 0, 4096);
                    System.Diagnostics.Debug.WriteLine("Time: " + DateTime.Now + " data read: " + bytesRead);
                }
                catch (Exception e)
                {
                    //a socket error has occured
                    System.Diagnostics.Debug.WriteLine(e.Message);
                    System.Diagnostics.Debug.WriteLine(e.StackTrace);

                    break;
                }

                if (bytesRead == 0)
                {
                    System.Diagnostics.Debug.WriteLine("Sorry, no data read, disconnecting");
                    //the client has disconnected from the server
                    break;
                }

                //message has successfully been received
                ASCIIEncoding encoder = new ASCIIEncoding();
                System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));

                try
                {
                    if (_tracker == null)
                    {
                        _tracker = Management.GetGPSTracker(message, bytesRead, client);
                    }
                    _tracker.RecievedMessage(message, bytesRead);
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine(e.Message);
                    System.Diagnostics.Debug.WriteLine(e.StackTrace);

                    eventLog1.WriteEntry("MyGPS Error processing received message. :" + e.Message, EventLogEntryType.Error);
                }
            }

            tcpClient.Close();
        }
Пример #4
0
 public BillCalculator(IGPSTracker gpsTracker, IDispatchOnUIThread dispatcher)
 {
     _gpsTracker = gpsTracker;
     _gpsTracker.StatusChanged += OnGeoLocatorStatusChanged;
     _gpsTracker.PositionChanged += OnPositionChangedForeground;
     _dispatcher = dispatcher;
     CostsPerKm = 0;
     //Register messages from App.xaml.cs telling if app is running in foreground or in background
     IMvxMessenger messenger = Mvx.Resolve<IMvxMessenger>();
     messenger.Subscribe<BackgroundRunningStatusMessage>(OnBackgroundRunningMessageReceived);
 }
Пример #5
0
        private void HandleClientComm(object udpMessage)
        {
            // this is for testing TODO - we need to change this to work with sending back data as udp
            // only done to test UDP reciving
            TcpClient client = new TcpClient();


            byte[] bytes = (byte[])udpMessage;

            IGPSTracker _tracker = null;

            if (bytes.Length == 0)
            {
                Utilities.writeLine("Dubug 4: Sorry, no data read, disconnecting");
                //the client has disconnected from the server
            }
            else
            {
                //message has successfully been received
                ASCIIEncoding encoder = new ASCIIEncoding();
                Utilities.writeLine("Debug 5: " + encoder.GetString(bytes, 0, bytes.Length));

                try
                {
                    if (_tracker == null)
                    {
                        _tracker = Management.GetGPSTracker(bytes, bytes.Length, client);
                    }
                    _tracker.RecievedMessage(bytes, bytes.Length);
                }
                catch (Exception e)
                {
                    Utilities.writeLine("Error 3: " + e.Message);
                    Utilities.writeLine("Error 4: " + e.StackTrace);
                    Utilities.writeLine("Error data: " + encoder.GetString(bytes, 0, bytes.Length));
                }
            }
        }