Commit 60e01ee8 by pye52

Merge branch 'new_master' into new_for_phone

parents 5523ffdc 0e901650
No preview for this file type
...@@ -174,40 +174,10 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe ...@@ -174,40 +174,10 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
sendText.setText(log); sendText.setText(log);
break; break;
case PayOnlineState.SUCCESS: case PayOnlineState.SUCCESS:
message.setTextColor(getResources().getColor(R.color.pay_success)); paidSuccess(event.getMessage());
String successText = event.getMessage();
if (TextUtils.isEmpty(successText)) {
message.setText(R.string.pay_success);
TTSHelper.speak(getString(R.string.pay_success));
} else {
message.setText(successText);
TTSHelper.speak(successText);
}
qrCodeViewModel.scan();
String successTime = payDateFormat.format(new Date());
String successLog = "支付成功: " + successTime + " - " + event.getMessage();
LogUtils.file(TAG, successLog);
if (debugVs != null) return;
recText.setText(successLog);
break; break;
case PayOnlineState.FAILED: case PayOnlineState.FAILED:
message.setTextColor(getResources().getColor(R.color.pay_failed)); paidFailed(event.getMessage());
String failedText = event.getMessage();
if (TextUtils.isEmpty(failedText)) {
message.setText(R.string.pay_failed);
TTSHelper.speak(getString(R.string.pay_failed));
} else {
message.setText(failedText);
TTSHelper.speak(failedText);
}
qrCodeViewModel.scan();
String failedTime = payDateFormat.format(new Date());
String failedLog = "支付失败: " + failedTime + " - " + event.getMessage();
LogUtils.file(TAG, failedLog);
if (debugVs != null) return;
recText.setText(failedLog);
break; break;
} }
}); });
...@@ -226,6 +196,9 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe ...@@ -226,6 +196,9 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
} }
qrCodeViewModel.scan(); qrCodeViewModel.scan();
break; break;
case PayOfflineState.MARK_FAILED:
paidFailed(event.getMessage());
break;
} }
}); });
...@@ -292,6 +265,42 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe ...@@ -292,6 +265,42 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
SCWebSocketClient.getInstance().tryConnect(); SCWebSocketClient.getInstance().tryConnect();
} }
private void paidSuccess(String successMessage) {
message.setTextColor(getResources().getColor(R.color.pay_success));
if (TextUtils.isEmpty(successMessage)) {
message.setText(R.string.pay_success);
TTSHelper.speak(getString(R.string.pay_success));
} else {
message.setText(successMessage);
TTSHelper.speak(successMessage);
}
qrCodeViewModel.scan();
String successTime = payDateFormat.format(new Date());
String successLog = "支付成功: " + successTime + " - " + successMessage;
LogUtils.file(TAG, successLog);
if (debugVs != null) return;
recText.setText(successLog);
}
private void paidFailed(String failedMessage) {
message.setTextColor(getResources().getColor(R.color.pay_failed));
if (TextUtils.isEmpty(failedMessage)) {
message.setText(R.string.pay_failed);
TTSHelper.speak(getString(R.string.pay_failed));
} else {
message.setText(failedMessage);
TTSHelper.speak(failedMessage);
}
qrCodeViewModel.scan();
String failedTime = payDateFormat.format(new Date());
String failedLog = "支付失败: " + failedTime + " - " + failedMessage;
LogUtils.file(TAG, failedLog);
if (debugVs != null) return;
recText.setText(failedLog);
}
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
......
...@@ -34,7 +34,7 @@ public abstract class BaseQRCodeScan implements IQRCodeScan { ...@@ -34,7 +34,7 @@ public abstract class BaseQRCodeScan implements IQRCodeScan {
} }
byte[] buf = null; byte[] buf = null;
int len = 0; int len = 0;
while (buf == null || end(buf)) { while (buf == null || !end(buf)) {
byte[] b = new byte[64]; byte[] b = new byte[64];
int l = serialPortIS.read(b); int l = serialPortIS.read(b);
if (l == 0) break; if (l == 0) break;
......
...@@ -4,7 +4,7 @@ import android.os.Build; ...@@ -4,7 +4,7 @@ import android.os.Build;
public class QRCodeScanFactory { public class QRCodeScanFactory {
private static final String TPS = "TPS"; private static final String TPS = "TPS";
private static final String T3 = "T3"; private static final String T3 = "56iqDS";
private static final String TPS_PORT = "/dev/ttyS0"; private static final String TPS_PORT = "/dev/ttyS0";
private static final String T3_PORT = "/dev/ttyS6"; private static final String T3_PORT = "/dev/ttyS6";
...@@ -14,7 +14,7 @@ public class QRCodeScanFactory { ...@@ -14,7 +14,7 @@ public class QRCodeScanFactory {
} else if (Build.DEVICE.contains(T3)) { } else if (Build.DEVICE.contains(T3)) {
return new T3(T3_PORT); return new T3(T3_PORT);
} else { } else {
throw new RuntimeException("不明设备型号"); throw new RuntimeException("不明设备型号: " + Build.DEVICE);
} }
} }
} }
...@@ -11,8 +11,9 @@ public class PayOfflineState { ...@@ -11,8 +11,9 @@ public class PayOfflineState {
public static final int SEND = 2; public static final int SEND = 2;
public static final int SUCCESS = 3; public static final int SUCCESS = 3;
public static final int FAILED = 4; public static final int FAILED = 4;
public static final int MARK_FAILED = 5;
@IntDef(value = {MARK, IDLE, SEND, SUCCESS, FAILED}) @IntDef(value = {MARK, IDLE, SEND, SUCCESS, FAILED, MARK_FAILED})
public @interface PAY_OFFLINE_STATE { public @interface PAY_OFFLINE_STATE {
} }
......
...@@ -70,7 +70,6 @@ public class PayOfflineViewModel extends ViewModel { ...@@ -70,7 +70,6 @@ public class PayOfflineViewModel extends ViewModel {
public void exec(PayData payData) { public void exec(PayData payData) {
MarkRunnable runnable = new MarkRunnable(payData); MarkRunnable runnable = new MarkRunnable(payData);
SCTaskExecutor.getInstance().executeOnDiskIO(runnable); SCTaskExecutor.getInstance().executeOnDiskIO(runnable);
LogUtils.w(TAG, "WebSocket未链接: " + payData.toString());
} }
private void cancelTimeout() { private void cancelTimeout() {
...@@ -113,10 +112,20 @@ public class PayOfflineViewModel extends ViewModel { ...@@ -113,10 +112,20 @@ public class PayOfflineViewModel extends ViewModel {
private PayData payData; private PayData payData;
MarkRunnable(PayData payData) { MarkRunnable(PayData payData) {
this.payData = payData;
}
@Override
public void run() {
payData.payOffline(); payData.payOffline();
payDataRepository.updatePayData(payData); int result = payDataRepository.updatePayData(payData);
if (result <= 0) {
payOfflineState.postValue(new PayOfflineState(PayOfflineState.MARK_FAILED));
LogUtils.w(TAG, "WebSocket未链接, 订单标记失败: " + payData.toString());
return;
}
payOfflineState.postValue(new PayOfflineState(PayOfflineState.MARK)); payOfflineState.postValue(new PayOfflineState(PayOfflineState.MARK));
LogUtils.w(TAG, "WebSocket未链接, 订单已标记为离线支付: " + payData.toString());
try { try {
Thread.sleep(DEFAULT_DELAY); Thread.sleep(DEFAULT_DELAY);
} catch (Exception ignored) { } catch (Exception ignored) {
...@@ -124,11 +133,6 @@ public class PayOfflineViewModel extends ViewModel { ...@@ -124,11 +133,6 @@ public class PayOfflineViewModel extends ViewModel {
payOfflineState.postValue(new PayOfflineState(PayOfflineState.IDLE)); payOfflineState.postValue(new PayOfflineState(PayOfflineState.IDLE));
} }
} }
@Override
public void run() {
}
} }
private class RequestRunnable implements Runnable { private class RequestRunnable implements Runnable {
...@@ -160,6 +164,7 @@ public class PayOfflineViewModel extends ViewModel { ...@@ -160,6 +164,7 @@ public class PayOfflineViewModel extends ViewModel {
public void run() { public void run() {
if (payRequest == null || payRequest.getData().isEmpty()) { if (payRequest == null || payRequest.getData().isEmpty()) {
LogUtils.w(TAG, "后台返回离线支付结果,但没有待处理任务"); LogUtils.w(TAG, "后台返回离线支付结果,但没有待处理任务");
payOfflineState.postValue(new PayOfflineState(PayOfflineState.IDLE));
return; return;
} }
...@@ -200,6 +205,7 @@ public class PayOfflineViewModel extends ViewModel { ...@@ -200,6 +205,7 @@ public class PayOfflineViewModel extends ViewModel {
payOfflineState.postValue(new PayOfflineState(PayOfflineState.FAILED, "支付超时")); payOfflineState.postValue(new PayOfflineState(PayOfflineState.FAILED, "支付超时"));
if (payRequest == null || payRequest.getData().isEmpty()) { if (payRequest == null || payRequest.getData().isEmpty()) {
LogUtils.w(TAG, "离线支付超时: 待处理任务为空"); LogUtils.w(TAG, "离线支付超时: 待处理任务为空");
payOfflineState.postValue(new PayOfflineState(PayOfflineState.IDLE));
return; return;
} }
List<PayData> payDataList = payRequest.getData(); List<PayData> payDataList = payRequest.getData();
......
...@@ -103,6 +103,7 @@ public class PayOnlineViewModel extends ViewModel { ...@@ -103,6 +103,7 @@ public class PayOnlineViewModel extends ViewModel {
// 1、action为"PAY_RESULT" // 1、action为"PAY_RESULT"
if (payRequest == null || payRequest.getData().isEmpty()) { if (payRequest == null || payRequest.getData().isEmpty()) {
LogUtils.w(TAG, "后台返回在线支付结果,但没有待处理任务"); LogUtils.w(TAG, "后台返回在线支付结果,但没有待处理任务");
payOnlineState.postValue(new PayOnlineState(PayOnlineState.IDLE));
return; return;
} }
LogUtils.d(TAG, "在线支付结果响应: " + original); LogUtils.d(TAG, "在线支付结果响应: " + original);
......
...@@ -77,25 +77,20 @@ public class QRCodeViewModel extends ViewModel { ...@@ -77,25 +77,20 @@ public class QRCodeViewModel extends ViewModel {
private Runnable scanRunnable = () -> { private Runnable scanRunnable = () -> {
qrCodeState.postValue(new QRCodeState(QRCodeState.IDLE)); qrCodeState.postValue(new QRCodeState(QRCodeState.IDLE));
LogUtils.i(TAG, "开始扫描二维码"); LogUtils.i(TAG, "开始扫描二维码");
// 等待串口数据 while (!scan.available()) {}
// noinspection LoopStatementThatDoesntLoop qrCodeState.postValue(new QRCodeState(QRCodeState.SCANNING));
while (!scan.available()) {
qrCodeState.postValue(new QRCodeState(QRCodeState.SCANNING));
break;
}
// 读取串口扫描的二维码数据 // 读取串口扫描的二维码数据
String scanData; String scanData;
// 若存在测试数据,则直接读取测试数据 try {
scanData = scan.scan();
} catch (Exception e) {
LogUtils.e(TAG, "读取串口数据失败: " + e.getMessage(), e);
qrCodeState.postValue(new QRCodeState(QRCodeState.FAILED));
return;
}
// 若存在测试数据,则使用测试数据覆盖
if (!TextUtils.isEmpty(SmartCanteenUtils.testQRCode)) { if (!TextUtils.isEmpty(SmartCanteenUtils.testQRCode)) {
scanData = SmartCanteenUtils.testQRCode; scanData = SmartCanteenUtils.testQRCode;
} else {
try {
scanData = scan.scan();
} catch (Exception e) {
LogUtils.e(TAG, "读取串口数据失败: " + e.getMessage(), e);
qrCodeState.postValue(new QRCodeState(QRCodeState.FAILED));
return;
}
} }
// 需要识别二维码的类型 // 需要识别二维码的类型
String terminalType = PayData.matchTerminalType(scanData); String terminalType = PayData.matchTerminalType(scanData);
......
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