lizhanwei
2020-03-28 babb05583f59eb5e2c853168490f54fc7261e84a
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
package safeluck.drive.evaluation.DB.exam_status
 
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 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 com.google.gson.reflect.TypeToken
import com.google.gson.stream.JsonReader
import org.json.JSONException
import org.json.JSONObject
import safeluck.drive.evaluation.DB.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.ExamPlatformData
import safeluck.drive.evaluation.bean.SignalConfigRemote
import safeluck.drive.evaluation.bean.SimulateNightBean
import safeluck.drive.evaluation.bean.SimulateNightBean.QuestionBean
import safeluck.drive.evaluation.util.CThreadPoolExecutor
import safeluck.drive.evaluation.util.FileUtil
import safeluck.drive.evaluation.util.Utils
import java.io.IOException
import java.io.InputStreamReader
 
class ExamStatusOutWorker(context: Context, workerParams: WorkerParameters) : Worker(context, workerParams) {
    val TAG:String = "ExamStatusOutWorker"
    private var signalConfiglist: MutableList<SignalConfigRemote> = ArrayList()
    private val gson = Gson()
    var  simulateNightBean:SimulateNightBean=SimulateNightBean()
    private var singalConfigs :List<SingalConfig> = ArrayList()
    private val tempQs: MutableList<QuestionBean> = java.util.ArrayList()
    override fun doWork(): Result {
        val examType =WorkRoomDataBase.getWorkRoomDataBase(applicationContext).examStatusDao.startExamInt;
 
        simulateNightBean.setExam(0)
 
        val list = ExamPlatformData.getInstance().simulate_light_tips
 
        tempQs.add(list[list.size-2])//提示下面将进行灯光考试
        tempQs.add(list[0])
        val a = Utils.getRandomInts(4, 12)
        for (i in a.indices) {
            tempQs.add(list[a[i]])
        }
        tempQs.add(list[list.size-1])//提示灯光考试结束
        simulateNightBean.setQuestion(tempQs)
        AYSdk.getInstance().sendCmd(0x8012, gson.toJson(simulateNightBean))
        sendMapInfo()
        sendVehicleInfo()
 
        MyLog.i(TAG,"查到的考试type="+examType)
        ExamPlatformData.getInstance().examType = examType
 
        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)
        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)
                singalConfigs = mstus
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }else{
 
        }
        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)
 
        val data = Data.Builder().putInt(Constant.EXAM_STATUS_TYPE,examType).build()
//        if (examType != safeluck.drive.evaluation.Constant.NONE_BEEN_START_EXAM) {
            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()
            }
//        }
        return Result.success(data)
    }
 
    private fun sendSignalConfigsToRemote(indexs: List<Int>) {
        if (signalConfiglist.size > 0) {
            if (listContainsSameValue(indexs)) {
                Log.i(TAG, "sendSignalConfigsToRemote: 有相同的index,不能发送" + gson.toJson(signalConfiglist))
            } else {
                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
    }
 
 
    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, "NDK_start RTK配置信息")
            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.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() }
//            }
            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))
            }
        }
    }
 
}