Commit cdd5b708 by patpat

二维码加入结束符

parent a3c5950e
...@@ -71,19 +71,20 @@ public class PayOfflineAction extends Action { ...@@ -71,19 +71,20 @@ public class PayOfflineAction extends Action {
} }
public void exec(String payCode, String payCodeType) { public void exec(String payCode, String payCodeType) {
setState(State.STARTED);
if (!QRCodeTask.TYPE_BHPAY.equals(payCodeType)) { if (!QRCodeTask.TYPE_BHPAY.equals(payCodeType)) {
setState(State.FAIL); setState(State.FAIL);
return; return;
} }
if (mPayCodeHistory.contains(payCode)) { if (mPayCodeHistory.contains(payCode)) {
clearTimeoutPayCodeHistory();
setState(State.FAIL, "请不要重复扫码"); setState(State.FAIL, "请不要重复扫码");
return; return;
} }
mPayCodeHistory.add(payCode); mPayCodeHistory.add(payCode);
clearTimeoutPayCodeHistory();
try { try {
setState(State.STARTED);
JSONObject params = new JSONObject(); JSONObject params = new JSONObject();
params.put("equipmentNo", App.Companion.getDeviceSN()); params.put("equipmentNo", App.Companion.getDeviceSN());
params.put("payCode", payCode); params.put("payCode", payCode);
......
...@@ -33,7 +33,8 @@ public class QRCodeTask { ...@@ -33,7 +33,8 @@ public class QRCodeTask {
} }
private ScanAsyncTask mScanAsyncTask; private ScanAsyncTask mScanAsyncTask;
private QRCodeThread mQRCodeThread; private SerialPort mSerial;
private long mTimerId = -1;
private QRCodeTask() {} private QRCodeTask() {}
...@@ -45,10 +46,10 @@ public class QRCodeTask { ...@@ -45,10 +46,10 @@ public class QRCodeTask {
if (!support()) return; if (!support()) return;
if (mScanAsyncTask == null) { if (mScanAsyncTask == null) {
mQRCodeThread = new QRCodeThread(); mSerial = SerialPort.build(new File("/dev/ttyS6"), 115200, 0);
mQRCodeThread.start(); if (mSerial != null) {
mScanAsyncTask = new ScanAsyncTask(); mScanAsyncTask = new ScanAsyncTask();
TimerHelper.INSTANCE.loop(new TimerHelper.LoopTask() { mTimerId = TimerHelper.INSTANCE.loop(new TimerHelper.LoopTask() {
@Override @Override
public void run(long id, boolean isLastTime) { public void run(long id, boolean isLastTime) {
if (mScanAsyncTask == null) return; if (mScanAsyncTask == null) return;
...@@ -58,67 +59,25 @@ public class QRCodeTask { ...@@ -58,67 +59,25 @@ public class QRCodeTask {
}, 200, -1); }, 200, -1);
} }
} }
}
public void stop() { public void stop() {
TimerHelper.INSTANCE.cancel(mTimerId);
mScanAsyncTask = null; mScanAsyncTask = null;
mQRCodeThread.interrupt(); if (mSerial != null) {
}
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;
}
}
try { try {
serial.getInputStream().close(); mSerial.getInputStream().close();
serial.getOutputStream().close(); mSerial.getOutputStream().close();
} catch (Exception e) {} } catch (Exception e) {}
serial.close(); mSerial.close();
mSerial = null;
} }
} }
private class ScanAsyncTask extends AsyncTask { private class ScanAsyncTask extends AsyncTask {
int lastLen = -1;
byte[] lastBuf; byte[] lastBuf;
int lastLen = -1;
@Override @Override
protected void run(int step, int progress) { protected void run(int step, int progress) {
...@@ -127,7 +86,25 @@ public class QRCodeTask { ...@@ -127,7 +86,25 @@ public class QRCodeTask {
case 0: case 0:
if (progress != 0) break; 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; if (str == null) break;
QRCodeEvent event = new QRCodeEvent(str.getBytes(), str, checkPayCodeType(str)); QRCodeEvent event = new QRCodeEvent(str.getBytes(), str, checkPayCodeType(str));
...@@ -136,8 +113,8 @@ public class QRCodeTask { ...@@ -136,8 +113,8 @@ public class QRCodeTask {
if (event.payCodeType == null) { if (event.payCodeType == null) {
parseCommand(event); parseCommand(event);
} else { } else {
if (MainWebSocket.isConnected()) {
// if (false) { // if (false) {
if (MainWebSocket.isConnected()) {
nextProgress(); nextProgress();
PayOnlineAction.getDefault().exec(event.string, event.payCodeType, new ActionResult() { PayOnlineAction.getDefault().exec(event.string, event.payCodeType, new ActionResult() {
@Override @Override
...@@ -147,7 +124,7 @@ public class QRCodeTask { ...@@ -147,7 +124,7 @@ public class QRCodeTask {
public void run() { public void run() {
resetStep(); resetStep();
} }
}, 1000); }, 3000);
} }
@Override @Override
public void onFail(String message) { public void onFail(String message) {
...@@ -156,12 +133,19 @@ public class QRCodeTask { ...@@ -156,12 +133,19 @@ public class QRCodeTask {
public void run() { public void run() {
resetStep(); resetStep();
} }
}, 1000); }, 3000);
} }
}); });
} else { } else {
PayOfflineAction.getDefault().exec(event.string, event.payCodeType); nextProgress();
// PayOfflineAction.getDefault().upload(); // PayOfflineAction.getDefault().upload();
PayOfflineAction.getDefault().exec(event.string, event.payCodeType);
timeout(new Runnable() {
@Override
public void run() {
resetStep();
}
}, 3000);
} }
} }
break; 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