예제 #1
0
            private void searchUsb()
            {
                Logger.Write("Start search Usb printers");

                // process connection to Usb
                PrinterZebra.EPrinterConnectionType connType = PrinterZebra.EPrinterConnectionType.eUSB;

                if (deviceAdress != null && !PrinterManager.Instance.hasPrinter(deviceAdress))
                {
                    Logger.Write("Start search in address: " + deviceAdress);

                    ConnecttionJob job = zebraSingleton.tryToConnect(0, deviceAdress, ZebraConstants.connectionTimeout, connType);

                    if (job.Connection != null)
                    {
                        PrinterManager.Instance.addPrinterWithID(deviceAdress, 0, connType);

                        if (!isSearchStopped)
                        {
                            zebraSingleton.sendConnectResult(job.FriendlyName, deviceAdress, 0, connType, oResult);
                        }

                        job.Close();
                    }
                }
                else
                {
                    List <String> usbPrinterAddresses = new List <String>();

                    usbPrinterAddresses.Add("LPT1:");
                    usbPrinterAddresses.Add("LPT2:");

                    foreach (String printerAddress in usbPrinterAddresses)
                    {
                        if (isSearchStopped)
                        {
                            break;
                        }

                        Logger.Write("searching in address: " + printerAddress);

                        if (!PrinterManager.Instance.hasPrinter(printerAddress))
                        {
                            ConnecttionJob job = zebraSingleton.tryToConnect(0, printerAddress, ZebraConstants.connectionTimeout, connType);

                            if (job.Connection != null)
                            {
                                Logger.Write("Found printer on address: " + printerAddress);

                                PrinterManager.Instance.addPrinterWithID(printerAddress, 0, connType);

                                zebraSingleton.sendConnectResult(job.FriendlyName, printerAddress, 0, connType, oResult);

                                job.Close();
                            }
                        }
                    }
                }
            }
예제 #2
0
            public ConnecttionJob tryToConnect(Int32 port, string deviceAdress, int timeout, PrinterZebra.EPrinterConnectionType connType)
            {
                Logger.Write("tryToConnect: " + port.ToString() + ", " + deviceAdress + ", " + timeout.ToString());

                ConnecttionJob job = new ConnecttionJob();

                job.MaxTimeoutForRead     = 0;
                job.TimeToWaitForMoreData = 0;
                job.Port           = port;
                job.Address        = deviceAdress;
                job.ConnectionType = connType;

                job.Connect(timeout);

                return(job);
            }
예제 #3
0
            private void tryToConnectInFoundPrinters(IMethodResult oResult)
            {
                IMethodResult result      = new SleepMethodResult(500);
                List <string> badPrinters = new List <string>();

                Logger.Write("tryToConnect start [found printers]");

                List <string> printerKeys = PrinterManager.Instance.getPrintersKeys();

                foreach (string printerKey in printerKeys)
                {
                    PrinterZebra printer = PrinterManager.Instance.getPrinter(printerKey);

                    string deviceAdress = printer.ID;
                    Int32  port         = printer.Port;

                    Logger.Write("searching in address [found printers]: " + deviceAdress);

                    ConnecttionJob job = tryToConnect(port, deviceAdress, ZebraConstants.connectionTimeout, printer.connectionType);

                    if (job.Connection != null)
                    {
                        Logger.Write("Found printer on address [found printers]: " + deviceAdress);

                        sendConnectResult(job.FriendlyName, deviceAdress, port, printer.connectionType, oResult);

                        job.Close();
                    }
                    else
                    {
                        Logger.Write("remove printer on address [found printers]: " + deviceAdress + " from cache.");
                        badPrinters.Add(printerKey);
                    }
                }

                PrinterManager.Instance.removePrinters(badPrinters);
            }
예제 #4
0
        public ConnecttionJob tryToConnect(Int32 port, string deviceAdress, int timeout, PrinterZebra.EPrinterConnectionType connType)
        {
            Logger.Write("tryToConnect: " + port.ToString() + ", " + deviceAdress + ", " + timeout.ToString());

            ConnecttionJob job = new ConnecttionJob();

            job.MaxTimeoutForRead     = 0;
            job.TimeToWaitForMoreData = 0;
            job.Port                  = port;
            job.Address               = deviceAdress;
            job.ConnectionType        = connType;

            job.Connect(timeout);

            return job;
        }
예제 #5
0
        public void connectWithOptions(IReadOnlyDictionary<string, string> options, IMethodResult oResult)
        {
            Logger.Write("connectWithOptions call");
            Logger.Write("options:", options);

            string valueObj = null;
            Int32  connectionTimeout = 0;

            if (m_connection != null && m_connection.IsConnected() && m_printer != null)
            {
                m_connection.Close();

                Thread.Sleep(1000);

                m_connection = null;
                m_printer    = null;
            }

            if (options.ContainsKey(ZebraConstants.HK_TIMEOUT))
            {
                valueObj = options[ZebraConstants.HK_TIMEOUT];

                if ((valueObj != null) && (valueObj is String))
                {
                    try
                    {
                        connectionTimeout = Int32.Parse(valueObj.ToString());
                    }
                    catch (System.FormatException)
                    {
                        m_maxTimeoutForRead = 0;
                    }
                }
            }

            if (options.ContainsKey(ZebraConstants.PROPERTY_MAX_TIMEOUT_FOR_READ))
            {
                valueObj = options[ZebraConstants.PROPERTY_MAX_TIMEOUT_FOR_READ];
                
                if ((valueObj != null) && (valueObj is String))
                {
                    try
                    {
                        m_maxTimeoutForRead = Int32.Parse(valueObj.ToString());
                    }
                    catch (System.FormatException)
                    {
                        m_maxTimeoutForRead = 0;
                    }
                }
            }

            if (options.ContainsKey(ZebraConstants.PROPERTY_TIME_TO_WAIT_FOR_MORE_DATA))
            {
                valueObj = options[ZebraConstants.PROPERTY_TIME_TO_WAIT_FOR_MORE_DATA];

                if ((valueObj != null) && (valueObj is String))
                {
                    try
                    {
                        m_timeToWaitForMoreData = Int32.Parse(valueObj.ToString());
                    }
                    catch (System.FormatException)
                    {
                        m_maxTimeoutForRead = 0;
                    }
                }
            }
            
            ConnecttionJob job = new ConnecttionJob();

            job.Address               = ID;
            job.Port                  = Port;
            job.ConnectionType        = connectionType;
            job.MaxTimeoutForRead     = m_maxTimeoutForRead;
            job.TimeToWaitForMoreData = m_timeToWaitForMoreData;

            if (connectionTimeout == 0)
            {
                job.Connect();
            }
            else
            {
                if (!job.Connect(connectionTimeout))
                {
                    oResult.set(ZebraConstants.PRINTER_STATUS_ERR_TIMEOUT);
                    return;
                }
            }
            
            if (job.Connection != null && job.Printer != null)
            {
                m_connection = job.Connection;
                m_printer    = job.Printer;

                oResult.set(ZebraConstants.PRINTER_STATUS_SUCCESS);
                return;
            }

            oResult.set(ZebraConstants.PRINTER_STATUS_ERR_NOT_CONNECTED);
        }
예제 #6
0
            public void connectWithOptions(IReadOnlyDictionary <string, string> options, IMethodResult oResult)
            {
                Logger.Write("connectWithOptions call");
                Logger.Write("options:", options);

                string valueObj          = null;
                Int32  connectionTimeout = 0;

                if (m_connection != null && m_connection.IsConnected() && m_printer != null)
                {
                    m_connection.Close();

                    Thread.Sleep(1000);

                    m_connection = null;
                    m_printer    = null;
                }

                if (options.ContainsKey(ZebraConstants.HK_TIMEOUT))
                {
                    valueObj = options[ZebraConstants.HK_TIMEOUT];

                    if ((valueObj != null) && (valueObj is String))
                    {
                        try
                        {
                            connectionTimeout = Int32.Parse(valueObj.ToString());
                        }
                        catch (System.FormatException)
                        {
                            m_maxTimeoutForRead = 0;
                        }
                    }
                }

                if (options.ContainsKey(ZebraConstants.PROPERTY_MAX_TIMEOUT_FOR_READ))
                {
                    valueObj = options[ZebraConstants.PROPERTY_MAX_TIMEOUT_FOR_READ];

                    if ((valueObj != null) && (valueObj is String))
                    {
                        try
                        {
                            m_maxTimeoutForRead = Int32.Parse(valueObj.ToString());
                        }
                        catch (System.FormatException)
                        {
                            m_maxTimeoutForRead = 0;
                        }
                    }
                }

                if (options.ContainsKey(ZebraConstants.PROPERTY_TIME_TO_WAIT_FOR_MORE_DATA))
                {
                    valueObj = options[ZebraConstants.PROPERTY_TIME_TO_WAIT_FOR_MORE_DATA];

                    if ((valueObj != null) && (valueObj is String))
                    {
                        try
                        {
                            m_timeToWaitForMoreData = Int32.Parse(valueObj.ToString());
                        }
                        catch (System.FormatException)
                        {
                            m_maxTimeoutForRead = 0;
                        }
                    }
                }

                ConnecttionJob job = new ConnecttionJob();

                job.Address               = ID;
                job.Port                  = Port;
                job.ConnectionType        = connectionType;
                job.MaxTimeoutForRead     = m_maxTimeoutForRead;
                job.TimeToWaitForMoreData = m_timeToWaitForMoreData;

                if (connectionTimeout == 0)
                {
                    job.Connect();
                }
                else
                {
                    if (!job.Connect(connectionTimeout))
                    {
                        oResult.set(ZebraConstants.PRINTER_STATUS_ERR_TIMEOUT);
                        return;
                    }
                }

                if (job.Connection != null && job.Printer != null)
                {
                    m_connection = job.Connection;
                    m_printer    = job.Printer;

                    oResult.set(ZebraConstants.PRINTER_STATUS_SUCCESS);
                    return;
                }

                oResult.set(ZebraConstants.PRINTER_STATUS_ERR_NOT_CONNECTED);
            }
예제 #7
0
            private void searchBluetooth()
            {
                Logger.Write("Start search Bluetooth printers");

                // process connection to Bluetooth
                PrinterZebra.EPrinterConnectionType connType = PrinterZebra.EPrinterConnectionType.eBluetooth;

                if (deviceAdress != null && !PrinterManager.Instance.hasPrinter(deviceAdress))
                {
                    Logger.Write("Start search in address: " + deviceAdress);

                    ConnecttionJob job = zebraSingleton.tryToConnect(0, deviceAdress, ZebraConstants.connectionTimeout, connType);

                    if (job.Connection != null)
                    {
                        PrinterManager.Instance.addPrinterWithID(deviceAdress, 0, connType);

                        if (!isSearchStopped)
                        {
                            zebraSingleton.sendConnectResult(job.FriendlyName, deviceAdress, 0, connType, oResult);
                        }

                        job.Close();
                    }
                }
                else
                {
                    DiscoveredPrinter[] printers = null;

                    for (int attempt = 0; attempt < connettionAttempts; attempt++)
                    {
                        printers = BluetoothDiscoverer.FindPrinters();

                        if (printers.Length > 0)
                        {
                            break;
                        }

                        Thread.Sleep(500);
                    }

                    LogDiscoveryPrinters(printers);

                    foreach (DiscoveredPrinter printer in printers)
                    {
                        Logger.Write("searching in address: " + printer.Address);

                        if (PrinterManager.Instance.hasPrinter(printer.Address))
                        {
                            continue;
                        }

                        if (isSearchStopped == true)
                        {
                            return;
                        }

                        Logger.Write("Found printer on address: " + printer.Address);

                        //zebraSingleton.sendConnectResult(job.FriendlyName, printer.Address, 0, connType, oResult);
                        zebraSingleton.sendConnectResult(printer.Address, printer.Address, 0, connType, oResult);
                    }
                }
            }
예제 #8
0
            private void searchWiFi()
            {
                Logger.Write("Start search TCP printers");

                // process connection to TCP address
                PrinterZebra.EPrinterConnectionType connType = PrinterZebra.EPrinterConnectionType.eTCP;

                if (deviceAdress != null && devicePort > 0 && !PrinterManager.Instance.hasPrinter(deviceAdress))
                {
                    Logger.Write("searching in address: " + deviceAdress);

                    ConnecttionJob job = zebraSingleton.tryToConnect(devicePort, deviceAdress, ZebraConstants.connectionTimeout, connType);

                    if (job.Connection != null)
                    {
                        Logger.Write("Found printer on address: " + deviceAdress);

                        PrinterManager.Instance.addPrinterWithID(deviceAdress, devicePort, connType);

                        if (!isSearchStopped)
                        {
                            zebraSingleton.sendConnectResult(job.FriendlyName, deviceAdress, devicePort, connType, oResult);
                        }

                        job.Close();
                    }
                }
                else
                {
                    Logger.Write("Start search in TCP network");

                    DiscoveredPrinter[] printers = null;

                    for (int attempt = 0; attempt < connettionAttempts; attempt++)
                    {
                        printers = NetworkDiscoverer.LocalBroadcast();

                        if (printers.Length > 0)
                        {
                            break;
                        }

                        Thread.Sleep(500);
                    }

                    if (printers.Length == 0)
                    {
                        for (int attempt = 0; attempt < connettionAttempts; attempt++)
                        {
                            printers = NetworkDiscoverer.Multicast(5);

                            if (printers.Length > 0)
                            {
                                break;
                            }

                            Thread.Sleep(500);
                        }
                    }

                    LogDiscoveryPrinters(printers);

                    foreach (DiscoveredPrinter printer in printers)
                    {
                        if (isSearchStopped)
                        {
                            break;
                        }

                        Logger.Write("searching in address: " + printer.Address);

                        if ((printer is DiscoveredPrinterNetwork) && !PrinterManager.Instance.hasPrinter(printer.Address))
                        {
                            DiscoveredPrinterNetwork networkPrinter = (DiscoveredPrinterNetwork)printer;

                            ConnecttionJob job = zebraSingleton.tryToConnect(networkPrinter.Port, networkPrinter.Address, ZebraConstants.connectionTimeout, connType);

                            if (job.Connection != null)
                            {
                                Logger.Write("Found printer on address: " + printer.Address);

                                PrinterManager.Instance.addPrinterWithID(networkPrinter.Address, networkPrinter.Port, connType);

                                zebraSingleton.sendConnectResult(job.FriendlyName, networkPrinter.Address, networkPrinter.Port, connType, oResult);

                                job.Close();
                            }
                        }
                    }
                }
            }