Admin
2021-02-22 f30e2f4dd073850abe4bec2624a18cefdabc2aad
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package safeluck.drive.evaluation.util
 
import android.content.Context
import android.os.Environment
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import androidx.work.Data
import com.anyun.basecommonlib.MyLog
import com.anyun.exam.lib.AYSdk
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.google.gson.stream.JsonReader
import okhttp3.ResponseBody
import org.json.JSONException
import org.json.JSONObject
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import safeluck.drive.evaluation.Constant
import safeluck.drive.evaluation.DB.WorkRoomDataBase
import safeluck.drive.evaluation.DB.rtktb.RTKConfig
import safeluck.drive.evaluation.DB.signalConfigdb.SingalConfig
import safeluck.drive.evaluation.app
import safeluck.drive.evaluation.bean.*
import safeluck.drive.evaluation.bean.SimulateNightBean.QuestionBean
import safeluck.drive.evaluation.httpmodule.*
import safeluck.drive.evaluation.httpmodule.RetrofitCreator.Companion.getRestService
import safeluck.drive.evaluation.platformMessage.PlatFormConstant
import java.io.File
import java.io.IOException
import java.io.InputStreamReader
import java.net.URLDecoder
import java.nio.charset.Charset
 
/**
 *
 * @ProjectName: DriveJudge
 * @Package: safeluck.drive.evaluation.util
 * @ClassName: DataInit
 * @Description: java类作用描述
 * @Author: 李占伟
 * @CreateDate: 20/7/26 下午4:44
 * @UpdateUser: 更新者
 * @UpdateDate: 20/7/26 下午4:44
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
 
fun MCUUpgrade(applicationContext:Context) {
    MyLog.i("升级单片机")
    try {
        val datas = FileUtil.readLocalFile(applicationContext, "dfu.bin")
        if (datas != null) {
            val strs = String(datas, Charset.forName("ISO-8859-1"))
            AYSdk.getInstance().sendCmd(Constant.UPGRADE_MCU_CONTENT_FILE, strs)
 
//            FileUtil.deleteFile("", Constant.MCU_FILE_NAME);
        } else {
 
            MyLog.i( "mcu升级文件不存在")
            var data = Data.Builder().putString(Constant.MCU_UPGRADE_FAIL_RESASON,"mcu升级文件不存在").build()
 
        }
    } catch (e: IOException) {
        e.printStackTrace()
    }
}
 
private val gson = Gson()
private val tempQs: MutableList<SimulateNightBean.QuestionBean> = java.util.ArrayList()
var  simulateNightBean:SimulateNightBean=SimulateNightBean()
fun sendExamLightsAndExamStatus(applicationContext: Context){
    MyLog.i("发送灯光")
 
    val examType = ExamPlatformData.getInstance().examType
    simulateNightBean.setExam(0)
 
    tempQs.clear()
    val list = WorkRoomDataBase.getWorkRoomDataBase(applicationContext).lightUseQuestionsDao.lightUseQuestionsNoLive()
    val listQuests=WorkRoomDataBase.getWorkRoomDataBase(applicationContext).lightAllQuestionsDao.allLightQuestions
    Log.i("AYJiaKao",list.size.toString())
    tempQs.add(QuestionBean(100, "下面将进行模拟夜间行驶场景灯光使用的考试,请按语音指令在5秒内做出相应的灯光操作"))//提示下面将进行灯光考试
 
//    if (list.size>5){
//        var questbean = QuestionBean(list[0].id,list[0].question)
//        questbean.parseSolutionAndProcess(list[0].answer)
//        questbean.wrong_code = listQuests[list[0].id].wrong_code
//        tempQs.add(questbean)
//        val a = Utils.getRandomInts(4, list.size)
//        for (i in a.indices) {
//            var te = list[a[i]]
//            Log.i("AYJiaKao",te.toString())
//            var questbean = QuestionBean(te.id,te.question)
//            questbean.parseSolutionAndProcess(te.answer)
//            questbean.wrong_code = listQuests[te.id].wrong_code
//            tempQs.add(questbean)
//
//        }
//    }else{
        for (i in list.indices){
 
        var questbean = QuestionBean(list[i].id,list[i].question)
        questbean.parseSolutionAndProcess(list[i].answer)
            questbean.wrong_code = listQuests[i].wrong_code
        tempQs.add(questbean)
    }
//    }
 
    tempQs.add(QuestionBean(101, "模拟夜间考试完成请关闭所有灯光,请起步继续完成考试"))//提示灯光考试结束
    simulateNightBean.setQuestion(tempQs)
 
    MyLog.i("开始发送灯光,考试项共有"+simulateNightBean.question.size)
    MyLog.i("开始发送灯光,gson="+gson.toJson(simulateNightBean))
    AYSdk.getInstance().sendCmd(0x8012, gson.toJson(simulateNightBean))
 
    //发送考试状态
    MyLog.i("查到的考试type=$examType")
    try {
        val jsonObject = JSONObject()
        if (examType == safeluck.drive.evaluation.Constant.NONE_BEEN_START_EXAM) {
            jsonObject.put("exam", 0)
        }else{
            jsonObject.put("exam", 1)
        }
 
        jsonObject.put("type", examType)
        val examJson = jsonObject.toString()
        AYSdk.getInstance().sendCmd(safeluck.drive.evaluation.Constant.EXAM_STATUS, examJson)
    } catch (e: JSONException) {
        e.printStackTrace()
    }
 
}
 
 
 
fun sendRtkConfig(applicationContext: Context){
    var rtk =  WorkRoomDataBase.getWorkRoomDataBase(applicationContext).rtkConfigDao._getRtkConfig()
    if (rtk == null){
        try {
            MyLog.i("读文件发送RTK配置")
            val inputStream = applicationContext.assets.open(safeluck.drive.evaluation.Constant.RTK_CONFIG_JSON)
            val inputStreamReader = InputStreamReader(inputStream)
            val jsonReader = JsonReader(inputStreamReader)
            val gson = Gson()
            val type = object : TypeToken<RTKConfig?>() {}.type
            val mstus = gson.fromJson<RTKConfig>(jsonReader, type)
            rtk = mstus
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }else{
 
    }
    sendRtkConfigInfo(rtk)
}
 
private var signalConfiglist: MutableList<SignalConfigRemote> = ArrayList()
private fun sendRtkConfigInfo(mRTKConfig: RTKConfig) {
    if (mRTKConfig != null) {
        var rtkjson = gson.toJson(mRTKConfig)
        //去除id字段
        var jsonObject: JSONObject? = null
        try {
            jsonObject = JSONObject(rtkjson)
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        jsonObject!!.remove("_id")
        rtkjson = null
        rtkjson = jsonObject.toString()
        MyLog.i("NDK_start RTK配置信息")
        AYSdk.getInstance().sendCmd(safeluck.drive.evaluation.Constant.PUSH_RTK_PLATFORM_INFO, rtkjson)
    } else {
        MyLog.i("RTKConfig未取到数据")
    }
}
 
private var singalConfigs :MutableList<SingalConfig> =  mutableListOf()
 
fun sendSignalConfigToRemote(applicationContext: Context){
    MyLog.i("sendSignalConfigToRemote")
    singalConfigs.clear()
    signalConfiglist.clear()
    singalConfigs = WorkRoomDataBase.getWorkRoomDataBase(applicationContext).signalConfigDao._getSignalConfigs()
    if (singalConfigs ==null){
        MyLog.i("读文件发送信号配置")
        try {
            val inputStream = applicationContext.assets.open("signal_config_file.json")
            val inputStreamReader = InputStreamReader(inputStream)
            val jsonReader = JsonReader(inputStreamReader)
            val gson = Gson()
            val type = object : TypeToken<List<SingalConfig?>?>() {}.type
            val mstus = gson.fromJson<List<SingalConfig>>(jsonReader, type)
            for (j in mstus.indices){
                singalConfigs[j] = mstus[j]
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }else{
        MyLog.i("读到数据库里面的信号配置"+ signalConfiglist.size);
    }
 
    for (i in singalConfigs.indices) {
        val signalConfigRemote = SignalConfigRemote()
        signalConfigRemote.func_id = singalConfigs.get(i).func_id
        signalConfigRemote.gpio_num = singalConfigs.get(i).getIndex()-1
        signalConfigRemote.level = singalConfigs.get(i).getHighLevel()
        signalConfiglist.add(signalConfigRemote)
    }
 
 
    var indexs = WorkRoomDataBase.getWorkRoomDataBase(applicationContext).signalConfigDao.allIndexs
    sendSignalConfigsToRemote_(indexs)
}
 
private fun sendSignalConfigsToRemote_(indexs: List<Int>) {
    if (signalConfiglist.size > 0) {
        if (listContainsSameValue(indexs)) {
            MyLog.i("sendSignalConfigsToRemote: 有相同的index,不能发送" + gson.toJson(signalConfiglist))
        } else {
            MyLog.i("调用发送命令接口发送信号配置")
            AYSdk.getInstance().sendCmd(safeluck.drive.evaluation.Constant.SEND_CONFIG_SIGNAL, gson.toJson(signalConfiglist))
        }
    }
}
 
private fun listContainsSameValue(indexs: List<Int>): Boolean {
    for (i in indexs.indices) { //可以默认是0  0不判断是否有相同的index
        if (indexs[i] == 0) {
            continue
        }
        for (j in i + 1 until indexs.size) {
            if (indexs[i] === indexs[j]) {
                return true
            }
        }
    }
    return false
}
 
 
 
 
 fun getMap(sn: String, type: String){
    lateinit var jsonObject1 :JSONObject
    lateinit var fileName :String
    when(type){
        "road" -> fileName =  safeluck.drive.evaluation.Constant.ROAD_MAP
        "yard" -> fileName =  safeluck.drive.evaluation.Constant.MAP
        else -> fileName = safeluck.drive.evaluation.Constant.VEHICLE
    }
        getRestService().getMapsAndVehicleModel(HttpRequetBean(sn, type)).enqueue(object : Callback<ResponseBody> {
            override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
                MyLog.i(PlatFormConstant.HTTPTAG,t.localizedMessage)
            }
 
            override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
                var str: String = response.body()!!.string()
                MyLog.i(PlatFormConstant.HTTPTAG,str)
                jsonObject1 = JSONObject(str)
                val result = jsonObject1!!.getBoolean("result")
                if (result) {
                    var mapInfoHead: MapInfoHead = MapInfoHead("","", BaseDataUIBean.TYPE_.car,"","")
 
                    when(type){
                        "road" ->{
                            mapInfoHead.type = BaseDataUIBean.TYPE_.road
                            val map = gson.fromJson<HttpRoadMapRsp>(str, HttpRoadMapRsp::class.java)
                            if (map.isResult) {
                                if (map.data != null) {
                                    mapInfoHead.version = map.data.version
 
                                    if (map.data.map_json.type.equals("road")) {
                                        mapInfoHead.str = map.data.map_json.name
                                        mapInfoHead.gps_moinitor_url =  URLDecoder.decode( map.data.gps_monitor_url,"UTF-8")
                                        mapInfoHead.arcgis_url = map.data.map_url
                                        ExamPlatformData.getInstance().setNewRoadMapPath(Environment.getExternalStorageDirectory().absolutePath + "/"
                                                + app.getAppContext().getPackageName() + "/" + fileName)
                                        CThreadPoolExecutor.runInBackground(Runnable {
 
                                            str = gson.toJson(map.data.map_json)
                                            MyLog.i(PlatFormConstant.HTTPTAG,"科三道路地图保存成功")
                                            FileUtil.writeTxtFileToSD(app.getAppContext(), fileName, str, "")
                                            sendRoadMapInfo()
                                        })
                                    }
 
 
                                }
 
 
                            }else{
                                MyLog.i(PlatFormConstant.HTTPTAG,"返回失败,则发送用户已经选择的道路文件")
                                sendRoadMapInfo()
                            }}
                        "yard" ->{
                            mapInfoHead.type = BaseDataUIBean.TYPE_.yard
                            val yard = gson.fromJson<HttpYardRsp>(str,HttpYardRsp::class.java)
                            if (yard.isResult){
                                if (yard.data !=null){
                                    mapInfoHead.version = yard.data.version
                                    mapInfoHead.gps_moinitor_url =             URLDecoder.decode(yard.data.gps_monitor_url,"UTF-8")
 
                                    mapInfoHead.str = yard.data.map_json.name
                                    mapInfoHead.arcgis_url = yard.data.map_url
                                    ExamPlatformData.getInstance().setNewMapPath(Environment.getExternalStorageDirectory().absolutePath + "/"
                                            + app.getAppContext().getPackageName() + "/"+fileName)
                                    CThreadPoolExecutor.runInBackground(Runnable{
                                        MyLog.i(PlatFormConstant.HTTPTAG,"场地地图保存成功")
                                        str = gson.toJson(yard.data.map_json)
                                        FileUtil.writeTxtFileToSD(app.getAppContext(),fileName,str,"")
                                        sendMapInfo()
                                    })
                                }
                            }else{
                                MyLog.i(PlatFormConstant.HTTPTAG,"返回失败,则发送用户已经选择的场地文件")
                                sendMapInfo()
                            }
                        }
                        else ->{
                            mapInfoHead.type = BaseDataUIBean.TYPE_.car
                            val car = gson.fromJson<HttpCarRespBean>(str,HttpCarRespBean::class.java)
                            val gcar = gson.fromJson<HttpGisCarRespBean>(str, HttpGisCarRespBean::class.java)
//                        car.isResult = false
                            if (car.isResult){
                                if (car.data !=null){
                                    mapInfoHead.version = car.data.version
                                    mapInfoHead.str = car.data.map_json.name
                                    ExamPlatformData.getInstance().carModelPath = Environment.getExternalStorageDirectory().absolutePath + "/" +
                                            app.getAppContext().getPackageName() + "/"+fileName
 
//                                    Log.i(TAG, "解析之后的车模型=$str")
                                    CThreadPoolExecutor.runInBackground(Runnable{
                                        str = gson.toJson(car.data.map_json)
                                        var giscarStr = gson.toJson(gcar.data.map_json)
                                        MyLog.i(PlatFormConstant.HTTPTAG,"车辆模型保存成功")
                                        FileUtil.writeTxtFileToSD(app.getAppContext(),fileName,str,"")
                                        FileUtil.writeTxtFileToSD(app.getAppContext(),"gis"+fileName,giscarStr,"")
                                        sendVehicleInfo()
                                    })
 
                                }
                            }else{
                                MyLog.i(PlatFormConstant.HTTPTAG,"返回失败,则发送用户已经选择的车辆模型文件")
                                sendVehicleInfo()
                            }
                        }
                    }
                    ExamPlatformData.getInstance().setMapHeadInfo(mapInfoHead)
                } else {
                    if (str.contains("car")){
                        sendVehicleInfo()
                    }
                    if (str.contains("yard")){
                        sendMapInfo()
                    }
                    if (str.contains("road")){
 
 
                        sendRoadMapInfo()
                    }
 
//                        Toast.makeText(applicationContext, jsonObject1!!.getString("data"), Toast.LENGTH_SHORT).show()
                }
            }
        })
 
}
 
 fun sendMapInfo(){
    val mapPath = ExamPlatformData.getInstance().mapPath
 
 
    if (!TextUtils.isEmpty(mapPath)) {
        MyLog.i("更新Map的地图$mapPath")
        val fileContent = FileUtil.readFile(mapPath)
        if (fileContent != null) {
            val str = String(fileContent)
            sendMapSucc = true
            AYSdk.getInstance().sendCmd(safeluck.drive.evaluation.Constant.PUSH_MAP_INFO, str)
        } else {
            sendMapSucc = false
            MyLog.i(String.format("文件:%s不存在", mapPath))
            CThreadPoolExecutor.runOnMainThread { Toast.makeText(app.getAppContext(), "文件:" + mapPath + "不存在", Toast.LENGTH_SHORT).show() }
        }
    }else{
        sendMapSucc = false
    }
}
//标记车辆模型或地图 发送成功的标志-true
var sendVehiSucc = false
var sendRoadMapSucc= false
var sendMapSucc= false
 fun sendVehicleInfo(){
    val carPath = ExamPlatformData.getInstance().carModelPath
    if (!TextUtils.isEmpty(carPath)) {
        MyLog.i("更新后的Car模型$carPath")
        val bytes = FileUtil.readFile(carPath)
        if (bytes != null) {
            val str = String(bytes)
            sendVehiSucc = true
            AYSdk.getInstance().sendCmd(safeluck.drive.evaluation.Constant.PUSH_VECHILE_PROFILE, str)
        } else {
            MyLog.i(String.format("文件:%s不存在", carPath))
            sendVehiSucc = false
        }
    }else{
        sendVehiSucc = false
    }
}
 fun sendRoadMapInfo(){
    val roadmapPath = ExamPlatformData.getInstance().roadMapPath
 
    if (!TextUtils.isEmpty(roadmapPath)) {
        MyLog.i("更新RoadMap地图$roadmapPath")
        val fileContent = FileUtil.readFile(roadmapPath)
        if (fileContent != null) {
            val str = String(fileContent)
            sendRoadMapSucc = true
            AYSdk.getInstance().sendCmd(safeluck.drive.evaluation.Constant.PUSH_ROAD_MAP_INFO, str)
        } else {
            MyLog.i(String.format("文件:%s不存在", roadmapPath))
            sendRoadMapSucc = false
            CThreadPoolExecutor.runOnMainThread { Toast.makeText(app.getAppContext(), "文件:" + roadmapPath + "不存在", Toast.LENGTH_SHORT).show() }
        }
    } else {
        sendRoadMapSucc = false
    }
}
 
 
 fun sendJudgeArgs() {
    val dir = File(Environment.getExternalStorageDirectory().absolutePath + File.separator + app.getAppContext().getPackageName())
    val names = ExamPlatformData.getRulesettingFilesName()
    for (i in names.indices) {
        val settFile = File(dir, names[i])
        if (settFile.exists()) {
            Log.i("app.TAG", settFile.name + "存在不复制")
        } else {
            Log.i("app.TAG", settFile.name + "不存在,复制...")
            FileUtil.copyAssertFileToSD(app.getAppContext().applicationContext, names[i])
        }
    }
    if (dir.exists()) {
        val files = dir.listFiles()
        for (i in files.indices) {
            Log.i("app.TAG", files[i].name+" names.size="+names.size)
            if (names.contains(files[i].name)) {
                val buffer = FileUtil.readFile(files[i].absolutePath)
                var str =String(buffer)
                AYSdk.getInstance().sendCmd(Constant.ID_judge_args, str)
            } else {
            }
        }
    }
}