lizhanwei
2020-04-23 b1241719066f2636d6e30be525cef8bcc6d30619
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
package safeluck.drive.evaluation.DB.rtktb
 
import android.content.Context
import android.os.Environment
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import androidx.work.Worker
import androidx.work.WorkerParameters
import com.anyun.exam.lib.AYSdk
import com.anyun.exam.lib.MyLog
import com.google.gson.Gson
import okhttp3.ResponseBody
import org.json.JSONException
import org.json.JSONObject
import retrofit2.Call
import retrofit2.Callback
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
import safeluck.drive.evaluation.httpmodule.HttpRoadMapRsp
import safeluck.drive.evaluation.httpmodule.HttpYardRsp
import safeluck.drive.evaluation.httpmodule.RetrofitCreator.Companion.getRestService
import safeluck.drive.evaluation.im.IMSClientBootstrap
import safeluck.drive.evaluation.platformMessage.PlatFormConstant
import safeluck.drive.evaluation.util.CThreadPoolExecutor
import safeluck.drive.evaluation.util.FileUtil
import java.io.IOException
 
class RTKConfigUpdateWorker(context:Context, workerParams: WorkerParameters) :Worker(context, workerParams){
 
    val TAG:String = "RTKConfigUpdateWorker"
    private val gson = Gson()
 
    override fun doWork(): Result {
 
        var data = inputData.getString(Constant.RTK_CONFIG_UPDATE_JSON)
        MyLog.i(PlatFormConstant.RTK,"更新RTK_config imei phone sn="+data)
        var imei:String=""
        var phone:String=""
        var sn:String=""
        if (!TextUtils.isEmpty(data)){
            var jsonObject: JSONObject? = null
            jsonObject = JSONObject(data)
            sn = jsonObject.getString(Constant.RTK_CONFIG_SN)
            ExamPlatformData.getInstance().setPhone(sn);
            Log.i(TAG,"sn="+sn)
            imei = sn.substring(1)
            Log.i(TAG,"imei="+imei)
            /**
             * SN与电话号码的关系(以AYKJ-4为例):
            0311200200000011,前4位是设备编码,接下来4位是年月,后面8位是流水号;
            电话号码前2位固定13,第三位取SN的第4位,第4-7位取SN的年月,第8-11位取SN流水号的后4位;即13120020011
             */
            phone = sn
            Log.i(TAG,"phone="+phone)
 
            getMap(sn, "road")
            getMap(sn, "yard")
            getMap(sn, "car")
 
 
            MyLog.i(PlatFormConstant.TAG,"重置TCP连接")
            IMSClientBootstrap.getInstance().resetConnect();
        }
        var rtkConfigDao = WorkRoomDataBase.getWorkRoomDataBase(applicationContext).rtkConfigDao.also {
            it.update(sn, imei, phone)
 
 
        }
 
        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) {
        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(TAG, "RTK配置信息:$rtkjson")
            AYSdk.getInstance().sendCmd(safeluck.drive.evaluation.Constant.PUSH_RTK_PLATFORM_INFO, rtkjson)
        } else {
            MyLog.d(TAG, "RTKConfig未取到数据")
        }
    }
 
 
 
    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))
            }
        }
    }
 
}