Commit 84e90b01 by patpat

优化

parent d2bceb1b
...@@ -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 1 versionCode 2
versionName "1.0.0" versionName "1.0.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
} }
buildTypes { buildTypes {
...@@ -25,6 +25,12 @@ android { ...@@ -25,6 +25,12 @@ android {
jniLibs.srcDirs = ['libs'] jniLibs.srcDirs = ['libs']
} }
} }
lintOptions {
checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false
}
} }
dependencies { dependencies {
......
...@@ -106,12 +106,12 @@ public class PayOfflineAction extends Action { ...@@ -106,12 +106,12 @@ public class PayOfflineAction extends Action {
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(long sessionId, JSONObject data, String message) {
cancel(); cancel();
PayStorage.getInstance().clearPayList(); PayStorage.getInstance().clearPayList();
} }
@Override @Override
protected void onFail(JSONObject data, String message, String code) { protected void onFail(long sessionId, JSONObject data, String message, String code) {
cancel(); cancel();
} }
}; };
......
...@@ -103,11 +103,11 @@ public class PayOnlineAction extends Action { ...@@ -103,11 +103,11 @@ public class PayOnlineAction extends Action {
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(long sessionId, JSONObject data, String message) {
setState(State.RESQUEST_SUCCESS, message); setState(State.RESQUEST_SUCCESS, message);
} }
@Override @Override
protected void onFail(JSONObject data, String message, String code) { protected void onFail(long sessionId, JSONObject data, String message, String code) {
setState(State.RESQUEST_FAIL, message); setState(State.RESQUEST_FAIL, message);
} }
}; };
...@@ -133,21 +133,21 @@ public class PayOnlineAction extends Action { ...@@ -133,21 +133,21 @@ public class PayOnlineAction extends Action {
MainWebSocket.Response mPayResultResponse = new MainWebSocket.Response() { MainWebSocket.Response mPayResultResponse = new MainWebSocket.Response() {
@Override @Override
protected void onSuccess(JSONObject data, String message) { protected void onSuccess(long sessionId, JSONObject data, String message) {
if (data == null) return; if (data == null) return;
String payCode = data.optString("payCode", ""); String payCode = data.optString("payCode", "");
if (payCode.equals(mPayCode)) { if (payCode.equals(mPayCode)) {
setState(State.RESPONSE_SUCCESS, message); setState(State.RESPONSE_SUCCESS, message);
MainWebSocket.response(data.optInt("serialNumber", -1), "0", "已接收"); MainWebSocket.response(sessionId, "0", "已接收");
} }
} }
@Override @Override
protected void onFail(JSONObject data, String message, String code) { protected void onFail(long sessionId, JSONObject data, String message, String code) {
if (data == null) return; if (data == null) return;
String payCode = data.optString("payCode", ""); String payCode = data.optString("payCode", "");
if (payCode.equals(mPayCode)) { if (payCode.equals(mPayCode)) {
setState(State.RESPONSE_FAIL, message); setState(State.RESPONSE_FAIL, message);
MainWebSocket.response(data.optInt("serialNumber", -1), "0", "已接收"); MainWebSocket.response(sessionId, "0", "已接收");
} }
} }
}; };
......
package com.bgycc.smartcanteen.server.websocket; package com.bgycc.smartcanteen.server.websocket;
import android.util.LongSparseArray;
import android.util.SparseArray; import android.util.SparseArray;
import com.bgycc.smartcanteen.App; import com.bgycc.smartcanteen.App;
import com.bgycc.smartcanteen.AppConfig; import com.bgycc.smartcanteen.AppConfig;
...@@ -110,10 +111,10 @@ public class MainWebSocket extends WebSocketClient { ...@@ -110,10 +111,10 @@ public class MainWebSocket extends WebSocketClient {
return list.remove(response); return list.remove(response);
} }
public static void response(int sessionId, String code, String message) { public static void response(long sessionId, String code, String message) {
try { try {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put(FieldEnum.equipmentId.name(), sDeviceSN); // json.put(FieldEnum.equipmentId.name(), sDeviceSN);
json.put(FieldEnum.serialNumber.name(), sessionId); json.put(FieldEnum.serialNumber.name(), sessionId);
json.put(FieldEnum.code.name(), code); json.put(FieldEnum.code.name(), code);
json.put(FieldEnum.message.name(), message); json.put(FieldEnum.message.name(), message);
...@@ -127,8 +128,8 @@ public class MainWebSocket extends WebSocketClient { ...@@ -127,8 +128,8 @@ public class MainWebSocket extends WebSocketClient {
} }
} }
private int mSessionId = 0; private long mSessionId = 0;
private final SparseArray<Request> mRequestList = new SparseArray<>(); private final LongSparseArray<Request> mRequestList = new LongSparseArray<>();
private final HashMap<String, ArrayList<Response>> mSubscribeList = new HashMap<>(); private final HashMap<String, ArrayList<Response>> mSubscribeList = new HashMap<>();
private MainWebSocket(URI serverUri) { private MainWebSocket(URI serverUri) {
...@@ -137,12 +138,11 @@ public class MainWebSocket extends WebSocketClient { ...@@ -137,12 +138,11 @@ public class MainWebSocket extends WebSocketClient {
private void send(String action, Object data, Response callback) { private void send(String action, Object data, Response callback) {
try { try {
if (mSessionId >= Integer.MAX_VALUE) mSessionId = 0; if (mSessionId >= Long.MAX_VALUE) mSessionId = 0;
mSessionId++; mSessionId++;
Request request = new Request(); Request request = new Request();
request.action = action; request.action = action;
request.time = System.currentTimeMillis();
request.response = callback; request.response = callback;
request.params = new JSONObject(); request.params = new JSONObject();
request.params.put(FieldEnum.equipmentId.name(), sDeviceSN); request.params.put(FieldEnum.equipmentId.name(), sDeviceSN);
...@@ -174,7 +174,7 @@ public class MainWebSocket extends WebSocketClient { ...@@ -174,7 +174,7 @@ public class MainWebSocket extends WebSocketClient {
try { try {
JSONObject json = new JSONObject(message); JSONObject json = new JSONObject(message);
final int sessionId = json.optInt(FieldEnum.serialNumber.name(), -1); final long sessionId = json.optLong(FieldEnum.serialNumber.name(), -1);
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(), "");
...@@ -183,6 +183,7 @@ public class MainWebSocket extends WebSocketClient { ...@@ -183,6 +183,7 @@ public class MainWebSocket extends WebSocketClient {
JSONObject data = new JSONObject(); JSONObject data = new JSONObject();
data.put(FieldEnum.equipmentId.name(), json.getString(FieldEnum.data.name())); data.put(FieldEnum.equipmentId.name(), json.getString(FieldEnum.data.name()));
JSONObject j = new JSONObject(); JSONObject j = new JSONObject();
j.put(FieldEnum.action.name(), ActionEnum.CHECK_DEVICE.name());
j.put(FieldEnum.data.name(), data); j.put(FieldEnum.data.name(), data);
j.put(FieldEnum.serialNumber.name(), sessionId); j.put(FieldEnum.serialNumber.name(), sessionId);
LogUtil.i(TAG, String.format("send: %s", j.toString())); LogUtil.i(TAG, String.format("send: %s", j.toString()));
...@@ -221,22 +222,8 @@ public class MainWebSocket extends WebSocketClient { ...@@ -221,22 +222,8 @@ public class MainWebSocket extends WebSocketClient {
public static class Request { public static class Request {
String action; String action;
long time = 0;
JSONObject params; JSONObject params;
Response response; Response response;
void parseResponse(String code, JSONObject res) {
if (res == null || response == null) return;
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(data, message);
} else {
response.onFail(data, message, code);
}
}
} }
public static abstract class Response { public static abstract class Response {
...@@ -246,12 +233,13 @@ public class MainWebSocket extends WebSocketClient { ...@@ -246,12 +233,13 @@ public class MainWebSocket extends WebSocketClient {
void parse(String code, JSONObject res) { void parse(String code, JSONObject res) {
if (res == null) return; if (res == null) return;
long sessionId = res.optLong(FieldEnum.serialNumber.name(), -1);
String message = res.optString(FieldEnum.message.name()); String message = res.optString(FieldEnum.message.name());
JSONObject data = res.optJSONObject(FieldEnum.data.name()); JSONObject data = res.optJSONObject(FieldEnum.data.name());
if (CODE_OK.equals(code)) { if (CODE_OK.equals(code)) {
success(data, message); success(sessionId, data, message);
} else { } else {
fail(data, message, code); fail(sessionId, data, message, code);
} }
} }
...@@ -262,17 +250,17 @@ public class MainWebSocket extends WebSocketClient { ...@@ -262,17 +250,17 @@ public class MainWebSocket extends WebSocketClient {
this.cancel = true; this.cancel = true;
} }
public void success(JSONObject data, String message) { public void success(long sessionId, JSONObject data, String message) {
if (this.cancel) return; if (this.cancel) return;
onSuccess(data, message); onSuccess(sessionId, data, message);
} }
public void fail(JSONObject data, String message, String code) { public void fail(long sessionId, JSONObject data, String message, String code) {
if (this.cancel) return; if (this.cancel) return;
onFail(data, message, code); onFail(sessionId, data, message, code);
} }
protected void onSuccess(JSONObject data, String message) {} protected void onSuccess(long sessionId, JSONObject data, String message) {}
protected void onFail(JSONObject data, String message, String code) {} protected void onFail(long sessionId, JSONObject data, String message, String code) {}
} }
} }
...@@ -7,7 +7,6 @@ import com.bgycc.smartcanteen.event.LogEvent; ...@@ -7,7 +7,6 @@ import com.bgycc.smartcanteen.event.LogEvent;
import com.bgycc.smartcanteen.helper.TimerHelper; import com.bgycc.smartcanteen.helper.TimerHelper;
import com.bgycc.smartcanteen.server.websocket.MainWebSocket; import com.bgycc.smartcanteen.server.websocket.MainWebSocket;
import com.bgycc.smartcanteen.util.ByteUtil; import com.bgycc.smartcanteen.util.ByteUtil;
import com.bgycc.smartcanteen.util.LogUtil;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject; import org.json.JSONObject;
......
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