feat: 完成开关执行命令

This commit is contained in:
niushuai233 2022-10-28 14:22:03 +08:00
parent 1a4ed9018e
commit 8183bbc170
6 changed files with 186 additions and 6 deletions

View File

@ -10,8 +10,10 @@ import java.util.Map;
import cc.niushuai.project.devcontrol.R; import cc.niushuai.project.devcontrol.R;
import cc.niushuai.project.devcontrol.base.util.ActivityUtil; import cc.niushuai.project.devcontrol.base.util.ActivityUtil;
import cc.niushuai.project.devcontrol.base.util.ExecUtil;
import cc.niushuai.project.devcontrol.base.util.Global; import cc.niushuai.project.devcontrol.base.util.Global;
import cc.niushuai.project.devcontrol.base.util.Keys; import cc.niushuai.project.devcontrol.base.util.Keys;
import cc.niushuai.project.devcontrol.base.util.ToastUtil;
import cc.niushuai.project.devcontrol.base.util.UiUtil; import cc.niushuai.project.devcontrol.base.util.UiUtil;
import cc.niushuai.project.devcontrol.ui.common.IconSelectDialogFragment; import cc.niushuai.project.devcontrol.ui.common.IconSelectDialogFragment;
import cc.niushuai.project.devcontrol.vo.DeviceInfo; import cc.niushuai.project.devcontrol.vo.DeviceInfo;
@ -179,7 +181,29 @@ public abstract class BaseActivity extends AppCompatActivity {
* @author niushuai * @author niushuai
* @date: 2022/10/24 17:09 * @date: 2022/10/24 17:09
*/ */
public void rebuildDeviceInfoMapCache() { protected void rebuildDeviceInfoMapCache() {
Global.initDeviceInfoMap(); Global.initDeviceInfoMap();
} }
/**
* 执行命令 返回成功与否
*
* @param command
* @author niushuai233
* @date: 2022/10/28 14:00
* @return: {@link boolean}
*/
protected boolean exec(String command) {
ExecUtil.CommandResult<String> commandResult = ExecUtil.exec4Str(command);
if (commandResult.getSuccess()) {
// 命令执行成功
ToastUtil.show(this, "执行成功");
return true;
} else {
// 命令执行失败
ToastUtil.show(this, "执行失败");
return false;
}
}
} }

View File

@ -0,0 +1,67 @@
package cc.niushuai.project.devcontrol.base.util;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import cn.hutool.core.util.RuntimeUtil;
/**
* 执行工具类 封装执行结果
*
* @author niushuai233
* @date 2022/10/28 13:43
*/
public class ExecUtil {
public static class CommandResult<T> {
private Boolean success;
private T result;
public CommandResult(Boolean success, T result) {
this.success = success;
this.result = result;
}
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public T getResult() {
return result;
}
public void setResult(T result) {
this.result = result;
}
}
public static CommandResult<String> exec4Str(String command) {
try {
String result = RuntimeUtil.execForStr(Charset.defaultCharset(), command);
XLog.i(Keys.Tag.EXEC_COMMAND, "执行命令: {}, 结果: {}", command, result);
return new CommandResult<>(true, result);
} catch (Exception e) {
XLog.e(Keys.Tag.EXEC_COMMAND, "执行命令: {}, 异常: {}", command, e.getMessage(), e);
return new CommandResult<>(false, e.getMessage());
}
}
public static CommandResult<List<String>> exec4Lines(String... command) {
try {
List<String> result = RuntimeUtil.execForLines(Charset.defaultCharset(), command);
XLog.i(Keys.Tag.EXEC_COMMAND, "执行多命令: {}, 结果: {}", command, result.stream().collect(Collectors.joining(System.lineSeparator())));
return new CommandResult<>(true, result);
} catch (Exception e) {
XLog.e(Keys.Tag.EXEC_COMMAND, "执行多命令: {}, 异常: {}", command, e.getMessage(), e);
return new CommandResult<>(false, Collections.singletonList(e.getMessage()));
}
}
}

View File

@ -65,4 +65,42 @@ public class Global {
public static String logAbsolutePath() { public static String logAbsolutePath() {
return Global.LOG_ROOT_PATH + DateUtil.formatDate(new Date()) + Keys.LOG_SUFFIX; return Global.LOG_ROOT_PATH + DateUtil.formatDate(new Date()) + Keys.LOG_SUFFIX;
} }
/**
* 拼接完整的status命令
*
* @param device
* @author niushuai233
* @date: 2022/10/28 13:36
* @return: {@link String}
*/
public static String getDeviceCommandStatus(DeviceInfo device) {
return StrUtil.join(StrUtil.SPACE, device.getCommandPath(), device.getCommandStatus());
}
/**
* 拼接完整的on命令
*
* @param device
* @author niushuai233
* @date: 2022/10/28 13:36
* @return: {@link String}
*/
public static String getDeviceCommandOpen(DeviceInfo device) {
return StrUtil.join(StrUtil.SPACE, device.getCommandPath(), device.getCommandOpen());
}
/**
* 拼接完整的off命令
*
* @param device
* @author niushuai233
* @date: 2022/10/28 13:36
* @return: {@link String}
*/
public static String getDeviceCommandClose(DeviceInfo device) {
return StrUtil.join(StrUtil.SPACE, device.getCommandPath(), device.getCommandClose());
}
} }

View File

@ -18,9 +18,9 @@ public interface Keys {
interface Tag { interface Tag {
String MY_OPEN_HELPER = "MyOpenHelper"; String MY_OPEN_HELPER = "MyOpenHelper";
String APP_INIT = "AppInit"; String APP_INIT = "AppInit";
String KEEP_DAY_SELECT = "KeepDaySelect"; String KEEP_DAY_SELECT = "KeepDaySelect";
String APP_SETUP = "AppSetup"; String APP_SETUP = "AppSetup";
String EXEC_COMMAND = "ExecCommand";
} }
} }

View File

@ -1,10 +1,12 @@
package cc.niushuai.project.devcontrol.db.util; package cc.niushuai.project.devcontrol.db.util;
import java.util.Date;
import java.util.List; import java.util.List;
import cc.niushuai.project.devcontrol.base.util.IdWorker; import cc.niushuai.project.devcontrol.base.util.IdWorker;
import cc.niushuai.project.devcontrol.db.DB; import cc.niushuai.project.devcontrol.db.DB;
import cc.niushuai.project.devcontrol.db.entity.SysConfig; import cc.niushuai.project.devcontrol.db.entity.SysConfig;
import cc.niushuai.project.devcontrol.vo.DeviceInfo;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
@ -31,8 +33,8 @@ public class DBHelper {
DB.getSysConfigDao().insert(new SysConfig(IdWorker.getNextId(), key, value, DateUtil.now(), DateUtil.now())); DB.getSysConfigDao().insert(new SysConfig(IdWorker.getNextId(), key, value, DateUtil.now(), DateUtil.now()));
} }
public static void configUpdate(String level, SysConfig updateEntity) { public static void configUpdate(String value, SysConfig updateEntity) {
updateEntity.setValue(level); updateEntity.setValue(value);
updateEntity.setUpdateTime(DateUtil.now()); updateEntity.setUpdateTime(DateUtil.now());
DB.getSysConfigDao().update(updateEntity); DB.getSysConfigDao().update(updateEntity);
@ -48,4 +50,10 @@ public class DBHelper {
List<SysConfig> list = DB.getSysConfigDao().queryRaw(CONFIG_WHERE_KEY, key); List<SysConfig> list = DB.getSysConfigDao().queryRaw(CONFIG_WHERE_KEY, key);
return CollUtil.isEmpty(list) ? null : list.get(0); return CollUtil.isEmpty(list) ? null : list.get(0);
} }
public static void deviceUpdate(DeviceInfo device) {
device.setUpdateTime(new Date());
DB.getDeviceDao().update(device.toDevice());
}
} }

View File

@ -11,9 +11,13 @@ import java.util.HashMap;
import cc.niushuai.project.devcontrol.R; import cc.niushuai.project.devcontrol.R;
import cc.niushuai.project.devcontrol.base.enums.OnOffEnum; import cc.niushuai.project.devcontrol.base.enums.OnOffEnum;
import cc.niushuai.project.devcontrol.base.ui.BaseActivity; import cc.niushuai.project.devcontrol.base.ui.BaseActivity;
import cc.niushuai.project.devcontrol.base.util.ExecUtil;
import cc.niushuai.project.devcontrol.base.util.Global; import cc.niushuai.project.devcontrol.base.util.Global;
import cc.niushuai.project.devcontrol.base.util.Keys; import cc.niushuai.project.devcontrol.base.util.Keys;
import cc.niushuai.project.devcontrol.base.util.ToastUtil;
import cc.niushuai.project.devcontrol.base.util.XLog;
import cc.niushuai.project.devcontrol.databinding.ActivityPowerSwitchBinding; import cc.niushuai.project.devcontrol.databinding.ActivityPowerSwitchBinding;
import cc.niushuai.project.devcontrol.db.util.DBHelper;
public class PowerSwitchActivity extends BaseActivity { public class PowerSwitchActivity extends BaseActivity {
@ -81,17 +85,56 @@ public class PowerSwitchActivity extends BaseActivity {
AppCompatImageView appCompatImageView = (AppCompatImageView) view; AppCompatImageView appCompatImageView = (AppCompatImageView) view;
int switchImageId, iconImageId; int switchImageId, iconImageId;
// 命令执行结果标识
boolean execFlag;
if (OnOffEnum.OFF.equals(device.getOnOff())) { if (OnOffEnum.OFF.equals(device.getOnOff())) {
device.setOnOff(OnOffEnum.ON); device.setOnOff(OnOffEnum.ON);
switchImageId = R.drawable.img_switch_open; switchImageId = R.drawable.img_switch_open;
iconImageId = R.drawable.ic_device_light_1_on; iconImageId = R.drawable.ic_device_light_1_on;
execFlag = switchOn();
} else { } else {
device.setOnOff(OnOffEnum.OFF); device.setOnOff(OnOffEnum.OFF);
switchImageId = R.drawable.img_switch_close; switchImageId = R.drawable.img_switch_close;
iconImageId = R.drawable.ic_device_light_1_close; iconImageId = R.drawable.ic_device_light_1_close;
execFlag = switchOff();
} }
if (execFlag) {
// 执行成功 设置icon
appCompatImageView.setImageResource(switchImageId); appCompatImageView.setImageResource(switchImageId);
((AppCompatImageView) findViewById(R.id.power_switch_activity_content_icon)).setImageResource(iconImageId); ((AppCompatImageView) findViewById(R.id.power_switch_activity_content_icon)).setImageResource(iconImageId);
// 写入数据库
DBHelper.deviceUpdate(device);
}
}
/**
* 执行开关开启命令
*
* @author niushuai233
* @date: 2022/10/28 13:35
*/
private boolean switchOn() {
String command = Global.getDeviceCommandOpen(device);
XLog.i(Keys.Tag.EXEC_COMMAND, "设备: {}, 命令: {}", device.getDeviceName(), command);
return exec(command);
}
/**
* 执行开关关闭命令
*
* @author niushuai233
* @date: 2022/10/28 13:35
*/
private boolean switchOff() {
String command = Global.getDeviceCommandClose(device);
XLog.i(Keys.Tag.EXEC_COMMAND, "设备: {}, 命令: {}", device.getDeviceName(), command);
return exec(command);
} }
@Override @Override