yy1717
2020-02-26 432b34202aed7b34d7c1719d4a056651b656ec62
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package safeluck.drive.evaluation;
 
import android.app.Application;
import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
 
 
import androidx.annotation.NonNull;
import androidx.work.Data;
import androidx.work.OneTimeWorkRequest;
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.anyun.im_lib.ExecutorServiceFactory;
import com.anyun.im_lib.listener.IMSConnectStatusCallback;
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.util.Date;
import java.util.Random;
 
import safeluck.drive.evaluation.DB.WorkRoomDataBase;
import safeluck.drive.evaluation.DB.appstatusdb.AppStatusWorker;
import safeluck.drive.evaluation.DB.exam_status.ExamStatusInitWorker;
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.gps.GpsInfoWorker;
import safeluck.drive.evaluation.DB.rtktb.RTKConfig;
import safeluck.drive.evaluation.DB.rtktb.RTKWorkRepository;
import safeluck.drive.evaluation.cEventCenter.CEvent;
import safeluck.drive.evaluation.cEventCenter.CEventCenter;
import safeluck.drive.evaluation.im.IMSClientBootstrap;
import safeluck.drive.evaluation.im.IMSConnectStatusListener;
import safeluck.drive.evaluation.im.MessageProcessor;
import safeluck.drive.evaluation.platformMessage.JKMessage0100;
import safeluck.drive.evaluation.platformMessage.JKMessage0101;
import safeluck.drive.evaluation.platformMessage.JKMessage0203;
import safeluck.drive.evaluation.util.FileUtil;
import safeluck.drive.evaluation.util.SPUtils;
import safeluck.drive.evaluation.util.SystemUtil;
 
/**
 * 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;
    FailedProjRepository failedProjRepository;//失败项目表数据库
    RTKWorkRepository rtkWorkRepository;//保存RTK配置表
    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(true)
                    .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());
            failedProjRepository = new FailedProjRepository(this);
            rtkWorkRepository = new RTKWorkRepository(this);
            MyLog.i(TAG,"拷贝assert目录下的map" +
                    "和vechile json到包目录下");
            new Thread(new Runnable() {
                @Override
                public void run() {
                    FileUtil.copyAssertFileToSD(getApplicationContext(),Constant.MAP);
                    FileUtil.copyAssertFileToSD(getApplicationContext(),Constant.VEHICLE);
                }
            }).start();
 
 
 
        }
 
    }
 
    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;
    @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:
                sendVechileInfo();
                sendMapInfo();
                CEventCenter.dispatchEvent(Constant.BIND_CONNECT_RTK_TOPIC,cmd,0,json);
 
                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)){
                    JKMessage0203 jkMessage0203 = new JKMessage0203();
                    jkMessage0203.phone = "0314200100000004";
                    jkMessage0203.timeBCD = new Date();
                    jkMessage0203.fail_item_id = 4;
                    jkMessage0203.fail_score = 10;
                    jkMessage0203.fail_reason = "发动机启动后,不及时松开启动开关";
                    jkMessage0203.ID = "123456789";
                    jkMessage0203.exam_id = 123456;
                    MessageProcessor.getInstance().sendMessage(jkMessage0203);
                    JsonArray jsonArray = JsonParser.parseString(json).getAsJsonArray();
                    for (int i = 0; i < jsonArray.size(); i++) {
                        JsonObject jsonObject = jsonArray.get(i).getAsJsonObject();
                        int emp_id = jsonObject.get("wrong_id").getAsInt();
                        if (emp_id>31|| emp_id<0){
                            MyLog.i(TAG,"emp_id超出范围不能插入数据库(I类考场)");
                            return;
                        }
                        failedProjRepository.insert(new FailedProj(Constant.SUBJECT_I, emp_id, Constant.TEST_STU_ID));
                    }
                }
 
                break;
            case Constant.EXAM_STATUS_REPLY:
                MyLog.d(TAG, "考试开始,复位数据库失败项目表");
 
                try {
                    JSONObject jsonObject =new JSONObject((String)json);
                    int errCode = jsonObject.getInt("error");
                    switch (errCode){
                        case -1:
                            sendMapInfo();
                            break;
                        case -2:
 
                            sendVechileInfo();
                            break;
                        case -3:
                            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:
                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:
                break;
            case Constant.IC_ID:
 
                CEventCenter.dispatchEvent(Constant.BIND_SPEED_TOPIC,cmd,0,json);
                break;
 
        }
 
    }
 
    private void sendVechileInfo() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                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() {
                StringBuffer 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()));
 
                }
            }
        }).start();
 
    }
 
 
}