private Bascula ObtenerBascula(int idBascula) { try { OpenConnection(); if (_myConnection != null) { if (_myConnection.State == ConnectionState.Open) { Bascula bs = null; DataSet ds = new DataSet(); string query = "SELECT * FROM SIPBECAMO_BASCULAS WHERE SIPBECAMO_BASCULASID = " + idBascula; SqlDataAdapter dataAdapter = new SqlDataAdapter(query, _myConnection); dataAdapter.Fill(ds); if (ds != null && ds.Tables[0].Rows.Count > 0) { bs = new Bascula(Convert.ToInt32(ds.Tables[0].Rows[0]["SipBecamo_BasculasId"]), ds.Tables[0].Rows[0]["Descripcion"].ToString(), ds.Tables[0].Rows[0]["DireccionIp"].ToString(), ds.Tables[0].Rows[0]["Ubicacion"].ToString(), ds.Tables[0].Rows[0]["Blt_Proceso"].ToString(), ds.Tables[0].Rows[0]["Blt_Periodo"].ToString(), ds.Tables[0].Rows[0]["Blt_Bodega"].ToString(), ds.Tables[0].Rows[0]["Blt_Orden"].ToString(), ds.Tables[0].Rows[0]["Blt_Procede"].ToString(), ds.Tables[0].Rows[0]["Blt_Bloque"].ToString(), ds.Tables[0].Rows[0]["Blt_Calidad"].ToString(), ds.Tables[0].Rows[0]["Blt_Calidad_Interna"].ToString(), ds.Tables[0].Rows[0]["Marca"].ToString()); } _myConnection.Close(); return bs; } } return new Bascula(); } catch (Exception ex) { XtraMessageBox.Show(ex.Message + "\n" + ex.StackTrace, "Error en funcion ObtenerBascula()", MessageBoxButtons.OK, MessageBoxIcon.Error); return null; } }
private void InsertScaleInfo(string text, Bascula bs) { try { BoletaEntidad nuevaBoleta = null; // Conexion string localConnectionString = "Data Source={0}; Initial Catalog={1}; {2}"; string user = Properties.Settings.Default.Usuario; string pass = Properties.Settings.Default.Contrasena; string isWA = Properties.Settings.Default.IsAutenticacionWindows; string strWindowsAutentication = (isWA == "Unchecked") ? "User ID=" + user + "; Password="******";" : string.Empty; localConnectionString = string.Format(localConnectionString, Properties.Settings.Default.Servidor, Properties.Settings.Default.BD, strWindowsAutentication); SqlConnection localConnection = new SqlConnection(localConnectionString); localConnection.Open(); if (localConnection.State == ConnectionState.Open) { string gross = GetValorTipoPeso(bs.Marca, MyResource.Gross, text); //text.Substring(13, 7); string tare = GetValorTipoPeso(bs.Marca, MyResource.Tare, text); //text.Substring(37, 6); string net = GetValorTipoPeso(bs.Marca, MyResource.Net, text); //text.Substring(61, 6); decimal grossDecimal = Convert.ToDecimal(gross); decimal tareDecimal = Convert.ToDecimal(tare); decimal netDecimal = Convert.ToDecimal(net); string blt_Serie = bs.Blt_Proceso + bs.Blt_Periodo + bs.Blt_Bodega; int correlativoBoleta = GetNumeroBoleta(blt_Serie); // Para registrarlo en el GridView nuevaBoleta = new BoletaEntidad() { Bascula = bs.Descripcion, DireccionIp = bs.DireccionIp, Fecha = DateTime.Now, NumeroBoleta = correlativoBoleta, SerieBoleta = blt_Serie, Bruto = grossDecimal, Tara = tareDecimal, Neto = netDecimal }; if (correlativoBoleta > 0) { string query = @" INSERT INTO SipBecamo_Boletas (Blt_Proceso, Blt_Periodo, Blt_Bodega, Blt_Serie, Blt_Numero, Blt_Fecha, Blt_Orden, Blt_Trilla, Blt_Procede, Blt_Bloque, Blt_Calidad, Blt_Calidad_Interna, Blt_Bruto, Blt_Tara, Blt_Neto) VALUES('{0}', '{1}', '{2}', '{3}', {4}, '{5}', '{6}', '{7}', '{8}', '{9}', '{10}', '{11}', {12}, {13}, {14}) "; query = string.Format(query, bs.Blt_Proceso, bs.Blt_Periodo, bs.Blt_Bodega, blt_Serie, correlativoBoleta, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), bs.Blt_Orden, "-----", bs.Blt_Procede, bs.Blt_Bloque, bs.Blt_Calidad, bs.Blt_Calidad_Interna, Convert.ToDecimal(gross), Convert.ToDecimal(tare), Convert.ToDecimal(net)); SqlCommand command = new SqlCommand(query, localConnection); command.ExecuteNonQuery(); } // Inserto en el GridView //lock (this) //{ // gridControl1.BeginInvoke(new MethodInvoker(delegate() // { // DataRow newRow = DsBasculas.Tables["TblMonitor"].NewRow(); // newRow["BASCULA"] = bs.Descripcion; // newRow["DIRECCION_IP"] = bs.DireccionIp; // newRow["FECHA"] = DateTime.Now; // newRow["NUMERO_BOLETA"] = correlativoBoleta; // newRow["SERIE_BOLETA"] = blt_Serie; // newRow["BRUTO"] = grossDecimal; // newRow["TARA"] = tareDecimal; // newRow["NETO"] = netDecimal; // DsBasculas.Tables["TblMonitor"].Rows.Add(newRow); // })); //} } localConnection.Close(); // Inserto en el GridView lock (this) { gridControl1.BeginInvoke(new MethodInvoker(delegate() { gridDataList.Add(nuevaBoleta); gridControl1.DataSource = gridDataList; })); } } catch (Exception ex) { XtraMessageBox.Show(ex.Message + "\n" + ex.StackTrace, "Error en funcion InsertScaleInfo()", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void CallTcpClient(Bascula bascula) { while (!_shouldStop) { try { // Create a TcpClient. // Note, for this client to work you need to have a TcpServer // connected to the same address as specified by the server, port // combination. Int32 port = 4001; Ping ping = new Ping(); string scaleIp = bascula.DireccionIp; if (scaleIp != string.Empty && scaleIp != null) { PingReply pingResult = ping.Send(scaleIp); if (pingResult.Status == IPStatus.Success) { TcpClient client = new TcpClient(scaleIp, port); if (client.Connected) { // Translate the passed message into ASCII and store it as a Byte array. //var data = System.Text.Encoding.ASCII.GetBytes("S"); // Get a client stream for reading and writing. // Stream stream = client.GetStream(); NetworkStream stream = client.GetStream(); // Send the message to the connected TcpServer. //stream.Write(data, 0, data.Length); //MessageBox.Show("Sent: " + message); // Receive the TcpServer.response. // Buffer to store the response bytes. var data = new byte[128]; // String to store the response ASCII representation. string responseData = string.Empty; // Read the first batch of the TcpServer response bytes. Int32 bytes = stream.Read(data, 0, data.Length); responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes); InsertScaleInfo(responseData, bascula); //InsertPingResult(bascula, responseData); //MessageBox.Show("Recieved: " + responseData); //SetMemoText(responseData + "IP BASCULA: " + scaleIp + " PUERTO: " + port + " HORA: " + DateTime.Now); // Close everything. stream.Close(); client.Close(); } } else { InsertPingResult(bascula, pingResult.Status.ToString()); } } } catch (Exception ex) { if(!ex.Message.Contains("Ping")) XtraMessageBox.Show(ex.Message + "\n" + ex.StackTrace, "Error en funcion CallTcpClient()", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
private void InsertPingResult(Bascula bs, string pingResult) { try { // Conexion BoletaEntidad nuevaBoleta = null; string localConnectionString = "Data Source={0}; Initial Catalog={1}; {2}"; string user = Properties.Settings.Default.Usuario; string pass = Properties.Settings.Default.Contrasena; string isWA = Properties.Settings.Default.IsAutenticacionWindows; string strWindowsAutentication = (isWA == "Unchecked") ? "User ID=" + user + "; Password="******";" : string.Empty; localConnectionString = string.Format(localConnectionString, Properties.Settings.Default.Servidor, Properties.Settings.Default.BD, strWindowsAutentication); SqlConnection localConnection = new SqlConnection(localConnectionString); localConnection.Open(); if (localConnection.State == ConnectionState.Open) { int correlativoBoleta = GetNumeroBoleta("P1112B5"); nuevaBoleta = new BoletaEntidad() { Bascula = bs.Descripcion, DireccionIp = bs.DireccionIp, Fecha = DateTime.Now, NumeroBoleta = correlativoBoleta, SerieBoleta = "P1112B5", Bruto = 100, Tara = 11, Neto = 89 }; if (correlativoBoleta > 0) { //SetCulture(); string query = @" INSERT INTO SipBecamo_Boletas (Blt_Proceso, Blt_Periodo, Blt_Bodega, Blt_Serie, Blt_Numero, Blt_Fecha, Blt_Orden, Blt_Trilla, Blt_Procede, Blt_Bloque, Blt_Calidad, Blt_Calidad_Interna, Blt_Bruto, Blt_Tara, Blt_Neto) VALUES('{0}', '{1}', '{2}', '{3}', {4}, '{5}', '{6}', '{7}', '{8}', '{9}', '{10}', '{11}', {12}, {13}, {14}) "; query = string.Format(query, bs.Blt_Proceso, bs.Blt_Periodo, bs.Blt_Bodega, "P1112B5", correlativoBoleta, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), bs.Blt_Orden, "-----", pingResult.Trim(), bs.Blt_Bloque, bs.Blt_Calidad, bs.Blt_Calidad_Interna, 150, 11, 139); SqlCommand command = new SqlCommand(query, localConnection); command.ExecuteNonQuery(); } } localConnection.Close(); // Inserto en el GridView lock (this) { gridControl1.BeginInvoke(new MethodInvoker(delegate() { //SetCulture(); gridDataList.Add(nuevaBoleta); gridControl1.DataSource = gridDataList; })); } //lock (this) //{ // gridControl1.BeginInvoke(new MethodInvoker(delegate() // { // // Para registrarlo en el GridView // DataRow newRow = DsBasculas.Tables["TblMonitor"].NewRow(); // newRow["BASCULA"] = bs.Descripcion; // newRow["DIRECCION_IP"] = bs.DireccionIp; // newRow["FECHA"] = DateTime.Now; // newRow["SERIE_BOLETA"] = pingResult; // newRow["BRUTO"] = Thread.CurrentThread.ManagedThreadId; // DsBasculas.Tables["TblMonitor"].Rows.Add(newRow); // })); //} } catch (Exception ex) { XtraMessageBox.Show(ex.Message + "\n" + ex.StackTrace, "Error en funcion InsertPingResult()", MessageBoxButtons.OK, MessageBoxIcon.Error); } }