/// <summary> /// Envia o texto de forma síncrona e em pequenos pacotes. /// <para>Isso foi feito para não dar erros no servidor ao enviar pacotes grandes</para> /// </summary> /// <param name="response"></param> static void SendText(ServerDataTransfer response) { client.SendText(response, 1024); ServerLog.Log(Settings.Server.IPAddress, TipoEvento.ClientOnSend, response.Message); }
private static void WhoIs(ServerDataTransfer response) { //Procurar se a maquina na qual está conectando é um PDV IList<IPDV> pdvs = new PDV().Find<IPDV>(new Where { { "cad_PDV.Nome", Settings.MachineName } }); //Se for PDV, manda as informações do PDV e impressora fiscal if(pdvs.Count > 0) { IList<IImpressora> impressoras = new Data.ECF.Cadastro.Impressora().Find<IImpressora>(new Where { {"cad_PDV.Nome LIKE @p1", new Parameter { ParameterName = "@p1", Value = Settings.MachineName } }}); response["PDV"] = Serializer.Serialize(pdvs[0]); if(impressoras.Count > 0) { IImpressora impressora = impressoras[0]; response["Impressora"] = Serializer.Serialize(impressora); } response["IsPDV"] = true; } else response["IsPDV"] = false; SendText(response); }
private static void SaveModelResponse(ServerDataTransfer received) { Connection connection = null; IParentModel model = null; if(!String.IsNullOrEmpty(received.Object)) model = Deserializer.Deserialize<IParentModel>(received.Object); if(received.Success == false) { CatchReturnServer(received); } else { try { if(received.Success && model is ILancamento) { connection = DbContext.CreateConnection(true, true); connection.ExecuteNonQuery("UPDATE fat_Lan SET StatusEnvio = @p1 WHERE @p2", new[] { new Parameter{ ParameterName = "@p1", Value = (int)Enuns.Faturamento.Lancamento.StatusEnvio.EnviadoServer }, new Parameter{ ParameterName = "@p2", Value = model.GUID } }); } else CatchReturnServer(received); } finally { if(connection != null) connection.Close(); } } if(model != null) BufferResponse[model.GUID] = false; }
private static void InitApp(ServerDataTransfer response, ServerDataTransfer received) { if(!received.Success) { Status = received.Status; CatchReturnServer(received); return; } else { #region Atualização dos lançamentos System.Timers.Timer timer = new System.Timers.Timer(10000) { AutoReset = true, Enabled = true }; timer.Elapsed += (s, e) => { if(Busy) return; Busy = true; timer.Stop(); if(Status == Enuns.UniBot.Server.Status.Connecting || SendToServerBuffer.Buffer.Busy) { timer.Start(); Busy = false; return; } #region não conectado if(!client.Connected) { Status = Enuns.UniBot.Server.Status.Connecting; client.Connect(); timer.Start(); Busy = false; return; } #endregion #region save model if(SendToServerBuffer.Buffer.Count == 0) SendToServerBuffer.Buffer.Refresh(); IParentModel model = SendToServerBuffer.Buffer.Get(); if(model != null) { response["Command"] = "SaveModel"; response["type"] = model.GetType().FullName; response[IntegrationClient.DoIntegration] = true; response.Object = Serializer.Serialize(model); SendText(response); //------------------------------------------------------------------------- // No método SaveModelResponse , esta variável será retornada para false //------------------------------------------------------------------------- BufferResponse[model.GUID] = true; int tick = (int)(DateTime.Now.Ticks / Math.Pow(10, 10)); int timeout = tick + 600000; //------------------------------------------------------------------------- // Vai sair por timeout, ou pela resposta do servidor //------------------------------------------------------------------------- while(timeout > tick && client.Connected && BufferResponse[model.GUID]) { Thread.Sleep(1000); tick = (int)(DateTime.Now.Ticks / Math.Pow(10, 10)); } BufferResponse.Remove(model.GUID); } else CatchReturnServer(received); #endregion #region Integration if(!waitStarted && integrationFinished) StartIntegration(true); #endregion timer.Start(); Busy = false; }; #endregion Success = true; Status = received.Status; } }
private static void ErrorState(ServerDataTransfer response, ServerDataTransfer received) { if(integrationClient != null) integrationClient.SetErrorEstate(); else { switch(received.Status) { case Status.NotAuthorized: case Status.Rejected: MessageBox.ShowWarning(received.Message); break; case Status.Error: case Status.Undefined: case Status.Connecting: case Status.Connected: default: break; } } }
static void client_OnRead(System.Net.Sockets.Socket soc) { ServerDataTransfer response = ServerDataTransfer.CreateClient(); string receivedText = client.ReceivedText.Replace("\0", ""); PDVClient.ReceivedText += receivedText; #region Log ServerLog.Log(Settings.Server.IPAddress, TipoEvento.ClientOnRead, receivedText); #endregion if(!PDVClient.ReceiveComplete) return; //------------------------------------------------------------------------- // Se o comando não veio do servidor ... ignorar //------------------------------------------------------------------------- if(soc.GetIPAddress().ToString() == Settings.Server.IPAddress.ToString()) { ServerDataTransfer received = new ServerDataTransfer(PDVClient.ReceivedText); PDVClient.ReceivedText = ""; #region whois if(received.Command == "WhoIs") { WhoIs(response); } #endregion #region SaveModel response else if(received.Command == "SaveModel") { SaveModelResponse(received); } #endregion #region Integration else if(received.Command == IntegrationClient.Started) { DoIntegration(); } else if(received.Command == IntegrationClient.ModelResult) { integrationClient.SetResponse(received); } #endregion #region InitApp else if(received.Command == "InitApp") { InitApp(response, received); } #endregion #region Error State else if(received.Command == "ErrorState") { ErrorState(response, received); } #endregion } }
private static void CatchReturnServer(ServerDataTransfer sdt) { if(sdt.ContainsKey(IntegrationClient.DoIntegration) && Unimake.Convert.ToBoolean(sdt[IntegrationClient.DoIntegration])) { ServerLog.Log(Settings.Server.IPAddress, TipoEvento.ClientOnError, sdt.Message); } else { switch(sdt.Status) { case OpenPOS.Enuns.UniBot.Server.Status.Connected: //Ok. Pode continuar sem erros //por enquanto não existe nenhum tratamento break; case OpenPOS.Enuns.UniBot.Server.Status.Rejected: MessageBox.Show("A conexão foi rejeitada pelo servidor.\r\n" + sdt.Message, MessageBoxIcon.Error); return; case OpenPOS.Enuns.UniBot.Server.Status.Error: MessageBox.ShowError("O Servidor retornou um erro.\r\n\r\n" + sdt.Message); return; case OpenPOS.Enuns.UniBot.Server.Status.NotAuthorized: MessageBox.Show("A conexão ao servidor não foi autorizada.\r\n" + sdt.Message, MessageBoxIcon.Stop); return; default: MessageBox.ShowWarning("O Servidor retornou um estado desconhecido.\r\n" + sdt.Message); return; } } return; }