From bc9b6489debabd7a20ea1bc1fbc2d6ebefceac49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E5=AE=8F=E5=AE=87?= Date: Thu, 30 Jul 2020 20:29:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86V2X=E8=BF=87?= =?UTF-8?q?=E6=9C=9F=E6=95=B0=E6=8D=AE=E6=B8=85=E7=90=86=E5=B7=A5=E4=BD=9C?= =?UTF-8?q?=EF=BC=8C=E5=8F=AA=E4=BF=9D=E7=95=99=E5=BD=93=E5=A4=A9=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/misc.xml | 2 +- .../com/mogo/utils/sqlite/BaseDaoFactory.kt | 4 +- .../java/com/mogo/utils/sqlite/IBaseDao.kt | 4 +- .../com/mogo/utils/sqlite/SQLBaseDao.java | 421 ++++++++++++++++++ .../utils/sqlite/proxy/BaseDaoProxyLog.kt | 10 +- .../common/entity/V2XHistoryScenarioData.java | 2 + .../mogo/module/v2x/V2XModuleProvider.java | 92 ++-- .../v2x/scenario/impl/V2XScenarioManager.java | 16 +- .../mogo/module/v2x/utils/V2XSQLiteUtils.java | 56 +++ 9 files changed, 548 insertions(+), 59 deletions(-) create mode 100644 foudations/mogo-utils/src/main/java/com/mogo/utils/sqlite/SQLBaseDao.java create mode 100644 modules/mogo-module-v2x/src/main/java/com/mogo/module/v2x/utils/V2XSQLiteUtils.java diff --git a/.idea/misc.xml b/.idea/misc.xml index 21e99e2dc0..cd77a1f062 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -4,7 +4,7 @@ - + \ No newline at end of file diff --git a/foudations/mogo-utils/src/main/java/com/mogo/utils/sqlite/BaseDaoFactory.kt b/foudations/mogo-utils/src/main/java/com/mogo/utils/sqlite/BaseDaoFactory.kt index 605043bf5a..b5325445c7 100644 --- a/foudations/mogo-utils/src/main/java/com/mogo/utils/sqlite/BaseDaoFactory.kt +++ b/foudations/mogo-utils/src/main/java/com/mogo/utils/sqlite/BaseDaoFactory.kt @@ -49,11 +49,13 @@ open class BaseDaoFactory { } //openOrCreateDatabase 如果不存在则先创建再打开数据库,如果存在则直接打开。 sqLiteDatabasePath = - "${context.getDir("database", Context.MODE_APPEND).path}/$dbName" + "${context.getDir("database", Context.MODE_APPEND).path}/$dbName" sqLiteDatabase = SQLiteDatabase.openOrCreateDatabase(sqLiteDatabasePath, null) // 这里为了演示,添加了日志工具的叠加使用,根据需要可以自己修改 // baseDao = BaseDaoProxyShow().bind(BaseDaoProxyLog().bind(BaseDao())) as IBaseDao + // baseDao = BaseDaoProxyLog().bind(SQLBaseDao()) as IBaseDao + // baseDao = BaseDao() baseDao = BaseDaoProxyLog().bind(BaseDao()) as IBaseDao baseDao.init(sqLiteDatabase!!, entityClass) diff --git a/foudations/mogo-utils/src/main/java/com/mogo/utils/sqlite/IBaseDao.kt b/foudations/mogo-utils/src/main/java/com/mogo/utils/sqlite/IBaseDao.kt index 7af36200b8..f3378ee1fd 100644 --- a/foudations/mogo-utils/src/main/java/com/mogo/utils/sqlite/IBaseDao.kt +++ b/foudations/mogo-utils/src/main/java/com/mogo/utils/sqlite/IBaseDao.kt @@ -23,12 +23,12 @@ interface IBaseDao { fun delete(where: T): Int /** - * 根据条件 [where] 进行数据更新,如果[where]==null 则代表删除所有数据 + * 根据条件 [where] 进行数据更新,如果[where]==只初始化不赋值 则代表删除所有数据 */ fun update(where: T, newEntity: T): Int /** - * 根据条件 [where] 进行数据查询,如果[where]==null 则代表查询所有数据 + * 根据条件 [where] 进行数据查询,如果[where]==只初始化不赋值 则代表查询所有数据 */ fun query(where: T): MutableList diff --git a/foudations/mogo-utils/src/main/java/com/mogo/utils/sqlite/SQLBaseDao.java b/foudations/mogo-utils/src/main/java/com/mogo/utils/sqlite/SQLBaseDao.java new file mode 100644 index 0000000000..a79fad7b94 --- /dev/null +++ b/foudations/mogo-utils/src/main/java/com/mogo/utils/sqlite/SQLBaseDao.java @@ -0,0 +1,421 @@ +package com.mogo.utils.sqlite; + +import android.content.ContentValues; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; + +import com.mogo.utils.logger.Logger; +import com.mogo.utils.sqlite.annotation.DbField; +import com.mogo.utils.sqlite.annotation.DbTable; + +import org.jetbrains.annotations.NotNull; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * 数据库操作 + * + * @param + * @author donghongyu + */ +public class SQLBaseDao implements IBaseDao { + private String TAG = "SQLBaseDao"; + + //数据库操作的引用 + private SQLiteDatabase sqLiteDatabase; + //要操作的数据实体的引用 + private Class entityClass; + //要操作的数据表名称 + private String tableName; + //记录数据表是否存在 + private boolean isInit = false; + + //因为反射会消耗时间,这里使用缓存,进行性能优化 + //缓存空间(key-字段名,标注的自定义注解 value-成员变量) + private HashMap cacheField; + + @Override + public boolean init(@NotNull SQLiteDatabase sqLiteDatabase, @NotNull Class entityClass) { + this.sqLiteDatabase = sqLiteDatabase; + this.entityClass = entityClass; + //自动建表(只创建一次) + if (!isInit) { + //获取表名 + tableName = entityClass.getAnnotation(DbTable.class).tableName(); + + //如果数据库没有建立连接跳出操作防止异常信息 + if (!sqLiteDatabase.isOpen()) { + return false; + } + + //执行Sql进行自动建表 + String createTableSql = getCreateTableSql(); + Logger.d(TAG, "执行SQL:" + createTableSql); + sqLiteDatabase.execSQL(createTableSql); + + //初始化缓存空间 + cacheField = new HashMap(); + initCacheField(); + + //标记已经创建过数据表 + isInit = true; + } + + return isInit; + } + + /** + * 插入数据 + */ + @Override + public long insert(T entity) { + //1、准备好ContentValues中的数据 + //2、设置插入的内容 + ContentValues values = getContentValuesForInsert(entity); + //3、执行插入 + long result; + if (sqLiteDatabase != null && sqLiteDatabase.isOpen()) { + result = sqLiteDatabase.insert(tableName, null, values); + } else { + result = -1; + } + return result; + } + + /** + * 删除数据 + */ + @Override + public int delete(T where) { + Condition condition = new Condition(getContentValuesForQuery(where)); + int result; + if (sqLiteDatabase != null && sqLiteDatabase.isOpen()) { + //受影响行数 + result = sqLiteDatabase.delete( + tableName, + condition.getWhereCause(), + condition.getWhereArgs() + ); + } else { + result = -1; + } + return result; + } + + /** + * 更新数据 + */ + @Override + public int update(T where, T newEntity) { + Condition condition = new Condition(getContentValuesForQuery(where)); + int result; + if (sqLiteDatabase != null && sqLiteDatabase.isOpen()) { + //受影响行数 + result = sqLiteDatabase.update( + tableName, + getContentValuesForInsert(newEntity), + condition.getWhereCause(), + condition.getWhereArgs() + ); + } else { + result = -1; + } + return result; + } + + /** + * 查询数据 + * + * @param where 查询条件对象,同时也用来初始化对象使用 + */ + @NotNull + @Override + public List query(T where) { + return query(where, null, null, null); + } + + /** + * 查询数据 + * + * @param where 查询条件对象 + * @param orderBy 排序规则 + * @param startIndex 开始的位置 + * @param limit 限制查询得到的数据个数 + */ + public ArrayList query(T where, String orderBy, Integer startIndex, Integer limit) { + //拼接分页语句 + String limitString = null; + if (startIndex != null && limit != null) { + limitString = startIndex + "," + limit; + } + Condition condition = new Condition(getContentValuesForQuery(where)); + Cursor cursor = null; + //定义查询结果 + ArrayList result = new ArrayList<>(); + if (sqLiteDatabase != null && sqLiteDatabase.isOpen()) { + try { + //查询数据库 + cursor = sqLiteDatabase.query( + tableName, + null, + condition.getWhereCause(), + condition.getWhereArgs(), + null, + null, + orderBy, + limitString + ); + //将查到结果添加到返回集合中 + result.addAll(getQueryResult(cursor, where)); + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (cursor != null) { + cursor.close(); + } + } + } + return result; + } + + /** + * 获取查询db结果 + */ + private ArrayList getQueryResult(Cursor cursor, T where) { + //定义查询结果 + ArrayList result = new ArrayList<>(); + //Cursor从头读到尾 + //游标从头读到尾 + cursor.moveToFirst(); + try { + if (where != null) { + //移动游标获取下一行数据 + while (!cursor.isAfterLast()) { + //通过反射构建一个查询结果对象 + T item = (T) where.getClass().newInstance(); + Set> fieldIterator = cacheField.entrySet(); + for (Map.Entry stringFieldEntry : fieldIterator) { + //获取数据库字段名称 + String columnName = stringFieldEntry.getKey(); + //数据库字段名对应的数据对象的成员变量 + Field field = stringFieldEntry.getValue(); + //获取指定列名对应的索引 + int columnIndex = cursor.getColumnIndex(columnName); + //获取成员变量数据类型 + Class fieldType = field.getType(); + if (columnIndex != -1) { + if (fieldType == (String.class)) { + field.set(item, cursor.getString(columnIndex)); + } else if (fieldType == (Integer.class)) { + field.set(item, cursor.getInt(columnIndex)); + } else if (fieldType == (Long.class)) { + field.set(item, cursor.getLong(columnIndex)); + } else if (fieldType == (Double.class)) { + field.set(item, cursor.getDouble(columnIndex)); + } else if (fieldType == (byte[].class)) { + field.set(item, cursor.getBlob(columnIndex)); + } else { + //未知类型 + throw new UnsupportedOperationException("未定义的数据类型:fieldName= " + columnName + " fieldType= " + fieldType); + } + } + } + //添加到结果集 + result.add(item); + //移动到下一个位置 + cursor.moveToNext(); + } + } + } catch (IllegalAccessException | InstantiationException e) { + e.printStackTrace(); + } + cursor.close(); + return result; + } + + /** + * 初始化字段缓存 + */ + private void initCacheField() { + //1.取到所有的列名(查询一个空表获取表结构,不影响性能) + if (sqLiteDatabase != null && sqLiteDatabase.isOpen()) { + String sqlQuery = "select * from " + tableName + " limit 1,0"; + Cursor cursor = sqLiteDatabase.rawQuery(sqlQuery, null); + //获取所有的列名 + String[] columnNames = cursor.getColumnNames(); + //关闭资源 + cursor.close(); + //2.取所有成员名 + Field[] columnFields = entityClass.getDeclaredFields(); + //3.通过两层循环,进行对应关系建立 + for (String columnName : columnNames) { + for (Field columnField : columnFields) { + if (columnName.equals(columnField.getAnnotation(DbField.class).fieldName())) { + columnField.setAccessible(true); + cacheField.put(columnName, columnField); + } + } + } + } + } + + /** + * 拼装创建数据表的SQL语句 + */ + private String getCreateTableSql() { + //create table if not exists tb_name(_id integer,name varchar2(20)) + StringBuffer sqlCreateTable = new StringBuffer(); + sqlCreateTable.append("create table if not exists "); + sqlCreateTable.append(tableName + " ("); + //反射获取所有的数据对象内的成员变量 + Field[] fields = entityClass.getDeclaredFields(); + + for (int index = 0; index < fields.length; index++) { + //字段名称 + String columnName = fields[index].getAnnotation(DbField.class).fieldName(); + //获取成员变量数据类型 + Class fieldType = fields[index].getType(); + + if (fieldType == (String.class)) { + sqlCreateTable.append(columnName).append(" TEXT,"); + } else if (fieldType == (Integer.class)) { + sqlCreateTable.append(columnName).append(" INTEGER,"); + } else if (fieldType == (Long.class)) { + sqlCreateTable.append(columnName).append(" BIGINT,"); + } else if (fieldType == (Double.class)) { + sqlCreateTable.append(columnName).append(" DOUBLE,"); + } else if (fieldType == (byte[].class)) { + sqlCreateTable.append(columnName).append(" BLOB,"); + } else { + //未知类型 + throw new UnsupportedOperationException("未定义的数据类型:fieldName= " + columnName + " fieldType= " + fieldType); + } + + if (index == fields.length - 1) { + if (sqlCreateTable.toString().endsWith(",")) { + sqlCreateTable.deleteCharAt(sqlCreateTable.length() - 1); + } + } + } + sqlCreateTable.append(")"); + return sqlCreateTable.toString(); + } + + /** + * 获取插入使用的ContentValues + */ + private ContentValues getContentValuesForInsert(T entity) { + ContentValues contentValues = new ContentValues(); + Set> fieldIterator = cacheField.entrySet(); + for (Map.Entry stringFieldEntry : fieldIterator) { + try { + //获取变量的值 + Object valueObject = stringFieldEntry.getValue().get(entity); + //获取列名 + String columnName = stringFieldEntry.getKey(); + //获取成员变量数据类型 + Class fieldType = stringFieldEntry.getValue().getType(); + + if (fieldType == (String.class)) { + contentValues.put(columnName, (String) valueObject); + } else if (fieldType == (Integer.class)) { + contentValues.put(columnName, (Integer) valueObject); + } else if (fieldType == (Long.class)) { + contentValues.put(columnName, (Long) valueObject); + } else if (fieldType == (Double.class)) { + contentValues.put(columnName, (Double) valueObject); + } else if (fieldType == (byte[].class)) { + contentValues.put(columnName, (byte[]) valueObject); + } else { + //未知类型 + throw new UnsupportedOperationException("未定义的数据类型:fieldName= " + columnName + " fieldType= " + fieldType); + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + Logger.d(TAG, "contentValues:" + contentValues); + return contentValues; + } + + /** + * 获取查询使用的ContentValues + */ + private ContentValues getContentValuesForQuery(T entity) { + ContentValues contentValues = new ContentValues(); + try { + if (entity != null) { + Set> fieldIterator = cacheField.entrySet(); + for (Map.Entry stringFieldEntry : fieldIterator) { + if (stringFieldEntry.getValue().get(entity) != null) { + contentValues.put(stringFieldEntry.getKey(), stringFieldEntry.getValue().get(entity).toString()); + } + } + } + } catch (IllegalAccessError | IllegalAccessException e) { + e.printStackTrace(); + } + + return contentValues; + } + + /** + * 条件拼接 + */ + static class Condition { + /** + * 条件拼接 + * _id=?&&name=? + */ + private String whereCause; + + private String[] whereArgs; + + //根据传入的contentValues转换成查询条件 + public Condition(ContentValues whereContent) { + //记录后面填充到查询语句“?”上的数据参数 + ArrayList argList = new ArrayList<>(); + //拼接查询语句 + StringBuilder whereCaseSb = new StringBuilder(); + + /* + * 是为了链接下面的查询条件条件,也或者是替换没有查询条件的语句。 + * 比如:要把检索条件作为一个参数传递给SQL, + * 那么,当这个检索语句不存在的话就可以给它赋值为1=1. + * 这样就避免了SQL出错,也就可以把加条件的SQL和不加条件的SQL合二为一。 + */ + whereCaseSb.append(" 1=1 "); + + Set keys = whereContent.keySet(); + + //因为使用了“1=1”,所以即便是这里没有任何数据拼接,也是可以正常 + for (String key : keys) { + Object valueObject = whereContent.get(key); + if (valueObject != null) { + String value = (String) valueObject; + //拼接查询条件语句 + //1:1 and _id=? and name=? + whereCaseSb.append(" and " + key + " =?"); + //记录?对应的value + argList.add(value); + } + } + //集合转成数组 + this.whereArgs = (String[]) argList.toArray(new String[argList.size()]); + this.whereCause = whereCaseSb.toString(); + } + + public String getWhereCause() { + return this.whereCause; + } + + public String[] getWhereArgs() { + return this.whereArgs; + } + } +} diff --git a/foudations/mogo-utils/src/main/java/com/mogo/utils/sqlite/proxy/BaseDaoProxyLog.kt b/foudations/mogo-utils/src/main/java/com/mogo/utils/sqlite/proxy/BaseDaoProxyLog.kt index d65dd810dd..b389cb86d7 100644 --- a/foudations/mogo-utils/src/main/java/com/mogo/utils/sqlite/proxy/BaseDaoProxyLog.kt +++ b/foudations/mogo-utils/src/main/java/com/mogo/utils/sqlite/proxy/BaseDaoProxyLog.kt @@ -1,6 +1,6 @@ package com.mogo.utils.sqlite.proxy -import android.util.Log +import com.mogo.utils.logger.Logger import java.lang.reflect.InvocationHandler import java.lang.reflect.Method import java.lang.reflect.Proxy @@ -24,8 +24,8 @@ class BaseDaoProxyLog : InvocationHandler { this.target = target //取得代理对象 return Proxy.newProxyInstance( - target.javaClass.classLoader, - target.javaClass.interfaces, this + target.javaClass.classLoader, + target.javaClass.interfaces, this ) } @@ -40,11 +40,11 @@ class BaseDaoProxyLog : InvocationHandler { override fun invoke(proxy: Any, method: Method, args: Array): Any? { var result: Any? = null //反射方法前调用 - Log.i("数据库代理", "当前执行>>${method.name}") + Logger.i("数据库代理", "当前执行>>${method.name}") //反射执行方法 相当于调用target.sayHelllo; result = method.invoke(target, *args) //反射方法后调用. - Log.i("数据库代理", "执行结果>>$result") + Logger.i("数据库代理", "执行结果>>$result") return result } } diff --git a/modules/mogo-module-common/src/main/java/com/mogo/module/common/entity/V2XHistoryScenarioData.java b/modules/mogo-module-common/src/main/java/com/mogo/module/common/entity/V2XHistoryScenarioData.java index 57e9953db1..046ca12db3 100644 --- a/modules/mogo-module-common/src/main/java/com/mogo/module/common/entity/V2XHistoryScenarioData.java +++ b/modules/mogo-module-common/src/main/java/com/mogo/module/common/entity/V2XHistoryScenarioData.java @@ -4,6 +4,8 @@ import com.mogo.utils.sqlite.annotation.DbDatabase; import com.mogo.utils.sqlite.annotation.DbField; import com.mogo.utils.sqlite.annotation.DbTable; +import java.util.Objects; + /** * V2X 道路历史事件 * diff --git a/modules/mogo-module-v2x/src/main/java/com/mogo/module/v2x/V2XModuleProvider.java b/modules/mogo-module-v2x/src/main/java/com/mogo/module/v2x/V2XModuleProvider.java index 05fa325662..1cc0229ed5 100644 --- a/modules/mogo-module-v2x/src/main/java/com/mogo/module/v2x/V2XModuleProvider.java +++ b/modules/mogo-module-v2x/src/main/java/com/mogo/module/v2x/V2XModuleProvider.java @@ -15,7 +15,6 @@ import com.alibaba.android.arouter.facade.annotation.Route; import com.mogo.map.MogoLatLng; import com.mogo.map.listener.IMogoMapListener; import com.mogo.map.location.IMogoLocationListener; -import com.mogo.map.marker.IMogoMarker; import com.mogo.map.marker.IMogoMarkerClickListener; import com.mogo.map.model.MogoPoi; import com.mogo.map.navi.IMogoNaviListener; @@ -34,6 +33,7 @@ import com.mogo.module.v2x.scenario.scene.park.V2XIllegalParkWindow; import com.mogo.module.v2x.scenario.scene.test.V2XTestConsoleWindow; import com.mogo.module.v2x.utils.FatigueDrivingUtils; import com.mogo.module.v2x.utils.TimeUtils; +import com.mogo.module.v2x.utils.V2XSQLiteUtils; import com.mogo.module.v2x.utils.V2XUtils; import com.mogo.module.v2x.voice.V2XVoiceManager; import com.mogo.service.module.IMogoModuleLifecycle; @@ -126,13 +126,10 @@ public class V2XModuleProvider implements V2XUtils.init(context); V2XServiceManager.init(context); - initVoice(context); - handleAdas(); + V2XVoiceManager.INSTANCE.init(context); + registerListener(); initData(); - - V2XServiceManager.getMogoRegisterCenter().registerMogoMapListener(V2XConst.MODULE_NAME, this); - // 注册广播接收场景弹窗使用的 SceneBroadcastReceiver localReceiver = new SceneBroadcastReceiver(); LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context); @@ -148,10 +145,6 @@ public class V2XModuleProvider implements } } - private void initVoice(Context context) { - V2XVoiceManager.INSTANCE.init(context); - } - private void initData() { try { // 查询ACC状态 @@ -171,33 +164,14 @@ public class V2XModuleProvider implements } // 刷新配置文件 refreshStrategyConfig(); - - // 响应违章停车的POI点击 - V2XServiceManager - .getMogoRegisterCenter() - .registerMogoMarkerClickListener(V2XConst.MODULE_NAME, - new IMogoMarkerClickListener() { - @Override - public boolean onMarkerClicked(IMogoMarker marker) { - try { - MarkerExploreWay markerExploreWay = - (MarkerExploreWay) ((MarkerShowEntity) marker.getObject()).getBindObj(); - Logger.d(V2XConst.MODULE_NAME, - "V2X===违章停车:onMarkerClicked=" + markerExploreWay); - - ((V2XIllegalParkWindow) V2XIllegalParkScenario.getInstance() - .getV2XWindow()) - .show(markerExploreWay, false); - } catch (Exception e) { - e.printStackTrace(); - } - return true; - } - }); + // 清除V2X事件数据库中过期的数据,数据有效期为24小时 + V2XSQLiteUtils.clearYesterdayScenarioHistoryData(); } + /** + * 获取疲劳驾驶的配置 + */ private void refreshStrategyConfig() { - // 获取疲劳驾驶的配置 V2XServiceManager .getV2XRefreshModel() .getStrategyPush(new V2XRefreshCallback() { @@ -219,12 +193,39 @@ public class V2XModuleProvider implements }); } - private void handleAdas() { + /** + * 注册各种监听 + */ + private void registerListener() { + // 初始化监听 + V2XServiceManager.getMogoRegisterCenter() + .registerMogoMapListener(V2XConst.MODULE_NAME, this); V2XServiceManager.getMoGoStatusManager() .registerStatusChangedListener(MODULE_NAME, StatusDescriptor.ACC_STATUS, this); V2XServiceManager.getMoGoStatusManager() .registerStatusChangedListener(MODULE_NAME, StatusDescriptor.SEEK_HELPING, this); + + // 响应违章停车的POI点击 + V2XServiceManager + .getMogoRegisterCenter() + .registerMogoMarkerClickListener(V2XConst.MODULE_NAME, + marker -> { + try { + MarkerExploreWay markerExploreWay = + (MarkerExploreWay) ((MarkerShowEntity) marker.getObject()).getBindObj(); + Logger.d(V2XConst.MODULE_NAME, + "V2X===违章停车:onMarkerClicked=" + markerExploreWay); + + ((V2XIllegalParkWindow) V2XIllegalParkScenario.getInstance() + .getV2XWindow()) + .show(markerExploreWay, false); + } catch (Exception e) { + e.printStackTrace(); + } + return true; + }); + // 注册V2X场景Socket V2XServiceManager.getV2XSocketManager().registerSocketMessage(); // 开启自动刷新 @@ -233,7 +234,12 @@ public class V2XModuleProvider implements V2XServiceManager.getMapUIController().recoverLockMode(); } - private void handleSeekHelp(boolean isTrue) { + /** + * 刷新自车求助状态 + * + * @param isTrue + */ + private void refreshMeSeekHelp(boolean isTrue) { V2XMessageEntity entity = new V2XMessageEntity<>(); entity.setType(V2XMessageEntity.V2XTypeEnum.ALERT_CAR_FOR_HELP); entity.setContent(isTrue); @@ -256,16 +262,24 @@ public class V2XModuleProvider implements .putString(V2XConst.V2X_ACC_OFF_TIME_STR, TimeUtils.getNowString()); } } else if (descriptor == StatusDescriptor.SEEK_HELPING) { - handleSeekHelp(isTrue); + refreshMeSeekHelp(isTrue); } } + /** + * 是否是ACC ON操作 + * + * @return true-ACC ON | false - ACC OFF + */ public boolean isAccOn() { int accState = Settings.System.getInt(mContext.getContentResolver(), "mcu_state", -0x02); Logger.d(MODULE_NAME, "状态发生改变\ndescriptor:ACC_STSTUS" + "\nisTrue:" + accState); return accState == 1; } + /** + * 刷新自车求助状态 + */ private void initCarForHelpStatus() { //本地查询是否超时 V2XServiceManager.getV2XRefreshModel().getHelpSignal(new V2XRefreshCallback() { @@ -278,12 +292,12 @@ public class V2XModuleProvider implements //故障车 if (vehicleType == 4) { if (!V2XServiceManager.getMoGoStatusManager().isSeekHelping()) { - handleSeekHelp(true); + refreshMeSeekHelp(true); V2XServiceManager.getMoGoStatusManager().setSeekHelping(MODULE_NAME, true); } } else { if (V2XServiceManager.getMoGoStatusManager().isSeekHelping()) { - handleSeekHelp(false); + refreshMeSeekHelp(false); V2XServiceManager.getMoGoStatusManager().setSeekHelping(MODULE_NAME, false); } } diff --git a/modules/mogo-module-v2x/src/main/java/com/mogo/module/v2x/scenario/impl/V2XScenarioManager.java b/modules/mogo-module-v2x/src/main/java/com/mogo/module/v2x/scenario/impl/V2XScenarioManager.java index 1d7082587b..e19283252b 100644 --- a/modules/mogo-module-v2x/src/main/java/com/mogo/module/v2x/scenario/impl/V2XScenarioManager.java +++ b/modules/mogo-module-v2x/src/main/java/com/mogo/module/v2x/scenario/impl/V2XScenarioManager.java @@ -18,11 +18,10 @@ import com.mogo.module.v2x.scenario.scene.road.V2XRoadEventScenario; import com.mogo.module.v2x.scenario.scene.seek.V2XSeekHelpScenario; import com.mogo.module.v2x.utils.TimeUtils; import com.mogo.module.v2x.utils.ToastUtils; +import com.mogo.module.v2x.utils.V2XSQLiteUtils; import com.mogo.module.v2x.utils.V2XUtils; import com.mogo.utils.logger.Logger; import com.mogo.utils.network.utils.GsonUtil; -import com.mogo.utils.sqlite.BaseDaoFactory; -import com.mogo.utils.sqlite.IBaseDao; import java.util.HashMap; @@ -40,11 +39,6 @@ public class V2XScenarioManager implements IV2XScenarioManager { private AbsV2XScenario mV2XScenario; private HashMap mV2XScenarioSet = new HashMap<>(); - /** - * 场景数据管理 - */ - private static IBaseDao mScenarioDao; - private V2XScenarioManager() { } @@ -53,13 +47,13 @@ public class V2XScenarioManager implements IV2XScenarioManager { synchronized (V2XScenarioManager.class) { if (mV2XScenarioManager == null) { mV2XScenarioManager = new V2XScenarioManager(); - mScenarioDao = BaseDaoFactory.Companion.getInstance().getBaseDao(V2XUtils.getApp(), V2XHistoryScenarioData.class); } } } return mV2XScenarioManager; } + @Override public void handlerMessage(V2XMessageEntity v2XMessageEntity) { Logger.d(MODULE_NAME, "处理V2X场景:" + GsonUtil.jsonFromObject(v2XMessageEntity)); @@ -68,16 +62,16 @@ public class V2XScenarioManager implements IV2XScenarioManager { V2XUtils.runOnUiThread(() -> { // 提取之前存储的场景 if (v2XMessageEntity != null) { - /*try { + try { // 进行数据库存储 V2XHistoryScenarioData v2XHistoryScenarioData = new V2XHistoryScenarioData(); v2XHistoryScenarioData.setScenarioType(v2XMessageEntity.getType()); v2XHistoryScenarioData.setTriggerTime(TimeUtils.getNowMills()); v2XHistoryScenarioData.setEventJsonData(GsonUtil.jsonFromObject(v2XMessageEntity.getContent())); - mScenarioDao.insert(v2XHistoryScenarioData); + V2XSQLiteUtils.getScenarioHistoryDao().insert(v2XHistoryScenarioData); } catch (Exception e) { e.printStackTrace(); - }*/ + } // 广播给应用内部其它模块 Intent intent = new Intent(V2XConst.BROADCAST_SCENE_ACTION); diff --git a/modules/mogo-module-v2x/src/main/java/com/mogo/module/v2x/utils/V2XSQLiteUtils.java b/modules/mogo-module-v2x/src/main/java/com/mogo/module/v2x/utils/V2XSQLiteUtils.java new file mode 100644 index 0000000000..5ad640126e --- /dev/null +++ b/modules/mogo-module-v2x/src/main/java/com/mogo/module/v2x/utils/V2XSQLiteUtils.java @@ -0,0 +1,56 @@ +package com.mogo.module.v2x.utils; + +import com.mogo.module.common.entity.V2XHistoryScenarioData; +import com.mogo.module.v2x.V2XConst; +import com.mogo.utils.logger.Logger; +import com.mogo.utils.sqlite.BaseDaoFactory; +import com.mogo.utils.sqlite.IBaseDao; + +import java.util.List; + +/** + * 数据库操作工具类 + * + * @author donghongyu + */ +public class V2XSQLiteUtils { + /** + * 场景数据管理 + */ + private static IBaseDao mScenarioHistoryDao; + + /** + * 场景数据管理 + */ + public static IBaseDao getScenarioHistoryDao() { + if (mScenarioHistoryDao == null) { + synchronized (V2XSQLiteUtils.class) { + if (mScenarioHistoryDao == null) { + mScenarioHistoryDao = BaseDaoFactory.Companion.getInstance() + .getBaseDao(V2XUtils.getApp(), V2XHistoryScenarioData.class); + } + } + } + return mScenarioHistoryDao; + } + + /** + * 删除数据库中存储的昨天的数据 + */ + public static void clearYesterdayScenarioHistoryData() { + try { + List historyScenarioData = getScenarioHistoryDao().query(new V2XHistoryScenarioData()); + for (V2XHistoryScenarioData historyScenarioDatum : historyScenarioData) { + Long triggerTime = historyScenarioDatum.getTriggerTime(); + if (!TimeUtils.isToday(triggerTime)) { + int result = getScenarioHistoryDao().delete(historyScenarioDatum); + if (result > 0) { + Logger.d(V2XConst.MODULE_NAME, "删除过期数据成功"); + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } +}