package safeluck.drive.evaluation; import android.app.Application; import android.content.Context; import android.os.Environment; import android.os.Looper; import android.text.TextUtils; import android.util.Log; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.lifecycle.Observer; import androidx.work.Data; import androidx.work.OneTimeWorkRequest; import androidx.work.WorkInfo; import androidx.work.WorkManager; import com.anyun.exam.lib.AYSdk; import com.anyun.exam.lib.IAYExamListener; import me.yokeyword.fragmentation.Fragmentation; import me.yokeyword.fragmentation.helper.ExceptionHandler; import com.anyun.exam.lib.MyLog; import com.anyun.exam.lib.crash.CrashHandler; import com.facebook.stetho.Stetho; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.safeluck.aykj.utils.BytesUtils; import org.json.JSONException; import org.json.JSONObject; import java.io.File; import java.io.IOException; import java.util.Date; import java.util.Random; import okhttp3.ResponseBody; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import safeluck.drive.evaluation.DB.WorkRoomDataBase; import safeluck.drive.evaluation.DB.appstatusdb.AppStatusWorker; import safeluck.drive.evaluation.DB.criterias.CriteriaForI; import safeluck.drive.evaluation.DB.criterias.CriteriaForIII; import safeluck.drive.evaluation.DB.exam_status.ExamStatusInitWorker; import safeluck.drive.evaluation.DB.exam_status.ExamStatusOutWorker; import safeluck.drive.evaluation.DB.exam_status.ExamStatusWoker; import safeluck.drive.evaluation.DB.failitems.FailedProj; import safeluck.drive.evaluation.DB.failitems.FailedProjRepository; import safeluck.drive.evaluation.DB.failitems.FailedProj_select; import safeluck.drive.evaluation.DB.failitems.LuKaoFailedProj; import safeluck.drive.evaluation.DB.gps.GpsInfoWorker; import safeluck.drive.evaluation.DB.rtktb.RTKConfig; import safeluck.drive.evaluation.DB.rtktb.RTKConfigUpdateWorker; import safeluck.drive.evaluation.DB.rtktb.RTKWorkRepository; import safeluck.drive.evaluation.bean.ExamMap; import safeluck.drive.evaluation.bean.ExamPlatformData; import safeluck.drive.evaluation.cEventCenter.CEvent; import safeluck.drive.evaluation.cEventCenter.CEventCenter; import safeluck.drive.evaluation.httpmodule.HttpRequetBean; import safeluck.drive.evaluation.httpmodule.RetrofitCreator; import safeluck.drive.evaluation.im.IMSClientBootstrap; import safeluck.drive.evaluation.im.IMSConnectStatusListener; import safeluck.drive.evaluation.im.MessageProcessor; import safeluck.drive.evaluation.platformMessage.AttachInfo; import safeluck.drive.evaluation.platformMessage.JKMessage0100; import safeluck.drive.evaluation.platformMessage.JKMessage0101; import safeluck.drive.evaluation.platformMessage.JKMessage0203; import safeluck.drive.evaluation.platformMessage.JKMessage0206; import safeluck.drive.evaluation.platformMessage.PlatFormConstant; import safeluck.drive.evaluation.util.CThreadPoolExecutor; import safeluck.drive.evaluation.util.FileUtil; import safeluck.drive.evaluation.util.SPUtils; import safeluck.drive.evaluation.util.SystemUtil; import safeluck.drive.evaluation.util.Utils; import safeluck.drive.evaluation.viewmodels.TimeViewModel; import safeluck.drive.evaluation.worker.MCUUpgradeWorker; import safeluck.drive.evaluation.worker.TTSWorker; /** * MyApplication2 * Created by lzw on 2019/3/15. 10:53:52 * 邮箱:632393724@qq.com * All Rights Saved! Chongqing AnYun Tech co. LTD */ public class app extends Application implements IAYExamListener { private static final String TAG = "app"; private Gson gson; RTKConfig rtkConfig; Random random = new Random(); private static Context appContext=null; @Override public void onCreate() { super.onCreate(); //初始化Fragment 建议在Application onCreate里面初始化 if (SystemUtil.compareProcessName(this)) { Fragmentation.builder().stackViewMode(Fragmentation.BUBBLE).debug(false) .handleException(new ExceptionHandler() { @Override public void onException(@NonNull Exception e) { MyLog.i(TAG, "onException: " + e.getMessage()); } }) .install(); MyLog.delSubDirLogs(); CrashHandler crashHandler = CrashHandler.getInstance(); crashHandler.init(getApplicationContext()); AYSdk.getInstance().init(getApplicationContext()); AYSdk.getInstance().registListener(this); MyLog.createIfNotExist(); Stetho.initializeWithDefaults(this); appContext = this; //数据库操作 MyLog.i(TAG, "onCreate111"); FileUtil.createdirs(getApplicationContext()); //1.先检查sd卡包名的目录下,是否存在地图文件和车辆模型文件 //2. 如果不能再就去下载 } } public static Context getAppContext(){ return appContext; } @Override public void onTerminate() { super.onTerminate(); MyLog.e(TAG, "OnTerminate()"); AYSdk.getInstance().uninit(); } @Override public void onLowMemory() { super.onLowMemory(); MyLog.e(TAG, "OnTerminate()"); } private String lastStr; private String lastSn; @Override public void callBackMsg(final int cmd, String json) { String strConent = String.format("收到命令[%d],Json内容为%s", cmd, json); if (strConent.equalsIgnoreCase(lastStr)){ }else{ MyLog.d(TAG,strConent ); lastStr = strConent; } switch (cmd) { case Constant.NDK_START: OneTimeWorkRequest examStatausOutWorker = OneTimeWorkRequest.from(ExamStatusOutWorker.class); OneTimeWorkRequest mcuUpgradeWorker = OneTimeWorkRequest.from(MCUUpgradeWorker.class); WorkManager.getInstance(getApplicationContext()).beginWith(examStatausOutWorker).then(mcuUpgradeWorker).enqueue(); break; case Constant.RTK_PLATFORM_REGISTER_STATUS: CEventCenter.dispatchEvent(Constant.BIND_CONNECT_RTK_TOPIC,cmd,0,json); //RTK平台注册状态,需要保存数据库 break; case Constant.RTK_PLATFORM_REGISTER_RESULT: String[] strs = new String[2]; strs[0] = Constant.RTK_LOGIN_CODE_COLUMN; strs[1] = json; //RTK平台登录结果 Data rtkLoginData = new Data.Builder().putStringArray(Constant.APP_STATUS,strs).build(); OneTimeWorkRequest loginWorkRequest = new OneTimeWorkRequest.Builder(AppStatusWorker.class).setInputData(rtkLoginData).build(); WorkManager.getInstance(getApplicationContext()).enqueue(loginWorkRequest); break; case Constant.FETCH_RTK_PLATFORM_INFO: // CEventCenter.dispatchEvent(Constant.BIND_RTKCONFIG_TOPIC,cmd,0,""); break; case Constant.JUDGE_INFO: if(!TextUtils.isEmpty(json)){ final JKMessage0203 jkMessage0203 = new JKMessage0203(); // Data judgeData = new Data.Builder().putString(Constant.TTS,json).build(); final JsonArray jsonArray = (JsonArray) JsonParser.parseString(json); // // OneTimeWorkRequest ttswoker = new OneTimeWorkRequest.Builder(TTSWorker.class).setInputData(judgeData).build(); // WorkManager.getInstance(getAppContext()).enqueue(ttswoker); CThreadPoolExecutor.runInBackground(new Runnable() { @Override public void run() { for (int i = 0; i < jsonArray.size(); i++) { JsonObject jsonObject = jsonArray.get(i).getAsJsonObject(); int emp_id = jsonObject.get("wrong_id").getAsInt(); String utc = jsonObject.get("utc").getAsString(); int sn = jsonObject.get("sn").getAsInt(); if (emp_id>1000){ CriteriaForIII ttsStr=WorkRoomDataBase.getWorkRoomDataBase(getAppContext()).getCriteriaIIIDao().queryItemForCriteriaIIINoLive(emp_id-1000); ExamPlatformData.getInstance().getTTS().speak(ttsStr.getDeducting_reason() + (ttsStr.getScore_deducting() == 100? "不合格 " : "扣" + ttsStr.getScore_deducting() + "分 ")); jkMessage0203.fail_item_id =ttsStr.getItem_id(); jkMessage0203.fail_score = ttsStr.getScore_deducting(); jkMessage0203.fail_reason = ttsStr.getDeducting_reason(); }else{ CriteriaForI ttsStr = WorkRoomDataBase.getWorkRoomDataBase(getAppContext()).getCriteriaIDao().queryItemForCriteriaINoLive(emp_id); ExamPlatformData.getInstance().getTTS().speak(ttsStr.getDeducting_reason() + (ttsStr.getScore_deducting() == 100? "不合格 " : "扣" + ttsStr.getScore_deducting() + "分 ")); jkMessage0203.fail_item_id =ttsStr.getItem_id(); jkMessage0203.fail_score = ttsStr.getScore_deducting(); jkMessage0203.fail_reason = ttsStr.getDeducting_reason(); } if (!TextUtils.isEmpty(utc) && utc.length() > 14) { utc = utc.substring(0, 14); } Date date = new Date(Utils.utc2NetWorkTime(utc)); jkMessage0203.timeBCD = date; jkMessage0203.exam_id = ExamPlatformData.getInstance().getExam_id(); MyLog.i(String.format("收到远程服务给的评判消息 timeBCD=%s",Utils.formatTimeYYMMDDHHmmSS(jkMessage0203.timeBCD.getTime()))); MessageProcessor.getInstance().sendMessage(jkMessage0203); if (emp_id<1000) { //场地评判消息 if (emp_id > 31 || emp_id < 0) { MyLog.i("emp_id超出范围不能插入数据库(I类考场)"); }else{ FailedProj failedProj = new FailedProj(Constant.SUBJECT_I, emp_id, Constant.TEST_STU_ID, utc, sn); MyLog.i("插入fail_projects表=$failedProj"); WorkRoomDataBase.getWorkRoomDataBase(getApplicationContext()).getFailProjDao().insert(failedProj); } } if (emp_id>1000) { //道路 评判消息 if (emp_id >1066 || emp_id < 1000) { MyLog.i("emp_id超出范围不能插入数据库(路考类考场)"); }else{ LuKaoFailedProj failedProj = new LuKaoFailedProj(Constant.SUBJECT_III, (emp_id-1000), Constant.TEST_STU_ID, utc, sn); MyLog.i("插入路考fail_projects表=$failedProj"); WorkRoomDataBase.getWorkRoomDataBase(getApplicationContext()).getLuKaoFailProjDao().insert(failedProj); } } } } }); } break; case Constant.EXAM_STATUS_REPLY: MyLog.d(TAG, "考试开始,复位数据库失败项目表"); try { JSONObject jsonObject =new JSONObject((String)json); int errCode = jsonObject.getInt("error"); MyLog.i("收到考试应答错误码="+errCode); switch (errCode){ case -1: // sendMapInfo(); // break; case -2: // sendVechileInfo(); // break; case -3: Data data = new Data.Builder().putInt(Constant.NEED_MAPS_CAR,1).build(); OneTimeWorkRequest examStatausOutWorker1 = new OneTimeWorkRequest.Builder(ExamStatusOutWorker.class) .setInputData(data).build(); WorkManager.getInstance(getApplicationContext()).enqueue(examStatausOutWorker1); break; } } catch (JSONException e) { e.printStackTrace(); } break; case Constant.RTK_PLATFORM_CONNECT_STATUS: String[] conn_strs = new String[2]; conn_strs[0] = Constant.RTK_CONN_STATUS_COLUMN; conn_strs[1] = json; //RTK平台连接状态 Data connStatusData = new Data.Builder().putStringArray(Constant.APP_STATUS,conn_strs).build(); OneTimeWorkRequest oneTimeWorkRequest = new OneTimeWorkRequest.Builder(AppStatusWorker.class).setInputData(connStatusData).build(); WorkManager.getInstance(getApplicationContext()).enqueue(oneTimeWorkRequest); break; case Constant.FETCH_MAP_INFO: // sendMapInfo(); break; case Constant.REQ_VECHILE_PROFILE: sendVechileInfo(); break; case Constant.GPS_INFO: Data gpsData = new Data.Builder().putString(Constant.GPS_INFO_DATA,json).build(); OneTimeWorkRequest gpsinfoWorkRequest = new OneTimeWorkRequest.Builder(GpsInfoWorker.class).setInputData(gpsData).build(); WorkManager.getInstance(getApplicationContext()).enqueue(gpsinfoWorkRequest); break; case Constant.ENTER_OR_EXIT_ITEM: int type = 0; int enter_status = -1; try { JSONObject rtkConfigUpdtea = new JSONObject(json); type = rtkConfigUpdtea.getInt("type"); enter_status = rtkConfigUpdtea.getInt("enter"); String itemstr=ExamPlatformData.getInstance().getItemStatusStr(type); MyLog.i(PlatFormConstant.LUKAO,String.format("%s",itemstr+(enter_status==Constant.EXIT_CURRENT_ITEM?"结束":"开始"))); if (ExamPlatformData.getInstance().getTrainingMode()== ExamPlatformData.TRAINING_MODE){ if (ExamPlatformData.getInstance().getExamType()>ExamPlatformData.EXAM_TYPE_ChangKAO){ MyLog.i("路考不报项目开始结束语音"); }else{ ExamPlatformData.getInstance().getTTS().speak(itemstr+(enter_status==Constant.EXIT_CURRENT_ITEM?"结束":"开始")); } }else{ if (enter_status!=Constant.EXIT_CURRENT_ITEM){ if (ExamPlatformData.getInstance().getExamType()>ExamPlatformData.EXAM_TYPE_ChangKAO){ MyLog.i("路考不报项目开始结束语音"); }else{ ExamPlatformData.getInstance().getTTS().speak(itemstr+"开始"); } } } } catch (JSONException e) { e.printStackTrace(); } Data exam_enter_exitdata = new Data.Builder().putString(Constant.exam_enter_exitdata,json).build(); OneTimeWorkRequest examStatusWorker = new OneTimeWorkRequest.Builder(ExamStatusWoker.class).setInputData(exam_enter_exitdata).build(); WorkManager.getInstance(getApplicationContext()).enqueue(examStatusWorker); break; case Constant.REAL_TIME_CAR_POS: try { JSONObject jsSpeed = new JSONObject(json); double speed = jsSpeed.getDouble("speed"); CEventCenter.dispatchEvent(Constant.BIND_SPEED_TOPIC,cmd,0,speed); } catch (JSONException e) { e.printStackTrace(); } CEventCenter.dispatchEvent(Constant.REAL_TIME_POS_CAR_TOPIC,cmd,0,json); break; case Constant.DEBUG_RTCM: CEventCenter.dispatchEvent(Constant.BIND_RTCM_TOPIC,cmd,0,json); break; case Constant.DEBUG_TXT: CEventCenter.dispatchEvent(Constant.BIND_DEBUG_TXT,cmd,0,json); break; case Constant.MCU_SN: String sn = null; try { JSONObject rtkConfigUpdtea = new JSONObject(json); sn = rtkConfigUpdtea.getString("sn"); sn = rtkConfigUpdtea.put(safeluck.drive.evaluation.DB.Constant.RTK_CONFIG_SN,sn).toString(); } catch (JSONException e) { e.printStackTrace(); } if (sn.equalsIgnoreCase(lastSn)){ }else{ if (TextUtils.isEmpty(lastSn)){ lastSn ="123"; return; } lastSn = sn; Data data = new Data.Builder().putString(safeluck.drive.evaluation.DB.Constant.RTK_CONFIG_UPDATE_JSON,sn).build(); OneTimeWorkRequest rtkConfigUpdateWorker= new OneTimeWorkRequest.Builder(RTKConfigUpdateWorker.class).setInputData(data).build(); WorkManager.getInstance(getAppContext()).enqueue(rtkConfigUpdateWorker); } CEventCenter.dispatchEvent(Constant.BIND_MCUINFO_TOPIC,cmd,0,json); break; case Constant.IC_ID: CEventCenter.dispatchEvent(Constant.BIND_SPEED_TOPIC,cmd,0,json); break; case Constant.RTK_INFO: JSONObject jsonObject = null; String utc=null; try { jsonObject = new JSONObject(json); int qf = jsonObject.getInt("qf"); int satNum = jsonObject.getInt("sat_num"); //需要将utc时间取出来,然后+8个小时为北京时间,再修改json字符串,然后发给服务器 utc = jsonObject.getString("utc"); //移动站服务给的是.40 的话 代表是400毫秒 utc=Utils.utc2NetWorkTimeMillSeconds(utc+"0"); if (lastSatEqualNow(satNum)&&lastQfEqualNow(qf)){ }else{ ExamPlatformData.getInstance().setQfAndSatNum(qf,satNum); } } catch (JSONException e) { e.printStackTrace(); } if (ExamPlatformData.getInstance().getExamplatformStatus()==ExamPlatformData.DEV_LOGIN){ try { JSONObject jsSpeed = new JSONObject(json); double speed = jsSpeed.getDouble("speed"); CEventCenter.dispatchEvent(Constant.BIND_RTK_SPEED_TOPIC,cmd,0,speed); JKMessage0206 jkMessage0206 = new JKMessage0206(); jkMessage0206.alert = 0; jkMessage0206.status = 0; utc = utc.substring(0,utc.length()-1); json=json.replaceFirst("[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*",utc); jkMessage0206.json =json; jkMessage0206.length = jkMessage0206.json.length(); AttachInfo attachInfo = new AttachInfo(); attachInfo.attach_message_id = Integer.parseInt(String.valueOf(0x40)); attachInfo.attach_message_length = 4; if (attachInfo.attach_message_id == 0x41){ attachInfo.attach_data="00000000"; }else{ //18个字节 ascii attachInfo.attach_data="303030303030303030303030303030303030"; } jkMessage0206.attachInfo = attachInfo; if (ExamPlatformData.getInstance().getTrainingMode()==ExamPlatformData.MODE_NONE){ //判断10s时间是否到 if (ExamPlatformData.getInstance().isTimeArrive_10s()){ //到了 就走下面发送0206消息 MessageProcessor.getInstance().sendMessage(jkMessage0206); //重置标记 ExamPlatformData.getInstance().setTimeArrive_10s(false); }else{ //没到,就不走下面 不发送0206,直接return return; } }else{ MessageProcessor.getInstance().sendMessage(jkMessage0206); } } catch (JSONException e) { e.printStackTrace(); } }else{ String str = "登录未成功,不能发送消息=0206"; if (str.equalsIgnoreCase(last0206Str)){ }else{ last0206Str = str; MyLog.i(PlatFormConstant.TAG,str); } } break; case Constant.LEFT_RIGHT_DISTANCE: CEventCenter.dispatchEvent(Constant.BIND_RTK_SPEED_TOPIC,cmd,0,json); break; } } private String last0206Str=""; private int lastSatNum= 0; private int lastQf = 0; private boolean lastSatEqualNow(int satNum) { if (lastSatNum == satNum){ return true; }else{ lastSatNum = satNum; } return false; } private boolean lastQfEqualNow(int qf) { if (lastQf == qf){ return true; }else{ lastQf = qf; } return false; } private void sendVechileInfo() { new Thread(new Runnable() { @Override public void run() { String carPath = ExamPlatformData.getInstance().getCarModelPath(); if (!TextUtils.isEmpty(carPath)){ MyLog.i("使用更新后的Car模型地图"+carPath); byte[] bytes = FileUtil.readFile(carPath); if (bytes != null){ String str = new String(bytes); AYSdk.getInstance().sendCmd(Constant.PUSH_VECHILE_PROFILE,str); }else{ MyLog.i(String.format("文件:%s不存在",carPath)); } }else{ StringBuffer vebuffer =FileUtil.readAssetTxtFile(getApplicationContext(),Constant.VEHICLE); if (vebuffer != null){ AYSdk.getInstance().sendCmd(Constant.PUSH_VECHILE_PROFILE,vebuffer.toString()); }else{ MyLog.d(TAG,String.format("车辆模型模型未拷入[%s]目录下", Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+getApplicationContext().getPackageName())); } } } }).start(); } private void sendMapInfo(){ new Thread(new Runnable() { @Override public void run() { final String mapPath = ExamPlatformData.getInstance().getMapPath(); final String roadmapPath = ExamPlatformData.getInstance().getRoadMapPath(); if (!TextUtils.isEmpty(roadmapPath)){ MyLog.i("调用更新RoadMap路径后的地图"+roadmapPath); byte[] fileContent = FileUtil.readFile(roadmapPath); if (fileContent != null){ String str = new String(fileContent); Log.i(TAG, "文件内容:"+str); AYSdk.getInstance().sendCmd(Constant.PUSH_ROAD_MAP_INFO,str); }else{ MyLog.i(String.format("文件:%s不存在",roadmapPath)); CThreadPoolExecutor.runOnMainThread(new Runnable() { @Override public void run() { Toast.makeText(getAppContext(), "文件:"+roadmapPath+"不存在", Toast.LENGTH_SHORT).show(); } }); } }else{ StringBuffer buffer; buffer =FileUtil.readAssetTxtFile(getApplicationContext(),Constant.ROAD_MAP); if (buffer != null){ AYSdk.getInstance().sendCmd(Constant.PUSH_ROAD_MAP_INFO,buffer.toString()); }else{ MyLog.d(TAG,String.format("地图模型未拷入[%s]目录下", Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+getApplicationContext().getPackageName())); CThreadPoolExecutor.runOnMainThread(new Runnable() { @Override public void run() { Toast.makeText(getAppContext(), String.format("地图模型未拷入[%s]目录下", Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+getApplicationContext().getPackageName()), Toast.LENGTH_SHORT).show(); } }); } } if (!TextUtils.isEmpty(mapPath)){ MyLog.i("调用更新Map路径后的地图"+mapPath); byte[] fileContent = FileUtil.readFile(mapPath); if (fileContent != null){ String str = new String(fileContent); Log.i(TAG, "文件内容:"+str); AYSdk.getInstance().sendCmd(Constant.PUSH_MAP_INFO,str); }else{ MyLog.i(String.format("文件:%s不存在",mapPath)); CThreadPoolExecutor.runOnMainThread(new Runnable() { @Override public void run() { Toast.makeText(getAppContext(), "文件:"+mapPath+"不存在", Toast.LENGTH_SHORT).show(); } }); } }else{ MyLog.i("读取Assert目录下初始化的地图"); CThreadPoolExecutor.runOnMainThread(new Runnable() { @Override public void run() { Toast.makeText(getAppContext(), "读取Assert目录下初始化的地图", Toast.LENGTH_SHORT).show(); } }); StringBuffer buffer; buffer =FileUtil.readAssetTxtFile(getApplicationContext(),Constant.MAP); if (buffer != null){ AYSdk.getInstance().sendCmd(Constant.PUSH_MAP_INFO,buffer.toString()); }else{ MyLog.d(TAG,String.format("地图模型未拷入[%s]目录下", Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+getApplicationContext().getPackageName())); CThreadPoolExecutor.runOnMainThread(new Runnable() { @Override public void run() { Toast.makeText(getAppContext(), String.format("地图模型未拷入[%s]目录下", Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+getApplicationContext().getPackageName()), Toast.LENGTH_SHORT).show(); } }); } } } }).start(); } }