Skip to content

Commit

Permalink
[-] Fixed error with null object TextDocumentFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
VitalyKravtsov2016 committed Mar 13, 2023
1 parent 9b7f2a1 commit 07c625f
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 63 deletions.
7 changes: 7 additions & 0 deletions History.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@

public interface PrinterProtocol {
public void connect() throws Exception;
public void disconnect();
public void send(PrinterCommand command) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ public void connect() throws Exception {
}
}

public void disconnect(){

}

public void setMaxEnqNumber(int value) {
if (!isReliable) {
maxEnqNumber = value;
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public void connect() throws Exception
{
}

public void disconnect(){
}

public int getByteTimeout() {
return byteTimeout;
}
Expand Down
51 changes: 27 additions & 24 deletions Source/Core/src/com/shtrih/fiscalprinter/PrinterProtocol_2.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<maxSyncCount;i++)
{
connect();

int timeout = command.getTimeout();
port.setTimeout(timeout + byteTimeout);

writeCommand(command.encodeData());
if (command.hasAnswer) {
int frameNum = readAnswer();
if (frameNum == frameNumber) {
command.decodeData(rx);
stepFrameNumber();
return;

} else {
logger.error("Incorrect frame number");
isSynchronized = false;
}
stepFrameNumber();
command.decodeData(rx);
return;
}
command.decodeData(rx);
}
stepFrameNumber();
return;
throw new IOException("Failed to synchronize frapme numbers");

} catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@

import java.util.Map;
import java.util.HashMap;
import java.nio.ByteOrder;
import java.nio.ByteBuffer;
import java.io.ByteArrayInputStream;

import com.shtrih.fiscalprinter.port.ClosedConnectionException;
import com.shtrih.util.Time;
import com.shtrih.jpos.fiscalprinter.receipt.FSSaleReceiptItem;
import com.shtrih.fiscalprinter.MCNotification;
import com.shtrih.fiscalprinter.GS1Barcode;
import com.shtrih.fiscalprinter.GS1BarcodeParser;
import com.shtrih.barcode.PrinterBarcode;
import com.shtrih.barcode.SmBarcode;
import com.shtrih.barcode.SmBarcodeEncoder;
Expand All @@ -33,7 +27,6 @@
import com.shtrih.fiscalprinter.model.PrinterModels;
import com.shtrih.fiscalprinter.model.XmlModelsWriter;
import com.shtrih.fiscalprinter.port.PrinterPort;
import com.shtrih.fiscalprinter.port.SerialPrinterPort;
import com.shtrih.fiscalprinter.table.PrinterField;
import com.shtrih.fiscalprinter.table.PrinterFields;
import com.shtrih.fiscalprinter.table.PrinterTable;
Expand Down Expand Up @@ -68,18 +61,13 @@
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Vector;

import jpos.JposConst;
import jpos.JposException;
import jpos.FiscalPrinterConst;

import static jpos.JposConst.JPOS_E_EXTENDED;
import static jpos.JposConst.JPOS_E_NOHARDWARE;

public class SMFiscalPrinterImpl implements SMFiscalPrinter, PrinterConst {

Expand Down Expand Up @@ -4150,7 +4138,7 @@ public int reboot() throws Exception {
ServiceCommand command = new ServiceCommand();
command.setFunctionCode(ServiceCommand.CODE_REBOOT);
command.setIntData(0);
command.readAnswer = false; // do not wait answer
command.hasAnswer = false; // do not wait answer
int rc = executeCommand(command);
if (succeeded(rc)) {
port.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@
public interface IPrinterEvents {

public void afterCommand(PrinterCommand command) throws Exception;

public void beforeCommand(PrinterCommand command) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public abstract class PrinterCommand {
private boolean errorReportEnabled = true;
private byte[] txData;
private byte[] rxData;
public boolean readAnswer = true;
public boolean hasAnswer = true;

/**
* Creates a new instance of PrinterCommand
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class TextDocumentFilter implements IPrinterEvents {

private static CompositeLogger logger = CompositeLogger.getLogger(TextDocumentFilter.class);

private final int lineLength;
private boolean enabled = true;
private int lineLength;
private boolean enabled = false;
private String deviceName = "ККМ";
private int operatorNumber = 1;
private LongPrinterStatus status = null;
Expand All @@ -35,9 +35,8 @@ public class TextDocumentFilter implements IPrinterEvents {
private boolean isEJPresent = false;
private boolean isDocumentPrinted = false;
private boolean connected = false;
private final PrinterHeader header;
private final SMFiscalPrinter printer;
private final boolean saveToStorage;
private SMFiscalPrinter printer;
private boolean saveToStorage;
private XReport report = new XReport();
private final String[] paymentNames = new String[16];
private final List<Operator> operators = new ArrayList<Operator>();
Expand Down Expand Up @@ -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;
}
Expand All @@ -86,6 +88,10 @@ public void setEnabled(boolean value) {

@Override
public void beforeCommand(PrinterCommand command) throws Exception {
if (printer == null) {
return;
}

if (!enabled) {
return;
}
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,17 @@ public void print(int[] params) throws Exception {
printer.resetPrinter();

List<String> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -245,6 +248,7 @@ public synchronized void close() {
receiverRegistered = false;
Context context = StaticContext.getContext();
if (context != null) {
logger.debug("unregisterReceiver");
context.unregisterReceiver(mBroadcastReceiver);
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/android/tinyJavaPosTester/assets/jpos.xml
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,6 @@
<!-- fdoMode, 0 - disabled, 1 - enabled, 2 - on z report, 3 - disable in receipt -->
<prop name="fdoMode" type="String" value="3"/>

<prop name="duplicateReceipt" type="String" value="1"/>
<prop name="duplicateReceipt" type="String" value="0"/>
</JposEntry>
</JposEntries>

0 comments on commit 07c625f

Please sign in to comment.