diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/MainActivity.java b/app/src/main/java/cc/niushuai/project/devcontrol/MainActivity.java index d8d2408..045f372 100644 --- a/app/src/main/java/cc/niushuai/project/devcontrol/MainActivity.java +++ b/app/src/main/java/cc/niushuai/project/devcontrol/MainActivity.java @@ -9,6 +9,7 @@ import androidx.navigation.ui.NavigationUI; import com.google.android.material.bottomnavigation.BottomNavigationView; +import cc.niushuai.project.devcontrol.base.App; import cc.niushuai.project.devcontrol.databinding.ActivityMainBinding; public class MainActivity extends AppCompatActivity { @@ -24,6 +25,8 @@ public class MainActivity extends AppCompatActivity { // 设置底部导航栏 setBottomNavigationView(); + + App.init(this); } /** diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/base/App.java b/app/src/main/java/cc/niushuai/project/devcontrol/base/App.java new file mode 100644 index 0000000..0485961 --- /dev/null +++ b/app/src/main/java/cc/niushuai/project/devcontrol/base/App.java @@ -0,0 +1,18 @@ +package cc.niushuai.project.devcontrol.base; + +import android.content.Context; + +import cc.niushuai.project.devcontrol.db.DB; + +public class App { + + public static void init(Context context) { + + // 初始化数据库 + initDb(context); + } + + private static void initDb(Context context) { + DB.getInstance().init(context); + } +} 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 4d18dd6..cea6a35 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 @@ -3,4 +3,10 @@ package cc.niushuai.project.devcontrol.base.util; public interface Keys { String ID = "id"; + + String DB_FILE_NAME = "dev_control.db"; + + interface Tag { + String MY_OPEN_HELPER = "MyOpenHelper"; + } } diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/db/DB.java b/app/src/main/java/cc/niushuai/project/devcontrol/db/DB.java new file mode 100644 index 0000000..ccd07d9 --- /dev/null +++ b/app/src/main/java/cc/niushuai/project/devcontrol/db/DB.java @@ -0,0 +1,105 @@ +package cc.niushuai.project.devcontrol.db; + +import android.content.Context; + +import cc.niushuai.project.devcontrol.base.util.Keys; +import cc.niushuai.project.devcontrol.db.greendao.gen.DaoMaster; +import cc.niushuai.project.devcontrol.db.greendao.gen.DaoSession; +import cc.niushuai.project.devcontrol.db.greendao.gen.DeviceDao; +import cc.niushuai.project.devcontrol.db.util.MyOpenHelper; + +/** + * 数据库初始化 + * + * @author niushuai233 + * @date 2022/10/24 10:44 + */ +public class DB { + + private static boolean INIT_FLAG = false; + + private static DB db; + + private DB() { + } + + public static DB getInstance() { + if (null == db) { + db = new DB(); + } + return db; + } + + private Context context; + private DaoMaster.OpenHelper openHelper; + private DaoMaster daoMaster; + private DaoSession daoSession; + + /** + * 初始化数据库表结构 + * + * @param context + * @author niushuai + * @date: 2022/10/24 10:46 + */ + public void init(Context context) { + if (!INIT_FLAG) { + DB instance = DB.getInstance(); + instance.context = context; +// instance.getOpenHelper(); +// instance.getDaoMaster(); + instance.getDaoSession(); + INIT_FLAG = true; + } + } + + /** + * 初始化open helper + * + * @author niushuai + * @date: 2022/10/24 10:53 + * @return: {@link DaoMaster.OpenHelper} + */ + public DaoMaster.OpenHelper getOpenHelper() { + if (null == openHelper) { + openHelper = new MyOpenHelper(context, Keys.DB_FILE_NAME); + } + return openHelper; + } + + public DaoMaster getDaoMaster() { + if (null == daoMaster) { + daoMaster = new DaoMaster(getOpenHelper().getWritableDb()); + } + return daoMaster; + } + + public DaoSession getDaoSession() { + if (null == daoSession) { + daoSession = getDaoMaster().newSession(); + } + return daoSession; + } + + /** + * daoSession 对外暴漏入口 + * + * @author niushuai + * @date: 2022/10/24 11:04 + * @return: {@link DaoSession} + */ + public static DaoSession session() { + return DB.getInstance().getDaoSession(); + } + + /** + * 设备操作入口 + * + * @author niushuai + * @date: 2022/10/24 11:05 + * @return: {@link DeviceDao} + */ + public static DeviceDao getDeviceDao() { + return session().getDeviceDao(); + } +} diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/db/entity/CommandExt.java b/app/src/main/java/cc/niushuai/project/devcontrol/db/entity/CommandExt.java new file mode 100644 index 0000000..9300cd1 --- /dev/null +++ b/app/src/main/java/cc/niushuai/project/devcontrol/db/entity/CommandExt.java @@ -0,0 +1,110 @@ +package cc.niushuai.project.devcontrol.db.entity; + +import org.greenrobot.greendao.annotation.Entity; +import org.greenrobot.greendao.annotation.Id; +import org.greenrobot.greendao.annotation.Generated; +import org.greenrobot.greendao.annotation.Index; + +/** + * 命令扩展 + * + * @author niushuai233 + * @date 2022/10/24 14:40 + */ +@Entity(nameInDb = "tb_dc_command_ext") +public class CommandExt { + + @Id + @Index + private Long id; + + /** + * 关联设备id + */ + @Index + private Long deviceId; + + /** + * 命令 + */ + private String command; + + /** + * 排序 + */ + private Integer order; + + /** + * 命令扩展描述 + */ + private String remark; + + /** + * 创建时间 + */ + private String createTime; + + @Generated(hash = 101490941) + public CommandExt(Long id, Long deviceId, String command, Integer order, + String remark, String createTime) { + this.id = id; + this.deviceId = deviceId; + this.command = command; + this.order = order; + this.remark = remark; + this.createTime = createTime; + } + + @Generated(hash = 962411676) + public CommandExt() { + } + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public Long getDeviceId() { + return this.deviceId; + } + + public void setDeviceId(Long deviceId) { + this.deviceId = deviceId; + } + + public String getCommand() { + return this.command; + } + + public void setCommand(String command) { + this.command = command; + } + + public Integer getOrder() { + return this.order; + } + + public void setOrder(Integer order) { + this.order = order; + } + + public String getCreateTime() { + return this.createTime; + } + + public void setCreateTime(String createTime) { + this.createTime = createTime; + } + + public String getRemark() { + return this.remark; + } + + public void setRemark(String remark) { + this.remark = remark; + } + +} 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 new file mode 100644 index 0000000..d78607a --- /dev/null +++ b/app/src/main/java/cc/niushuai/project/devcontrol/db/entity/Device.java @@ -0,0 +1,214 @@ +package cc.niushuai.project.devcontrol.db.entity; + +import org.greenrobot.greendao.annotation.Entity; + +import org.greenrobot.greendao.annotation.Generated; +import org.greenrobot.greendao.annotation.Id; + +import java.io.Serializable; + +/** + * 设备数据库实体 + * + * @author niushuai233 + * @date 2022/10/24 14:11 + */ +@Entity(nameInDb = "tb_dc_device") +public class Device implements Serializable { + + private static final long serialVersionUID = -3161350126173955730L; + + @Id + private Long id; + + /** + * 排序 + */ + private Integer order; + + /** + * 设备名称 + */ + private String deviceName; + + /** + * 设备类型 + */ + private String deviceType; + + /** + * 开关状态 + */ + private String onOff; + + /** + * 设备描述信息 + */ + private String description; + + /** + * 设备列表界面 list icon id + */ + private Integer iconId; + + /** + *
+ * 二进制文件存放位置 + * /文件夹/二进制文件 参数 + *+ */ + private String commandPath; + + /** + * 开启参数 + */ + private String commandOpen; + + /** + * 关闭参数 + */ + private String commandClose; + + /** + * 删除标志 + */ + private Integer isDeleted; + + /** + * 备注 + */ + private String remark; + + /** + * 创建时间 + */ + private String createTime; + + @Generated(hash = 2026639556) + public Device(Long id, Integer order, String deviceName, String deviceType, + String onOff, String description, Integer iconId, String commandPath, + 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.commandOpen = commandOpen; + this.commandClose = commandClose; + this.isDeleted = isDeleted; + this.remark = remark; + this.createTime = createTime; + } + + @Generated(hash = 1469582394) + public Device() { + } + + public Integer getOrder() { + return this.order; + } + + public void setOrder(Integer order) { + this.order = order; + } + + public String getDeviceName() { + return this.deviceName; + } + + public void setDeviceName(String deviceName) { + this.deviceName = deviceName; + } + + public String getDeviceType() { + return this.deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } + + public String getOnOff() { + return this.onOff; + } + + public void setOnOff(String onOff) { + this.onOff = onOff; + } + + public String getDescription() { + return this.description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Integer getIconId() { + return this.iconId; + } + + public void setIconId(Integer iconId) { + this.iconId = iconId; + } + + public String getCommandPath() { + return this.commandPath; + } + + public void setCommandPath(String commandPath) { + this.commandPath = commandPath; + } + + public String getCommandOpen() { + return this.commandOpen; + } + + public void setCommandOpen(String commandOpen) { + this.commandOpen = commandOpen; + } + + public String getCommandClose() { + return this.commandClose; + } + + public void setCommandClose(String commandClose) { + this.commandClose = commandClose; + } + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public Integer getIsDeleted() { + return this.isDeleted; + } + + public void setIsDeleted(Integer isDeleted) { + this.isDeleted = isDeleted; + } + + public String getRemark() { + return this.remark; + } + + public void setRemark(String remark) { + this.remark = remark; + } + + public String getCreateTime() { + return this.createTime; + } + + public void setCreateTime(String createTime) { + this.createTime = createTime; + } +} diff --git a/app/src/main/java/cc/niushuai/project/devcontrol/db/greendao/gen/CommandExtDao.java b/app/src/main/java/cc/niushuai/project/devcontrol/db/greendao/gen/CommandExtDao.java new file mode 100644 index 0000000..ed606dd --- /dev/null +++ b/app/src/main/java/cc/niushuai/project/devcontrol/db/greendao/gen/CommandExtDao.java @@ -0,0 +1,190 @@ +package cc.niushuai.project.devcontrol.db.greendao.gen; + +import android.database.Cursor; +import android.database.sqlite.SQLiteStatement; + +import org.greenrobot.greendao.AbstractDao; +import org.greenrobot.greendao.Property; +import org.greenrobot.greendao.internal.DaoConfig; +import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.database.DatabaseStatement; + +import cc.niushuai.project.devcontrol.db.entity.CommandExt; + +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. +/** + * DAO for table "tb_dc_command_ext". +*/ +public class CommandExtDao extends AbstractDao