Exemplo n.º 1
0
        private void Command82()
        {
            if (Received.Count < 3)
            {
                return;
            }
            int numReceived = Received[1] * 255 + Received[2] + 4;

            // Recebe a resposta em +- 4 pacotes diferentes
            if (Received.Count != numReceived)
            {
                return;
            }

            if (!CheckSum(82))
            {
                return;
            }

            int _LoteAnterior = Convert.ToInt32(Received[3] * 255 + Received[4]);

            if (LoteAnterior == _LoteAnterior && Received[0] == 131)
            {
                isUltimoLote = true;
            }
            else
            {
                isUltimoLote = false;
                LoteAnterior = _LoteAnterior;
            }
            if (Received[0] == 130)
            {
                isFirst = false;
            }

            int    NumRegistro = Convert.ToInt32(Received[5] * 255 + Received[6]);
            int    NumLote     = Convert.ToInt32(Received[7]);
            string Calendario  = GetDate(new List <byte>()
            {
                Received[8], Received[9], Received[10], Received[11]
            });

            Lote novoLote = new Lote();

            novoLote.Maquina    = CurrentMachine;
            novoLote.NumLote    = NumLote;
            novoLote.NumeroLeit = NumRegistro;
            novoLote.Calendario = Calendario;
            if (Received[0] == 130)
            {
                novoLote.Endereco = 65536;
            }
            else
            {
                novoLote.Endereco = Convert.ToInt32(lastCommand[1] * 255 + lastCommand[2]);
            }
            lote = novoLote;

            lbCalendario.Text = Calendario;
            lbLote.Text       = NumLote.ToString();
            lbNumero.Text     = NumRegistro.ToString();
            if (isUltimoLote)
            {
                lbAnterior.Text = "--";
            }
            else
            {
                lbAnterior.Text = LoteAnterior.ToString();
            }

            Received.RemoveRange(0, 15);

            List <Leitura> list = new List <Leitura>();

            listView.Items.Clear();
            for (int i = 0; i < NumRegistro; i++)
            {
                try
                {
                    List <byte> data = new List <byte>();
                    data.Add(Received[i * 8 + 0]);
                    data.Add(Received[i * 8 + 1]);
                    data.Add(Received[i * 8 + 2]);
                    data.Add(Received[i * 8 + 3]);


                    string writeData   = GetDate(data);
                    string escala      = Received[i * 8 + 4].ToString();
                    string temperatura = Received[i * 8 + 5].ToString();
                    string leitura     = (Received[i * 8 + 6] * 255 + Received[i * 8 + 7]).ToString();

                    Leitura itemLeitura = new Leitura();
                    itemLeitura.Maquina      = CurrentMachine;
                    itemLeitura.Lote         = NumLote;
                    itemLeitura.LoteCal      = Calendario;
                    itemLeitura.Calendario   = writeData.Remove(Calendario.Length - 3);
                    itemLeitura.TipoMadeira  = GetTipo(Convert.ToInt32(escala));
                    itemLeitura.Temperatura  = Convert.ToInt32(temperatura) * 5 + 10;
                    itemLeitura.Densidade    = (Convert.ToInt32(escala) * 5 + 20) * 10;
                    itemLeitura.ValorLeitura = Convert.ToInt32(leitura);
                    list.Add(itemLeitura);
                }
                catch { }
            }
            listaLeituras = list;

            if (Firebird.LoteExists(lote))
            {
                lbExistente.Text = "Lote já Salvo";
            }
            else
            {
                lbExistente.Text = "Novo Lote";
            }

            WriteLeituras();
            SalvarLote();
            EndCommand();
        }
Exemplo n.º 2
0
        // Retirado pois agora salva de modo automático
        //private void btSalvarLote_Click(object sender, EventArgs e)
        //{
        //    if (lote == null || listaLeituras.Count == 0) return;
        //    if (!Firebird.UserHasPermission(0)) return;

        //    var lista = Firebird.ReturnListMaquina();
        //    bool Exists = false;
        //    foreach (var item in lista)
        //    {
        //        if (item == CurrentMachine)
        //        {
        //            Exists = true;
        //            break;
        //        }
        //    }

        //    if (!Exists)
        //    {
        //        DialogResult result = MessageBox.Show("Máquina não registrada, deseja salvá-la?", "Aviso", MessageBoxButtons.YesNo);
        //        if (result == DialogResult.Yes)
        //        {
        //            NovaMaquina form = new NovaMaquina(this);
        //            form.ShowDialog();

        //            if (!form.MachineSaved) return;
        //        }
        //        else return;
        //    }

        //    lote.Usuario = CurrentUser.UserName;
        //    Firebird.SaveNewLote(lote);
        //    Firebird.SaveLeituras(listaLeituras);

        //    MessageBox.Show("Lote salvo com sucesso!", "Sucesso!", MessageBoxButtons.OK, MessageBoxIcon.Information);
        //    lbExistente.Text = "Lote Salvo!";

        //    btSalvarLote.Enabled = false;
        //}
        public void SalvarLote()
        {
            if (lote == null || listaLeituras.Count == 0)
            {
                return;
            }
            if (!Firebird.UserHasPermission(0))
            {
                return;
            }

            if (!Firebird.LoteExists(lote))
            {
                var  lista  = Firebird.ReturnListMaquina();
                bool Exists = false;
                foreach (var item in lista)
                {
                    if (item == CurrentMachine)
                    {
                        Exists = true;
                        break;
                    }
                }

                if (!Exists)
                {
                    DialogResult result = MessageBox.Show("Máquina não registrada, deseja salvá-la?", "Aviso", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        NovaMaquina form = new NovaMaquina(this);
                        form.ShowDialog();

                        if (!form.MachineSaved)
                        {
                            return;
                        }
                    }
                    else
                    {
                        return;
                    }
                }

                lote.Usuario = CurrentUser.UserName;
                Firebird.SaveNewLote(lote);
                Firebird.SaveLeituras(listaLeituras);

                MessageBox.Show("Lote salvo com sucesso!", "Sucesso!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                lbExistente.Text = "Lote Salvo!";
            }
            else if (Firebird.LoteExists(lote))
            {
                if (Firebird.ReturnLeituras(lote).LastOrDefault() != Firebird.ReturnLeituras(lote).FirstOrDefault())
                {
                    Firebird.DeleteAll(lote);
                    lote.Usuario = CurrentUser.UserName;
                    Firebird.SaveNewLote(lote);
                    Firebird.SaveLeituras(listaLeituras);
                    //MessageBox.Show("Lote atualizado com sucesso!", "Sucesso!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                return;
            }
        }