Commit cdd5b708 by patpat

二维码加入结束符

parent a3c5950e
......@@ -71,19 +71,20 @@ public class PayOfflineAction extends Action {
}
public void exec(String payCode, String payCodeType) {
setState(State.STARTED);
if (!QRCodeTask.TYPE_BHPAY.equals(payCodeType)) {
setState(State.FAIL);
return;
}
if (mPayCodeHistory.contains(payCode)) {
clearTimeoutPayCodeHistory();
setState(State.FAIL, "请不要重复扫码");
return;
}
mPayCodeHistory.add(payCode);
clearTimeoutPayCodeHistory();
try {
setState(State.STARTED);
JSONObject params = new JSONObject();
params.put("equipmentNo", App.Companion.getDeviceSN());
params.put("payCode", payCode);
......
......@@ -33,7 +33,8 @@ public class QRCodeTask {
}
private ScanAsyncTask mScanAsyncTask;
private QRCodeThread mQRCodeThread;
private SerialPort mSerial;
private long mTimerId = -1;
private QRCodeTask() {}
......@@ -45,80 +46,38 @@ public class QRCodeTask {
if (!support()) return;
if (mScanAsyncTask == null) {
mQRCodeThread = new QRCodeThread();
mQRCodeThread.start();
mScanAsyncTask = new ScanAsyncTask();
TimerHelper.INSTANCE.loop(new TimerHelper.LoopTask() {
@Override
public void run(long id, boolean isLastTime) {
if (mScanAsyncTask == null) return;
mScanAsyncTask.run();
}
}, 200, -1);
mSerial = SerialPort.build(new File("/dev/ttyS6"), 115200, 0);
if (mSerial != null) {
mScanAsyncTask = new ScanAsyncTask();
mTimerId = TimerHelper.INSTANCE.loop(new TimerHelper.LoopTask() {
@Override
public void run(long id, boolean isLastTime) {
if (mScanAsyncTask == null) return;
mScanAsyncTask.run();
}
}, 200, -1);
}
}
}
public void stop() {
TimerHelper.INSTANCE.cancel(mTimerId);
mScanAsyncTask = null;
mQRCodeThread.interrupt();
}
private class QRCodeThread extends Thread {
long lastReadTime = 0;
byte[] buf = null;
int len = 0;
private synchronized void pushBuf(byte[] b, int l) {
if (b == null || b.length == 0 || l <= 0) return;
b = Arrays.copyOfRange(b, 0, l);
buf = ByteUtil.merge(buf, b);
len += l;
lastReadTime = System.currentTimeMillis();
}
public synchronized String getResult() {
if (len == 0) return null;
if ((System.currentTimeMillis() - lastReadTime) < 190) return null;
String result = new String(buf, 0, len);
buf = null;
len = 0;
return result;
}
@Override
public void run() {
SerialPort serial = SerialPort.build(new File("/dev/ttyS6"), 115200, 0);
if (serial == null) return;
while (true) {
try {
byte[] b = new byte[64];
int l = serial.getInputStream().read(b);
pushBuf(b, l);
} catch (Exception e) {}
try {
Thread.sleep(0);
} catch (InterruptedException e) {
break;
}
}
if (mSerial != null) {
try {
serial.getInputStream().close();
serial.getOutputStream().close();
mSerial.getInputStream().close();
mSerial.getOutputStream().close();
} catch (Exception e) {}
serial.close();
mSerial.close();
mSerial = null;
}
}
private class ScanAsyncTask extends AsyncTask {
int lastLen = -1;
byte[] lastBuf;
int lastLen = -1;
@Override
protected void run(int step, int progress) {
......@@ -127,7 +86,25 @@ public class QRCodeTask {
case 0:
if (progress != 0) break;
String str = mQRCodeThread.getResult();
String str = null;
byte[] buf = null;
int len = 0;
while (true) {
try {
byte[] b = new byte[64];
int l = mSerial.getInputStream().read(b);
if (l == 0) break;
b = Arrays.copyOfRange(b, 0, l);
buf = ByteUtil.merge(buf, b);
len += l;
if (buf.length >= 2 && buf[buf.length - 2] == 0x0D && buf[buf.length - 1] == 0x0A) {
str = new String(buf, 0, len - 2);
break;
}
} catch (Exception e) {
break;
}
}
if (str == null) break;
QRCodeEvent event = new QRCodeEvent(str.getBytes(), str, checkPayCodeType(str));
......@@ -136,8 +113,8 @@ public class QRCodeTask {
if (event.payCodeType == null) {
parseCommand(event);
} else {
if (MainWebSocket.isConnected()) {
// if (false) {
if (MainWebSocket.isConnected()) {
nextProgress();
PayOnlineAction.getDefault().exec(event.string, event.payCodeType, new ActionResult() {
@Override
......@@ -147,7 +124,7 @@ public class QRCodeTask {
public void run() {
resetStep();
}
}, 1000);
}, 3000);
}
@Override
public void onFail(String message) {
......@@ -156,12 +133,19 @@ public class QRCodeTask {
public void run() {
resetStep();
}
}, 1000);
}, 3000);
}
});
} else {
PayOfflineAction.getDefault().exec(event.string, event.payCodeType);
nextProgress();
// PayOfflineAction.getDefault().upload();
PayOfflineAction.getDefault().exec(event.string, event.payCodeType);
timeout(new Runnable() {
@Override
public void run() {
resetStep();
}
}, 3000);
}
}
break;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment