package safeluck.drive.evaluation.util
|
|
import android.content.Context
|
import androidx.work.Data
|
import androidx.work.ListenableWorker
|
import com.anyun.exam.lib.AYSdk
|
import com.anyun.basecommonlib.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.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.bean.ExamPlatformData
|
import safeluck.drive.evaluation.bean.SignalConfigRemote
|
import safeluck.drive.evaluation.bean.SimulateNightBean
|
import safeluck.drive.evaluation.worker.MCUUpgradeWorker
|
import java.io.IOException
|
import java.io.InputStreamReader
|
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 sendExamLights(applicationContext: Context){
|
MyLog.i("发送灯光")
|
|
val examType = ExamPlatformData.getInstance().examType
|
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)
|
|
MyLog.i("开始发送灯光,考试项共有"+simulateNightBean.question.size)
|
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
|
}
|