From 07c625f9d936ed218102049ceead867935e6c28e Mon Sep 17 00:00:00 2001 From: Vitaly Kravtsov Date: Mon, 13 Mar 2023 16:36:17 +0300 Subject: [PATCH] [-] Fixed error with null object TextDocumentFilter --- History.txt | 7 +++ .../shtrih/fiscalprinter/PrinterProtocol.java | 1 + .../fiscalprinter/PrinterProtocol_1.java | 8 ++- .../fiscalprinter/PrinterProtocol_1_1.java | 3 ++ .../fiscalprinter/PrinterProtocol_2.java | 51 ++++++++++--------- .../fiscalprinter/SMFiscalPrinterImpl.java | 14 +---- .../fiscalprinter/command/IPrinterEvents.java | 1 - .../fiscalprinter/command/PrinterCommand.java | 2 +- .../command/TextDocumentFilter.java | 26 +++++++--- .../jpos/fiscalprinter/FiscalPrinterImpl.java | 19 ++++--- .../jpos/fiscalprinter/TextReportPrinter.java | 9 ++-- .../src/com/shtrih/util/ServiceVersion.java | 2 +- .../fiscalprinter/port/BluetoothPort.java | 6 ++- .../android/tinyJavaPosTester/assets/jpos.xml | 2 +- 14 files changed, 88 insertions(+), 63 deletions(-) diff --git a/History.txt b/History.txt index f21c18ce..2a86c433 100644 --- a/History.txt +++ b/History.txt @@ -5,6 +5,13 @@ Company : SHTRIH-M www.shtrih-m.ru (495) 787-6090 Url : https://github.com/shtrih-m/javapos_shtrih +******************************************************************************** + + 13.03.2023 + deviceServiceVersion = 1013692 + + [-] Fixed error with null object TextDocumentFilter + ******************************************************************************** 09.03.2023 diff --git a/Source/Core/src/com/shtrih/fiscalprinter/PrinterProtocol.java b/Source/Core/src/com/shtrih/fiscalprinter/PrinterProtocol.java index 34ee83b2..0a0cbafb 100644 --- a/Source/Core/src/com/shtrih/fiscalprinter/PrinterProtocol.java +++ b/Source/Core/src/com/shtrih/fiscalprinter/PrinterProtocol.java @@ -18,5 +18,6 @@ public interface PrinterProtocol { public void connect() throws Exception; + public void disconnect(); public void send(PrinterCommand command) throws Exception; } diff --git a/Source/Core/src/com/shtrih/fiscalprinter/PrinterProtocol_1.java b/Source/Core/src/com/shtrih/fiscalprinter/PrinterProtocol_1.java index f7ba59fb..371d6d5f 100644 --- a/Source/Core/src/com/shtrih/fiscalprinter/PrinterProtocol_1.java +++ b/Source/Core/src/com/shtrih/fiscalprinter/PrinterProtocol_1.java @@ -281,6 +281,10 @@ public void connect() throws Exception { } } + public void disconnect(){ + + } + public void setMaxEnqNumber(int value) { if (!isReliable) { maxEnqNumber = value; @@ -362,8 +366,8 @@ public void send(PrinterCommand command) throws Exception { logger.debug("sendCommand: " + command.getText() + ", " + command.getIsRepeatable()); byte[] tx = frame.encode(command.encodeData()); - byte[] rx = send(tx, command.getTimeout(), command.readAnswer); - if (command.readAnswer) { + byte[] rx = send(tx, command.getTimeout(), command.hasAnswer); + if (command.hasAnswer) { command.decodeData(rx); } } diff --git a/Source/Core/src/com/shtrih/fiscalprinter/PrinterProtocol_1_1.java b/Source/Core/src/com/shtrih/fiscalprinter/PrinterProtocol_1_1.java index a109e980..93c003ea 100644 --- a/Source/Core/src/com/shtrih/fiscalprinter/PrinterProtocol_1_1.java +++ b/Source/Core/src/com/shtrih/fiscalprinter/PrinterProtocol_1_1.java @@ -84,6 +84,9 @@ public void connect() throws Exception { } + public void disconnect(){ + } + public int getByteTimeout() { return byteTimeout; } diff --git a/Source/Core/src/com/shtrih/fiscalprinter/PrinterProtocol_2.java b/Source/Core/src/com/shtrih/fiscalprinter/PrinterProtocol_2.java index 978af3b3..804e7481 100644 --- a/Source/Core/src/com/shtrih/fiscalprinter/PrinterProtocol_2.java +++ b/Source/Core/src/com/shtrih/fiscalprinter/PrinterProtocol_2.java @@ -2,13 +2,12 @@ import com.shtrih.fiscalprinter.command.CommandOutputStream; import com.shtrih.fiscalprinter.command.PrinterCommand; -import com.shtrih.fiscalprinter.command.PrinterConst; import com.shtrih.fiscalprinter.port.PrinterPort; import com.shtrih.util.CompositeLogger; -import com.shtrih.util.Localizer; import com.shtrih.util.Logger2; import java.io.ByteArrayOutputStream; +import java.io.IOException; /** * @author V.Kravtsov @@ -41,33 +40,37 @@ public void connect() throws Exception synchronizeFrames(byteTimeout); } + public void disconnect(){ + isSynchronized = false; + } + public void send(PrinterCommand command) throws Exception { - try { - connect(); - - int timeout = command.getTimeout(); - port.setTimeout(timeout + byteTimeout); - - writeCommand(command.encodeData()); - if (command.readAnswer) { - int frameNum = readAnswer(); - if (frameNum != frameNumber) { - logger.error("Incorrect frame number"); - for (; ; ) { - frameNum = readAnswer(); - if (frameNum == frameNumber) { - break; - } + try + { + int maxSyncCount = 3; + for (int i=0;i operators = new ArrayList(); @@ -68,14 +67,17 @@ public class TextDocumentFilter implements IPrinterEvents { public static final String SINN = "ИНН"; private static String[] docNames = {SSaleText, SBuyText, SRetSaleText, SRetBuyText}; - public TextDocumentFilter(SMFiscalPrinter printer, PrinterHeader header) throws Exception { - this.header = header; + public TextDocumentFilter() { + } + + public void init(SMFiscalPrinter printer) throws Exception + { this.printer = printer; saveToStorage = printer.getParams().textReportEnabled; lineLength = printer.getMessageLength(FontNumber.getNormalFont()); readLastDoc(); } - + public boolean getEnabled() { return enabled; } @@ -86,6 +88,10 @@ public void setEnabled(boolean value) { @Override public void beforeCommand(PrinterCommand command) throws Exception { + if (printer == null) { + return; + } + if (!enabled) { return; } @@ -115,6 +121,10 @@ public void beforeCommand(PrinterCommand command) throws Exception { @Override public void afterCommand(PrinterCommand command) throws Exception { + if (printer == null) { + return; + } + if (!enabled) { return; } diff --git a/Source/Core/src/com/shtrih/jpos/fiscalprinter/FiscalPrinterImpl.java b/Source/Core/src/com/shtrih/jpos/fiscalprinter/FiscalPrinterImpl.java index 4277b5cc..104149d9 100644 --- a/Source/Core/src/com/shtrih/jpos/fiscalprinter/FiscalPrinterImpl.java +++ b/Source/Core/src/com/shtrih/jpos/fiscalprinter/FiscalPrinterImpl.java @@ -149,7 +149,7 @@ public class FiscalPrinterImpl extends DeviceService implements PrinterConst, private PrinterHeader header; private int[] vatValues; private PrinterPort port; - private PrinterProtocol device; + private PrinterProtocol device = null; private SMFiscalPrinter printer; private ReceiptPrinter receiptPrinter; private final FiscalPrinterStatistics statistics; @@ -267,7 +267,7 @@ public class FiscalPrinterImpl extends DeviceService implements PrinterConst, private boolean isRecPresent = true; private boolean filterEnabled = true; private boolean isInReceiptTrailer = false; - private TextDocumentFilter filter = null; + private TextDocumentFilter filter = new TextDocumentFilter(); private FDOService fdoService; private FirmwareUpdaterService firmwareUpdaterService; private boolean docEndEnabled = true; @@ -276,8 +276,8 @@ public class FiscalPrinterImpl extends DeviceService implements PrinterConst, private volatile boolean eventStopFlag = false; private ReceiptContext receiptContext = null; - public void enableTextDocumentFilter(boolean value) { - filter.setEnabled(value); + public TextDocumentFilter getTextDocumentFilter() { + return filter; } public void setFirmwareUpdateObserver(FirmwareUpdateObserver observer) { @@ -960,9 +960,10 @@ public void setDeviceEnabled(boolean deviceEnabled) throws Exception { logger.error("Failed to start JsonUpdateService", e); } + filter.init(getPrinter()); + getPrinter().addEvents(filter); if (params.textReportEnabled || (params.duplicateReceipt == SmFptrConst.DUPLICATE_RECEIPT_DRIVER)) { - filter = new TextDocumentFilter(getPrinter(), header); - getPrinter().addEvents(filter); + filter.setEnabled(true); } } else { @@ -2282,8 +2283,12 @@ public void onConnect() { setPowerState(JPOS_PS_ONLINE); } - public void onDisconnect() { + public void onDisconnect() + { setPowerState(JPOS_PS_OFFLINE); + if (device != null) { + device.disconnect(); + } } } diff --git a/Source/Core/src/com/shtrih/jpos/fiscalprinter/TextReportPrinter.java b/Source/Core/src/com/shtrih/jpos/fiscalprinter/TextReportPrinter.java index 66af74b6..18db72cf 100644 --- a/Source/Core/src/com/shtrih/jpos/fiscalprinter/TextReportPrinter.java +++ b/Source/Core/src/com/shtrih/jpos/fiscalprinter/TextReportPrinter.java @@ -90,16 +90,17 @@ public void print(int[] params) throws Exception { printer.resetPrinter(); List lines = readReport(params); - - printer.enableTextDocumentFilter(false); + boolean filterEnabled = printer.getTextDocumentFilter().getEnabled(); + printer.getTextDocumentFilter().setEnabled(false); try { for (String line : lines) { printer.getPrinter().printLine(PrinterConst.SMFP_STATION_REC, line, FontNumber.getNormalFont()); } printer.printEndFiscal(); - } finally { - printer.enableTextDocumentFilter(true); + } finally + { + printer.getTextDocumentFilter().setEnabled(filterEnabled); } } diff --git a/Source/FiscalPrinterService/src/com/shtrih/util/ServiceVersion.java b/Source/FiscalPrinterService/src/com/shtrih/util/ServiceVersion.java index a1306bbe..76c319ea 100644 --- a/Source/FiscalPrinterService/src/com/shtrih/util/ServiceVersion.java +++ b/Source/FiscalPrinterService/src/com/shtrih/util/ServiceVersion.java @@ -1,5 +1,5 @@ package com.shtrih.util; public class ServiceVersion { - public static final String VERSION = "690-2-gc0c4ad4"; + public static final String VERSION = "691"; } \ No newline at end of file diff --git a/Source/android/FptrServiceAndroid/src/com/shtrih/fiscalprinter/port/BluetoothPort.java b/Source/android/FptrServiceAndroid/src/com/shtrih/fiscalprinter/port/BluetoothPort.java index 1c9b5972..deb8ba41 100644 --- a/Source/android/FptrServiceAndroid/src/com/shtrih/fiscalprinter/port/BluetoothPort.java +++ b/Source/android/FptrServiceAndroid/src/com/shtrih/fiscalprinter/port/BluetoothPort.java @@ -178,7 +178,9 @@ private void connectDevice(BluetoothDevice device) throws Exception { } } - private void registerReceiver() { + private void registerReceiver() + { + logger.debug("registerReceiver"); try { // events IntentFilter filter = new IntentFilter(); @@ -188,6 +190,7 @@ private void registerReceiver() { if (context != null) { context.registerReceiver(mBroadcastReceiver, filter); receiverRegistered = true; + logger.debug("Receiver registered"); } } catch (Exception e) { logger.error("Failed to register receiver, ", e); @@ -245,6 +248,7 @@ public synchronized void close() { receiverRegistered = false; Context context = StaticContext.getContext(); if (context != null) { + logger.debug("unregisterReceiver"); context.unregisterReceiver(mBroadcastReceiver); } } diff --git a/Source/android/tinyJavaPosTester/assets/jpos.xml b/Source/android/tinyJavaPosTester/assets/jpos.xml index c55673ec..bf48cbfc 100644 --- a/Source/android/tinyJavaPosTester/assets/jpos.xml +++ b/Source/android/tinyJavaPosTester/assets/jpos.xml @@ -364,6 +364,6 @@ - + \ No newline at end of file