Commit 41cfa9b9 by pye52

优化扫码流程,保证每次扫码成功都会播报提示音

parent 2f0afa13
...@@ -190,6 +190,15 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe ...@@ -190,6 +190,15 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
qrCodeViewModel.getQRCodeStateEvent().observe(this, event -> { qrCodeViewModel.getQRCodeStateEvent().observe(this, event -> {
switch (event.getState()) { switch (event.getState()) {
case QRCodeState.INIT_FAILED:
if (debugLayoutIsNotInflate()) return;
message.setVisibility(View.GONE);
sendText.setTextColor(getResources().getColor(R.color.serial_port_error));
sendText.setText(R.string.serial_port_init_failed);
String logMessage = String.format(Locale.getDefault(), getString(R.string.log_dir), LogUtils.getConfig().getDir());
recText.setTextColor(getResources().getColor(R.color.serial_port_error));
recText.setText(logMessage);
break;
case QRCodeState.IDLE: case QRCodeState.IDLE:
message.setTextColor(getResources().getColor(R.color.pay_idle)); message.setTextColor(getResources().getColor(R.color.pay_idle));
message.setText(R.string.pay_idle); message.setText(R.string.pay_idle);
...@@ -200,13 +209,17 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe ...@@ -200,13 +209,17 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
break; break;
case QRCodeState.SUCCESS: case QRCodeState.SUCCESS:
TTSHelper.speak("beep"); TTSHelper.speak("beep");
break;
case QRCodeState.FAILED:
TTSHelper.speak("二维码读取失败");
qrCodeViewModel.scan();
break;
case QRCodeState.PAY:
PayData data = event.getData(); PayData data = event.getData();
if (data != null) { if (SCWebSocketClient.getInstance().isOpen()) {
if (SCWebSocketClient.getInstance().isOpen()) { payOnlineViewModel.exec(data);
payOnlineViewModel.exec(data); } else {
} else { payOfflineViewModel.exec(data);
payOfflineViewModel.exec(data);
}
} }
if (debugLayoutIsNotInflate()) return; if (debugLayoutIsNotInflate()) return;
...@@ -221,7 +234,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe ...@@ -221,7 +234,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
} }
qrCodeText.setText(msg); qrCodeText.setText(msg);
break; break;
case QRCodeState.FAILED: case QRCodeState.ILLEGAL:
String failedText = event.getMessage(); String failedText = event.getMessage();
if (TextUtils.isEmpty(failedText)) { if (TextUtils.isEmpty(failedText)) {
TTSHelper.speak("无效二维码"); TTSHelper.speak("无效二维码");
...@@ -230,15 +243,6 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe ...@@ -230,15 +243,6 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
} }
qrCodeViewModel.scan(); qrCodeViewModel.scan();
break; break;
case QRCodeState.ILLEGAL:
if (debugLayoutIsNotInflate()) return;
message.setVisibility(View.GONE);
sendText.setTextColor(getResources().getColor(R.color.serial_port_error));
sendText.setText(R.string.serial_port_init_failed);
String logMessage = String.format(Locale.getDefault(), getString(R.string.log_dir), LogUtils.getConfig().getDir());
recText.setTextColor(getResources().getColor(R.color.serial_port_error));
recText.setText(logMessage);
break;
} }
}); });
......
...@@ -11,9 +11,11 @@ public class QRCodeState { ...@@ -11,9 +11,11 @@ public class QRCodeState {
public static final int SCANNING = 1; public static final int SCANNING = 1;
public static final int SUCCESS = 2; public static final int SUCCESS = 2;
public static final int FAILED = 3; public static final int FAILED = 3;
public static final int ILLEGAL = 4; public static final int PAY = 4;
public static final int ILLEGAL = 5;
public static final int INIT_FAILED = -1;
@IntDef(value = {IDLE, SCANNING, SUCCESS, FAILED, ILLEGAL}) @IntDef(value = {IDLE, SCANNING, SUCCESS, FAILED, PAY, ILLEGAL, INIT_FAILED})
public @interface QRCODE_STATE { public @interface QRCODE_STATE {
} }
......
...@@ -34,6 +34,7 @@ import static com.bgycc.smartcanteen.utils.SmartCanteenUtils.TAG; ...@@ -34,6 +34,7 @@ import static com.bgycc.smartcanteen.utils.SmartCanteenUtils.TAG;
*/ */
public class QRCodeViewModel extends ViewModel { public class QRCodeViewModel extends ViewModel {
private static final String RESPONSE_ACTION = "action"; private static final String RESPONSE_ACTION = "action";
private static final long SCAN_DELAY = 100;
private static final long DELAY = 1500; private static final long DELAY = 1500;
private PayDataRepository payDataRepository; private PayDataRepository payDataRepository;
...@@ -63,7 +64,7 @@ public class QRCodeViewModel extends ViewModel { ...@@ -63,7 +64,7 @@ public class QRCodeViewModel extends ViewModel {
} catch (Exception e) { } catch (Exception e) {
LogUtils.e(TAG, "串口初始化失败: " + e.getMessage(), e); LogUtils.e(TAG, "串口初始化失败: " + e.getMessage(), e);
scan = null; scan = null;
qrCodeState.postValue(new QRCodeState(QRCodeState.ILLEGAL)); qrCodeState.postValue(new QRCodeState(QRCodeState.INIT_FAILED));
} }
} }
...@@ -83,11 +84,16 @@ public class QRCodeViewModel extends ViewModel { ...@@ -83,11 +84,16 @@ public class QRCodeViewModel extends ViewModel {
String scanData; String scanData;
try { try {
scanData = scan.scan(); scanData = scan.scan();
qrCodeState.postValue(new QRCodeState(QRCodeState.SUCCESS));
} catch (Exception e) { } catch (Exception e) {
LogUtils.e(TAG, "读取串口数据失败: " + e.getMessage(), e); LogUtils.e(TAG, "读取串口数据失败: " + e.getMessage(), e);
qrCodeState.postValue(new QRCodeState(QRCodeState.FAILED)); qrCodeState.postValue(new QRCodeState(QRCodeState.FAILED));
return; return;
} }
try {
Thread.sleep(SCAN_DELAY);
} catch (InterruptedException ignored) {
}
// 若存在测试数据,则使用测试数据覆盖 // 若存在测试数据,则使用测试数据覆盖
if (!TextUtils.isEmpty(SmartCanteenUtils.testQRCode)) { if (!TextUtils.isEmpty(SmartCanteenUtils.testQRCode)) {
scanData = SmartCanteenUtils.testQRCode; scanData = SmartCanteenUtils.testQRCode;
...@@ -101,7 +107,7 @@ public class QRCodeViewModel extends ViewModel { ...@@ -101,7 +107,7 @@ public class QRCodeViewModel extends ViewModel {
jsonObject = JsonParser.parseString(scanData).getAsJsonObject(); jsonObject = JsonParser.parseString(scanData).getAsJsonObject();
} catch (JsonSyntaxException jse) { } catch (JsonSyntaxException jse) {
LogUtils.e(TAG, "非法的指令格式: " + scanData); LogUtils.e(TAG, "非法的指令格式: " + scanData);
qrCodeState.postValue(new QRCodeState(QRCodeState.FAILED)); qrCodeState.postValue(new QRCodeState(QRCodeState.ILLEGAL));
return; return;
} }
if (jsonObject.has(RESPONSE_ACTION)) { if (jsonObject.has(RESPONSE_ACTION)) {
...@@ -109,11 +115,10 @@ public class QRCodeViewModel extends ViewModel { ...@@ -109,11 +115,10 @@ public class QRCodeViewModel extends ViewModel {
Command command = new Command(scanData, action); Command command = new Command(scanData, action);
commandRepository.insertCommand(command); commandRepository.insertCommand(command);
LogUtils.d(TAG, "扫码结果: " + command.toString()); LogUtils.d(TAG, "扫码结果: " + command.toString());
// 任务写入数据库成功后,主动发送一次IDLE,以重置界面提示语 qrCodeState.postValue(new QRCodeState(QRCodeState.IDLE));
qrCodeState.postValue(new QRCodeState(QRCodeState.SUCCESS, scanData));
againDelay(); againDelay();
} else { } else {
qrCodeState.postValue(new QRCodeState(QRCodeState.FAILED)); qrCodeState.postValue(new QRCodeState(QRCodeState.ILLEGAL));
} }
return; return;
} }
...@@ -123,7 +128,7 @@ public class QRCodeViewModel extends ViewModel { ...@@ -123,7 +128,7 @@ public class QRCodeViewModel extends ViewModel {
PayData existsPay = payDataRepository.queryPayDataByPayCode(scanData); PayData existsPay = payDataRepository.queryPayDataByPayCode(scanData);
if (existsPay != null) { if (existsPay != null) {
LogUtils.w(TAG, "该订单已存在: " + scanData); LogUtils.w(TAG, "该订单已存在: " + scanData);
qrCodeState.postValue(new QRCodeState(QRCodeState.FAILED, "请不要重复扫码")); qrCodeState.postValue(new QRCodeState(QRCodeState.ILLEGAL, "请不要重复扫码"));
return; return;
} }
// 扫描到的二维码保存到数据库 // 扫描到的二维码保存到数据库
...@@ -132,7 +137,7 @@ public class QRCodeViewModel extends ViewModel { ...@@ -132,7 +137,7 @@ public class QRCodeViewModel extends ViewModel {
LogUtils.e(TAG, "数据库插入失败: " + newPay.toString()); LogUtils.e(TAG, "数据库插入失败: " + newPay.toString());
} }
LogUtils.d(TAG, "扫码结果: " + newPay.toString()); LogUtils.d(TAG, "扫码结果: " + newPay.toString());
qrCodeState.postValue(new QRCodeState(QRCodeState.SUCCESS, newPay, newPay.getPayCode())); qrCodeState.postValue(new QRCodeState(QRCodeState.PAY, newPay, newPay.getPayCode()));
}; };
private void againDelay() { private void againDelay() {
......
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