app/src/main/java/safeluck/drive/evaluation/DB/rtktb/RTKConfigUpdateWorker.kt
@@ -18,6 +18,7 @@
import retrofit2.Response
import safeluck.drive.evaluation.DB.Constant
import safeluck.drive.evaluation.DB.WorkRoomDataBase
import safeluck.drive.evaluation.app
import safeluck.drive.evaluation.bean.ExamPlatformData
import safeluck.drive.evaluation.httpmodule.HttpCarRespBean
import safeluck.drive.evaluation.httpmodule.HttpRequetBean
@@ -58,109 +59,10 @@
            phone = sn
            Log.i(TAG,"phone="+phone)
            if (!FileUtil.isMapExist(applicationContext,"map.json")){
                MyLog.i("地图文件不存在,去平台取")
                getRestService().getMapsAndVehicleModel(HttpRequetBean(sn, "place")).enqueue(object : Callback<ResponseBody> {
                    override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
                        var str: String? = null
                        try {
                            str = response.body()!!.string()
                        } catch (e: IOException) {
                            e.printStackTrace()
                        }
                        Log.i(TAG, "服务器返回的body=$str")
            getMap(sn, "road")
            getMap(sn, "yard")
            getMap(sn, "car")
                        jsonObject = JSONObject(str)
                        val result = jsonObject!!.getBoolean("result")
                        if (result){
                            if (str!!.contains("road")){
                                val map = gson.fromJson<HttpRoadMapRsp>(str,HttpRoadMapRsp::class.java)
                                if (map.isResult){
                                    if (map.data != null){
                                        if (map.data.map_json.type.equals("road")){
                                            ExamPlatformData.getInstance().setNewRoadMapPath(Environment.getExternalStorageDirectory().absolutePath + "/"
                                                    + applicationContext.getPackageName() + "/roadmap.json")
                                            CThreadPoolExecutor.runInBackground(Runnable{
                                                str = gson.toJson(map.data.map_json)
                                                FileUtil.writeTxtFileToSD(applicationContext,"roadmap.json",str,"")
                                            })
                                        }
                                    }
                                }
                            }else{
                                val yard = gson.fromJson<HttpYardRsp>(str,HttpYardRsp::class.java)
                                if (yard.isResult){
                                    if (yard.data !=null){
                                        Log.i(TAG,"yard====")
                                        ExamPlatformData.getInstance().setNewMapPath(Environment.getExternalStorageDirectory().absolutePath + "/"
                                                + applicationContext.getPackageName() + "/yard.json")
                                        CThreadPoolExecutor.runInBackground(Runnable{
                                            str = gson.toJson(yard.data.map_json)
                                            FileUtil.writeTxtFileToSD(applicationContext,"yard.json",str,"")
                                        })
                                    }
                                }
                            }
                        }else{
                            Toast.makeText(applicationContext,jsonObject!!.getString("data"),Toast.LENGTH_SHORT).show()
                        }
                    }
                    override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
                        Log.i(TAG, t.localizedMessage)
                    }
                })
            }
            if (!FileUtil.isMapExist(applicationContext,"car.json")){
                MyLog.i("车辆模型文件不存在,去平台取")
                getRestService().getMapsAndVehicleModel(HttpRequetBean(sn, "car")).enqueue(object : Callback<ResponseBody> {
                    override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
                        var str: String? = null
                        try {
                            str = response.body()!!.string()
                            Log.i(TAG, "服务器返回的body=$str")
                            jsonObject = JSONObject(str)
                            val result = jsonObject!!.getBoolean("result")
                            if (result){
                                val car = gson.fromJson<HttpCarRespBean>(str,HttpCarRespBean::class.java)
                                if (car.isResult){
                                    if (car.data !=null){
                                        ExamPlatformData.getInstance().carModelPath = Environment.getExternalStorageDirectory().absolutePath + "/" +
                                                applicationContext.getPackageName() + "/car.json"
                                        Log.i(TAG, "解析之后的车模型=$str")
                                        CThreadPoolExecutor.runInBackground(Runnable{
                                            str = gson.toJson(car.data.map_json)
                                            FileUtil.writeTxtFileToSD(applicationContext,"car.json",str,"")
                                        })
                                    }
                                }
                            }else{
                                Toast.makeText(applicationContext,jsonObject!!.getString("data"),Toast.LENGTH_SHORT).show()
                            }
                        } catch (e: IOException) {
                            e.printStackTrace()
                        }
                    }
                    override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
                        Log.i(TAG, t.localizedMessage)
                    }
                })
            }
            MyLog.i(PlatFormConstant.TAG,"重置TCP连接")
            IMSClientBootstrap.getInstance().resetConnect();
@@ -173,6 +75,124 @@
        sendRtkConfigInfo(WorkRoomDataBase.getWorkRoomDataBase(applicationContext).rtkConfigDao._getRtkConfig())
        return Result.success()
    }
    private 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
        }
        if (!FileUtil.isMapExist(applicationContext,fileName)) {
            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) {
                        when(type){
                            "road" ->{
                                val map = gson.fromJson<HttpRoadMapRsp>(str, HttpRoadMapRsp::class.java)
                            if (map.isResult) {
                                if (map.data != null) {
                                    if (map.data.map_json.type.equals("road")) {
                                        ExamPlatformData.getInstance().setNewRoadMapPath(Environment.getExternalStorageDirectory().absolutePath + "/"
                                                + applicationContext.getPackageName() + "/" + fileName)
                                        CThreadPoolExecutor.runInBackground(Runnable {
                                            str = gson.toJson(map.data.map_json)
                                            FileUtil.writeTxtFileToSD(applicationContext, fileName, str, "")
                                            sendRoadMapInfo()
                                        })
                                    }
                                }
                            }}
                                "yard" ->{
                                    val yard = gson.fromJson<HttpYardRsp>(str,HttpYardRsp::class.java)
                                    if (yard.isResult){
                                        if (yard.data !=null){
                                            Log.i(TAG,"yard====")
                                            ExamPlatformData.getInstance().setNewMapPath(Environment.getExternalStorageDirectory().absolutePath + "/"
                                                    + applicationContext.getPackageName() + "/"+fileName)
                                            CThreadPoolExecutor.runInBackground(Runnable{
                                                str = gson.toJson(yard.data.map_json)
                                                FileUtil.writeTxtFileToSD(applicationContext,fileName,str,"")
                                                sendMapInfo()
                                            })
                                        }
                                    }
                                }
                            else ->{
                                val car = gson.fromJson<HttpCarRespBean>(str,HttpCarRespBean::class.java)
                                if (car.isResult){
                                    if (car.data !=null){
                                        ExamPlatformData.getInstance().carModelPath = Environment.getExternalStorageDirectory().absolutePath + "/" +
                                                applicationContext.getPackageName() + "/"+fileName
                                        Log.i(TAG, "解析之后的车模型=$str")
                                        CThreadPoolExecutor.runInBackground(Runnable{
                                            str = gson.toJson(car.data.map_json)
                                            FileUtil.writeTxtFileToSD(applicationContext,fileName,str,"")
                                            sendVehicleInfo()
                                        })
                                    }
                                }
                            }
                        }
                    } else {
                        Toast.makeText(applicationContext, jsonObject1!!.getString("data"), Toast.LENGTH_SHORT).show()
                    }
                }
            })
        }else{
            MyLog.i(PlatFormConstant.HTTPTAG,"已存在直接发送车辆和地图")
            when(type){
                "road" -> sendRoadMapInfo()
                "yard" -> sendMapInfo()
                else -> sendVehicleInfo()
            }
        }
    }
    private 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)
                Log.i(TAG,"文件内容:$str")
                AYSdk.getInstance().sendCmd(safeluck.drive.evaluation.Constant.PUSH_ROAD_MAP_INFO, str)
            } else {
                MyLog.i(String.format("文件:%s不存在", roadmapPath))
                CThreadPoolExecutor.runOnMainThread { Toast.makeText(app.getAppContext(), "文件:" + roadmapPath + "不存在", Toast.LENGTH_SHORT).show() }
            }
        } else {
            val buffer: StringBuffer?
            buffer = FileUtil.readAssetTxtFile(applicationContext, safeluck.drive.evaluation.Constant.ROAD_MAP)
            if (buffer != null) {
                AYSdk.getInstance().sendCmd(safeluck.drive.evaluation.Constant.PUSH_ROAD_MAP_INFO, buffer.toString())
            } else {
                MyLog.i(String.format("地图模型未拷入[%s]目录下", Environment.getExternalStorageDirectory().absolutePath + "/" + applicationContext.packageName))
                CThreadPoolExecutor.runOnMainThread { Toast.makeText(app.getAppContext(), String.format("地图模型未拷入[%s]目录下", Environment.getExternalStorageDirectory().absolutePath + "/" + applicationContext.packageName), Toast.LENGTH_SHORT).show() }
            }
        }
    }
    private fun sendRtkConfigInfo(mRTKConfig: RTKConfig) {
@@ -195,4 +215,55 @@
        }
    }
    private 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)
                Log.i(TAG,"文件内容:$str")
                AYSdk.getInstance().sendCmd(safeluck.drive.evaluation.Constant.PUSH_MAP_INFO, str)
            } else {
                MyLog.i(String.format("文件:%s不存在", mapPath))
                CThreadPoolExecutor.runOnMainThread { Toast.makeText(app.getAppContext(), "文件:" + mapPath + "不存在", Toast.LENGTH_SHORT).show() }
            }
        } else {
            MyLog.i("读取Assert目录下初始化的地图")
            var buffer: StringBuffer?
            buffer = FileUtil.readAssetTxtFile(applicationContext, safeluck.drive.evaluation.Constant.MAP)
            if (buffer != null) {
                AYSdk.getInstance().sendCmd(safeluck.drive.evaluation.Constant.PUSH_MAP_INFO, buffer.toString())
            } else {
                MyLog.i( String.format("地图模型未拷入[%s]目录下", Environment.getExternalStorageDirectory().absolutePath + "/" + applicationContext.packageName))
                CThreadPoolExecutor.runOnMainThread { Toast.makeText(app.getAppContext(), String.format("地图模型未拷入[%s]目录下", Environment.getExternalStorageDirectory().absolutePath + "/" + applicationContext.packageName), Toast.LENGTH_SHORT).show() }
            }
        }
    }
    private 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)
                AYSdk.getInstance().sendCmd(safeluck.drive.evaluation.Constant.PUSH_VECHILE_PROFILE, str)
            } else {
                MyLog.i(String.format("文件:%s不存在", carPath))
            }
        } else {
            val vebuffer = FileUtil.readAssetTxtFile(applicationContext, safeluck.drive.evaluation.Constant.VEHICLE)
            if (vebuffer != null) {
                AYSdk.getInstance().sendCmd(safeluck.drive.evaluation.Constant.PUSH_VECHILE_PROFILE, vebuffer.toString())
            } else {
                MyLog.i(String.format("车辆模型模型未拷入[%s]目录下", Environment.getExternalStorageDirectory().absolutePath + "/" + applicationContext.packageName))
            }
        }
    }
}