private void PBtnSalvar_Click(object sender, EventArgs e)
        {
            bool bValidarTela = ValidarTela();

            if (!bValidarTela)
            {
                return;
            }

            ConexaoBO conBO   = new ConexaoBO();
            bool      bSalvou = conBO.Salvar_LocalHost(pEdIP.Text, pEdNomeDB.Text, pEdPorta.Text, pEdUsuario.Text, pEdSenha.Text);

            string sMensagem = string.Empty;

            if (bSalvou)
            {
                sMensagem = "Configurações salvas com sucesso.";
            }
            else
            {
                sMensagem = "Erro ao salvar configurações.";
            }

            Toast.MakeText(this, sMensagem, ToastLength.Short).Show();
        }
Exemple #2
0
 public void loop_enviaExistentes()
 {
     tempo_decorrido.Restart();
     while (!para_looping_envios)
     {
         if (tempo_decorrido.ElapsedMilliseconds >= 2000)
         {
             if (esperandoF)
             {
                 cont_conexao_erro++;
                 if (cont_conexao_erro >= 3)
                 {
                     MessageBox.Show("Conexão perdida.", "Erro!",
                                     MessageBoxButtons.OK,
                                     MessageBoxIcon.Error);
                     esperandoF        = false;
                     cont_conexao_erro = 0;
                     Ligar.Invoke(new MethodInvoker(() =>
                     {
                         Ligar.Enabled = true;
                         Ligar.PerformClick();
                     }));
                 }
             }
             envios.Fila.Add(15);
             tempo_decorrido.Restart();
             esperandoF = true;
         }
         if (envios.Fila.Count > 0 && em_uso == false)
         {
             em_uso = true;
             var tmpDados = new List <byte>();
             foreach (var dadoItem in envios.Fila.ToList())
             {
                 if (dadoItem == -5)
                 {
                     ConexaoBO.enviaDados(tmpDados, SrpComm);
                     tmpDados.Clear();
                 }
                 else
                 {
                     tmpDados.Add(dadoItem);
                 }
             }
             if (tmpDados.Count > 0)
             {
                 ConexaoBO.enviaDados(tmpDados, SrpComm);
             }
             envios.Fila.Clear();
             em_uso = false;
         }
         //Thread.Sleep(50);
         ProcessaDados();
     }
     Thread.Sleep(1000);
     SrpComm.Close();
 }
Exemple #3
0
        private void RetornaAnunciosVitrine()
        {
            AnuncioBO obj = new AnuncioBO();

            DataList1.DataSource = obj.RetornaAnuncioVitrine();
            DataList1.DataBind();

            ConexaoBO con = new ConexaoBO();

            con.Desconectar();
        }
        private void CarregarConexao()
        {
            ConexaoBO  cBO  = new ConexaoBO();
            ConexaoDTO cDTO = cBO.ObterConexao_LocalHost();

            if (cDTO == null)
            {
                string sMensagem = "Preencha os campos!";
                Toast.MakeText(this, sMensagem, ToastLength.Long).Show();

                return;
            }

            pEdIP.Text      = cDTO.IP;
            pEdNomeDB.Text  = cDTO.NomeDB;
            pEdPorta.Text   = cDTO.Porta;
            pEdUsuario.Text = cDTO.Usuario;
            pEdSenha.Text   = cDTO.Senha;
        }
Exemple #5
0
        public bool AtualizaCiclo()
        {
            List <string> mensagens = new List <string>();
            //Lista ciclos em Andamento
            List <VO.Ciclos> ciclos = CicloDAO.listaCiclosPorSituacaoAll(0);

            t = new Thread(new ThreadStart(carregarAguarde));
            //this.Enabled = false;

            try
            {
                for (int i = 0; i < ciclos.Count; i++)
                {
                    VO.Ciclos           cic           = ciclos[i];
                    List <ProdutoCiclo> listaProdutos = ProdutoCicloDAO.listaProdutosCiclo(cic);
                    VO.Ciclos           ciclo         = new VO.Ciclos();

                    string porta    = ConfiguracaoDAO.retornaPorta();
                    bool   continua = false;
                    if (!t.IsAlive)
                    {
                        t.Start();
                    }

                    var srpComm = new SerialPort(porta, 19200, Parity.None, 8, StopBits.One);
                    srpComm.DtrEnable       = true;
                    srpComm.RtsEnable       = false;
                    srpComm.ReadBufferSize  = 100000;
                    srpComm.WriteBufferSize = 100000;

                    Conexao.srpComm = srpComm;
                    Conexao.srpComm.Open();
                    Thread.Sleep(100);

                    int numBytes = 0;
                    int aux      = cic.crg + 63;
                    Conexao.srpComm.Write(((char)19).ToString());
                    Thread.Sleep(100);
                    Conexao.srpComm.Write(((char)17).ToString());
                    Thread.Sleep(100);
                    Conexao.srpComm.Write(((char)aux).ToString());
                    Thread.Sleep(100);

                    if (Conexao.srpComm.BytesToRead != 0)
                    {
                        numBytes       = byte.Parse(Conexao.srpComm.BytesToRead.ToString());
                        Conexao.buffer = new byte[numBytes];
                        Conexao.srpComm.Read(Conexao.buffer, 0, numBytes);
                    }
                    continua = true;
                    if (continua)
                    {
                        // int cont = 0;
                        //Verifica se é aparelho novo ou antigo
                        switch (Conexao.buffer[0])
                        {
                        //Versão nova até 150ºC
                        case 167:
                            string binario = decimalParaBinario(cic.nTrat);
                            numTrat1 = binarioParaDecimal(binario.Substring(0, 8));
                            numTrat2 = binarioParaDecimal(binario.Substring(8, 8));

                            Conexao.srpComm.Write(((char)5).ToString());
                            Thread.Sleep(100);
                            Conexao.srpComm.Write(((char)numTrat1).ToString());
                            Thread.Sleep(100);
                            Conexao.srpComm.Write(((char)numTrat2).ToString());
                            Thread.Sleep(8000);

                            int contador = 0;
                            do
                            {
                                if (Conexao.srpComm.BytesToRead != 0)
                                {
                                    numBytes       = int.Parse(Conexao.srpComm.BytesToRead.ToString());
                                    Conexao.buffer = new byte[numBytes];
                                    Conexao.srpComm.Read(Conexao.buffer, 0, numBytes);
                                }
                                contador++;
                            } while (Conexao.srpComm.BytesToRead != 0);

                            int checksum = 0;
                            //Verifica se o ciclo ainda está na memória do equipamento
                            if (Conexao.buffer.Length > 2)
                            {
                                for (int j = 0; j < Conexao.buffer.Length - 2; j++)
                                {
                                    checksum ^= Conexao.buffer[j];
                                }
                                if (checksum == Conexao.buffer[Conexao.buffer.Length - 2])
                                {
                                    int aux2 = Conexao.buffer.Length - 10;
                                    cic.numSerie = ((char)Conexao.buffer[aux2]).ToString() +
                                                   ((char)Conexao.buffer[aux2 + 1]).ToString() +
                                                   ((char)Conexao.buffer[aux2 + 2]).ToString() +
                                                   ((char)Conexao.buffer[aux2 + 3]).ToString() +
                                                   ((char)Conexao.buffer[aux2 + 4]).ToString() +
                                                   ((char)Conexao.buffer[aux2 + 5]).ToString() +
                                                   ((char)Conexao.buffer[aux2 + 6]).ToString() +
                                                   ((char)Conexao.buffer[aux2 + 7]).ToString();
                                    cic.nTrat = (Conexao.buffer[0] * 256) + Conexao.buffer[1];
                                    string dataInicio =
                                        string.Concat((Conexao.buffer[7] / 16), (Conexao.buffer[7] % 16)) +
                                        "/" +
                                        string.Concat((Conexao.buffer[8] / 16), (Conexao.buffer[8] % 16)) +
                                        "/20" +
                                        string.Concat((Conexao.buffer[9] / 16), (Conexao.buffer[9] % 16)) +
                                        " " +
                                        string.Concat((Conexao.buffer[5] / 16), (Conexao.buffer[5] % 16)) +
                                        ":" +
                                        string.Concat((Conexao.buffer[4] / 16), (Conexao.buffer[4] % 16));
                                    cic.dataInicio = Convert.ToDateTime(dataInicio);
                                    cic.dataColeta = DateTime.Now;

                                    bool retorno = false;


                                    int indiceLeitCiclo = LeiturasCicloDAO.retornaQtdeLeituras(cic.id,
                                                                                               cic.crg);
                                    int indiceLeitTrat = LeiturasTratDAO.retornaQtdeLeituras(cic.id, cic.crg);
                                    retorno = ConexaoBO.EnviaBancodeDados(cic, Conexao.buffer, true,
                                                                          listaProdutos, indiceLeitCiclo, indiceLeitTrat, true);
                                    if (retorno)
                                    {
                                        AtualizaUltimo(cic.operador, cic.responsavel);
                                        mensagens.Add("O ciclo nº " + cic.nTrat + " do aparelho nº " + cic.crg +
                                                      " foi atualizado com sucesso.");
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("Erro no checksum.", "Erro", MessageBoxButtons.OK,
                                                    MessageBoxIcon.Information);
                                }
                            }
                            else
                            {
                                mensagens.Add("O ciclo nº " + cic.nTrat + " do aparelho nº " + cic.crg +
                                              " nao está mais na memória do equipamento.");
                            }
                            break;

                        //Versão nova até 102ºC
                        case 166:
                            string binario1 = decimalParaBinario(cic.nTrat);
                            numTrat1 = binarioParaDecimal(binario1.Substring(0, 8));
                            numTrat2 = binarioParaDecimal(binario1.Substring(8, 8));

                            Conexao.srpComm.Write(((char)5).ToString());
                            Thread.Sleep(100);
                            Conexao.srpComm.Write(((char)numTrat1).ToString());
                            Thread.Sleep(100);
                            Conexao.srpComm.Write(((char)numTrat2).ToString());
                            Thread.Sleep(8000);

                            int contador1 = 0;
                            do
                            {
                                if (Conexao.srpComm.BytesToRead != 0)
                                {
                                    numBytes       = int.Parse(Conexao.srpComm.BytesToRead.ToString());
                                    Conexao.buffer = new byte[numBytes];
                                    Conexao.srpComm.Read(Conexao.buffer, 0, numBytes);
                                }
                                contador1++;
                            } while (Conexao.srpComm.BytesToRead != 0);

                            int checksum1 = 0;
                            //Verifica se o ciclo ainda está na memória do equipamento
                            if (Conexao.buffer.Length > 2)
                            {
                                for (int j = 0; j < Conexao.buffer.Length - 2; j++)
                                {
                                    checksum1 ^= Conexao.buffer[j];
                                }
                                if (checksum1 == Conexao.buffer[Conexao.buffer.Length - 2])
                                {
                                    int aux2 = Conexao.buffer.Length - 10;
                                    ciclo.numSerie = ((char)Conexao.buffer[aux2]).ToString() +
                                                     ((char)Conexao.buffer[aux2 + 1]).ToString() +
                                                     ((char)Conexao.buffer[aux2 + 2]).ToString() +
                                                     ((char)Conexao.buffer[aux2 + 3]).ToString() +
                                                     ((char)Conexao.buffer[aux2 + 4]).ToString() +
                                                     ((char)Conexao.buffer[aux2 + 5]).ToString() +
                                                     ((char)Conexao.buffer[aux2 + 6]).ToString() +
                                                     ((char)Conexao.buffer[aux2 + 7]).ToString();
                                    ciclo.nTrat = (Conexao.buffer[0] * 256) + Conexao.buffer[1];
                                    string dataInicio =
                                        string.Concat((Conexao.buffer[7] / 16), (Conexao.buffer[7] % 16)) +
                                        "/" +
                                        string.Concat((Conexao.buffer[8] / 16), (Conexao.buffer[8] % 16)) +
                                        "/20" +
                                        string.Concat((Conexao.buffer[9] / 16), (Conexao.buffer[9] % 16)) +
                                        " " +
                                        string.Concat((Conexao.buffer[5] / 16), (Conexao.buffer[5] % 16)) +
                                        ":" +
                                        string.Concat((Conexao.buffer[4] / 16), (Conexao.buffer[4] % 16));
                                    ciclo.dataInicio = Convert.ToDateTime(dataInicio);
                                    ciclo.dataColeta = DateTime.Now;

                                    bool retorno = false;


                                    int indiceLeitCiclo = LeiturasCicloDAO.retornaQtdeLeituras(ciclo.id,
                                                                                               ciclo.crg);
                                    int indiceLeitTrat = LeiturasTratDAO.retornaQtdeLeituras(ciclo.id, ciclo.crg);
                                    retorno = ConexaoBO.EnviaBancodeDados(ciclo, Conexao.buffer, true,
                                                                          listaProdutos, indiceLeitCiclo, indiceLeitTrat, false);
                                    if (retorno)
                                    {
                                        AtualizaUltimo(cic.operador, cic.responsavel);
                                        mensagens.Add("O ciclo nº " + cic.nTrat + " do aparelho nº " + cic.crg +
                                                      " foi atualizado com sucesso.");
                                    }
                                }
                                else
                                {
                                    mensagens.Add("Não foi possível atualizar o ciclo nº " + cic.nTrat +
                                                  " do aparelho nº " + cic.crg + ". Erro de Checksum.");
                                }
                            }
                            else
                            {
                                mensagens.Add("O ciclo nº " + cic.nTrat + " do aparelho nº " + cic.crg +
                                              " nao está mais na memória do equipamento.");
                            }
                            break;

                        //Versão antiga até 102ºC apenas
                        case 165:
                            Conexao.srpComm.Write(((char)6).ToString());
                            Thread.Sleep(8000);

                            int cont = 0;
                            do
                            {
                                if (Conexao.srpComm.BytesToRead != 0)
                                {
                                    numBytes       = int.Parse(Conexao.srpComm.BytesToRead.ToString());
                                    Conexao.buffer = new byte[numBytes];
                                    Conexao.srpComm.Read(Conexao.buffer, 0, numBytes);
                                }
                                cont++;
                            } while (Conexao.srpComm.BytesToRead != 0);

                            int chk = 0;
                            for (int j = 0; j < Conexao.buffer.Length - 2; j++)
                            {
                                chk ^= Conexao.buffer[j];
                            }

                            if (((Conexao.buffer[5] * 256) + Conexao.buffer[2]) == cic.nTrat)
                            {
                                if (chk == Conexao.buffer[Conexao.buffer.Length - 2])
                                {
                                    int aux2 = Conexao.buffer.Length - 10;
                                    cic.numSerie = ((char)Conexao.buffer[aux2]).ToString() +
                                                   ((char)Conexao.buffer[aux2 + 1]).ToString() +
                                                   ((char)Conexao.buffer[aux2 + 2]).ToString() +
                                                   ((char)Conexao.buffer[aux2 + 3]).ToString() +
                                                   ((char)Conexao.buffer[aux2 + 4]).ToString() +
                                                   ((char)Conexao.buffer[aux2 + 5]).ToString() +
                                                   ((char)Conexao.buffer[aux2 + 6]).ToString() +
                                                   ((char)Conexao.buffer[aux2 + 7]).ToString();
                                    cic.nTrat = (Conexao.buffer[5] * 256) + Conexao.buffer[2];
                                    string dataInicio =
                                        string.Concat((Conexao.buffer[6] / 16), (Conexao.buffer[6] % 16)) + "/" +
                                        string.Concat((Conexao.buffer[7] / 16), (Conexao.buffer[7] % 16)) + "/20" +
                                        string.Concat((Conexao.buffer[8] / 16), (Conexao.buffer[8] % 16)) + " " +
                                        string.Concat((Conexao.buffer[4] / 16), (Conexao.buffer[4] % 16)) + ":" +
                                        string.Concat((Conexao.buffer[3] / 16), (Conexao.buffer[3] % 16));
                                    cic.dataInicio = Convert.ToDateTime(dataInicio);
                                    cic.dataColeta = DateTime.Now;

                                    bool retorno = false;

                                    int indiceLeitCiclo = LeiturasCicloDAO.retornaQtdeLeituras(cic.id,
                                                                                               cic.crg);
                                    int indiceLeitTrat = LeiturasTratDAO.retornaQtdeLeituras(cic.id,
                                                                                             cic.crg);
                                    retorno = ConexaoBO.EnviaBancodeDadosAntigo(cic, Conexao.buffer, true,
                                                                                listaProdutos, indiceLeitCiclo, indiceLeitTrat);
                                    if (retorno)
                                    {
                                        AtualizaUltimo(cic.operador, cic.responsavel);
                                        mensagens.Add("O ciclo nº " + cic.nTrat + " do aparelho nº " + cic.crg +
                                                      " foi atualizado com sucesso.");
                                    }
                                }
                                else
                                {
                                    mensagens.Add("Não foi possível atualizar o ciclo nº " + cic.nTrat +
                                                  " do aparelho nº " + cic.crg + ". Erro de Checksum.");
                                }
                            }
                            else
                            {
                                mensagens.Add("O ciclo nº " + cic.nTrat + " do aparelho nº " + cic.crg +
                                              " não está mais no histórico.");
                            }


                            break;
                        }
                    }
                    Conexao.srpComm.Close();
                    Thread.Sleep(100);
                }
                t.Abort();

                if (mensagens.Count > 0)
                {
                    StatusAtualizacao frm = new StatusAtualizacao(mensagens);
                    frm.ShowDialog(this);
                }
                return(true);
            }
            catch (Exception error)
            {
                VO.LogErro logErro = new VO.LogErro();
                logErro.crg          = 0;
                logErro.descricao    = "Erro ao tentar receber o ciclo";
                logErro.maisDetalhes = error.Message + " " + error.StackTrace;
                logErro.data         = DateTime.Now;
                LogErroDAO.inserirLogErro(logErro, logErro.crg);
                t.Abort();
                return(false);
            }
        }