Commit a0a2109e by patpat

增加离线支付

parent 5407211d
package com.bgycc.smartcanteen.action;
import com.bgycc.smartcanteen.App;
import com.bgycc.smartcanteen.Storage.PayStorage;
import com.bgycc.smartcanteen.event.PayStateEvent;
import com.bgycc.smartcanteen.server.websocket.MainWebSocket;
import com.bgycc.smartcanteen.util.LogUtil;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONObject;
import java.text.SimpleDateFormat;
......@@ -12,64 +15,83 @@ import java.util.Locale;
public class PayOfflineAction extends Action {
private static final String TAG = PayOfflineAction.class.getSimpleName();
private static PayOfflineAction sInstance;
public static PayOfflineAction getDefault() {
if (sInstance == null) {
sInstance = new PayOfflineAction();
}
return sInstance;
}
public PayOfflineAction() {
super(ActionEnum.PAY_OFFLINE.name());
}
@Override
protected void setState(State state) {
if (state != getState()) {
switch (state) {
case RESQUEST_SUCCESS:
success("");
case INITED:
EventBus.getDefault().post(new PayStateEvent(PayStateEvent.StateEnum.IDLE, ""));
break;
case FAIL:
EventBus.getDefault().post(new PayStateEvent(PayStateEvent.StateEnum.FAIL, ""));
timeout(new Runnable() {
@Override
public void run() {
setState(State.INITED);
}
}, 3000);
break;
case RESQUEST_FAIL:
case RESQUEST_TIMEOUT:
fail("");
case RESQUEST_SUCCESS:
EventBus.getDefault().post(new PayStateEvent(PayStateEvent.StateEnum.SUCCESS, ""));
timeout(new Runnable() {
@Override
public void run() {
setState(State.INITED);
}
}, 3000);
break;
}
}
setState(state);
super.setState(state);
}
public void exec(String payCode, String payCodeType, ActionResult result) {
if (getState() != State.INITED) return;
public void exec(String payCode, String payCodeType) {
try {
setState(State.STARTED);
JSONObject params = new JSONObject();
params.put("equipmentNo", App.Companion.getDeviceSN());
params.put("payCode", payCode);
params.put("terminalType", payCodeType);
params.put("time", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault()).format(new Date()));
PayStorage.getInstance().add(params);
PayStorage.getInstance().savePayList();
setState(State.RESQUEST_SUCCESS);
LogUtil.i(TAG, params.toString());
} catch (Exception e) {
e.printStackTrace();
setState(State.FAIL);
}
}
setState(State.STARTED);
setActionResult(result);
public void upload() {
JSONArray list = PayStorage.getInstance().getPayList();
if (list == null || list.length() == 0) return;
final MainWebSocket.Response response = new MainWebSocket.Response() {
@Override
protected void onSuccess(JSONObject data, String message) {
cancel();
setState(State.RESQUEST_SUCCESS);
PayStorage.getInstance().clearPayList();
}
@Override
protected void onFail(JSONObject data, String message, String code) {
cancel();
setState(State.RESQUEST_FAIL);
}
};
timeout(new Runnable() {
@Override
public void run() {
if (response.isCancelled()) return;
response.cancel();
setState(State.RESQUEST_TIMEOUT);
}
}, 10000);
try {
JSONObject params = new JSONObject();
params.put("equipmentNo", App.Companion.getDeviceSN());
params.put("payCode", payCode);
params.put("terminalType", payCodeType);
params.put("time", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault()).format(new Date()));
setState(State.RESQUEST);
MainWebSocket.action(getAction(), params, response);
} catch (Exception e) {
e.printStackTrace();
}
MainWebSocket.action(getAction(), list, response);
LogUtil.i(TAG, list.toString());
}
}
......@@ -12,6 +12,14 @@ import java.util.Locale;
public class PayOnlineAction extends Action {
private static PayOnlineAction sInstance;
public static PayOnlineAction getDefault() {
if (sInstance == null) {
sInstance = new PayOnlineAction();
}
return sInstance;
}
private String mPayCode;
public PayOnlineAction() {
......
......@@ -3,7 +3,11 @@ package com.bgycc.smartcanteen.activity
import android.media.AudioManager
import android.media.SoundPool
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.TextView
import com.bgycc.smartcanteen.App
import com.bgycc.smartcanteen.AppConfig
import com.bgycc.smartcanteen.QRCodeEvent
import com.bgycc.smartcanteen.R
import com.bgycc.smartcanteen.event.PayStateEvent
......@@ -12,6 +16,7 @@ import com.bgycc.smartcanteen.server.websocket.MainWebSocket
import com.bgycc.smartcanteen.server.websocket.event.RecvMessageEvent
import com.bgycc.smartcanteen.server.websocket.event.SendMessageEvent
import com.bgycc.smartcanteen.task.QRCodeTask
import com.bgycc.smartcanteen.util.StringUtil
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
......@@ -91,9 +96,11 @@ class MainActivity : BaseActivity() {
mMessageTextView.text = "交易处理中"
} else if (event.state == PayStateEvent.StateEnum.SUCCESS) {
mMessageTextView.setTextColor(0xFF009900.toInt())
if (StringUtil.isEmpty(event.message)) event.message = "支付成功"
mMessageTextView.text = event.message
} else if (event.state == PayStateEvent.StateEnum.FAIL) {
mMessageTextView.setTextColor(0xFFFF0000.toInt())
if (StringUtil.isEmpty(event.message)) event.message = "支付失败"
mMessageTextView.text = event.message
}
}
......
......@@ -2,8 +2,11 @@ package com.bgycc.smartcanteen.server.websocket;
import android.util.Log;
import android.util.SparseArray;
import com.bgycc.smartcanteen.App;
import com.bgycc.smartcanteen.AppConfig;
import com.bgycc.smartcanteen.action.ActionEnum;
import com.bgycc.smartcanteen.action.Action;
import com.bgycc.smartcanteen.action.PayOfflineAction;
import com.bgycc.smartcanteen.server.websocket.event.ConnectStateEvent;
import com.bgycc.smartcanteen.server.websocket.event.RecvMessageEvent;
import com.bgycc.smartcanteen.server.websocket.event.SendMessageEvent;
......@@ -13,6 +16,7 @@ import org.greenrobot.eventbus.EventBus;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
......@@ -77,6 +81,10 @@ public class MainWebSocket extends WebSocketClient {
return sInstance != null;
}
public static boolean isConnected() {
return sInstance.isOpen();
}
public static void payOnline(String payCode, String payCodeType, Response callback) {
if (!isInited()) return;
......@@ -98,6 +106,12 @@ public class MainWebSocket extends WebSocketClient {
sInstance.send(action, params, callback);
}
public static void action(String action, JSONArray params, Response callback) {
if (!isInited()) return;
sInstance.send(action, params, callback);
}
public static boolean subscribe(String action, Response response) {
if (action == null || action.isEmpty()) return false;
......@@ -144,7 +158,7 @@ public class MainWebSocket extends WebSocketClient {
super(serverUri, new Draft_6455(), null, 10000);
}
private void send(String action, JSONObject data, Response callback) {
private void send(String action, Object data, Response callback) {
try {
if (mSessionId >= Integer.MAX_VALUE) mSessionId = 0;
mSessionId++;
......@@ -173,6 +187,7 @@ public class MainWebSocket extends WebSocketClient {
public void onOpen(ServerHandshake handshakedata) {
Log.i(TAG, String.format("onOpen: %d %s", handshakedata.getHttpStatus(), handshakedata.getHttpStatusMessage()));
EventBus.getDefault().post(new ConnectStateEvent(ConnectStateEvent.CONNECTED));
PayOfflineAction.getDefault().upload();
}
@Override
......
......@@ -2,15 +2,12 @@ package com.bgycc.smartcanteen.task;
import android.util.Log;
import com.bgycc.smartcanteen.QRCodeEvent;
import com.bgycc.smartcanteen.action.Action;
import com.bgycc.smartcanteen.action.ActionEnum;
import com.bgycc.smartcanteen.action.ActionResult;
import com.bgycc.smartcanteen.action.PayOfflineAction;
import com.bgycc.smartcanteen.action.PayOnlineAction;
import com.bgycc.smartcanteen.event.PayStateEvent;
import com.bgycc.smartcanteen.server.websocket.MainWebSocket;
import com.szxb.jni.libszxb;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;
import java.util.Timer;
import java.util.TimerTask;
......@@ -60,12 +57,10 @@ public class QRCodeTask {
int lastLen = -1;
byte[] lastBuf;
PayOnlineAction payOnlineAction = new PayOnlineAction();
AsyncTask asyncTask = mAsyncTask = new AsyncTask() {
@Override
protected void run(int step, int progress) {
Log.i("LoopTimerTask", String.format("step: %d progress: %d", step, progress));
switch (step) {
case 0:
if (progress != 0) break;
......@@ -84,17 +79,21 @@ public class QRCodeTask {
if (event.payCodeType == null) {
parseCommand(event);
} else {
payOnlineAction.exec(event.string, event.payCodeType, new ActionResult() {
@Override
public void onSuccess(String message) {
resetStep();
}
@Override
public void onFail(String message) {
resetStep();
}
});
nextProgress();
if (MainWebSocket.isConnected()) {
PayOnlineAction.getDefault().exec(event.string, event.payCodeType, new ActionResult() {
@Override
public void onSuccess(String message) {
resetStep();
}
@Override
public void onFail(String message) {
resetStep();
}
});
nextProgress();
} else {
PayOfflineAction.getDefault().exec(event.string, event.payCodeType);
}
}
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