private void InsertaLectura(FMPacket packet) { SqlGps gps = new SqlGps() { lng = Convert.ToDouble(packet.Longitud), lat = Convert.ToDouble(packet.Latitud), imei = packet.Imei, fecha = packet.Fecha, velocidad = packet.Velocidad, datosJson = packet.DataJson }; gps.InsertaLectura(); }
private void HandleConnection(object client) { const int TAM_BUFFER = 2048; TcpClient tcpCli = client as TcpClient; NetworkStream clientStream = tcpCli.GetStream(); clientStream.ReadTimeout = 5000; string teltonikaImei = string.Empty; byte[] message = new byte[TAM_BUFFER]; List <byte> contenido = new List <byte>(); int bytesRead = 0; try { bytesRead = clientStream.Read(message, 0, TAM_BUFFER); //blocks until a client sends a message for (int i = 0; i < bytesRead; i++) { contenido.Add(message[i]); } } catch (Exception) { bytesRead = 0; } if (bytesRead == 0) { clientStream.Close(); tcpCli.Close(); return; } if (FMPacket.IsTeltonika(message)) { teltonikaImei = FMPacket.GetIMEI(message); contenido.Clear(); try { message[0] = 0x01; clientStream.Write(message, 0, 1); clientStream.Flush(); } catch (System.IO.IOException) { } } Array.Clear(message, 0, TAM_BUFFER); try { bytesRead = clientStream.Read(message, 0, TAM_BUFFER); while (bytesRead > 0) { for (int i = 0; i < bytesRead; i++) { contenido.Add(message[i]); } Array.Clear(message, 0, TAM_BUFFER); bytesRead = clientStream.Read(message, 0, TAM_BUFFER); } } catch (Exception) { } message = contenido.ToArray(); bytesRead = message.Length; if (bytesRead > 0) { Array.Resize(ref message, bytesRead); if (FMPacket.IsTeltonika(message)) { var listaFM = FMPacket.DecodeMessage(message, teltonikaImei); try { byte[] resp = Int2BigEndian(listaFM.Count); clientStream.Write(resp, 0, resp.Length); clientStream.Flush(); } catch (System.IO.IOException) { } foreach (FMPacket fmP in listaFM) { this.InsertaLectura(fmP); } } else if (MeiligaoProcessor.IsMeiligao(message)) { MeiligaoPacket meiP = MeiligaoProcessor.DecodeMessage(message); if (meiP.HeaderOK && meiP.Checksum != 0) { this.InsertaLectura(meiP); } else { SqlGps.InsertaError(message); } } else if (SyrusProcessor.IsSyrus(message)) { //AuxSystemLog.Information("SYRUS: [{0}]{1}{2}", message.Length, Environment.NewLine, BitConverter.ToString(message)); string idGps = SyrusProcessor.GetIDFromRXART(message); if (!string.IsNullOrEmpty(idGps)) { try { byte[] resp = System.Text.Encoding.ASCII.GetBytes(idGps); clientStream.Write(resp, 0, resp.Length); clientStream.Flush(); } catch (System.IO.IOException) { } } else { var listaSyrus = SyrusProcessor.DecodeMessage(message); foreach (SyrusPacket syrP in listaSyrus) { if (syrP.OK) { this.InsertaLectura(syrP); try { byte[] resp = System.Text.Encoding.ASCII.GetBytes(syrP.ID); clientStream.Write(resp, 0, resp.Length); clientStream.Flush(); } catch (System.IO.IOException) { } } else { AuxSystemLog.Warning("SYRUS-ERROR: [{0}]{1}{2}", message.Length, Environment.NewLine, BitConverter.ToString(message)); } } } } else { AuxSystemLog.Warning("ERROR: [{0}]{1}{2}", message.Length, Environment.NewLine, BitConverter.ToString(message)); SqlGps.InsertaError(message); } } clientStream.Close(); tcpCli.Close(); }