Skip to content

Commit

Permalink
[-] Fixed error with device disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaly Kravtsov committed Oct 28, 2021
1 parent fb9b263 commit f791ca1
Show file tree
Hide file tree
Showing 19 changed files with 275 additions and 141 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

********************************************************************************

28.10.2021
deviceServiceVersion = 1013635

[-] Fixed error with device disconnect

********************************************************************************

19.10.2021
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ private void portWrite(int b) throws Exception {

private void portWrite(byte[] data) throws Exception {
Logger2.logTx(logger, data);
if (Thread.currentThread().isInterrupted()){
throw new InterruptedException();
}
port.write(data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ private void synchronizeFrames(int timeout) throws Exception {
private void sendCommand(byte[] data) throws Exception {
byte[] tx = frame.encode(data, frameNumber);
Logger2.logTx(logger, tx);
if (Thread.currentThread().isInterrupted()){
throw new InterruptedException();
}
port.write(tx);
}

Expand Down
3 changes: 3 additions & 0 deletions Source/Core/src/com/shtrih/fiscalprinter/SMFiscalPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -713,4 +713,7 @@ public ReadOperationRegister readOperationRegister2(int number)
public int mcClearBuffer() throws Exception;

public Object getSyncObject() throws Exception;

public String getFullSerial();

}
22 changes: 19 additions & 3 deletions Source/Core/src/com/shtrih/fiscalprinter/SMFiscalPrinterImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,10 @@ enum Boolean {
private PrinterDate lastDocDate = new PrinterDate();
private PrinterTime lastDocTime = new PrinterTime();
private long lastDocTotal = 0;
private volatile boolean stopFlag = true;
private volatile boolean stopFlag = false;
private volatile boolean interrupted = false;
private Integer fdVersion = null;
private String fullSerial = "";

public SMFiscalPrinterImpl(PrinterPort port, PrinterProtocol device,
FptrParameters params) {
Expand Down Expand Up @@ -229,7 +231,7 @@ public void deviceExecute(PrinterCommand command) throws Exception {
correctDate();
}

if (Thread.currentThread().isInterrupted()) {
if (interrupted || Thread.currentThread().isInterrupted()) {
throw new InterruptedException();
}

Expand Down Expand Up @@ -301,6 +303,7 @@ public LongPrinterStatus connect() throws Exception {

check(readDeviceMetrics());
model = selectPrinterModel(getDeviceMetrics());
fullSerial = readFullSerial();
return checkEcrMode();
}
}
Expand Down Expand Up @@ -1470,8 +1473,10 @@ public int fsStartDayOpen() throws Exception {
return executeCommand(command);
}

public void resetPrinter() throws Exception {
public void resetPrinter() throws Exception
{
tlvItems.clear();
interrupted = false;
}

public void writeTLVItems() throws Exception {
Expand Down Expand Up @@ -3340,6 +3345,11 @@ public FSReadSerial fsReadSerial() throws Exception {
return command;
}

@Override
public String getFullSerial(){
return fullSerial;
}

@Override
public String readFullSerial() throws Exception {
if (serial.isEmpty()) {
Expand Down Expand Up @@ -3965,8 +3975,14 @@ public int reboot() throws Exception {
return rc;
}

public void interrupt() {
interrupted = true;
}

public void cancelWait() {

stopFlag = true;

}

public void rebootAndWait() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1276,4 +1276,8 @@ public Object getSyncObject() throws Exception {
return null;
}

public String getFullSerial(){
return "";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public class CashDrawerImpl extends DeviceService implements
private boolean deviceEnabled = false;
// internal classes
private Thread deviceThread = null; // device poll thread
private volatile boolean deviceStopFlag = false;
private Thread eventThread = null; // event delivery thread
private final Vector events = new Vector();
private PrinterPort port;
Expand Down Expand Up @@ -218,7 +219,7 @@ private void setDrawerOpened(boolean drawerOpened) {

public void deviceProc() {
try {
while (!deviceThread.isInterrupted()) {
while (!deviceStopFlag) {
synchronized (printer) {
try {
connect();
Expand Down Expand Up @@ -273,11 +274,13 @@ public void setDeviceEnabled(boolean deviceEnabled) throws JposException {
connect();

if (fptrParams.pollEnabled) {
deviceStopFlag = false;
deviceThread = new Thread(new DeviceTarget(this));
deviceThread.start();
}
} else
{
deviceStopFlag = true;
deviceThread.interrupt();
deviceThread.join();
deviceThread = null;
Expand Down
14 changes: 12 additions & 2 deletions Source/Core/src/com/shtrih/jpos/fiscalprinter/DeviceHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.shtrih.fiscalprinter.command.ReadTableInfo;
import com.shtrih.util.CompositeLogger;
import com.shtrih.util.Localizer;
import java.util.List;

import java.util.Vector;

Expand All @@ -15,8 +16,8 @@
public class DeviceHeader implements PrinterHeader {

private final SMFiscalPrinter printer;
private final Vector<HeaderLine> header = new Vector<HeaderLine>();
private final Vector<HeaderLine> trailer = new Vector<HeaderLine>();
private final List<HeaderLine> header = new Vector<HeaderLine>();
private final List<HeaderLine> trailer = new Vector<HeaderLine>();
private final CompositeLogger logger = CompositeLogger.getLogger(DeviceHeader.class);

public DeviceHeader(SMFiscalPrinter printer) {
Expand Down Expand Up @@ -177,4 +178,13 @@ public void endNonFiscal(String additionalTrailer)
public void endFiscal(String additionalTrailer)
throws Exception {
}

public List<HeaderLine> getHeaderLines(){
return header;
}

public List<HeaderLine> getTrailerLines(){
return trailer;
}

}
12 changes: 10 additions & 2 deletions Source/Core/src/com/shtrih/jpos/fiscalprinter/DriverHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public class DriverHeader implements JposConst, PrinterHeader {
private int lineNumber = 0;
private int numHeaderLines = 0;
private final SMFiscalPrinter printer;
private final Vector<HeaderLine> header = new Vector<HeaderLine>();
private final Vector<HeaderLine> trailer = new Vector<HeaderLine>();
private final List<HeaderLine> header = new Vector<HeaderLine>();
private final List<HeaderLine> trailer = new Vector<HeaderLine>();
private final CompositeLogger logger = CompositeLogger.getLogger(DriverHeader.class);

/**
Expand Down Expand Up @@ -337,4 +337,12 @@ public void beginDocument(String additionalHeader) throws Exception
printText(additionalHeader);
}

public List<HeaderLine> getHeaderLines(){
return header;
}

public List<HeaderLine> getTrailerLines(){
return trailer;
}

}
13 changes: 7 additions & 6 deletions Source/Core/src/com/shtrih/jpos/fiscalprinter/FSService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class FSService implements Runnable {
private final int connectTimeout;
private final SMFiscalPrinter printer;
private volatile Thread thread = null;
private volatile boolean stopFlag = false;

public FSService(SMFiscalPrinter printer, FptrParameters parameters, FDOParameters ofdParameters) {
if (printer == null) {
Expand All @@ -51,6 +52,7 @@ private boolean isStarted() {
public void start() throws Exception {
if (!isStarted()) {
logger.debug("FSService starting");
stopFlag = false;
thread = new Thread(this);
thread.start();
}
Expand All @@ -60,6 +62,7 @@ public void stop() throws Exception {

if (isStarted()) {
logger.debug("FSService stopping");
stopFlag = true;
thread.interrupt();
thread.join();
thread = null;
Expand All @@ -73,10 +76,8 @@ public void run() {
parameters.getHost(), parameters.getPort(), connectTimeout,
parameters.getPollPeriodSeconds() * 1000));

while (!Thread.currentThread().isInterrupted()) {
synchronized (printer.getSyncObject()) {
checkData();
}
while (!stopFlag) {
checkData();
Time.delay(parameters.getPollPeriodSeconds() * 1000);
}
} catch (InterruptedException e) {
Expand All @@ -94,15 +95,15 @@ private void checkData() throws Exception {
return;
}
// System.out.println("FS -> OFD: " + Hex.toHex(data));
if (thread.isInterrupted()) {
if (stopFlag) {
return;
}
byte[] answer = sendData(data);
if (answer.length == 0) {
return;
}
// System.out.println("FS <- OFD: " + Hex.toHex(answer));
if (thread.isInterrupted()) {
if (stopFlag) {
return;
}
printer.fsWriteBlockData(answer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public class FirmwareUpdaterService implements Runnable, IPrinterEvents {

private final SMFiscalPrinter printer;

private volatile Thread thread = null;
private long oldFirmwareVersion;
private long newFirmwareVersion;
private byte[] firmware;

private volatile Thread thread = null;
private volatile boolean stopFlag = false;
private FirmwareUpdateObserver listener = new FirmwareUpdateObserver();

public FirmwareUpdaterService(SMFiscalPrinter printer) {
Expand All @@ -54,12 +54,12 @@ public void run() {
Time.delay(5 * 1000);

logger.debug("Starting FirmwareUpdaterService");
while (!Thread.currentThread().isInterrupted())
while (!stopFlag)
{
if (firmware == null) {
checkData();
}
if (thread.isInterrupted()) break;
if (stopFlag) break;

updateFirmware();
Time.delay(pollPeriodSeconds * 1000);
Expand All @@ -76,7 +76,7 @@ public void run() {

private void checkData() {
try {
if (thread.isInterrupted()) return;
if (stopFlag) return;

logger.debug("Checking for firmware update");
listener.OnCheckingForUpdate();
Expand All @@ -102,7 +102,7 @@ private void checkData() {
firmwareVersion = printer.getDeviceMetrics().getModel() * 1000000 + printer.readLongStatus().getFirmwareBuild();
}

if (thread.isInterrupted()) return;
if (stopFlag) return;

ScocClient client = new ScocClient(serialNumber, uin.longValue());

Expand All @@ -124,7 +124,7 @@ private void checkData() {

out.write(firstResponse.getData());

if (thread.isInterrupted()) return;
if (stopFlag) return;

long newVersion = firstResponse.getFirmwareVersion();

Expand All @@ -137,7 +137,7 @@ private void checkData() {

for (int i = 2; i <= firstResponse.getPartsCount(); i++) {

if (thread.isInterrupted()) return;
if (stopFlag) return;

DeviceFirmwareResponse nextPart = client.readFirmware(newVersion, i);

Expand Down Expand Up @@ -166,6 +166,7 @@ private boolean isStarted() {
public void start() {
if (!isStarted()) {
firmware = null;
stopFlag = false;
thread = new Thread(this);
thread.start();
}
Expand All @@ -174,6 +175,7 @@ public void start() {
public void stop() throws Exception {
if (isStarted()) {
printer.cancelWait();
stopFlag = true;
thread.interrupt();
thread.join();
thread = null;
Expand Down Expand Up @@ -225,7 +227,7 @@ private void updateFirmware() throws Exception

writeFirmware();

if (thread.isInterrupted()) return;
if (stopFlag) return;

long doneAt = System.currentTimeMillis();

Expand Down Expand Up @@ -259,7 +261,7 @@ private void writeFirmware() throws Exception {

while (stream.available() > 0) {

if (thread.isInterrupted()) return;
if (stopFlag) return;

stream.read(block, 0, 128);
printer.writeFirmwareBlockToSDCard(fileType, blockNumber, block);
Expand Down
Loading

0 comments on commit f791ca1

Please sign in to comment.