a
This commit is contained in:
84
ScanBox/Controller/DbController.cs
Normal file
84
ScanBox/Controller/DbController.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.OleDb;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ScaBox30.Controller
|
||||
{
|
||||
internal class DbController
|
||||
{
|
||||
private string _path;
|
||||
private OleDbConnection _connection;
|
||||
private OleDbCommand _command;
|
||||
private OleDbDataReader _reader;
|
||||
|
||||
|
||||
|
||||
public void SetWorkingDb(string path)
|
||||
{
|
||||
_path = path;
|
||||
}
|
||||
|
||||
|
||||
public void Open()
|
||||
{
|
||||
_connection = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;" + "Data Source=" + _path);
|
||||
try
|
||||
{
|
||||
_connection.Open();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void Close()
|
||||
{
|
||||
try
|
||||
{
|
||||
_connection.Close();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public OleDbDataReader Query(string query)
|
||||
{
|
||||
try
|
||||
{
|
||||
_command = new OleDbCommand(query, _connection);
|
||||
_reader = _command.ExecuteReader();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
return _reader;
|
||||
}
|
||||
|
||||
|
||||
public void InsertQuery(String query)
|
||||
{
|
||||
try
|
||||
{
|
||||
_command = new OleDbCommand(query, _connection);
|
||||
_command.ExecuteNonQuery();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
43
ScanBox/Controller/NetworkDiscoveryHandler.cs
Normal file
43
ScanBox/Controller/NetworkDiscoveryHandler.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Zebra.Sdk.Printer.Discovery;
|
||||
|
||||
namespace ScaBox30
|
||||
{
|
||||
//CLASE ZEBRA PARA LA BUSQUEDA DE IMPRESORAS EN LA RED
|
||||
class NetworkDiscoveryHandler : DiscoveryHandler
|
||||
{
|
||||
|
||||
private bool discoveryComplete = false;
|
||||
public List<DiscoveredPrinter> printers = new List<DiscoveredPrinter>();
|
||||
|
||||
public void DiscoveryError(string message)
|
||||
{
|
||||
Console.WriteLine($"An error occurred during discovery: {message}.");
|
||||
discoveryComplete = true;
|
||||
}
|
||||
|
||||
public void DiscoveryFinished()
|
||||
{
|
||||
foreach (DiscoveredPrinter printer in printers)
|
||||
{
|
||||
Console.WriteLine(printer);
|
||||
}
|
||||
Console.WriteLine($"Discovered {printers.Count} Link-OS(TM) printers.");
|
||||
discoveryComplete = true;
|
||||
}
|
||||
|
||||
public void FoundPrinter(DiscoveredPrinter printer)
|
||||
{
|
||||
printers.Add(printer);
|
||||
}
|
||||
|
||||
public bool DiscoveryComplete
|
||||
{
|
||||
get => discoveryComplete;
|
||||
}
|
||||
}
|
||||
}
|
||||
174
ScanBox/Controller/ZebraController.cs
Normal file
174
ScanBox/Controller/ZebraController.cs
Normal file
@@ -0,0 +1,174 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using Zebra.Sdk.Comm;
|
||||
using Zebra.Sdk.Printer.Discovery;
|
||||
|
||||
// *************************************
|
||||
// * Angel Ivan 07/Nov/2024
|
||||
// *************************************
|
||||
|
||||
namespace ScaBox30.Controller
|
||||
{
|
||||
class ZebraController
|
||||
{
|
||||
String prnfile = "";
|
||||
String prnpath = Application.StartupPath + "\\";
|
||||
string etiqueta;
|
||||
StreamReader reader;
|
||||
|
||||
public ZebraController(String prnpath)
|
||||
{
|
||||
this.prnpath += prnpath;
|
||||
}
|
||||
|
||||
|
||||
#region ZEBRA -> initDiscovery()
|
||||
public void initDiscovery()
|
||||
{
|
||||
NetworkDiscoveryHandler discoveryHandler = new NetworkDiscoveryHandler();
|
||||
try
|
||||
{
|
||||
Console.WriteLine("Starting printer discovery.");
|
||||
NetworkDiscoverer.FindPrinters(discoveryHandler);
|
||||
while (!discoveryHandler.DiscoveryComplete)
|
||||
{
|
||||
System.Threading.Thread.Sleep(10);
|
||||
}
|
||||
}
|
||||
catch (DiscoveryException e)
|
||||
{
|
||||
Console.WriteLine(e.ToString());
|
||||
}
|
||||
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ZEBRA -> buildPrn()
|
||||
public void buildPrn(String v1, String v2, String v3, String barcode)
|
||||
{
|
||||
try
|
||||
{
|
||||
etiqueta = "";
|
||||
reader = new StreamReader(prnpath);
|
||||
prnfile = reader.ReadToEnd();
|
||||
reader.Close();
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
MessageBox.Show("No se ha podido abrir la plantilla de impresión\n" + prnpath, "Error de impresión", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
|
||||
|
||||
string[] lines = prnfile.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
|
||||
|
||||
foreach (string line in lines)
|
||||
{
|
||||
if (line.Contains("$V1$"))
|
||||
{
|
||||
/*Falta el dato desde master_codigos.csv posiblemente*/
|
||||
etiqueta += v1 != null ? line.Replace("$V1$", v1) : line.Replace("$V1$", "null");
|
||||
}
|
||||
else if (line.Contains("$V2$"))
|
||||
{
|
||||
etiqueta += v2 != null ? line.Replace("$V2$", v2) : line.Replace("$V2$", "null" + " / " + "null");
|
||||
}
|
||||
else if (line.Contains("0000000000"))
|
||||
{
|
||||
etiqueta += !barcode.Contains("-") ? line.Replace("0000000000", barcode) : line.Replace("0000000000", "0000000000");
|
||||
}
|
||||
else if (line.Contains("$V3$"))
|
||||
{
|
||||
etiqueta += line.Replace("$V3$", v3);
|
||||
}
|
||||
else
|
||||
{
|
||||
etiqueta += line;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ZEBRA -> SendZplOverTcp(string theIpAddress)
|
||||
public void SendZplOverTcp(string theIpAddress)
|
||||
{
|
||||
// Instantiate connection for ZPL TCP port at given address
|
||||
Connection thePrinterConn = new TcpConnection(theIpAddress, TcpConnection.DEFAULT_ZPL_TCP_PORT);
|
||||
|
||||
try
|
||||
{
|
||||
// Open the connection - physical connection is established here.
|
||||
thePrinterConn.Open();
|
||||
|
||||
//se obtiene la plantilla de la etiqueta y se reemplazan los valores
|
||||
//readprn();
|
||||
|
||||
// This example prints "This is a ZPL test." near the top of the label.
|
||||
//string zplData = "^XA^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^XZ";
|
||||
string zplData = etiqueta;
|
||||
|
||||
// Send the data to printer as a byte array.
|
||||
thePrinterConn.Write(Encoding.UTF8.GetBytes(zplData));
|
||||
}
|
||||
catch (ConnectionException e)
|
||||
{
|
||||
Console.WriteLine(e.ToString());
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Close the connection to release resources.
|
||||
thePrinterConn.Close();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region SendZplOverUsb(string usbDriverName)
|
||||
public void SendZplOverUsb(string usbDriverName)
|
||||
{
|
||||
Connection thePrinterConn = null;
|
||||
try
|
||||
{
|
||||
// Instantiate USB connection for ZPL printer through its driver
|
||||
thePrinterConn = ConnectionBuilder.Build($"USB:{usbDriverName}");
|
||||
|
||||
// Open the connection - physical connection is established here.
|
||||
thePrinterConn.Open();
|
||||
|
||||
//se obtiene la plantilla de la etiqueta y se reemplazan los valores
|
||||
//readprn();
|
||||
|
||||
// This example prints "This is a ZPL test." near the top of the label.
|
||||
string zplData = "^XA^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^XZ";
|
||||
//string zplData = etiqueta;
|
||||
|
||||
// Send the data to printer as a byte array.
|
||||
thePrinterConn.Write(Encoding.UTF8.GetBytes(zplData));
|
||||
}
|
||||
catch (ConnectionException e)
|
||||
{
|
||||
// Handle communications error here.
|
||||
Console.WriteLine(e.ToString());
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Close the connection to release resources.
|
||||
if (thePrinterConn != null)
|
||||
{
|
||||
thePrinterConn.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user