Commit 4628c6b3 by pye52

各设备指令改为单例的模式(避免多次执行)

parent 8c787126
......@@ -40,6 +40,7 @@ public class LogCommandHandler extends CommandHandler {
private static final String UPLOAD_DIR = File.separator + "upload_log";
private static final long DEFAULT_DELAY = 1000;
private volatile boolean start = false;
private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
private String deviceSN;
private CommandLog commandLog;
......@@ -48,7 +49,33 @@ public class LogCommandHandler extends CommandHandler {
private Date startTime;
private Date endTime;
public LogCommandHandler(Command command, Gson gson, String deviceSN, CommandProgressCallback callback) {
private static LogCommandHandler instance;
public static LogCommandHandler getInstance(Command command, Gson gson, String deviceSN, CommandProgressCallback callback) {
if (instance == null) {
synchronized (LogCommandHandler.class) {
if (instance == null) {
instance = new LogCommandHandler(command, gson, deviceSN, callback);
} else {
instance.init(command, gson, deviceSN, callback);
}
}
} else {
instance.init(command, gson, deviceSN, callback);
}
return instance;
}
private void init(Command command, Gson gson, String deviceSN, CommandProgressCallback callback) {
this.command = command;
this.gson = gson;
this.commandProgressCallback = callback;
this.deviceSN = deviceSN;
this.commandLog = gson.fromJson(command.getData(), CommandLog.class);
parseDate();
}
private LogCommandHandler(Command command, Gson gson, String deviceSN, CommandProgressCallback callback) {
super(command, gson, callback);
this.deviceSN = deviceSN;
this.commandLog = gson.fromJson(command.getData(), CommandLog.class);
......@@ -179,16 +206,24 @@ public class LogCommandHandler extends CommandHandler {
}
@Override
public CommandResponse run() throws InterruptedException {
public synchronized CommandResponse run() throws InterruptedException {
if (start) {
LogUtils.w(TAG, "日志上传任务已启动");
return null;
}
start = true;
if (!checkLogCommand()) {
start = false;
return failedResult("日志上传指令不符合规范");
}
File logFile = getZipLogs();
if (logFile != null) {
wait("上传压缩文件至服务器", 60);
upload(logFile);
start = false;
return successResult("");
}
start = false;
return failedResult("");
}
}
......@@ -16,15 +16,49 @@ public class WifiConfigCommandHandler extends CommandHandler {
private static final long POLLING_DELAY = 100;
private CommandWifiConfig wifiConfig;
public WifiConfigCommandHandler(Command command, Gson gson, CommandProgressCallback callback) {
private volatile boolean start = false;
private static WifiConfigCommandHandler instance;
public static WifiConfigCommandHandler getInstance(Command command, Gson gson, CommandProgressCallback callback) {
if (instance == null) {
synchronized (WifiConfigCommandHandler.class) {
if (instance == null) {
instance = new WifiConfigCommandHandler(command, gson, callback);
} else {
instance.init(command, gson, callback);
}
}
} else {
instance.init(command, gson, callback);
}
return instance;
}
private WifiConfigCommandHandler(Command command, Gson gson, CommandProgressCallback callback) {
super(command, gson, callback);
this.wifiConfig = gson.fromJson(command.getData(), CommandWifiConfig.class);
}
private void init(Command command, Gson gson, CommandProgressCallback callback) {
this.command = command;
this.gson = gson;
this.commandProgressCallback = callback;
this.wifiConfig = gson.fromJson(command.getData(), CommandWifiConfig.class);
}
@Override
public CommandResponse run() throws InterruptedException {
public synchronized CommandResponse run() throws InterruptedException {
if (start) {
LogUtils.w(TAG, "Wifi配置任务已启动");
return null;
}
start = true;
CommandWifiConfig.CommandWifiConfigData data = wifiConfig.getData();
if (data == null) return null;
if (data == null) {
start = false;
return null;
}
if (!NetworkUtils.isWifiEnabled()) {
wait("正在启动Wifi", 5);
......@@ -33,6 +67,7 @@ public class WifiConfigCommandHandler extends CommandHandler {
wait(failedMessage, 5);
LogUtils.e(TAG, failedMessage);
Thread.sleep(DEFAULT_DELAY);
start = false;
return failedResult(failedMessage);
}
}
......@@ -63,6 +98,8 @@ public class WifiConfigCommandHandler extends CommandHandler {
LogUtils.e(TAG, "链接wifi过程出错: " + e.getMessage(), e);
Thread.sleep(DEFAULT_DELAY);
return failedResult(e.getMessage());
} finally {
start = false;
}
return failedResult("无法连接Wifi");
}
......
......@@ -129,13 +129,13 @@ public class CommandViewModel extends ViewModel implements CommandProgressCallba
try {
switch (command.getAction()) {
case Command.LOG_UPLOAD:
handler = new LogCommandHandler(command, gson, deviceSN,CommandViewModel.this);
handler = LogCommandHandler.getInstance(command, gson, deviceSN,CommandViewModel.this);
break;
case Command.APP_UPDATE:
handler = UpdateCommandHandler.getInstance(command, gson, CommandViewModel.this);
break;
case Command.CONFIG_WIFI:
handler = new WifiConfigCommandHandler(command, gson, CommandViewModel.this);
handler = WifiConfigCommandHandler.getInstance(command, gson, CommandViewModel.this);
break;
case Command.CONFIG_LOG:
// CONFIG_LOG后直接return
......@@ -156,7 +156,7 @@ public class CommandViewModel extends ViewModel implements CommandProgressCallba
commandState.postValue(new CommandState(CommandState.WAIT));
CommandResponse response = handler.run();
if (response == null) {
LogUtils.d(TAG, "指令无返回结果");
LogUtils.d(TAG, "指令无返回结果: " + command.toString());
return;
}
if (response.success()) {
......
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