Пример #1
0
        private static void ThreadProc(object state)
        {
            var client = ((TcpClient)state);

            byte[]        buffer    = new byte[client.ReceiveBufferSize];
            NetworkStream stream    = ((TcpClient)state).GetStream();
            int           bytesRead = stream.Read(buffer, 0, client.ReceiveBufferSize);

            string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead).Replace("\r\n", "").Replace("\r", "").Replace("\n", "");;

            try
            {
                if (dataReceived.Contains(","))
                {
                    // il s'agit du format de nouveaux boitiers créent par khaled
                    NewBoxParser parser = new NewBoxParser();

                    var result = parser.Parse(dataReceived.Split('\r'));
                    foreach (var r in result)
                    {
                        //   Task.Run(async () => { await SendCommand(stream, r.Value, client); });
                        _bus.Publish <CreateNewBoxGps>(r);
                    }
                }
                else
                {
                    // boitier GT02A
                    Tk1003Parser parser = new Tk1003Parser();
                    var          result = parser.Parse(dataReceived.Split('\r'));
                    foreach (var r in result)
                    {
                        Task.Run(async() => { await SendCommand(stream, r.Value, client); });
                        foreach (var createTk103Gpse in r.Key)
                        {
                            _bus.Publish <CreateTk103Gps>(createTk103Gpse);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                // client.Close();
            }


            // Console.ReadLine();
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="state"></param>
        private static async void ThreadProc(object state)
        {
            var client = (TcpClient)state;

            byte[]        buffer       = new byte[client.ReceiveBufferSize];
            NetworkStream stream       = ((TcpClient)state).GetStream();
            int           bytesRead    = stream.Read(buffer, 0, client.ReceiveBufferSize) - 2;
            string        dataReceived = Encoding.ASCII.GetString(buffer, 2, bytesRead);

            try
            {
                if (Commonhelper.IsValidImei(dataReceived))
                {
                    await ParseTeltonikaData(client, stream, buffer, dataReceived);

                    //client.Close();
                }
                // il s'agit du format de nouveaux boitiers créent par khaled
                else if (dataReceived.Contains(",") && !dataReceived.Contains("("))
                {
                    arduinoBox = true;
                    NewBoxParser parser = new NewBoxParser();
                    dataReceived = dataReceived.Replace("\r\n", "").Replace("\r", "").Replace("\n", "");
                    try
                    {
                        var result = parser.Parse(dataReceived.Split('\r'));
                        foreach (var r in result)
                        {
                            await _bus.Publish(r);
                        }
                    }
                    catch (ValidationException e)
                    {
                        _log.Error("error message :" + e.Message + " details:" + e.InnerException + " at:" + DateTime.Now);
                        //throw;
                    }
                }
                else
                {
                    // boitier GT02A
                    Tk1003Parser parser = new Tk1003Parser();
                    arduinoBox = false;
                    var result = parser.Parse(dataReceived.Split('\r'));
                    // envoyer une message de reception et une déconnexion aux clients
                    if (client.Connected)
                    {
                        await SendCommand(stream, result.FirstOrDefault().Value, client);
                    }

                    foreach (var r in result)
                    {
                        foreach (var createTk103Gpse in r.Key)
                        {
                            await _bus.Publish(createTk103Gpse);
                        }
                    }

                    // client.Close();
                }
            }
            catch (Exception e)
            {
                _log.Error("error message :" + e.Message + " details:" + e.InnerException + " at:" + DateTime.Now);
                Console.WriteLine(e);
                if (!arduinoBox)
                {
                    client.Close();
                }
            }



            // Console.ReadLine();
        }