Commit dbe4bfb5 authored by Brayan Sarmiento's avatar Brayan Sarmiento
Browse files

Conexion Posbc -> ApiGateway

parent e59eba09
...@@ -17,6 +17,10 @@ namespace gatewayGK.POSBC ...@@ -17,6 +17,10 @@ namespace gatewayGK.POSBC
/// <summary> /// <summary>
/// Propiedad que toma el buffer de entrada de CHEC para enviarselo al POSBC /// Propiedad que toma el buffer de entrada de CHEC para enviarselo al POSBC
/// </summary> /// </summary>
public byte[] BufferSalida { get; set; } = new byte[0];
/// <summary>
/// Propiedad que toma el buffer de salida del POSBC para mandarselo a chec
/// </summary>
public int CantBytes { get; set; } = 0; public int CantBytes { get; set; } = 0;
/// <summary> /// <summary>
/// Propiedad que toma la cantidad de bytes que vienen de CHEC /// Propiedad que toma la cantidad de bytes que vienen de CHEC
......
...@@ -44,7 +44,6 @@ namespace gatewayGK.POSBC ...@@ -44,7 +44,6 @@ namespace gatewayGK.POSBC
{ {
// Crear un socket TCP/IP // Crear un socket TCP/IP
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Configurar el endpoint (IP y puerto) // Configurar el endpoint (IP y puerto)
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(direccionIpPosbc), puerto); IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(direccionIpPosbc), puerto);
...@@ -52,20 +51,57 @@ namespace gatewayGK.POSBC ...@@ -52,20 +51,57 @@ namespace gatewayGK.POSBC
socket.Connect(endPoint); socket.Connect(endPoint);
Log.Information("Conectandose al servidor POSBC"); Log.Information("Conectandose al servidor POSBC");
// Enviar los datos
socket.Send(bufferRecibido, SocketFlags.None);
Log.Information($"Buffer de entrada enviado al servidor POSBC {bufferRecibido}");
// Buffer para recibir la respuesta - Revisar cual es la mejor manera de recibir ese
List<byte> receivedBytes = new List<byte>();
byte[] bufferSalida = new byte[1024];
// Enviar los datos
socket.Send(bufferRecibido, SocketFlags.None);
Log.Information($"Datos enviados al servidor POSBC {dataToSend}");
// Buffer para recibir la respuesta while (true)
byte[] buffer = new byte[1024]; {
int bytesReceived = socket.Receive(buffer); int bytesLeidos = 0;
bytesLeidos = socket.Receive(bufferSalida, 0, bufferSalida.Length, SocketFlags.None);
//Coloque 1 Porque POSBC si no tiene mas bytes que mandar, manda 1 y no 0
if (bytesLeidos == 0 || bytesLeidos == 1)
{
// El socket se ha cerrado
break;
}
// Añadir los bytes leídos a la lista
for (int i = 0; i < bytesLeidos; i++)
{
receivedBytes.Add(bufferSalida[i]);
}
}
byte[] data = receivedBytes.ToArray();
// Convertir el array de bytes a una cadena
string result = Encoding.UTF8.GetString(data);
Entorno<EntornoPOSBC>.Instancia.get().BufferSalida = data;
// Convertir los datos recibidos a una cadena
string response = Encoding.UTF8.GetString(buffer, 0, bytesReceived);
Log.Information($"Respuesta recibida del POSBC {response}");
//buffer de salida del POSBC
//byte[] bufferSalida = new byte[3048];
//int bytesLeidos = 0;
//bytesLeidos = socket.Receive(bufferSalida, bytesLeidos, bufferSalida.Length, SocketFlags.None);
//string pasarXml = Encoding.UTF8.GetString(bufferSalida, 0, bytesLeidos);
//Log.Information($"Xml Armado {pasarXml}");
//byte[] datosRecibidos = new byte[bytesLeidos];
//// Copiar los datos recibidos al nuevo array
//Array.Copy(bufferSalida, datosRecibidos, bytesLeidos);
//// Convertir los datos recibidos a una cadena
//bufferSalida = datosRecibidos;
////string response = Encoding.UTF8.GetString(buffer, 0, bytesReceived);
//Log.Information($"buffer de salida recibido del POSBC {bufferSalida}");
//Entorno<EntornoPOSBC>.Instancia.get().BufferSalida = bufferSalida;
// Cerrar el socket // Cerrar el socket
socket.Shutdown(SocketShutdown.Both); socket.Shutdown(SocketShutdown.Both);
socket.Close(); socket.Close();
...@@ -80,7 +116,7 @@ namespace gatewayGK.POSBC ...@@ -80,7 +116,7 @@ namespace gatewayGK.POSBC
{ {
Log.Error("Error:", ex.Message); Log.Error("Error:", ex.Message);
} }
//Esto como se manejara?
return new Respuestas(); return new Respuestas();
} }
......
using EvaPOS_API_FRAME.DTO; using EvaPOS_API_FRAME.DTO;
using EvaPosSrvDTO; using EvaPosSrvDTO;
using EvaPosSrvResp; using EvaPosSrvResp;
using GatewaySCO;
using Serilog;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
...@@ -23,9 +27,73 @@ namespace gatewayGK.POSBC ...@@ -23,9 +27,73 @@ namespace gatewayGK.POSBC
/// <summary> /// <summary>
/// Procesa y responde QueryStatusRequest. /// Procesa y responde QueryStatusRequest.
/// </summary> /// </summary>
///
public Respuestas Ejecutar() public Respuestas Ejecutar()
{ {
string direccionIpPosbc = "127.0.0.1";
int puerto = 6697;
byte[] bufferRecibido = Entorno<EntornoPOSBC>.Instancia.get().BufferEntrada;
int cantBytes = Entorno<EntornoPOSBC>.Instancia.get().CantBytes;
try
{
// Crear un socket TCP/IP
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Configurar el endpoint (IP y puerto)
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(direccionIpPosbc), puerto);
//Conectar al endpoint
socket.Connect(endPoint);
Log.Information("Conectandose al servidor POSBC");
// Enviar los datos
socket.Send(bufferRecibido, SocketFlags.None);
Log.Information($"Buffer de entrada enviado al servidor POSBC {bufferRecibido}");
// Buffer para recibir la respuesta - Revisar cual es la mejor manera de recibir ese
List<byte> receivedBytes = new List<byte>();
byte[] bufferSalida = new byte[1024];
while (true)
{
int bytesLeidos = 0;
bytesLeidos = socket.Receive(bufferSalida, 0, bufferSalida.Length, SocketFlags.None);
//Coloque 1 Porque POSBC si no tiene mas bytes que mandar, manda 1 y no 0
if (bytesLeidos == 0 || bytesLeidos == 1)
{
// El socket se ha cerrado
break;
}
// Añadir los bytes leídos a la lista
for (int i = 0; i < bytesLeidos; i++)
{
receivedBytes.Add(bufferSalida[i]);
}
}
byte[] data = receivedBytes.ToArray();
// Convertir el array de bytes a una cadena
string result = Encoding.UTF8.GetString(data);
Entorno<EntornoPOSBC>.Instancia.get().BufferSalida = data;
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
catch (SocketException ex)
{
Log.Error($"Error de Socket: {ex.Message}");
Log.Error($"Código de error de Socket: {ex.SocketErrorCode}");
Log.Error(ex.StackTrace);
}
catch (Exception ex)
{
Log.Error("Error:", ex.Message);
}
//Esto como se manejara?
return new Respuestas(); return new Respuestas();
} }
......
using EvaPOS_API_FRAME.DTO; using EvaPOS_API_FRAME.DTO;
using EvaPosSrvDTO; using EvaPosSrvDTO;
using EvaPosSrvResp; using EvaPosSrvResp;
using GatewaySCO;
using Serilog;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
...@@ -25,8 +29,71 @@ namespace gatewayGK.POSBC ...@@ -25,8 +29,71 @@ namespace gatewayGK.POSBC
/// </summary> /// </summary>
/// ///
public Respuestas Ejecutar() public Respuestas Ejecutar()
{ {
return new Respuestas(); string direccionIpPosbc = "127.0.0.1";
int puerto = 6697;
byte[] bufferRecibido = Entorno<EntornoPOSBC>.Instancia.get().BufferEntrada;
int cantBytes = Entorno<EntornoPOSBC>.Instancia.get().CantBytes;
try
{
// Crear un socket TCP/IP
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Configurar el endpoint (IP y puerto)
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(direccionIpPosbc), puerto);
//Conectar al endpoint
socket.Connect(endPoint);
Log.Information("Conectandose al servidor POSBC");
// Enviar los datos
socket.Send(bufferRecibido, SocketFlags.None);
Log.Information($"Buffer de entrada enviado al servidor POSBC {bufferRecibido}");
// Buffer para recibir la respuesta - Revisar cual es la mejor manera de recibir ese
List<byte> receivedBytes = new List<byte>();
byte[] bufferSalida = new byte[1024];
while (true)
{
int bytesLeidos = 0;
bytesLeidos = socket.Receive(bufferSalida, 0, bufferSalida.Length, SocketFlags.None);
//Coloque 1 Porque POSBC si no tiene mas bytes que mandar, manda 1 y no 0
if (bytesLeidos == 0 || bytesLeidos == 1)
{
// El socket se ha cerrado
break;
}
// Añadir los bytes leídos a la lista
for (int i = 0; i < bytesLeidos; i++)
{
receivedBytes.Add(bufferSalida[i]);
}
}
byte[] data = receivedBytes.ToArray();
// Convertir el array de bytes a una cadena
string result = Encoding.UTF8.GetString(data);
Entorno<EntornoPOSBC>.Instancia.get().BufferSalida = data;
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
catch (SocketException ex)
{
Log.Error($"Error de Socket: {ex.Message}");
Log.Error($"Código de error de Socket: {ex.SocketErrorCode}");
Log.Error(ex.StackTrace);
}
catch (Exception ex)
{
Log.Error("Error:", ex.Message);
}
//Esto como se manejara?
return new Respuestas();
} }
public IComando CreaCopia() public IComando CreaCopia()
......
...@@ -56,7 +56,7 @@ namespace GatewaySCO ...@@ -56,7 +56,7 @@ namespace GatewaySCO
Entorno<EntornoGK>.Instancia.get().ConfigGk = configGk; Entorno<EntornoGK>.Instancia.get().ConfigGk = configGk;
Log.Information($"GK {Entorno<EntornoGK>.Instancia.get().UrlBase}"); Log.Information($"GK {Entorno<EntornoGK>.Instancia.get().UrlBase}");
} }
if(config.POS == "posbc") if(config.POS == "posbc" || config.POS == "pruebas")
{ {
Entorno<EntornoPOSBC>.Instancia.set(new EntornoPOSBC()); Entorno<EntornoPOSBC>.Instancia.set(new EntornoPOSBC());
Log.Information($"Inicio del entorno POSBC"); Log.Information($"Inicio del entorno POSBC");
......
...@@ -14,6 +14,7 @@ using EvaPosSrvAplicacion; ...@@ -14,6 +14,7 @@ using EvaPosSrvAplicacion;
using EvaPOS_API_FRAME.RespuestasXML; using EvaPOS_API_FRAME.RespuestasXML;
using EvaPOS_API_FRAME.Comandos; using EvaPOS_API_FRAME.Comandos;
using gatewayGK.POSBC; using gatewayGK.POSBC;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace EvaPosSCOSrv namespace EvaPosSCOSrv
{ {
/// <summary> /// <summary>
...@@ -291,6 +292,8 @@ namespace EvaPosSCOSrv ...@@ -291,6 +292,8 @@ namespace EvaPosSCOSrv
byte[] datosRecibidos = new byte[bytesLeidos]; byte[] datosRecibidos = new byte[bytesLeidos];
// Copiar los datos recibidos al nuevo array // Copiar los datos recibidos al nuevo array
Array.Copy(bufferEntrada, datosRecibidos, bytesLeidos); Array.Copy(bufferEntrada, datosRecibidos, bytesLeidos);
//Revisamos que xml envio Chec
string resultadoXml = Encoding.UTF8.GetString(bufferEntrada);
bufferEntrada = datosRecibidos; bufferEntrada = datosRecibidos;
} }
else else
...@@ -339,17 +342,28 @@ namespace EvaPosSCOSrv ...@@ -339,17 +342,28 @@ namespace EvaPosSCOSrv
} }
respuestas = _aplicacion.Procesar(cmd); respuestas = _aplicacion.Procesar(cmd);
Log.Debug("Respuestas de cmd ref '{cmd}' : {nroRespuestas}", cmd.Referencia, respuestas.Count); //TODO - Logica para mandar la respuesta del POSBC
if(entornoApi == "posbc")
// Enviando respuestas.
int i = 1;
foreach (var respuesta in respuestas)
{ {
Log.Debug("Respuesta #{i}", i++); byte[] bufferSalidaRecibido = Entorno<EntornoPOSBC>.Instancia.get().BufferSalida;
var bufferSalida = _sesion.Salida(respuesta.TramaSCO); socket.Send(bufferSalidaRecibido, 0, bufferSalidaRecibido.Length, SocketFlags.None);
socket.Send(bufferSalida, 0, bufferSalida.Length, SocketFlags.None); Log.Information("Bytes enviados {bytes} del servidor POSBC", bufferSalidaRecibido.Length);
Log.Information("Bytes enviados {bytes}", bufferSalida.Length);
} }
else
{
Log.Debug("Respuestas de cmd ref '{cmd}' : {nroRespuestas}", cmd.Referencia, respuestas.Count);
// Enviando respuestas.
int i = 1;
foreach (var respuesta in respuestas)
{
Log.Debug("Respuesta #{i}", i++);
var bufferSalida = _sesion.Salida(respuesta.TramaSCO);
socket.Send(bufferSalida, 0, bufferSalida.Length, SocketFlags.None);
Log.Information("Bytes enviados {bytes}", bufferSalida.Length);
}
}
log.Information("Fin del ciclo, se enviaron {nro} respuestas", respuestas.Count); log.Information("Fin del ciclo, se enviaron {nro} respuestas", respuestas.Count);
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
"GatewayConfig": { "GatewayConfig": {
"POS": "posbc", "POS": "posbc",
"POS_comment": "Indicates the set of commands to instantiate, according to the type of POS: evapos, tests, gk,posbc etc.", "POS_comment": "Indicates the set of commands to instantiate, according to the type of POS: evapos, tests, gk,posbc etc.",
"IpSCO": "192.168.168.135", "IpSCO": "10.89.81.102",
"IpSCO_comment": "SCO IP, local or remote", "IpSCO_comment": "SCO IP, local or remote",
"PortSCO": 6690, "PortSCO": 6690,
"PortSCO_comment": "SCO IP Port", "PortSCO_comment": "SCO IP Port",
...@@ -24,6 +24,10 @@ ...@@ -24,6 +24,10 @@
"BusinessUnitGroupID": "1000", "BusinessUnitGroupID": "1000",
"TillID": "103" "TillID": "103"
}, },
"posbc": {
"DireccionIpPosbc": "127.0.0.1",
"PuertoPosbc": "6697"
},
"Serilog": { "Serilog": {
"Using": [ "Using": [
"Serilog.Sinks.Console", "Serilog.Sinks.Console",
......
...@@ -29,4 +29,6 @@ ...@@ -29,4 +29,6 @@
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" /> <PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup> </ItemGroup>
<ProjectExtensions><VisualStudio><UserProperties appsettings_1json__JsonSchema="" /></VisualStudio></ProjectExtensions>
</Project> </Project>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment