Exemple #1
0
        internal static void setSerialPort(PrinterConfigEntity printerConfigEntity, Action <object> webCallback)
        {
            ResponseEntity responseEntity = new ResponseEntity();

            if (printerConfigEntity != null)
            {
                string port    = printerConfigEntity.port;
                string intBaud = printerConfigEntity.intBaud;
                if (!String.IsNullOrEmpty(port) && !String.IsNullOrEmpty(intBaud))
                {
                    serialPort m_serialPort;
                    if (PrinterManager.Instance.PortPrinter == null)
                    {
                        m_serialPort           = new serialPort(port, intBaud);
                        m_serialPort.pageWidth = printerConfigEntity.pageWidth;
                        m_serialPort.brand     = printerConfigEntity.printerBrand;
                        m_serialPort.init();
                    }
                    else
                    {
                        m_serialPort = PrinterManager.Instance.PortPrinter;
                        m_serialPort.Close();
                    }
                    if (m_serialPort.Open(port, int.Parse(intBaud)))
                    {
                        PrinterManager.Instance.Init            = true;
                        PrinterManager.Instance.PrinterTypeEnum = PrinterTypeEnum.port;
                        PrinterManager.Instance.PortPrinter     = m_serialPort;

                        m_serialPort.PrintString("串口打印机连接成功!\n\n\n\n\n");
                        responseEntity.code = ResponseCode.SUCCESS;
                        responseEntity.msg  = "打印机设置成功";
                    }
                    else
                    {
                        m_serialPort.Close();
                        responseEntity.code = ResponseCode.Failed;
                        responseEntity.msg  = "该端口不可用";
                    }
                }
                else
                {
                    responseEntity.code = ResponseCode.Failed;
                    responseEntity.msg  = "端口或波特率参数不能为空";
                }


                if (webCallback != null)
                {
                    webCallback.Invoke(new object[] { "setPrinterCallBack", responseEntity });
                }
            }
        }
Exemple #2
0
        internal void SetPrinter(PrinterConfigEntity printerConfigEntity, Action <object> webCallback)
        {
            if (printerConfigEntity != null)
            {
                //add 2018/01/15 保存设置的小票打印份数
                PrinterManager.Instance.PrintNumber = int.Parse(printerConfigEntity.printernumber);
                responseEntity = new ResponseEntity();

                switch (printerConfigEntity.printerType)
                {
                case "usb":
                    USBPrinterSetter usbPrinterSetter = new USBPrinterSetter();
                    usbPrinterSetter.setUSBPrinter(printerConfigEntity, webCallback);
                    break;

                case "bluetooth":
                    BluethoothPrinterSetter bluethoothPrinterSetter = new BluethoothPrinterSetter();
                    bluethoothPrinterSetter.setBluethoothPrinter(printerConfigEntity, webCallback);
                    break;

                case "port":
                    SerialPortPrinterSetter.setSerialPort(printerConfigEntity, webCallback);
                    break;

                //add 增加并口 2018年5月29日
                case "LPT":
                    LPTPrinterSetter.setLPT(printerConfigEntity, webCallback);
                    break;

                //add 增加驱动打印 2018年10月15日
                case "drive":
                    DrivePrinterSetter drivePrinterSetter = new DrivePrinterSetter();
                    drivePrinterSetter.SetDrivePrinterSetter(printerConfigEntity, webCallback);
                    break;

                default:
                    responseEntity.code = ResponseCode.Failed;
                    responseEntity.msg  = "打印机类型不可用";
                    //L.i(TAG, "打印机类型不可用");
                    if (webCallback != null)
                    {
                        webCallback.Invoke(new object[] { "setPrinterCallBack", responseEntity });
                    }
                    break;
                }
            }
        }
Exemple #3
0
        internal static void setLPT(PrinterConfigEntity printerConfigEntity, Action <object> webCallback)
        {
            ResponseEntity responseEntity = new ResponseEntity();

            if (printerConfigEntity != null)
            {
                string lpt = printerConfigEntity.port;
                if (!string.IsNullOrEmpty(lpt))
                {
                    LPTPrinter lptPrinter;
                    if (PrinterManager.Instance.LptPrinter == null)
                    {
                        lptPrinter           = new LPTPrinter();
                        lptPrinter.lptPort   = lpt;
                        lptPrinter.pageWidth = printerConfigEntity.pageWidth;
                    }
                    else
                    {
                        lptPrinter = PrinterManager.Instance.LptPrinter;
                        lptPrinter.Close();
                    }

                    if (lptPrinter.Init())
                    {
                        lptPrinter.pageWidth                        = printerConfigEntity.pageWidth;
                        PrinterManager.Instance.Init                = true;
                        PrinterManager.Instance.LptPrinter          = lptPrinter;
                        PrinterManager.Instance.PrinterTypeEnum     = PrinterTypeEnum.LPT;
                        PrinterManager.Instance.PrinterConfigEntity = printerConfigEntity;

                        lptPrinter.PrintString("并口打印机连接成功!\n\n\n\n\n");
                        responseEntity.code = ResponseCode.SUCCESS;
                        responseEntity.msg  = "打印机设置成功";
                    }
                    else
                    {
                        lptPrinter.Close();
                        responseEntity.code = ResponseCode.Failed;
                        responseEntity.msg  = "该端口不可用";
                    }

                    webCallback?.Invoke(new object[] { "setPrinterCallBack", responseEntity });
                }
            }
        }
Exemple #4
0
        internal void SetDrivePrinterSetter(PrinterConfigEntity printerConfigEntity, Action <object> webCallback)
        {
            responseEntity = new ResponseEntity();
            if (printerConfigEntity != null)
            {
                PrinterManager.Instance.PrinterConfigEntity = printerConfigEntity;
                try
                {
                    //DrivePrinter最好还是采用单例比较好
                    DrivePrinter drivePrinter = DrivePrinter.Instance;
                    drivePrinter.printerName = printerConfigEntity.printerName; //"GP-5890X Series";

                    drivePrinter.SetPrinterName();

                    drivePrinter.Print("驱动连接打印机成功\r\n\r\n\r\n");
                    drivePrinter.pageWidth                      = printerConfigEntity.pageWidth;
                    PrinterManager.Instance.Init                = true;
                    PrinterManager.Instance.PrinterTypeEnum     = Enums.PrinterTypeEnum.drive;
                    PrinterManager.Instance.DrivePrinter        = drivePrinter;
                    PrinterManager.Instance.PrinterConfigEntity = printerConfigEntity;


                    responseEntity.code = ResponseCode.SUCCESS;
                    responseEntity.msg  = "打印机设置成功";
                }
                catch (Exception e)
                {
                    responseEntity.code = ResponseCode.Failed;
                    responseEntity.msg  = "驱动打印出错";
                    logger.Error("drive print err", e);
                }
            }
            else
            {
                responseEntity.code = ResponseCode.Failed;
                responseEntity.msg  = "参数不能为空";
            }
            if (webCallback != null)
            {
                webCallback.Invoke(new object[] { "setPrinterCallBack", responseEntity });
            }
        }
Exemple #5
0
        //public object USBPrinter { get; internal set; }
        internal void setUSBPrinter(PrinterConfigEntity printerConfigEntity, Action <ResponseEntity> p)
        {
            listener       = p;
            responseEntity = new ResponseEntity();
            if (printerConfigEntity != null)
            {
                BJQPrinterManager.Instance.printerConfigEntity = printerConfigEntity;

                //TOFIX...以下直接用Gprinter的 usb代码凑活一下
                //GPrinterManager.Instance.Init = true;
                List <string> usblist = GPrinterUtils.Instance.FindUSBPrinter();
                if (usblist == null)
                {
                    responseEntity.code = ResponseCode.Failed;
                    responseEntity.msg  = "未发现可用USB设备";
                    listener?.Invoke(responseEntity);
                }
                else
                {
                    GPrinterManager.Instance.usbDeviceArrayList = usblist;
                    if (GPrinterUtils.Instance.Connect_Printer())
                    {
                        GPrinterManager.Instance.Init            = true;
                        GPrinterManager.Instance.PrinterTypeEnum = "usb";
                        responseEntity.code = ResponseCode.SUCCESS;
                        responseEntity.msg  = "打印机设置成功";
                    }
                    else
                    {
                        responseEntity.code = ResponseCode.Failed;
                        responseEntity.msg  = "打印机设置失败";
                    }
                }
            }
            else
            {
                responseEntity.code = ResponseCode.Failed;
                responseEntity.msg  = "打印机设置失败,参数为空";
            }
            listener?.Invoke(responseEntity);
        }
Exemple #6
0
        internal void setBluethoothPrinter(PrinterConfigEntity printerConfigEntity, Action <object> webCallback)
        {
            if (printerConfigEntity != null)
            {
                this.listener            = webCallback;
                this.printerConfigEntity = printerConfigEntity;
                responseEntity           = new ResponseEntity();
                if (PrinterManager.Instance.BluetoothDeviceArrayList == null)
                {
                    getBluetoothDevices();
                }
                if (PrinterManager.Instance.BluetoothDeviceArrayList != null)
                {
                    if (!setBluetooth(printerConfigEntity))
                    {
                        responseEntity.code = ResponseCode.Failed;
                        responseEntity.msg  = "该设备不可用";
                        if (listener != null)
                        {
                            listener.Invoke(new object[] { "setPrinterCallBack", responseEntity });
                        }
                    }
                }
            }
            else
            {
                responseEntity.code = ResponseCode.Failed;
                responseEntity.msg  = "参数不能为空";
                if (listener != null)
                {
                    listener.Invoke(new object[] { "setPrinterCallBack", responseEntity });
                }
            }

            //if (webCallback != null)
            //{
            //    webCallback.Invoke(new object[] { "setPrinterCallBack", responseEntity });
            //}
        }
Exemple #7
0
        public void setPrinter(PrinterConfigEntity printerConfigEntity, Action <ResponseEntity> p)
        {
            if (printerConfigEntity != null)
            {
                GPrinterManager.Instance.PrintNumber = int.Parse(printerConfigEntity.printernumber);
                responseEntity = new ResponseEntity();
                switch (printerConfigEntity.printerType)
                {
                case "usb":
                    USBGPrinterSetter usbGPrinterSetter = new USBGPrinterSetter();
                    usbGPrinterSetter.setUSBPrinter(printerConfigEntity, (res) =>
                    {
                        if (res.code == ResponseCode.SUCCESS)
                        {
                            logger.Info("usb标签打印机设置成功");
                        }
                        else
                        {
                            logger.Info("usb标签打印机设置失败");
                        }
                        p?.Invoke(res);
                    });
                    break;

                case "port":
                    break;

                case "bluetooth":
                    break;

                default:
                    responseEntity.code = ResponseCode.Failed;
                    responseEntity.msg  = "打印机类型不可用";
                    logger.Info("打印机类型不可用");
                    p?.Invoke(responseEntity);
                    break;
                }
            }
        }
Exemple #8
0
        private bool setBluetooth(PrinterConfigEntity printerConfigEntity)
        {
            List <BluetoothDeviceInfo> bluetoothDeviceArrayList = PrinterManager.Instance.BluetoothDeviceArrayList;

            foreach (BluetoothDeviceInfo bluetoothDevice in bluetoothDeviceArrayList)
            {
                if (bluetoothDevice.DeviceName.Equals(printerConfigEntity.deviceId))
                {
                    BluetoothPrinter bluetoothPrinter;
                    if (PrinterManager.Instance.Init && PrinterManager.Instance.BluetoothPrinter != null)
                    {
                        if (bluetoothDevice.DeviceAddress.Equals(PrinterManager.Instance.BluetoothPrinter.MacAddress))
                        {
                            bluetoothPrinter = PrinterManager.Instance.BluetoothPrinter;
                        }
                        else
                        {
                            PrinterManager.Instance.BluetoothPrinter.closeConnection();         //关闭之前的蓝牙打印机
                            bluetoothPrinter           = new BluetoothPrinter(bluetoothDevice); //创建新的蓝牙打印机实例
                            bluetoothPrinter.pageWidth = printerConfigEntity.pageWidth;
                        }
                    }
                    else
                    {
                        bluetoothPrinter = new BluetoothPrinter(bluetoothDevice);
                    }

                    bluetoothPrinter.Encoding = "GBK";
                    if (printerConfigEntity.pageWidth == "small")
                    {
                        bluetoothPrinter.CurrentPrintType = PrinterType.T8;
                    }
                    else
                    {
                        bluetoothPrinter.CurrentPrintType = PrinterType.T5;
                    }

                    //bluetoothPrinter.Handler(bluetoothHandler);
                    if (!bluetoothPrinter.isConnected())
                    {
                        bluetoothPrinter.openConnection();
                    }
                    else
                    {
                        //                    savePrinterConfig(printerConfigEntity);
                        //                    PrintUtils.printText(bluetoothPrinter);
                    }
                    PrinterManager.Instance.Init                = true;
                    PrinterManager.Instance.PrinterTypeEnum     = PrinterTypeEnum.bluetooth;
                    PrinterManager.Instance.BluetoothPrinter    = bluetoothPrinter;
                    PrinterManager.Instance.PrinterConfigEntity = printerConfigEntity;
                    bluetoothPrinter.PrintString("蓝牙打印机连接成功\n\n\n\n\n");
                    responseEntity.code = ResponseCode.SUCCESS;
                    responseEntity.msg  = "打印机设置成功";
                    if (listener != null)
                    {
                        listener.Invoke(new object[] { "setPrinterCallBack", responseEntity });
                    }
                    return(true);
                }
            }
            return(false);
        }
Exemple #9
0
        //internal void setUSBPrinter(PrinterConfigEntity printerConfigEntity, JSBridge.JsCallbackHandle webCallback)
        internal void setUSBPrinter(PrinterConfigEntity printerConfigEntity, Action <object> webCallback)
        {
            IntPtr hUsb;

            if (printerConfigEntity != null)
            {
                listener       = webCallback;
                responseEntity = new ResponseEntity();
                PrinterManager.Instance.PrinterConfigEntity = printerConfigEntity;
                //edit by sven 2018年5月14日 不管USBprint是否已经init 都重新初始化一次
                //if (PrinterManager.Instance.UsbPrinter == null)
                {
                    USBPrinter usbPrinter = new USBPrinter();
                    //if (!usbPrinter.Init)
                    {
                        hUsb = usbPrinter.open();
                        if ((int)hUsb == -1)
                        {
                            responseEntity.code = ResponseCode.Failed;
                            responseEntity.msg  = "USB打印机打开失败";
                            listener?.Invoke(new object[] { "setPrinterCallBack", responseEntity });
                            return;
                        }
                        usbPrinter.HDevice = hUsb;
                    }
                    usbPrinter.pageWidth                        = printerConfigEntity.pageWidth;
                    PrinterManager.Instance.Init                = true;
                    PrinterManager.Instance.PrinterTypeEnum     = PrinterTypeEnum.usb;
                    PrinterManager.Instance.UsbPrinter          = usbPrinter;
                    PrinterManager.Instance.PrinterConfigEntity = printerConfigEntity;

                    usbPrinter.PrintString("USB打印机连接成功\n\n\n\n\n");
                    responseEntity.code = ResponseCode.SUCCESS;
                    responseEntity.msg  = "打印机设置成功";
                    //if (listener != null)
                    //{
                    //    listener.Invoke(new object[] { "setPrinterCallBack", responseEntity });
                    //}
                }
                //else
                //{
                //    if (PrinterManager.Instance.Init)
                //    {
                //        PrinterManager.Instance.UsbPrinter.PrintString("usb打印机测试成功\r\n\r\n\r\n\r\n");
                //        responseEntity.code = ResponseCode.SUCCESS;
                //        responseEntity.msg = "打印机设置成功";
                //    }
                //}


                //getUsbDevices();


                //debug

                //PrintServices.USBPrinterService upt = new PrintServices.USBPrinterService();
                //upt.TestPrint();
                //responseEntity.code = ResponseCode.SUCCESS;
            }
            else
            {
                responseEntity.code = ResponseCode.Failed;
                responseEntity.msg  = "参数不能为空";
            }
            if (listener != null)
            {
                listener.Invoke(new object[] { "setPrinterCallBack", responseEntity });
            }
        }