Commit 2683985d by patpat

调整超时逻辑,超时后自动转为离线支付

parent 4a2f1ade
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="JDK" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="JDK" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">
......
...@@ -10,8 +10,8 @@ android { ...@@ -10,8 +10,8 @@ android {
applicationId "com.bgycc.smartcanteen" applicationId "com.bgycc.smartcanteen"
minSdkVersion 21 minSdkVersion 21
targetSdkVersion 22 targetSdkVersion 22
versionCode 5 versionCode 6
versionName "1.0.5" versionName "1.0.6"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
} }
buildTypes { buildTypes {
......
...@@ -24,6 +24,8 @@ class App : Application() { ...@@ -24,6 +24,8 @@ class App : Application() {
private var sVersionName = "0.0.0" private var sVersionName = "0.0.0"
private var sVersionCode = 0 private var sVersionCode = 0
var testQRCode = ""
fun getDefault(): App { fun getDefault(): App {
return sDefault return sDefault
} }
......
...@@ -59,7 +59,6 @@ public class PayOnlineAction extends Action { ...@@ -59,7 +59,6 @@ public class PayOnlineAction extends Action {
break; break;
case RESQUEST_FAIL: case RESQUEST_FAIL:
case RESPONSE_FAIL: case RESPONSE_FAIL:
case TIMEOUT:
EventBus.getDefault().post(new PayStateEvent(PayStateEvent.StateEnum.FAIL, message)); EventBus.getDefault().post(new PayStateEvent(PayStateEvent.StateEnum.FAIL, message));
cancelTimeout(); cancelTimeout();
mPayCode = ""; mPayCode = "";
...@@ -71,12 +70,18 @@ public class PayOnlineAction extends Action { ...@@ -71,12 +70,18 @@ public class PayOnlineAction extends Action {
} }
}, 3000); }, 3000);
break; break;
case TIMEOUT:
cancelTimeout();
mPayCode = "";
fail(message);
setState(State.INITED);
return;
} }
} }
setState(state); setState(state);
} }
public void exec(String payCode, String payCodeType, ActionResult result) { public void exec(final String payCode, final String payCodeType, ActionResult result) {
if (getState() != State.INITED) { if (getState() != State.INITED) {
if (result != null) { if (result != null) {
result.onFail("正在忙"); result.onFail("正在忙");
...@@ -93,13 +98,14 @@ public class PayOnlineAction extends Action { ...@@ -93,13 +98,14 @@ public class PayOnlineAction extends Action {
public void run(long id, boolean isLastTime) { public void run(long id, boolean isLastTime) {
if (isLastTime) { if (isLastTime) {
setState(State.TIMEOUT, "交易超时"); setState(State.TIMEOUT, "交易超时");
PayOfflineAction.getDefault().exec(payCode, payCodeType);
} else { } else {
if (mActionRunnable != null) { if (mActionRunnable != null) {
mActionRunnable.run(); mActionRunnable.run();
} }
} }
} }
}, 10000, 4, 10000); }, 5000, 2, 5000);
final MainWebSocket.Response response = new MainWebSocket.Response() { final MainWebSocket.Response response = new MainWebSocket.Response() {
@Override @Override
......
...@@ -12,8 +12,11 @@ import android.view.animation.Animation ...@@ -12,8 +12,11 @@ import android.view.animation.Animation
import android.view.animation.RotateAnimation import android.view.animation.RotateAnimation
import com.bgycc.smartcanteen.App import com.bgycc.smartcanteen.App
import com.bgycc.smartcanteen.AppConfig import com.bgycc.smartcanteen.AppConfig
import com.bgycc.smartcanteen.BuildConfig
import com.bgycc.smartcanteen.R import com.bgycc.smartcanteen.R
import com.bgycc.smartcanteen.action.ActionEnum import com.bgycc.smartcanteen.action.ActionEnum
import com.bgycc.smartcanteen.action.ActionResult
import com.bgycc.smartcanteen.action.PayOnlineAction
import com.bgycc.smartcanteen.action.UpdateAction import com.bgycc.smartcanteen.action.UpdateAction
import com.bgycc.smartcanteen.event.* import com.bgycc.smartcanteen.event.*
import com.bgycc.smartcanteen.helper.EthernetHelper import com.bgycc.smartcanteen.helper.EthernetHelper
...@@ -87,11 +90,7 @@ class MainActivity : BaseActivity() { ...@@ -87,11 +90,7 @@ class MainActivity : BaseActivity() {
} }
private fun initView() { private fun initView() {
if (!AppConfig.DEBUG) { _debug.visibility = View.GONE
_debug.visibility = View.GONE
} else {
_logo.visibility = View.GONE
}
_setting.alpha = 0f _setting.alpha = 0f
_setting_img.post{ _setting_img.post{
val anim = RotateAnimation(0f, 360f, 0.5f * _setting_img.width, 0.5f * _setting_img.height) val anim = RotateAnimation(0f, 360f, 0.5f * _setting_img.width, 0.5f * _setting_img.height)
...@@ -103,6 +102,19 @@ class MainActivity : BaseActivity() { ...@@ -103,6 +102,19 @@ class MainActivity : BaseActivity() {
_version.text = "Version: v${App.getVersionName()}(${App.getVersionCode()})" _version.text = "Version: v${App.getVersionName()}(${App.getVersionCode()})"
_eth.text = "Ethernet Mac: ${EthernetHelper.getMacAddress()}" _eth.text = "Ethernet Mac: ${EthernetHelper.getMacAddress()}"
_sn.text = "SN: ${App.getDeviceSN()}" _sn.text = "SN: ${App.getDeviceSN()}"
if (BuildConfig.DEBUG) {
_message.setOnClickListener {
App.testQRCode = "380000000000000000"
}
_debug.setOnClickListener {
_debug.visibility = View.GONE
_logo.visibility = View.VISIBLE
}
_logo.setOnClickListener {
_debug.visibility = View.VISIBLE
_logo.visibility = View.GONE
}
}
} }
private fun initTimer() { private fun initTimer() {
......
...@@ -165,7 +165,6 @@ public class MainWebSocket extends WebSocketClient { ...@@ -165,7 +165,6 @@ public class MainWebSocket extends WebSocketClient {
public void onOpen(ServerHandshake handshakeData) { public void onOpen(ServerHandshake handshakeData) {
LogUtil.i(TAG, String.format("onOpen: %d %s", handshakeData.getHttpStatus(), handshakeData.getHttpStatusMessage())); LogUtil.i(TAG, String.format("onOpen: %d %s", handshakeData.getHttpStatus(), handshakeData.getHttpStatusMessage()));
EventBus.getDefault().post(new ConnectStateEvent(ConnectStateEvent.CONNECTED)); EventBus.getDefault().post(new ConnectStateEvent(ConnectStateEvent.CONNECTED));
PayOfflineAction.getDefault().upload();
} }
@Override @Override
......
package com.bgycc.smartcanteen.task; package com.bgycc.smartcanteen.task;
import android_serialport_api.SerialPort; import android_serialport_api.SerialPort;
import com.bgycc.smartcanteen.App;
import com.bgycc.smartcanteen.event.QRCodeEvent; import com.bgycc.smartcanteen.event.QRCodeEvent;
import com.bgycc.smartcanteen.action.*; import com.bgycc.smartcanteen.action.*;
import com.bgycc.smartcanteen.event.LogEvent; import com.bgycc.smartcanteen.event.LogEvent;
...@@ -36,6 +37,7 @@ public class QRCodeTask { ...@@ -36,6 +37,7 @@ public class QRCodeTask {
private ScanAsyncTask mScanAsyncTask; private ScanAsyncTask mScanAsyncTask;
private SerialPort mSerial; private SerialPort mSerial;
private long mTimerId = -1; private long mTimerId = -1;
private long mOfflineUpdateDelay = 0;
private QRCodeTask() {} private QRCodeTask() {}
...@@ -48,17 +50,23 @@ public class QRCodeTask { ...@@ -48,17 +50,23 @@ public class QRCodeTask {
if (mScanAsyncTask == null) { if (mScanAsyncTask == null) {
mSerial = SerialPort.build(new File("/dev/ttyS6"), 115200, 0); mSerial = SerialPort.build(new File("/dev/ttyS6"), 115200, 0);
if (mSerial != null) { mScanAsyncTask = new ScanAsyncTask();
mScanAsyncTask = new ScanAsyncTask(); mTimerId = 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) {
if (mScanAsyncTask == null) return;
mScanAsyncTask.run(); mScanAsyncTask.run();
} }
}, 200, -1); if (mOfflineUpdateDelay >= 30000) {
} if (MainWebSocket.isConnected()) {
PayOfflineAction.getDefault().upload();
mOfflineUpdateDelay = 0;
}
} else {
mOfflineUpdateDelay += 200;
}
}
}, 200, -1);
} }
} }
...@@ -85,30 +93,35 @@ public class QRCodeTask { ...@@ -85,30 +93,35 @@ public class QRCodeTask {
// Log.i(TAG, String.format("step: %d progress: %d", step, progress)); // Log.i(TAG, String.format("step: %d progress: %d", step, progress));
switch (step) { switch (step) {
case 0: case 0:
if (progress != 0) break;
LogUtils.file(TAG, "QRCode wait"); LogUtils.file(TAG, "QRCode wait");
String str = null; String str = null;
byte[] buf = null; if (mSerial != null) {
int len = 0; byte[] buf = null;
while (true) { int len = 0;
try { while (true) {
byte[] b = new byte[64]; try {
int l = mSerial.getInputStream().read(b); byte[] b = new byte[64];
if (l == 0) break; int l = mSerial.getInputStream().read(b);
b = Arrays.copyOfRange(b, 0, l); if (l == 0) break;
buf = ByteUtil.merge(buf, b); b = Arrays.copyOfRange(b, 0, l);
len += l; buf = ByteUtil.merge(buf, b);
if (buf.length >= 2 && buf[buf.length - 2] == 0x0D && buf[buf.length - 1] == 0x0A) { len += l;
str = new String(buf, 0, len - 2); 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; break;
} }
} catch (Exception e) {
break;
} }
} }
if (str == null) break; if (!App.Companion.getTestQRCode().isEmpty()) {
str = App.Companion.getTestQRCode();
App.Companion.setTestQRCode("");
}
if (progress != 0 || str == null) break;
mOfflineUpdateDelay = 0;
LogUtils.file(TAG, "QRCode: " + str); LogUtils.file(TAG, "QRCode: " + str);
QRCodeEvent event = new QRCodeEvent(str.getBytes(), str, checkPayCodeType(str)); QRCodeEvent event = new QRCodeEvent(str.getBytes(), str, checkPayCodeType(str));
EventBus.getDefault().post(event); EventBus.getDefault().post(event);
......
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