Commit 9239cb6e by patpat

完善action处理

parent f13aa4d3
......@@ -12,6 +12,8 @@ import java.util.Locale;
public class PayOnlineAction extends Action {
private String mPayCode;
public PayOnlineAction() {
super(ActionEnum.PAY_ONLINE.name());
MainWebSocket.subscribe(ActionEnum.PAY_RESULT.name(), mPayResultResponse);
......@@ -28,6 +30,7 @@ public class PayOnlineAction extends Action {
break;
case RESPONSE_SUCCESS:
EventBus.getDefault().post(new PayStateEvent(PayStateEvent.StateEnum.SUCCESS, message));
success(message);
timeout(new Runnable() {
@Override
public void run() {
......@@ -40,6 +43,7 @@ public class PayOnlineAction extends Action {
case RESPONSE_FAIL:
case RESPONSE_TIMEOUT:
EventBus.getDefault().post(new PayStateEvent(PayStateEvent.StateEnum.FAIL, message));
fail(message);
timeout(new Runnable() {
@Override
public void run() {
......@@ -55,7 +59,9 @@ public class PayOnlineAction extends Action {
public void exec(String payCode, String payCodeType, ActionResult result) {
if (getState() != State.INITED) return;
setState(State.STARTED, "");
setActionResult(result);
mPayCode = payCode;
final MainWebSocket.Response response = new MainWebSocket.Response() {
@Override
......@@ -63,8 +69,8 @@ public class PayOnlineAction extends Action {
setState(State.RESQUEST_SUCCESS, message);
}
@Override
protected void onFail(String code, String message) {
setState(State.RESPONSE_FAIL, message);
protected void onFail(JSONObject data, String message, String code) {
setState(State.RESQUEST_FAIL, message);
}
};
timeout(new Runnable() {
......@@ -81,6 +87,7 @@ public class PayOnlineAction extends Action {
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();
......@@ -90,11 +97,19 @@ public class PayOnlineAction extends Action {
MainWebSocket.Response mPayResultResponse = new MainWebSocket.Response() {
@Override
protected void onSuccess(JSONObject data, String message) {
super.onSuccess(data, message);
if (data == null) return;
String payCode = data.optString("payCode", "");
if (payCode.equals(mPayCode)) {
setState(State.RESPONSE_SUCCESS, message);
}
}
@Override
protected void onFail(String code, String message) {
super.onFail(code, message);
protected void onFail(JSONObject data, String message, String code) {
if (data == null) return;
String payCode = data.optString("payCode", "");
if (payCode.equals(mPayCode)) {
setState(State.RESPONSE_FAIL, message);
}
}
};
}
......@@ -52,7 +52,7 @@ public class MainWebSocket extends WebSocketClient {
if (sDeviceSN == null || sDeviceSN.isEmpty()) return;
EventBus.getDefault().post(new ConnectStateEvent(ConnectStateEvent.CONNECTING));
sInstance = new MainWebSocket(new URI("ws://10.181.0.56:5000/websocket/" + sDeviceSN));
sInstance = new MainWebSocket(new URI("ws://10.187.18.204:9001/websocket/" + sDeviceSN));
sInstance.connect();
} catch (URISyntaxException e) {
e.printStackTrace();
......@@ -225,10 +225,11 @@ public class MainWebSocket extends WebSocketClient {
response.parse(code, res);
String message = res.optString(FieldEnum.message.name());
JSONObject data = res.optJSONObject(FieldEnum.data.name());
if (CODE_OK.equals(code)) {
response.onSuccess(res.optJSONObject(FieldEnum.data.name()), message);
response.onSuccess(data, message);
} else {
response.onFail(code, message);
response.onFail(data, message, code);
}
}
}
......@@ -241,10 +242,11 @@ public class MainWebSocket extends WebSocketClient {
if (res == null) return;
String message = res.optString(FieldEnum.message.name());
JSONObject data = res.optJSONObject(FieldEnum.data.name());
if (CODE_OK.equals(code)) {
success(res.optJSONObject(FieldEnum.data.name()), message);
success(data, message);
} else {
fail(code, message);
fail(data, message, code);
}
}
......@@ -257,12 +259,12 @@ public class MainWebSocket extends WebSocketClient {
onSuccess(data, message);
}
public void fail(String code, String message){
public void fail(JSONObject data, String message, String code){
if (this.cancel) return;
onFail(code, message);
onFail(data, message, code);
}
protected void onSuccess(JSONObject data, String message) {}
protected void onFail(String code, String message) {}
protected void onFail(JSONObject data, String message, String code) {}
}
}
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