diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/base/enums/OnOffEnum.java b/app/src/main/java/cc/niushuai/project/devcontrol/base/enums/OnOffEnum.java index a758b90..9856296 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/base/enums/OnOffEnum.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/base/enums/OnOffEnum.java @@ -2,5 +2,32 @@ package cc.niushuai.project.devcontrol.base.enums; public enum OnOffEnum { - ON, OFF; + ON("ON", "开"), + OFF("OFF", "关"), + ; + + private String value; + private String text; + + OnOffEnum(String value, String text) { + this.value = value; + this.text = text; + } + + public String getValue() { + return value; + } + + public String getText() { + return text; + } + + public static OnOffEnum matchByValue(String value) { + for (OnOffEnum onOffEnum : OnOffEnum.values()) { + if (onOffEnum.getValue().equals(value)) { + return onOffEnum; + } + } + return null; + } } diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/base/enums/YesNoEnum.java b/app/src/main/java/cc/niushuai/project/devcontrol/base/enums/YesNoEnum.java new file mode 100644 index 0000000..339648c --- /dev/null +++ b/app/src/main/java/cc/niushuai/project/devcontrol/base/enums/YesNoEnum.java @@ -0,0 +1,39 @@ +package cc.niushuai.project.devcontrol.base.enums; + +public enum YesNoEnum { + + YES("1", 1, "是"), + NO("0",0, "否"), + ; + + private String value; + private Integer integerValue; + private String text; + + YesNoEnum(String value, Integer integerValue, String text) { + this.value = value; + this.integerValue = integerValue; + this.text = text; + } + + public String getValue() { + return value; + } + + public Integer getIntegerValue() { + return integerValue; + } + + public String getText() { + return text; + } + + public static YesNoEnum matchByValue(String value) { + for (YesNoEnum onOffEnum : YesNoEnum.values()) { + if (onOffEnum.getValue().equals(value)) { + return onOffEnum; + } + } + return null; + } +} 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 c35d03c..58998aa 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 @@ -9,6 +9,7 @@ import androidx.appcompat.widget.AppCompatImageButton; import java.util.Map; import cc.niushuai.project.devcontrol.R; +import cc.niushuai.project.devcontrol.base.util.GlobalVariables; import cc.niushuai.project.devcontrol.vo.DeviceInfo; import cc.niushuai.project.devcontrol.base.util.ActivityUtil; import cc.niushuai.project.devcontrol.base.util.Keys; @@ -146,4 +147,13 @@ public abstract class BaseActivity extends AppCompatActivity { findViewById(R.id.activity_title_more_set).setOnClickListener(onClickListener); } + /** + * 重建缓存 + * + * @author niushuai + * @date: 2022/10/24 17:09 + */ + public void rebuildDeviceInfoMapCache() { + GlobalVariables.initDeviceInfoMap(); + } } diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/base/util/GlobalVariables.java b/app/src/main/java/cc/niushuai/project/devcontrol/base/util/GlobalVariables.java index 33d9033..542fae3 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/base/util/GlobalVariables.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/base/util/GlobalVariables.java @@ -1,8 +1,11 @@ package cc.niushuai.project.devcontrol.base.util; import java.util.HashMap; +import java.util.List; import java.util.Map; +import cc.niushuai.project.devcontrol.db.DB; +import cc.niushuai.project.devcontrol.db.entity.Device; import cc.niushuai.project.devcontrol.vo.DeviceInfo; import cc.niushuai.project.devcontrol.base.ui.BaseActivity; @@ -14,6 +17,21 @@ public class GlobalVariables { */ public static final Map DEVICE_INFO_MAP = new HashMap<>(16); + /** + * 初始化设备列表到缓存中 + * + * @author niushuai + * @date: 2022/10/24 17:08 + */ + public static void initDeviceInfoMap() { + DEVICE_INFO_MAP.clear(); + List deviceList = DB.getDeviceDao().loadAll(); + + for (Device device : deviceList) { + DEVICE_INFO_MAP.put(device.getId() + "", DeviceInfo.convert(device)); + } + } + public static Class getDeviceAddActivity(String deviceId) { return getDeviceInfo(deviceId).getDeviceType().getDeviceAddActivity(); } diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/db/entity/Device.java b/app/src/main/java/cc/niushuai/project/devcontrol/db/entity/Device.java index a7b7a94..8bf4402 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/db/entity/Device.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/db/entity/Device.java @@ -40,12 +40,7 @@ public class Device implements Serializable { * 开关状态 */ private String onOff; - - /** - * 设备描述信息 - */ - private String description; - + /** * 设备列表界面 list icon id */ @@ -89,17 +84,16 @@ public class Device implements Serializable { */ private String createTime; - @Generated(hash = 94069086) + @Generated(hash = 1723639212) public Device(Long id, Integer order, String deviceName, String deviceType, - String onOff, String description, Integer iconId, String commandPath, - String commandStatus, String commandOpen, String commandClose, - Integer isDeleted, String remark, String createTime) { + String onOff, Integer iconId, String commandPath, String commandStatus, + String commandOpen, String commandClose, Integer isDeleted, + String remark, String createTime) { this.id = id; this.order = order; this.deviceName = deviceName; this.deviceType = deviceType; this.onOff = onOff; - this.description = description; this.iconId = iconId; this.commandPath = commandPath; this.commandStatus = commandStatus; @@ -146,14 +140,6 @@ public class Device implements Serializable { this.onOff = onOff; } - public String getDescription() { - return this.description; - } - - public void setDescription(String description) { - this.description = description; - } - public Integer getIconId() { return this.iconId; } diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/ui/deviceadd/DeviceAddPowerSwitchActivity.java b/app/src/main/java/cc/niushuai/project/devcontrol/ui/deviceadd/DeviceAddPowerSwitchActivity.java index 29d3f4b..462d01b 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/ui/deviceadd/DeviceAddPowerSwitchActivity.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/ui/deviceadd/DeviceAddPowerSwitchActivity.java @@ -5,6 +5,10 @@ import android.os.Bundle; import android.view.View; import cc.niushuai.project.devcontrol.R; +import cc.niushuai.project.devcontrol.base.enums.YesNoEnum; +import cc.niushuai.project.devcontrol.base.util.IdWorker; +import cc.niushuai.project.devcontrol.db.DB; +import cc.niushuai.project.devcontrol.db.entity.Device; import cc.niushuai.project.devcontrol.vo.DeviceInfo; import cc.niushuai.project.devcontrol.base.enums.DeviceTypeEnum; import cc.niushuai.project.devcontrol.base.enums.OnOffEnum; @@ -15,6 +19,7 @@ import cc.niushuai.project.devcontrol.base.util.Keys; import cc.niushuai.project.devcontrol.base.util.ToastUtil; import cc.niushuai.project.devcontrol.databinding.ActivityDeviceAddPowerSwitchBinding; import cc.niushuai.project.devcontrol.ui.powerswitch.PowerSwitchActivity; +import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.StrUtil; @@ -63,23 +68,31 @@ public class DeviceAddPowerSwitchActivity extends BaseActivity { * @date: 2022/10/21 14:20 */ private void confirm4SaveDataClickListener(View view) { - DeviceInfo device = new DeviceInfo(); + Device device = new Device(); - device.setId(IdUtil.nanoId()); + device.setId(IdWorker.getNextId()); device.setIconId(R.drawable.ic_device_type_switch); - device.setDeviceType(DeviceTypeEnum.Power_Switch); - device.setOnOff(OnOffEnum.OFF); + device.setDeviceType(DeviceTypeEnum.Power_Switch.getValue()); + device.setOnOff(OnOffEnum.OFF.getValue()); device.setDeviceName(binding.deviceAddName.getText().toString()); - device.setRemark("默认设备"); device.setCommandPath(binding.deviceAddParamProgram.getText().toString()); + device.setCommandStatus(binding.deviceAddParamStatus.getText().toString()); device.setCommandOpen(binding.deviceAddParamOpen.getText().toString()); device.setCommandClose(binding.deviceAddParamClose.getText().toString()); + device.setRemark(binding.deviceAddParamRemark.getText().toString()); + device.setCreateTime(DateUtil.now()); + device.setOrder(1); + device.setIsDeleted(YesNoEnum.NO.getIntegerValue()); - GlobalVariables.DEVICE_INFO_MAP.put(device.getId(), device); + // 新增到数据库 + DB.getDeviceDao().insert(device); + + // 重建缓存 + super.rebuildDeviceInfoMapCache(); ToastUtil.show(this, StrUtil.format("设备: {} 已新增", device.getDeviceName())); this.finish(); - ActivityUtil.startActivity(this, PowerSwitchActivity.class, new String[]{Keys.ID}, new String[]{device.getId()}); + ActivityUtil.startActivity(this, PowerSwitchActivity.class, new String[]{Keys.ID}, new String[]{device.getId() + ""}); } } \ No newline at end of file diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/vo/DeviceInfo.java b/app/src/main/java/cc/niushuai/project/devcontrol/vo/DeviceInfo.java index 9bbeba2..e3c89aa 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/vo/DeviceInfo.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/vo/DeviceInfo.java @@ -8,6 +8,8 @@ import cc.niushuai.project.devcontrol.base.enums.DeviceTypeEnum; import cc.niushuai.project.devcontrol.base.enums.OnOffEnum; import cc.niushuai.project.devcontrol.base.util.GlobalVariables; import cc.niushuai.project.devcontrol.base.util.IdWorker; +import cc.niushuai.project.devcontrol.db.entity.Device; +import cn.hutool.core.date.DateUtil; /** * 设备信息实体数据类 @@ -45,6 +47,11 @@ public class DeviceInfo extends BaseVO { */ private String commandPath; + /** + * 状态值 + */ + private String commandStatus; + /** * 开启参数 */ @@ -60,6 +67,19 @@ public class DeviceInfo extends BaseVO { */ private List commandExtra; + /** + * 排序 + */ + private Integer order; + + public Integer getOrder() { + return order; + } + + public void setOrder(Integer order) { + this.order = order; + } + public String getDeviceName() { return deviceName; } @@ -100,6 +120,14 @@ public class DeviceInfo extends BaseVO { this.commandPath = commandPath; } + public String getCommandStatus() { + return commandStatus; + } + + public void setCommandStatus(String commandStatus) { + this.commandStatus = commandStatus; + } + public String getCommandOpen() { return commandOpen; } @@ -152,4 +180,24 @@ public class DeviceInfo extends BaseVO { return list; } + + public static DeviceInfo convert(Device device) { + + DeviceInfo _this = new DeviceInfo(); + _this.setId(device.getId() + ""); + _this.setDeviceName(device.getDeviceName()); + _this.setDeviceType(DeviceTypeEnum.matchByValue(device.getDeviceType())); + _this.setOnOff(OnOffEnum.matchByValue(device.getOnOff())); + _this.setRemark(device.getRemark()); + _this.setIconId(device.getIconId()); + _this.setCommandPath(device.getCommandPath()); + _this.setCommandStatus(device.getCommandStatus()); + _this.setCommandOpen(device.getCommandOpen()); + _this.setCommandClose(device.getCommandClose()); + _this.setOrder(device.getOrder()); + _this.setIsDeleted(device.getIsDeleted()); + _this.setCreateTime(DateUtil.parseDateTime(device.getCreateTime())); + + return _this; + } } diff --git a/app/src/main/res/layout/activity_device_add_power_switch.xml b/app/src/main/res/layout/activity_device_add_power_switch.xml index ace0d92..265a92c 100644 --- a/app/src/main/res/layout/activity_device_add_power_switch.xml +++ b/app/src/main/res/layout/activity_device_add_power_switch.xml @@ -21,6 +21,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content"> + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/values/ids_device_add.xml b/app/src/main/res/values/ids_device_add.xml index 623c4bf..97d34c3 100644 --- a/app/src/main/res/values/ids_device_add.xml +++ b/app/src/main/res/values/ids_device_add.xml @@ -4,16 +4,20 @@ + + + + diff --git a/app/src/main/res/values/strings_device_add.xml b/app/src/main/res/values/strings_device_add.xml index 1487ea0..908847e 100644 --- a/app/src/main/res/values/strings_device_add.xml +++ b/app/src/main/res/values/strings_device_add.xml @@ -3,9 +3,11 @@ 选择图标 + 状态指令 开启指令 关闭指令 关联程序 + 扩展描述 添加开关设备