feat: 开关新增保存到数据库

This commit is contained in:
niushuai233 2022-10-24 17:41:46 +08:00
parent e000f72f07
commit 211b18ff12
10 changed files with 235 additions and 28 deletions

View File

@ -2,5 +2,32 @@ package cc.niushuai.project.devcontrol.base.enums;
public enum OnOffEnum { 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;
}
} }

View File

@ -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;
}
}

View File

@ -9,6 +9,7 @@ import androidx.appcompat.widget.AppCompatImageButton;
import java.util.Map; import java.util.Map;
import cc.niushuai.project.devcontrol.R; 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.vo.DeviceInfo;
import cc.niushuai.project.devcontrol.base.util.ActivityUtil; import cc.niushuai.project.devcontrol.base.util.ActivityUtil;
import cc.niushuai.project.devcontrol.base.util.Keys; 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); findViewById(R.id.activity_title_more_set).setOnClickListener(onClickListener);
} }
/**
* 重建缓存
*
* @author niushuai
* @date: 2022/10/24 17:09
*/
public void rebuildDeviceInfoMapCache() {
GlobalVariables.initDeviceInfoMap();
}
} }

View File

@ -1,8 +1,11 @@
package cc.niushuai.project.devcontrol.base.util; package cc.niushuai.project.devcontrol.base.util;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; 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.vo.DeviceInfo;
import cc.niushuai.project.devcontrol.base.ui.BaseActivity; import cc.niushuai.project.devcontrol.base.ui.BaseActivity;
@ -14,6 +17,21 @@ public class GlobalVariables {
*/ */
public static final Map<String, DeviceInfo> DEVICE_INFO_MAP = new HashMap<>(16); public static final Map<String, DeviceInfo> DEVICE_INFO_MAP = new HashMap<>(16);
/**
* 初始化设备列表到缓存中
*
* @author niushuai
* @date: 2022/10/24 17:08
*/
public static void initDeviceInfoMap() {
DEVICE_INFO_MAP.clear();
List<Device> deviceList = DB.getDeviceDao().loadAll();
for (Device device : deviceList) {
DEVICE_INFO_MAP.put(device.getId() + "", DeviceInfo.convert(device));
}
}
public static Class<? extends BaseActivity> getDeviceAddActivity(String deviceId) { public static Class<? extends BaseActivity> getDeviceAddActivity(String deviceId) {
return getDeviceInfo(deviceId).getDeviceType().getDeviceAddActivity(); return getDeviceInfo(deviceId).getDeviceType().getDeviceAddActivity();
} }

View File

@ -40,12 +40,7 @@ public class Device implements Serializable {
* 开关状态 * 开关状态
*/ */
private String onOff; private String onOff;
/**
* 设备描述信息
*/
private String description;
/** /**
* 设备列表界面 list icon id * 设备列表界面 list icon id
*/ */
@ -89,17 +84,16 @@ public class Device implements Serializable {
*/ */
private String createTime; private String createTime;
@Generated(hash = 94069086) @Generated(hash = 1723639212)
public Device(Long id, Integer order, String deviceName, String deviceType, public Device(Long id, Integer order, String deviceName, String deviceType,
String onOff, String description, Integer iconId, String commandPath, String onOff, Integer iconId, String commandPath, String commandStatus,
String commandStatus, String commandOpen, String commandClose, String commandOpen, String commandClose, Integer isDeleted,
Integer isDeleted, String remark, String createTime) { String remark, String createTime) {
this.id = id; this.id = id;
this.order = order; this.order = order;
this.deviceName = deviceName; this.deviceName = deviceName;
this.deviceType = deviceType; this.deviceType = deviceType;
this.onOff = onOff; this.onOff = onOff;
this.description = description;
this.iconId = iconId; this.iconId = iconId;
this.commandPath = commandPath; this.commandPath = commandPath;
this.commandStatus = commandStatus; this.commandStatus = commandStatus;
@ -146,14 +140,6 @@ public class Device implements Serializable {
this.onOff = onOff; this.onOff = onOff;
} }
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getIconId() { public Integer getIconId() {
return this.iconId; return this.iconId;
} }

View File

@ -5,6 +5,10 @@ import android.os.Bundle;
import android.view.View; import android.view.View;
import cc.niushuai.project.devcontrol.R; 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.vo.DeviceInfo;
import cc.niushuai.project.devcontrol.base.enums.DeviceTypeEnum; import cc.niushuai.project.devcontrol.base.enums.DeviceTypeEnum;
import cc.niushuai.project.devcontrol.base.enums.OnOffEnum; 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.base.util.ToastUtil;
import cc.niushuai.project.devcontrol.databinding.ActivityDeviceAddPowerSwitchBinding; import cc.niushuai.project.devcontrol.databinding.ActivityDeviceAddPowerSwitchBinding;
import cc.niushuai.project.devcontrol.ui.powerswitch.PowerSwitchActivity; 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.IdUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
@ -63,23 +68,31 @@ public class DeviceAddPowerSwitchActivity extends BaseActivity {
* @date: 2022/10/21 14:20 * @date: 2022/10/21 14:20
*/ */
private void confirm4SaveDataClickListener(View view) { 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.setIconId(R.drawable.ic_device_type_switch);
device.setDeviceType(DeviceTypeEnum.Power_Switch); device.setDeviceType(DeviceTypeEnum.Power_Switch.getValue());
device.setOnOff(OnOffEnum.OFF); device.setOnOff(OnOffEnum.OFF.getValue());
device.setDeviceName(binding.deviceAddName.getText().toString()); device.setDeviceName(binding.deviceAddName.getText().toString());
device.setRemark("默认设备");
device.setCommandPath(binding.deviceAddParamProgram.getText().toString()); device.setCommandPath(binding.deviceAddParamProgram.getText().toString());
device.setCommandStatus(binding.deviceAddParamStatus.getText().toString());
device.setCommandOpen(binding.deviceAddParamOpen.getText().toString()); device.setCommandOpen(binding.deviceAddParamOpen.getText().toString());
device.setCommandClose(binding.deviceAddParamClose.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())); ToastUtil.show(this, StrUtil.format("设备: {} 已新增", device.getDeviceName()));
this.finish(); 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() + ""});
} }
} }

View File

@ -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.enums.OnOffEnum;
import cc.niushuai.project.devcontrol.base.util.GlobalVariables; import cc.niushuai.project.devcontrol.base.util.GlobalVariables;
import cc.niushuai.project.devcontrol.base.util.IdWorker; 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 commandPath;
/**
* 状态值
*/
private String commandStatus;
/** /**
* 开启参数 * 开启参数
*/ */
@ -60,6 +67,19 @@ public class DeviceInfo extends BaseVO {
*/ */
private List<String> commandExtra; private List<String> commandExtra;
/**
* 排序
*/
private Integer order;
public Integer getOrder() {
return order;
}
public void setOrder(Integer order) {
this.order = order;
}
public String getDeviceName() { public String getDeviceName() {
return deviceName; return deviceName;
} }
@ -100,6 +120,14 @@ public class DeviceInfo extends BaseVO {
this.commandPath = commandPath; this.commandPath = commandPath;
} }
public String getCommandStatus() {
return commandStatus;
}
public void setCommandStatus(String commandStatus) {
this.commandStatus = commandStatus;
}
public String getCommandOpen() { public String getCommandOpen() {
return commandOpen; return commandOpen;
} }
@ -152,4 +180,24 @@ public class DeviceInfo extends BaseVO {
return list; 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;
}
} }

View File

@ -21,6 +21,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content">
<!--设备名称-->
<LinearLayout <LinearLayout
android:id="@id/device_add_ll_name" android:id="@id/device_add_ll_name"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -48,6 +49,7 @@
</LinearLayout> </LinearLayout>
<!--选择icon-->
<LinearLayout <LinearLayout
android:id="@id/device_add_ll_select_icon" android:id="@id/device_add_ll_select_icon"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -81,6 +83,7 @@
</LinearLayout> </LinearLayout>
<!--关联程序-->
<LinearLayout <LinearLayout
android:id="@id/device_add_ll_program" android:id="@id/device_add_ll_program"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -108,11 +111,40 @@
</LinearLayout> </LinearLayout>
<!--状态指令-->
<LinearLayout
android:id="@id/device_add_ll_status"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_below="@id/device_add_ll_program"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="@string/device_add_param_status"
android:textSize="16sp" />
<EditText
android:id="@id/device_add_param_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="9"
android:inputType="text" />
</LinearLayout>
<!--开启指令-->
<LinearLayout <LinearLayout
android:id="@id/device_add_ll_open" android:id="@id/device_add_ll_open"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="48dp" android:layout_height="48dp"
android:layout_below="@id/device_add_ll_program" android:layout_below="@id/device_add_ll_status"
android:layout_marginStart="10dp" android:layout_marginStart="10dp"
android:layout_marginTop="10dp" android:layout_marginTop="10dp"
android:layout_marginEnd="10dp" android:layout_marginEnd="10dp"
@ -135,6 +167,7 @@
</LinearLayout> </LinearLayout>
<!--关闭指令-->
<LinearLayout <LinearLayout
android:id="@id/device_add_ll_close" android:id="@id/device_add_ll_close"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -161,6 +194,33 @@
android:inputType="text" /> android:inputType="text" />
</LinearLayout> </LinearLayout>
<!--备注信息-->
<LinearLayout
android:id="@id/device_add_ll_remark"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_below="@id/device_add_ll_close"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="@string/device_add_param_remark"
android:textSize="16sp" />
<EditText
android:id="@id/device_add_param_remark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="9"
android:inputType="text" />
</LinearLayout>
</RelativeLayout> </RelativeLayout>
</ScrollView> </ScrollView>
</RelativeLayout> </RelativeLayout>

View File

@ -4,16 +4,20 @@
<!--开关--> <!--开关-->
<item name="device_add_name" type="id"/> <item name="device_add_name" type="id"/>
<item name="device_add_select_icon" type="id"/> <item name="device_add_select_icon" type="id"/>
<item name="device_add_param_status" type="id"/>
<item name="device_add_param_open" type="id"/> <item name="device_add_param_open" type="id"/>
<item name="device_add_param_close" type="id"/> <item name="device_add_param_close" type="id"/>
<item name="device_add_param_program" type="id"/> <item name="device_add_param_program" type="id"/>
<item name="device_add_param_remark" type="id"/>
<!--layout--> <!--layout-->
<item name="device_add_ll_name" type="id"/> <item name="device_add_ll_name" type="id"/>
<item name="device_add_ll_select_icon" type="id"/> <item name="device_add_ll_select_icon" type="id"/>
<item name="device_add_ll_status" type="id"/>
<item name="device_add_ll_program" type="id"/> <item name="device_add_ll_program" type="id"/>
<item name="device_add_ll_open" type="id"/> <item name="device_add_ll_open" type="id"/>
<item name="device_add_ll_close" type="id"/> <item name="device_add_ll_close" type="id"/>
<item name="device_add_ll_remark" type="id"/>

View File

@ -3,9 +3,11 @@
<!--common--> <!--common-->
<string name="device_add_select_icon">选择图标</string> <string name="device_add_select_icon">选择图标</string>
<string name="device_add_param_status">状态指令</string>
<string name="device_add_param_open">开启指令</string> <string name="device_add_param_open">开启指令</string>
<string name="device_add_param_close">关闭指令</string> <string name="device_add_param_close">关闭指令</string>
<string name="device_add_param_program">关联程序</string> <string name="device_add_param_program">关联程序</string>
<string name="device_add_param_remark">扩展描述</string>
<!--开关--> <!--开关-->
<string name="power_switch_add">添加开关设备</string> <string name="power_switch_add">添加开关设备</string>