diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/base/ui/BaseActivity.java b/app/src/main/java/cc/niushuai/project/devcontrol/base/ui/BaseActivity.java index 5b8dd3c..a89d7f8 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/base/ui/BaseActivity.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/base/ui/BaseActivity.java @@ -10,8 +10,10 @@ import java.util.Map; import cc.niushuai.project.devcontrol.R; 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.Keys; +import cc.niushuai.project.devcontrol.base.util.ToastUtil; import cc.niushuai.project.devcontrol.base.util.UiUtil; import cc.niushuai.project.devcontrol.ui.common.IconSelectDialogFragment; import cc.niushuai.project.devcontrol.vo.DeviceInfo; @@ -179,7 +181,29 @@ public abstract class BaseActivity extends AppCompatActivity { * @author niushuai * @date: 2022/10/24 17:09 */ - public void rebuildDeviceInfoMapCache() { + protected void rebuildDeviceInfoMapCache() { Global.initDeviceInfoMap(); } + + + /** + * 执行命令 返回成功与否 + * + * @param command + * @author niushuai233 + * @date: 2022/10/28 14:00 + * @return: {@link boolean} + */ + protected boolean exec(String command) { + ExecUtil.CommandResult commandResult = ExecUtil.exec4Str(command); + if (commandResult.getSuccess()) { + // 命令执行成功 + ToastUtil.show(this, "执行成功"); + return true; + } else { + // 命令执行失败 + ToastUtil.show(this, "执行失败"); + return false; + } + } } diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/base/util/ExecUtil.java b/app/src/main/java/cc/niushuai/project/devcontrol/base/util/ExecUtil.java new file mode 100644 index 0000000..a4611e4 --- /dev/null +++ b/app/src/main/java/cc/niushuai/project/devcontrol/base/util/ExecUtil.java @@ -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 { + 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 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> exec4Lines(String... command) { + + try { + List 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())); + } + } + +} diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/base/util/Global.java b/app/src/main/java/cc/niushuai/project/devcontrol/base/util/Global.java index 709362b..fe87cb8 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/base/util/Global.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/base/util/Global.java @@ -65,4 +65,42 @@ public class Global { public static String logAbsolutePath() { 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()); + } + + } diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/base/util/Keys.java b/app/src/main/java/cc/niushuai/project/devcontrol/base/util/Keys.java index a687228..4ea6f1c 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/base/util/Keys.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/base/util/Keys.java @@ -18,9 +18,9 @@ public interface Keys { interface Tag { String MY_OPEN_HELPER = "MyOpenHelper"; - String APP_INIT = "AppInit"; String KEEP_DAY_SELECT = "KeepDaySelect"; String APP_SETUP = "AppSetup"; + String EXEC_COMMAND = "ExecCommand"; } } diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/db/util/DBHelper.java b/app/src/main/java/cc/niushuai/project/devcontrol/db/util/DBHelper.java index e052056..137e547 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/db/util/DBHelper.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/db/util/DBHelper.java @@ -1,10 +1,12 @@ package cc.niushuai.project.devcontrol.db.util; +import java.util.Date; import java.util.List; import cc.niushuai.project.devcontrol.base.util.IdWorker; import cc.niushuai.project.devcontrol.db.DB; 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.date.DateUtil; @@ -31,8 +33,8 @@ public class DBHelper { DB.getSysConfigDao().insert(new SysConfig(IdWorker.getNextId(), key, value, DateUtil.now(), DateUtil.now())); } - public static void configUpdate(String level, SysConfig updateEntity) { - updateEntity.setValue(level); + public static void configUpdate(String value, SysConfig updateEntity) { + updateEntity.setValue(value); updateEntity.setUpdateTime(DateUtil.now()); DB.getSysConfigDao().update(updateEntity); @@ -48,4 +50,10 @@ public class DBHelper { List list = DB.getSysConfigDao().queryRaw(CONFIG_WHERE_KEY, key); return CollUtil.isEmpty(list) ? null : list.get(0); } + + public static void deviceUpdate(DeviceInfo device) { + device.setUpdateTime(new Date()); + + DB.getDeviceDao().update(device.toDevice()); + } } diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/ui/powerswitch/PowerSwitchActivity.java b/app/src/main/java/cc/niushuai/project/devcontrol/ui/powerswitch/PowerSwitchActivity.java index afebe7b..8d24127 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/ui/powerswitch/PowerSwitchActivity.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/ui/powerswitch/PowerSwitchActivity.java @@ -11,9 +11,13 @@ import java.util.HashMap; import cc.niushuai.project.devcontrol.R; import cc.niushuai.project.devcontrol.base.enums.OnOffEnum; 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.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.db.util.DBHelper; public class PowerSwitchActivity extends BaseActivity { @@ -81,17 +85,56 @@ public class PowerSwitchActivity extends BaseActivity { AppCompatImageView appCompatImageView = (AppCompatImageView) view; int switchImageId, iconImageId; + // 命令执行结果标识 + boolean execFlag; if (OnOffEnum.OFF.equals(device.getOnOff())) { device.setOnOff(OnOffEnum.ON); switchImageId = R.drawable.img_switch_open; iconImageId = R.drawable.ic_device_light_1_on; + + execFlag = switchOn(); } else { device.setOnOff(OnOffEnum.OFF); switchImageId = R.drawable.img_switch_close; iconImageId = R.drawable.ic_device_light_1_close; + + execFlag = switchOff(); } - appCompatImageView.setImageResource(switchImageId); - ((AppCompatImageView) findViewById(R.id.power_switch_activity_content_icon)).setImageResource(iconImageId); + if (execFlag) { + // 执行成功 设置icon + appCompatImageView.setImageResource(switchImageId); + ((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