Commit bf2438fc by pye52

1、优化设备指令的识别(增加白名单限制,不在白名单的下发通知不再认为是指令)

2、增加离线支付完成的日志输出
3、延长离线支付的超时到30秒(给服务器预留更多的处理时间)
parent 73234f6d
package com.bgycc.smartcanteen.entity;
import androidx.annotation.StringDef;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
......@@ -9,14 +8,6 @@ import java.util.Objects;
@Entity(tableName = Command.TABLE_NAME)
public class Command {
public static final String TABLE_NAME = "command";
public static final String LOG_UPLOAD = "LOG_PULL";
public static final String APP_UPDATE = "CONFIG_UPDATE";
public static final String CONFIG_WIFI = "CONFIG_WIFI";
public static final String CONFIG_LOG = "CONFIG_LOG";
@StringDef(value = {LOG_UPLOAD, APP_UPDATE, CONFIG_WIFI, CONFIG_LOG})
public @interface COMMAND_ACTION {
}
@PrimaryKey(autoGenerate = true)
private long id;
......@@ -28,7 +19,7 @@ public class Command {
}
public Command(String command, @COMMAND_ACTION String action) {
public Command(String command, String action) {
this.data = command;
this.action = action;
this.finish = false;
......
package com.bgycc.smartcanteen.viewModel;
import android.text.TextUtils;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
......@@ -24,18 +22,33 @@ import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static com.bgycc.smartcanteen.utils.SmartCanteenUtils.TAG;
/**
* 监听本地扫码、服务器下发的设置指令 <br/>
* 设备指令需要匹配以下规则: <br/>
* 1、action非"PAY_RESULT" <br/><br/>
* 监听Command数据库的变动,按如下流程进行处理: <br/>
* 从数据库读取 -> 解析执行 -> 更新到数据库
*/
public class CommandViewModel extends ViewModel implements CommandProgressCallback {
private static final String LOG_UPLOAD = "LOG_PULL";
private static final String APP_UPDATE = "CONFIG_UPDATE";
private static final String CONFIG_WIFI = "CONFIG_WIFI";
private static final String CONFIG_LOG = "CONFIG_LOG";
// 设备指令白名单
private static final Set<String> COMMAND_WHITELIST = new HashSet<String>()
{
{
add(LOG_UPLOAD);
add(APP_UPDATE);
add(CONFIG_WIFI);
add(CONFIG_LOG);
}
};
private CommandRepository commandRepository;
private Gson gson;
private String deviceSN;
......@@ -104,9 +117,7 @@ public class CommandViewModel extends ViewModel implements CommandProgressCallba
@Override
public void onMessage(String action, JsonObject obj, String original) {
// 设备指令需要匹配以下规则:
// 1、action非"PAY_RESULT"
if (TextUtils.isEmpty(action) || action.equals(RESPONSE_PAY_RESULT)) return;
if (!COMMAND_WHITELIST.contains(action)) return;
LogUtils.d(TAG, "设备下发指令: " + original);
ResponseRunnable runnable = new ResponseRunnable(original, action);
SCTaskExecutor.getInstance().executeOnDiskIO(runnable);
......@@ -126,16 +137,16 @@ public class CommandViewModel extends ViewModel implements CommandProgressCallba
LogUtils.d(TAG, "开始执行指令: " + command.toString());
try {
switch (command.getAction()) {
case Command.LOG_UPLOAD:
case LOG_UPLOAD:
handler = LogCommandHandler.getInstance(command, gson, deviceSN,CommandViewModel.this);
break;
case Command.APP_UPDATE:
case APP_UPDATE:
handler = UpdateCommandHandler.getInstance(command, gson, CommandViewModel.this);
break;
case Command.CONFIG_WIFI:
case CONFIG_WIFI:
handler = WifiConfigCommandHandler.getInstance(command, gson, CommandViewModel.this);
break;
case Command.CONFIG_LOG:
case CONFIG_LOG:
// CONFIG_LOG后直接return
commandState.postValue(new CommandState(CommandState.TOGGLE_DEBUG));
commandFinishAndUpdateDB();
......
......@@ -36,7 +36,8 @@ import static com.bgycc.smartcanteen.utils.SmartCanteenUtils.TAG;
* 后台会在离线订单写入到数据库时下发"写入成功"通知,此时更新订单状态为"支付成功" <br/>
*/
public class PayOfflineViewModel extends ViewModel {
private static final long TIMEOUT = 10 * 1000;
// 离线支付的超时时长需要被在线支付更长(因为发送的订单量更多)
private static final long TIMEOUT = 30 * 1000;
// 在线支付延迟150ms执行,留出时间给扫码反馈
private static final long REQUEST_DELAY = 150;
private static final long DEFAULT_DELAY = 3 * 1000;
......@@ -102,7 +103,6 @@ public class PayOfflineViewModel extends ViewModel {
if (action.equals(RESPONSE_PAY_OFFLINE_RESULT)) {
// 离线订单已写入到服务器数据库,可以标记为支付成功
LogUtils.d(TAG, "离线支付结果响应: " + original);
cancelTimeout();
ResponseRunnable runnable = new ResponseRunnable(original);
SCTaskExecutor.getInstance().executeOnDiskIO(runnable);
return;
......@@ -160,6 +160,7 @@ public class PayOfflineViewModel extends ViewModel {
if (payOfflineData == null || payOfflineData.isEmpty()) {
cancelTimeout();
payRequest = null;
LogUtils.d(TAG, "所有离线订单处理完毕");
return;
}
payRequest = new PayRequest(deviceSN, payOfflineData);
......@@ -201,6 +202,7 @@ public class PayOfflineViewModel extends ViewModel {
@Override
public void run() {
cancelTimeout();
if (payRequest == null || payRequest.getData().isEmpty()) {
LogUtils.w(TAG, "后台返回离线支付结果,但没有待处理任务");
payOfflineState.postValue(new PayOfflineState(PayOfflineState.IDLE));
......@@ -234,6 +236,8 @@ public class PayOfflineViewModel extends ViewModel {
} catch (Exception ignored) {
} finally {
payOfflineState.postValue(new PayOfflineState(PayOfflineState.IDLE));
// 继续处理下一批离线订单
traversalPayOfflineData();
}
}
}
......
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