Commit 1d396c11 by patpat

修改action架构(未完成)

parent a2b15449
...@@ -2,9 +2,17 @@ package com.bgycc.smartcanteen ...@@ -2,9 +2,17 @@ package com.bgycc.smartcanteen
import android.app.Application import android.app.Application
import com.bgycc.smartcanteen.helper.WifiHelpler import com.bgycc.smartcanteen.helper.WifiHelpler
import com.example.zhoukai.modemtooltest.ModemToolTest
import com.example.zhoukai.modemtooltest.NvConstants
class App : Application() { class App : Application() {
companion object {
fun getDeviceSN(): String {
return ModemToolTest.getItem(NvConstants.REQUEST_GET_SN);
}
}
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
WifiHelpler.initialize(this) WifiHelpler.initialize(this)
......
...@@ -26,6 +26,7 @@ public abstract class Action { ...@@ -26,6 +26,7 @@ public abstract class Action {
} }
private String mAction; private String mAction;
private State mState; private State mState;
private ActionResult mActionResult;
protected Action(String action) { protected Action(String action) {
mState = State.UNINIT; mState = State.UNINIT;
...@@ -43,12 +44,26 @@ public abstract class Action { ...@@ -43,12 +44,26 @@ public abstract class Action {
return mState; return mState;
} }
protected void setState(State state) {
mState = state;
}
protected void setActionResult(ActionResult result) {
mActionResult = result;
}
boolean isAction(String action) { boolean isAction(String action) {
if (mAction == null || mAction.isEmpty()) return false; if (mAction == null || mAction.isEmpty()) return false;
return mAction.equals(action); return mAction.equals(action);
} }
public void response(JSONObject response) { public void response(JSONObject response) {
if (response == null) {
fail("");
}
}
protected void response(int sessionId, JSONObject data) {
} }
...@@ -61,11 +76,11 @@ public abstract class Action { ...@@ -61,11 +76,11 @@ public abstract class Action {
}, ms); }, ms);
} }
protected void success(String message, ActionResult result) { protected void success(String message) {
if (result != null) result.onSuccess(message); if (mActionResult != null) mActionResult.onSuccess(message);
} }
protected void fail(String message, ActionResult result) { protected void fail(String message) {
if (result != null) result.onFail(message); if (mActionResult != null) mActionResult.onFail(message);
} }
} }
package com.bgycc.smartcanteen.action;
import org.json.JSONObject;
public class PayAction extends Action {
public PayAction(String action) {
super(action);
}
@Override
public void exec(JSONObject data, ActionResult result) {
if (isAction(ActionEnum.PAY_RESULT.name())) {
if (data == null) {
fail("数据格式错误", result);
return;
}
String payCode = data.optString("payCode", "");
if (payCode.isEmpty()) {
fail("payCode不能为空", result);
return;
}
success("", result);
} else {
fail("action无效", result);
}
}
}
package com.bgycc.smartcanteen.action; package com.bgycc.smartcanteen.action;
import com.bgycc.smartcanteen.App;
import com.bgycc.smartcanteen.event.PayStateEvent; import com.bgycc.smartcanteen.event.PayStateEvent;
import com.bgycc.smartcanteen.server.websocket.MainWebSocket; import com.bgycc.smartcanteen.server.websocket.MainWebSocket;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
...@@ -11,53 +12,89 @@ import java.util.Locale; ...@@ -11,53 +12,89 @@ import java.util.Locale;
public class PayOnlineAction extends Action { public class PayOnlineAction extends Action {
public PayOnlineAction(String action) { public PayOnlineAction() {
super(action); super(ActionEnum.PAY_ONLINE.name());
MainWebSocket.subscribe(ActionEnum.PAY_RESULT.name(), mPayResultResponse);
}
protected void setState(State state, String message) {
if (state != getState()) {
switch (state) {
case INITED:
EventBus.getDefault().post(new PayStateEvent(PayStateEvent.StateEnum.IDLE, message));
break;
case STARTED:
EventBus.getDefault().post(new PayStateEvent(PayStateEvent.StateEnum.WAIT, message));
break;
case RESPONSE_SUCCESS:
EventBus.getDefault().post(new PayStateEvent(PayStateEvent.StateEnum.SUCCESS, message));
timeout(new Runnable() {
@Override
public void run() {
setState(State.INITED, "");
}
}, 3000);
break;
case RESQUEST_FAIL:
case RESQUEST_TIMEOUT:
case RESPONSE_FAIL:
case RESPONSE_TIMEOUT:
EventBus.getDefault().post(new PayStateEvent(PayStateEvent.StateEnum.FAIL, message));
timeout(new Runnable() {
@Override
public void run() {
setState(State.INITED, "");
}
}, 3000);
break;
}
}
setState(state);
} }
public void exec(String payCode, String payCodeType, ActionResult result) { public void exec(String payCode, String payCodeType, ActionResult result) {
if (getState() != State.INITED) return; if (getState() != State.INITED) return;
setActionResult(result);
final MainWebSocket.Response response = new MainWebSocket.Response() { final MainWebSocket.Response response = new MainWebSocket.Response() {
@Override @Override
protected void onSuccess(JSONObject data, String message) { protected void onSuccess(JSONObject data, String message) {
EventBus.getDefault().post(new PayStateEvent(PayStateEvent.StateEnum.SUCCESS, message)); setState(State.RESQUEST_SUCCESS, message);
} }
@Override @Override
protected void onFail(String code, String message) { protected void onFail(String code, String message) {
EventBus.getDefault().post(new PayStateEvent(PayStateEvent.StateEnum.FAIL, message)); setState(State.RESPONSE_FAIL, message);
resetStep();
timeout(new Runnable() {
@Override
public void run() {
restart();
}
}, 3000);
} }
}; };
timeout(new Runnable() { timeout(new Runnable() {
@Override @Override
public void run() { public void run() {
response.fail(null, "交易失败");
response.cancel(); response.cancel();
setState(State.RESPONSE_FAIL, "交易失败");
} }
}, 10000); }, 10000);
MainWebSocket.payOnline(event.string, event.payCodeType, response);
try { try {
JSONObject json = new JSONObject(); JSONObject params = new JSONObject();
json.put("equipmentNo", sDeviceSN); params.put("equipmentNo", App.Companion.getDeviceSN());
json.put("payCode", payCode); params.put("payCode", payCode);
json.put("terminalType", payCodeType); params.put("terminalType", payCodeType);
json.put("time", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault()).format(new Date())); params.put("time", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault()).format(new Date()));
MainWebSocket.action(getAction(), json, response); MainWebSocket.action(getAction(), params, response);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@Override MainWebSocket.Response mPayResultResponse = new MainWebSocket.Response() {
public void response(JSONObject response) { @Override
protected void onSuccess(JSONObject data, String message) {
} super.onSuccess(data, message);
}
@Override
protected void onFail(String code, String message) {
super.onFail(code, message);
}
};
} }
package com.bgycc.smartcanteen.helper;
import com.bgycc.smartcanteen.QRCodeEvent;
import com.szxb.jni.libszxb;
import org.greenrobot.eventbus.EventBus;
import java.util.regex.Pattern;
public class QRCodeHelper {
public static String TYPE_ALIPAY = "ALIPAY";
public static String TYPE_WXPAY = "WXPAY";
public static String TYPE_BHPAY = "BHPAY";
private static QRCodeHelper sInstance;
public static synchronized QRCodeHelper getInstance() {
if (sInstance == null) {
sInstance = new QRCodeHelper();
}
return sInstance;
}
private LoopThread mLoopThread;
private QRCodeHelper() {}
public boolean support() {
return true;
}
public void start() {
if (!support()) return;
if (mLoopThread == null) {
mLoopThread = new LoopThread();
mLoopThread.start();
}
}
public void stop() {
if (mLoopThread != null) {
mLoopThread.exit = true;
}
}
private class LoopThread extends Thread {
boolean exit = false;
@Override
public void run() {
while(true) {
byte[] buf = new byte[1024];
int len = libszxb.getBarcode(buf);
if (len > 0) {
String str = new String(buf, 0, len);
EventBus.getDefault().post(new QRCodeEvent(str.getBytes(), str, checkPayCodeType(str)));
}
if (this.exit) {
mLoopThread = null;
break;
}
}
}
}
private String checkPayCodeType(String code) {
if (Pattern.matches("^(10|11|12|13|14|15)\\d{16}$", code)) return TYPE_WXPAY;
if (Pattern.matches("^(25|26|27|28|29|30)\\d{14,22}$", code)) return TYPE_ALIPAY;
if (Pattern.matches("^(38|39)\\d{16}$", code)) return TYPE_BHPAY;
return null;
}
}
...@@ -3,9 +3,7 @@ package com.bgycc.smartcanteen.server.websocket; ...@@ -3,9 +3,7 @@ package com.bgycc.smartcanteen.server.websocket;
import android.util.Log; import android.util.Log;
import android.util.SparseArray; import android.util.SparseArray;
import com.bgycc.smartcanteen.action.ActionEnum; import com.bgycc.smartcanteen.action.ActionEnum;
import com.bgycc.smartcanteen.action.ActionResult;
import com.bgycc.smartcanteen.action.Action; import com.bgycc.smartcanteen.action.Action;
import com.bgycc.smartcanteen.action.PayAction;
import com.bgycc.smartcanteen.server.websocket.event.ConnectStateEvent; import com.bgycc.smartcanteen.server.websocket.event.ConnectStateEvent;
import com.bgycc.smartcanteen.server.websocket.event.RecvMessageEvent; import com.bgycc.smartcanteen.server.websocket.event.RecvMessageEvent;
import com.bgycc.smartcanteen.server.websocket.event.SendMessageEvent; import com.bgycc.smartcanteen.server.websocket.event.SendMessageEvent;
...@@ -100,31 +98,25 @@ public class MainWebSocket extends WebSocketClient { ...@@ -100,31 +98,25 @@ public class MainWebSocket extends WebSocketClient {
sInstance.send(action, params, callback); sInstance.send(action, params, callback);
} }
public static boolean subscribe(Action action) { public static boolean subscribe(String action, Response response) {
if (action == null) return false; if (action == null || action.isEmpty()) return false;
String a = action.getAction(); ArrayList<Response> list = sInstance.mSubscribeList.get(action);
if (a == null || a.isEmpty()) return false;
ArrayList<Action> list = sInstance.mSubscribeList.get(a);
if (list == null) { if (list == null) {
list = new ArrayList<Action>(); list = new ArrayList<Response>();
sInstance.mSubscribeList.put(a, list); sInstance.mSubscribeList.put(action, list);
} }
list.add(action); list.add(response);
return true; return true;
} }
public static boolean unsubscribe(Action action) { public static boolean unsubscribe(String action, Response response) {
if (action == null) return false; if (action == null || action.isEmpty()) return false;
String a = action.getAction();
if (a == null || a.isEmpty()) return false;
ArrayList<Action> list = sInstance.mSubscribeList.get(a); ArrayList<Response> list = sInstance.mSubscribeList.get(action);
if (list == null) return false; if (list == null) return false;
return list.remove(action); return list.remove(response);
} }
private static void response(int sessionId, String code, String message) { private static void response(int sessionId, String code, String message) {
...@@ -146,7 +138,7 @@ public class MainWebSocket extends WebSocketClient { ...@@ -146,7 +138,7 @@ public class MainWebSocket extends WebSocketClient {
private int mSessionId = 0; private int mSessionId = 0;
private final SparseArray<Resquest> mResquestList = new SparseArray<>(); private final SparseArray<Resquest> mResquestList = new SparseArray<>();
private final HashMap<String, ArrayList<Action>> mSubscribeList = new HashMap<>(); private final HashMap<String, ArrayList<Response>> mSubscribeList = new HashMap<>();
private MainWebSocket(URI serverUri) { private MainWebSocket(URI serverUri) {
super(serverUri, new Draft_6455(), null, 10000); super(serverUri, new Draft_6455(), null, 10000);
...@@ -158,6 +150,7 @@ public class MainWebSocket extends WebSocketClient { ...@@ -158,6 +150,7 @@ public class MainWebSocket extends WebSocketClient {
mSessionId++; mSessionId++;
Resquest resquest = new Resquest(); Resquest resquest = new Resquest();
resquest.action = action;
resquest.time = System.currentTimeMillis(); resquest.time = System.currentTimeMillis();
resquest.response = callback; resquest.response = callback;
resquest.params = new JSONObject(); resquest.params = new JSONObject();
...@@ -193,23 +186,15 @@ public class MainWebSocket extends WebSocketClient { ...@@ -193,23 +186,15 @@ public class MainWebSocket extends WebSocketClient {
String action = json.optString(FieldEnum.action.name(), ""); String action = json.optString(FieldEnum.action.name(), "");
String code = json.optString(FieldEnum.code.name(), ""); String code = json.optString(FieldEnum.code.name(), "");
if (code.isEmpty()) { ArrayList<Response> list = mSubscribeList.get(action);
if (action.startsWith(ActionEnum.PAY_.name())) { if (list != null && list.size() > 0) {
new PayAction(action).exec(json.optJSONObject(FieldEnum.data.name()), new ActionResult() { for (Response res : list) {
@Override if (res != null) res.parse(code, json);
public void onSuccess(String message) {
response(sessionId, CODE_OK, message);
}
@Override
public void onFail(String message) {
response(sessionId, CODE_FAIL, message);
}
});
} }
} else { } else {
Resquest resquest = mResquestList.get(sessionId); Resquest resquest = mResquestList.get(sessionId);
if (resquest != null) { if (resquest != null && resquest.response != null) {
resquest.parseResponse(code, json); resquest.response.parse(code, json);
} }
} }
} catch (JSONException e) { } catch (JSONException e) {
...@@ -230,13 +215,15 @@ public class MainWebSocket extends WebSocketClient { ...@@ -230,13 +215,15 @@ public class MainWebSocket extends WebSocketClient {
} }
public static class Resquest { public static class Resquest {
String action;
long time = 0; long time = 0;
JSONObject params; JSONObject params;
Response response; Response response;
void parseResponse(String code, JSONObject res) { void parseResponse(String code, JSONObject res) {
if (res == null) return; if (res == null || response == null) return;
response.parse(code, res);
String message = res.optString(FieldEnum.message.name()); String message = res.optString(FieldEnum.message.name());
if (CODE_OK.equals(code)) { if (CODE_OK.equals(code)) {
response.onSuccess(res.optJSONObject(FieldEnum.data.name()), message); response.onSuccess(res.optJSONObject(FieldEnum.data.name()), message);
...@@ -250,6 +237,17 @@ public class MainWebSocket extends WebSocketClient { ...@@ -250,6 +237,17 @@ public class MainWebSocket extends WebSocketClient {
boolean cancel = false; boolean cancel = false;
void parse(String code, JSONObject res) {
if (res == null) return;
String message = res.optString(FieldEnum.message.name());
if (CODE_OK.equals(code)) {
success(res.optJSONObject(FieldEnum.data.name()), message);
} else {
fail(code, message);
}
}
public void cancel() { public void cancel() {
this.cancel = true; this.cancel = true;
} }
......
...@@ -5,6 +5,7 @@ import com.bgycc.smartcanteen.QRCodeEvent; ...@@ -5,6 +5,7 @@ import com.bgycc.smartcanteen.QRCodeEvent;
import com.bgycc.smartcanteen.action.Action; import com.bgycc.smartcanteen.action.Action;
import com.bgycc.smartcanteen.action.ActionEnum; import com.bgycc.smartcanteen.action.ActionEnum;
import com.bgycc.smartcanteen.action.ActionResult; import com.bgycc.smartcanteen.action.ActionResult;
import com.bgycc.smartcanteen.action.PayOnlineAction;
import com.bgycc.smartcanteen.event.PayStateEvent; import com.bgycc.smartcanteen.event.PayStateEvent;
import com.bgycc.smartcanteen.server.websocket.MainWebSocket; import com.bgycc.smartcanteen.server.websocket.MainWebSocket;
import com.szxb.jni.libszxb; import com.szxb.jni.libszxb;
...@@ -59,37 +60,14 @@ public class QRCodeTask { ...@@ -59,37 +60,14 @@ public class QRCodeTask {
int lastLen = -1; int lastLen = -1;
byte[] lastBuf; byte[] lastBuf;
PayOnlineAction payOnlineAction = new PayOnlineAction();
AsyncTask asyncTask = mAsyncTask = new AsyncTask() { AsyncTask asyncTask = mAsyncTask = new AsyncTask() {
@Override @Override
protected void init() {
MainWebSocket.subscribe(new Action(ActionEnum.PAY_RESULT.name()) {
@Override
protected void exec(JSONObject data, ActionResult result) {
if (data == null) {
fail("数据格式错误", result);
return;
}
String payCode = data.optString("payCode", "");
if (payCode.isEmpty()) {
fail("payCode不能为空", result);
return;
}
success("", result);
}
});
}
@Override
protected void run(int step, int progress) { protected void run(int step, int progress) {
Log.i("LoopTimerTask", String.format("step: %d progress: %d", step, progress)); Log.i("LoopTimerTask", String.format("step: %d progress: %d", step, progress));
switch (step) { switch (step) {
case 0: case 0:
EventBus.getDefault().post(new PayStateEvent(PayStateEvent.StateEnum.IDLE));
nextStep();
break;
case 1:
if (progress != 0) break; if (progress != 0) break;
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
...@@ -106,32 +84,16 @@ public class QRCodeTask { ...@@ -106,32 +84,16 @@ public class QRCodeTask {
if (event.payCodeType == null) { if (event.payCodeType == null) {
parseCommand(event); parseCommand(event);
} else { } else {
final MainWebSocket.Response response = new MainWebSocket.Response() { payOnlineAction.exec(event.string, event.payCodeType, new ActionResult() {
@Override @Override
protected void onSuccess(JSONObject data, String message) { public void onSuccess(String message) {
EventBus.getDefault().post(new PayStateEvent(PayStateEvent.StateEnum.SUCCESS, message));
}
@Override
protected void onFail(String code, String message) {
EventBus.getDefault().post(new PayStateEvent(PayStateEvent.StateEnum.FAIL, message));
resetStep(); resetStep();
timeout(new Runnable() {
@Override
public void run() {
restart();
}
}, 3000);
} }
};
timeout(new Runnable() {
@Override @Override
public void run() { public void onFail(String message) {
response.fail(null, "交易失败"); resetStep();
response.cancel();
} }
}, 10000); });
MainWebSocket.payOnline(event.string, event.payCodeType, response);
EventBus.getDefault().post(new PayStateEvent(PayStateEvent.StateEnum.WAIT));
nextProgress(); nextProgress();
} }
break; 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