feat: 增加greendao设备对应实体类

This commit is contained in:
niushuai233 2022-10-24 15:55:13 +08:00
parent 7a2544b5ca
commit af5212a0be
8 changed files with 681 additions and 0 deletions

View File

@ -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);
}
/**

View File

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

View File

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

View File

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

View File

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

View File

@ -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;
/**
* <pre>
* 二进制文件存放位置
* /文件夹/二进制文件 参数
* </pre>
*/
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;
}
}

View File

@ -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<CommandExt, Long> {
public static final String TABLENAME = "tb_dc_command_ext";
/**
* Properties of entity CommandExt.<br/>
* Can be used for QueryBuilder and for referencing column names.
*/
public static class Properties {
public final static Property Id = new Property(0, Long.class, "id", true, "_id");
public final static Property DeviceId = new Property(1, Long.class, "deviceId", false, "DEVICE_ID");
public final static Property Command = new Property(2, String.class, "command", false, "COMMAND");
public final static Property Order = new Property(3, Integer.class, "order", false, "ORDER");
public final static Property Remark = new Property(4, String.class, "remark", false, "REMARK");
public final static Property CreateTime = new Property(5, String.class, "createTime", false, "CREATE_TIME");
}
public CommandExtDao(DaoConfig config) {
super(config);
}
public CommandExtDao(DaoConfig config, DaoSession daoSession) {
super(config, daoSession);
}
/** Creates the underlying database table. */
public static void createTable(Database db, boolean ifNotExists) {
String constraint = ifNotExists? "IF NOT EXISTS ": "";
db.execSQL("CREATE TABLE " + constraint + "\"tb_dc_command_ext\" (" + //
"\"_id\" INTEGER PRIMARY KEY ," + // 0: id
"\"DEVICE_ID\" INTEGER," + // 1: deviceId
"\"COMMAND\" TEXT," + // 2: command
"\"ORDER\" INTEGER," + // 3: order
"\"REMARK\" TEXT," + // 4: remark
"\"CREATE_TIME\" TEXT);"); // 5: createTime
// Add Indexes
db.execSQL("CREATE INDEX " + constraint + "IDX_tb_dc_command_ext__id ON \"tb_dc_command_ext\"" +
" (\"_id\" ASC);");
db.execSQL("CREATE INDEX " + constraint + "IDX_tb_dc_command_ext_DEVICE_ID ON \"tb_dc_command_ext\"" +
" (\"DEVICE_ID\" ASC);");
}
/** Drops the underlying database table. */
public static void dropTable(Database db, boolean ifExists) {
String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"tb_dc_command_ext\"";
db.execSQL(sql);
}
@Override
protected final void bindValues(DatabaseStatement stmt, CommandExt entity) {
stmt.clearBindings();
Long id = entity.getId();
if (id != null) {
stmt.bindLong(1, id);
}
Long deviceId = entity.getDeviceId();
if (deviceId != null) {
stmt.bindLong(2, deviceId);
}
String command = entity.getCommand();
if (command != null) {
stmt.bindString(3, command);
}
Integer order = entity.getOrder();
if (order != null) {
stmt.bindLong(4, order);
}
String remark = entity.getRemark();
if (remark != null) {
stmt.bindString(5, remark);
}
String createTime = entity.getCreateTime();
if (createTime != null) {
stmt.bindString(6, createTime);
}
}
@Override
protected final void bindValues(SQLiteStatement stmt, CommandExt entity) {
stmt.clearBindings();
Long id = entity.getId();
if (id != null) {
stmt.bindLong(1, id);
}
Long deviceId = entity.getDeviceId();
if (deviceId != null) {
stmt.bindLong(2, deviceId);
}
String command = entity.getCommand();
if (command != null) {
stmt.bindString(3, command);
}
Integer order = entity.getOrder();
if (order != null) {
stmt.bindLong(4, order);
}
String remark = entity.getRemark();
if (remark != null) {
stmt.bindString(5, remark);
}
String createTime = entity.getCreateTime();
if (createTime != null) {
stmt.bindString(6, createTime);
}
}
@Override
public Long readKey(Cursor cursor, int offset) {
return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
}
@Override
public CommandExt readEntity(Cursor cursor, int offset) {
CommandExt entity = new CommandExt( //
cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1), // deviceId
cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // command
cursor.isNull(offset + 3) ? null : cursor.getInt(offset + 3), // order
cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // remark
cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5) // createTime
);
return entity;
}
@Override
public void readEntity(Cursor cursor, CommandExt entity, int offset) {
entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
entity.setDeviceId(cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1));
entity.setCommand(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
entity.setOrder(cursor.isNull(offset + 3) ? null : cursor.getInt(offset + 3));
entity.setRemark(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
entity.setCreateTime(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
}
@Override
protected final Long updateKeyAfterInsert(CommandExt entity, long rowId) {
entity.setId(rowId);
return rowId;
}
@Override
public Long getKey(CommandExt entity) {
if(entity != null) {
return entity.getId();
} else {
return null;
}
}
@Override
public boolean hasKey(CommandExt entity) {
return entity.getId() != null;
}
@Override
protected final boolean isEntityUpdateable() {
return true;
}
}

View File

@ -0,0 +1,35 @@
package cc.niushuai.project.devcontrol.db.util;
import android.content.Context;
import android.util.Log;
import org.greenrobot.greendao.database.Database;
import cc.niushuai.project.devcontrol.base.util.Keys;
import cc.niushuai.project.devcontrol.db.greendao.gen.DaoMaster;
/**
* greendao 自定义
*
* @author niushuai233
* @date 2022/10/24 10:22
*/
public class MyOpenHelper extends DaoMaster.OpenHelper {
public MyOpenHelper(Context context, String name) {
super(context, name);
}
/**
* greendao.schemaVersion 发生变更时触发
*
* @param db 数据库
* @param oldVersion 旧版本
* @param newVersion 新版本
*/
@Override
public void onUpgrade(Database db, int oldVersion, int newVersion) {
super.onUpgrade(db, oldVersion, newVersion);
Log.w(Keys.Tag.MY_OPEN_HELPER, "onUpgrade 未实现");
}
}