Commit 949ca291 by patpat

拆分action处理

parent 93a43838
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeSettings">
<configurations>
<configuration PROFILE_NAME="Debug" CONFIG_NAME="Debug" />
</configurations>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" 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" />
</component>
<component name="ProjectType">
......
package com.bgycc.smartcanteen.action;
public enum ActionEnum {
// 支付
PAY_,
PAY_ONLINE,
PAY_OFFLINE,
PAY_RESULT,
// 配置
CONFIG_,
CONFIG_POWER,
CONFIG_WIFI,
CONFIG_SERVER,
CONFIG_PAYCODE_RULE
}
package com.bgycc.smartcanteen.action;
public interface ActionResult {
void onSuccess();
void onFail(String message);
}
package com.bgycc.smartcanteen.action;
import org.json.JSONObject;
public abstract class BaseAction {
private String mAction;
public BaseAction(String action) {
mAction = action;
}
boolean isAction(String action) {
if (mAction == null || mAction.isEmpty()) return false;
return mAction.equals(action);
}
void exec(String data, ActionResult result) {
try {
JSONObject json = new JSONObject(data);
exec(json, result);
} catch (Exception e) {
onFail("数据格式错误", result);
}
}
void exec(JSONObject data, ActionResult result) {
}
void onSuccess(ActionResult result) {
if (result != null) result.onSuccess();
}
void onFail(String message, ActionResult result) {
if (result != null) result.onFail(message);
}
}
package com.bgycc.smartcanteen.action;
import org.json.JSONObject;
public class ConfigAction extends BaseAction {
public ConfigAction(String action) {
super(action);
}
@Override
void exec(JSONObject data, ActionResult result) {
}
}
package com.bgycc.smartcanteen.action;
import org.json.JSONObject;
public class PayAction extends BaseAction {
public PayAction(String action) {
super(action);
}
@Override
public void exec(JSONObject data, ActionResult result) {
if (isAction(ActionEnum.PAY_RESULT.name())) {
if (data == null) {
onFail("数据格式错误", result);
return;
}
String payCode = data.optString("payCode", "");
if (payCode.isEmpty()) {
onFail("payCode不能为空", result);
return;
}
onSuccess(result);
} else {
onFail("action无效", result);
}
}
}
......@@ -9,6 +9,7 @@ import com.bgycc.smartcanteen.server.websocket.ConnectStateEvent
import com.bgycc.smartcanteen.server.websocket.MainWebSocket
import com.bgycc.smartcanteen.server.websocket.RecvMessageEvent
import com.bgycc.smartcanteen.server.websocket.SendMessageEvent
import com.bgycc.smartcanteen.task.QRCodeTask
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
......@@ -43,7 +44,7 @@ class MainActivity : BaseActivity() {
fun init() {
// WifiHelpler.connect("BGY-802.1X", "dengquanye", "pat2019@", "wpa_eap")
MainWebSocket.initialize()
QRCodeHelper.getInstance().start()
QRCodeTask.getInstance().start()
}
fun initView() {
......@@ -61,16 +62,9 @@ class MainActivity : BaseActivity() {
mQRCodeText.text = msg
}
fun parseCommand(event: QRCodeEvent) {
if (event.payCodeType != null) {
MainWebSocket.payOnline(event.string, event.payCodeType)
}
}
@Subscribe(threadMode = ThreadMode.MAIN)
fun onMessageEvent(event: QRCodeEvent) {
printQRCode(event)
parseCommand(event)
}
@Subscribe(threadMode = ThreadMode.MAIN)
......
package com.bgycc.smartcanteen.server.websocket;
import android.util.Log;
import android.util.SparseArray;
import com.bgycc.smartcanteen.action.ActionEnum;
import com.bgycc.smartcanteen.action.ActionResult;
import com.bgycc.smartcanteen.action.PayAction;
import com.example.zhoukai.modemtooltest.ModemToolTest;
import com.example.zhoukai.modemtooltest.NvConstants;
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.JSONException;
import org.json.JSONObject;
import java.net.URI;
......@@ -18,6 +23,22 @@ public class MainWebSocket extends WebSocketClient {
private static final String TAG = MainWebSocket.class.getSimpleName();
private enum FieldEnum {
action,
data,
code,
message,
serialNumber,
equipmentId,
equipmentNo,
payCode,
terminalType,
time
}
public static final String CODE_OK = "0";
public static final String CODE_FAIL = "-1";
private static MainWebSocket sInstance;
private static String sDeviceSN;
private static Timer sKeepAliveTimer;
......@@ -54,38 +75,61 @@ public class MainWebSocket extends WebSocketClient {
return sInstance != null;
}
public static void payOnline(String payCode, String payCodeType) {
public static void payOnline(String payCode, String payCodeType, Response callback) {
if (!isInited()) return;
try {
JSONObject json = new JSONObject();
json.put("equipmentNo", sDeviceSN);
json.put("payCode", payCode);
json.put("terminalType", payCodeType);
json.put("time", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault()).format(new Date()));
sInstance.send("PAY_ONLINE", json);
json.put(FieldEnum.equipmentNo.name(), sDeviceSN);
json.put(FieldEnum.payCode.name(), payCode);
json.put(FieldEnum.terminalType.name(), payCodeType);
json.put(FieldEnum.time.name(), new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault()).format(new Date()));
sInstance.send(ActionEnum.PAY_ONLINE.name(), json, callback);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void response(int sessionId, String code, String message) {
try {
JSONObject json = new JSONObject();
json.put(FieldEnum.equipmentId.name(), sDeviceSN);
json.put(FieldEnum.serialNumber.name(), sessionId);
json.put(FieldEnum.code.name(), code);
json.put(FieldEnum.message.name(), message);
String msg = json.toString();
Log.i(TAG, String.format("send: %s", msg));
EventBus.getDefault().post(new SendMessageEvent(msg));
sInstance.send(msg);
} catch (JSONException e) {
e.printStackTrace();
}
}
private int mSessionId = 0;
private final SparseArray<Resquest> mResquestList = new SparseArray<>();
private MainWebSocket(URI serverUri) {
super(serverUri, new Draft_6455(), null, 10000);
}
private void send(String action, JSONObject data) {
private void send(String action, JSONObject data, Response callback) {
try {
if (mSessionId >= Integer.MAX_VALUE) mSessionId = 0;
mSessionId++;
JSONObject json = new JSONObject();
json.put("equipmentId", sDeviceSN);
json.put("serialNumber", mSessionId);
json.put("action", action);
json.put("data", data);
String msg = json.toString();
Resquest resquest = new Resquest();
resquest.time = System.currentTimeMillis();
resquest.response = callback;
resquest.params = new JSONObject();
resquest.params.put(FieldEnum.equipmentId.name(), sDeviceSN);
resquest.params.put(FieldEnum.serialNumber.name(), mSessionId);
resquest.params.put(FieldEnum.action.name(), action);
resquest.params.put(FieldEnum.data.name(), data);
mResquestList.put(mSessionId, resquest);
String msg = resquest.params.toString();
Log.i(TAG, String.format("send: %s", msg));
EventBus.getDefault().post(new SendMessageEvent(msg));
super.send(msg);
......@@ -104,6 +148,35 @@ public class MainWebSocket extends WebSocketClient {
public void onMessage(String message) {
Log.i(TAG, String.format("onMessage: %s", message));
EventBus.getDefault().post(new RecvMessageEvent(message));
try {
JSONObject json = new JSONObject(message);
final int sessionId = json.optInt(FieldEnum.serialNumber.name(), -1);
String action = json.optString(FieldEnum.action.name(), "");
String code = json.optString(FieldEnum.code.name(), "");
if (code.isEmpty()) {
if (action.startsWith(ActionEnum.PAY_.name())) {
new PayAction(action).exec(json.optJSONObject(FieldEnum.data.name()), new ActionResult() {
@Override
public void onSuccess() {
response(sessionId, CODE_OK, "");
}
@Override
public void onFail(String message) {
response(sessionId, CODE_FAIL, message);
}
});
}
} else {
Resquest resquest = mResquestList.get(sessionId);
if (resquest != null) {
resquest.parseResponse(code, json);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
......@@ -118,8 +191,23 @@ public class MainWebSocket extends WebSocketClient {
ex.printStackTrace();
}
public static interface Response {
public static class Resquest {
long time = 0;
JSONObject params;
Response response;
void parseResponse(String code, JSONObject res) {
if (res == null) return;
if (CODE_OK.equals(code)) {
response.onSuccess(res.optJSONObject(FieldEnum.data.name()));
} else {
response.onFail(code, res.optString(FieldEnum.message.name()));
}
}
}
public interface Response {
void onSuccess(JSONObject data);
void onFail(String code, String message);
}
......
package com.bgycc.smartcanteen.task;
import com.bgycc.smartcanteen.QRCodeEvent;
import com.bgycc.smartcanteen.helper.QRCodeHelper;
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,7 +60,7 @@ public class QRCodeTask {
int len = libszxb.getBarcode(buf);
if (len <= 0) break;
delay(1000);
delay(500);
String str = new String(buf, 0, len);
QRCodeEvent event = new QRCodeEvent(str.getBytes(), str, checkPayCodeType(str));
QRCodeEvent le = lastEvent;
......@@ -72,7 +72,16 @@ public class QRCodeTask {
if (event.payCodeType == null) {
parseCommand(event);
} else {
MainWebSocket.payOnline(event.string, event.payCodeType);
MainWebSocket.payOnline(event.string, event.payCodeType, new MainWebSocket.Response() {
@Override
public void onSuccess(JSONObject data) {
}
@Override
public void onFail(String code, String message) {
}
});
nextStep();
}
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