app/src/main/assets/students_file.json
@@ -4,6 +4,8 @@ "stu_id":1001, "ID": "410923198910162545", "name": "李四", "sex":2 "sex":2, "head_url":"" } ] app/src/main/java/safeluck/drive/evaluation/Constant.java
@@ -75,5 +75,6 @@ public static final String MCU_FILE_NAME = "dfu.bin"; public static final int MCU_SN = 8; public static final int SEND_CONFIG_SIGNAL = 0x8008; public static final String STU_INFO_PLATFORM = "STU_INFO_PLATFORM"; public static String exam_enter_exitdata="exam_enter_exitdata"; } app/src/main/java/safeluck/drive/evaluation/DB/Student.java
@@ -29,6 +29,17 @@ //性别 0-男 1-女 @ColumnInfo(name = "sex") private int sex; //性别 0-男 1-女 @ColumnInfo(name = "head_url") private String head_url; public String getHead_url() { return head_url; } public void setHead_url(String head_url) { this.head_url = head_url; } public long getBegin_time() { return begin_time; app/src/main/java/safeluck/drive/evaluation/DB/StudentDao.java
@@ -17,7 +17,7 @@ */ @Dao public interface StudentDao { @Insert(onConflict = OnConflictStrategy.IGNORE) @Insert(onConflict = OnConflictStrategy.REPLACE) void insert(Student student); app/src/main/java/safeluck/drive/evaluation/DB/StudentInfoUpdateWork.java
New file @@ -0,0 +1,47 @@ package safeluck.drive.evaluation.DB; import android.content.Context; import android.text.TextUtils; import android.util.Log; import androidx.annotation.NonNull; import androidx.work.Worker; import androidx.work.WorkerParameters; import org.json.JSONException; import org.json.JSONObject; import safeluck.drive.evaluation.Constant; public class StudentInfoUpdateWork extends Worker { private static final String TAG = "StudentInfoUpdateWork"; public StudentInfoUpdateWork(@NonNull Context context, @NonNull WorkerParameters workerParams) { super(context, workerParams); } @NonNull @Override public Result doWork() { String str = getInputData().getString(Constant.STU_INFO_PLATFORM); if (!TextUtils.isEmpty(str)){ Log.i(TAG, "doWork: "+str); JSONObject jsonObject = null; try { jsonObject = new JSONObject(str); String headUrl = jsonObject.getString("head_url"); String id = jsonObject.getString("ID"); String name = jsonObject.getString("name"); Student student =new Student(1001,name,id,2); student.setHead_url(headUrl); WorkRoomDataBase.getWorkRoomDataBase(getApplicationContext()).getstudentDao().insert(student); return Result.success(); } catch (JSONException e) { e.printStackTrace(); return Result.failure(); } } return Result.success(); } } app/src/main/java/safeluck/drive/evaluation/app.java
@@ -1,6 +1,7 @@ package safeluck.drive.evaluation; import android.app.Application; import android.content.Context; import android.os.Environment; import android.text.TextUtils; import android.util.Log; @@ -61,7 +62,7 @@ FailedProjRepository failedProjRepository;//失败项目表数据库 RTKWorkRepository rtkWorkRepository;//保存RTK配置表 Random random = new Random(); private static Context appContext=null; @Override public void onCreate() { super.onCreate(); @@ -84,7 +85,7 @@ MyLog.createIfNotExist(); Stetho.initializeWithDefaults(this); appContext = this; //数据库操作 MyLog.i(TAG, "onCreate111"); @@ -105,6 +106,10 @@ } public static Context getAppContext(){ return appContext; } @Override public void onTerminate() { super.onTerminate(); app/src/main/java/safeluck/drive/evaluation/fragment/TcpFragment.java
@@ -5,6 +5,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; import android.view.View; @@ -22,6 +23,9 @@ import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import me.yokeyword.fragmentation.SupportFragment; import safeluck.drive.evaluation.DB.criterias.CriteriaForI; @@ -29,6 +33,7 @@ import safeluck.drive.evaluation.R; import safeluck.drive.evaluation.im.MessageProcessor; import safeluck.drive.evaluation.platformMessage.JKMessage0001; import safeluck.drive.evaluation.platformMessage.JKMessage0002; import safeluck.drive.evaluation.platformMessage.JKMessage0100; import safeluck.drive.evaluation.platformMessage.JKMessage0101; import safeluck.drive.evaluation.platformMessage.JKMessage0201; @@ -36,6 +41,7 @@ import safeluck.drive.evaluation.platformMessage.JKMessage0203; import safeluck.drive.evaluation.platformMessage.JKMessage0204; import safeluck.drive.evaluation.im.IMSClientBootstrap; import safeluck.drive.evaluation.util.SPUtils; /** * MyApplication2 @@ -51,6 +57,7 @@ private TextInputEditText port; private TextView tv_content; private Button btn_connect; private Button btn_auth; private Button btn_send; private AppCompatEditText sendEditText; private CriteriaIViewModel workViewModel; @@ -81,12 +88,14 @@ btn_connect = view.findViewById(R.id.btn_connect); btn_send = view.findViewById(R.id.btn_send); view.findViewById(R.id.btn_register).setOnClickListener(this); view.findViewById(R.id.btn_auth).setOnClickListener(this); btn_auth = view.findViewById(R.id.btn_auth); btn_auth.setOnClickListener(this); view.findViewById(R.id.btn_gain).setOnClickListener(this); view.findViewById(R.id.btn_start_exam).setOnClickListener(this); view.findViewById(R.id.btn_stop_exam).setOnClickListener(this); view.findViewById(R.id.btn_fail).setOnClickListener(this); view.findViewById(R.id.btn_gps).setOnClickListener(this); view.findViewById(R.id.btn_keepalive).setOnClickListener(this); btn_send.setOnClickListener(this); btn_connect.setOnClickListener(this); @@ -123,8 +132,8 @@ String userId = "100002"; String token = "token_" + userId; // String hosts = "[{\"host\":\"47.93.80.84\", \"port\":12125}]"; // String hosts = "[{\"host\":\"47.93.157.103\", \"port\":2020}]"; String hosts = "[{\"host\":\"192.168.31.77\", \"port\":12125}]"; String hosts = "[{\"host\":\"47.93.157.103\", \"port\":2020}]"; // String hosts = "[{\"host\":\"192.168.31.77\", \"port\":12125}]"; IMSClientBootstrap.getInstance().init(userId,token,hosts,1); break; @@ -159,6 +168,8 @@ Log.i(TAG, "onClick: string="+string); break; case R.id.btn_register: String hexPwd = (String) SPUtils.get(getActivity(),SPUtils.DES_HEX_PWD,""); if (TextUtils.isEmpty(hexPwd)){ JKMessage0100 jkRegisterMessage = new JKMessage0100(); jkRegisterMessage.proviceid = 23; @@ -171,6 +182,10 @@ byte[] str = jkRegisterMessage.toBytes(); MessageProcessor.getInstance().sendMessage(jkRegisterMessage); Log.i(TAG, "onClick: "+ BytesUtils.bytesToHexString(str)+" ============"); }else{ Log.i(TAG, "onClick: 密码="+hexPwd); btn_auth.performClick(); } break; case R.id.btn_auth: JKMessage0101 jkMessage0101 = new JKMessage0101(); @@ -216,6 +231,16 @@ jkMessage0204.exam_id = 123456; MessageProcessor.getInstance().sendMessage(jkMessage0204); break; case R.id.btn_keepalive: ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); scheduledExecutorService.scheduleAtFixedRate(new Runnable() { @Override public void run() { MessageProcessor.getInstance().sendMessage(new JKMessage0002()); } },1000,1000, TimeUnit.MICROSECONDS); break; } } app/src/main/java/safeluck/drive/evaluation/im/MessageProcessor.java
@@ -1,18 +1,29 @@ package safeluck.drive.evaluation.im; import android.app.Application; import android.util.Log; import androidx.work.Data; import androidx.work.OneTimeWorkRequest; import androidx.work.WorkManager; import com.anyun.exam.lib.MyLog; import com.anyun.im_lib.util.ByteUtil; import com.google.gson.Gson; import com.safeluck.aykj.utils.BytesUtils; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import safeluck.drive.evaluation.Constant; import safeluck.drive.evaluation.DB.StudentInfoUpdateWork; import safeluck.drive.evaluation.app; import safeluck.drive.evaluation.platformMessage.JK2019MessageBase; import safeluck.drive.evaluation.platformMessage.JKMessage0001; import safeluck.drive.evaluation.platformMessage.JKMessage0100; import safeluck.drive.evaluation.platformMessage.JKMessage8001; import safeluck.drive.evaluation.platformMessage.JKMessage8100; import safeluck.drive.evaluation.platformMessage.JKMessage8201; import safeluck.drive.evaluation.platformMessage.PlatFormConstant; import safeluck.drive.evaluation.platformMessage.utils.MessageEscaper; import safeluck.drive.evaluation.platformMessage.utils.MessageManager; @@ -29,9 +40,11 @@ private static final String TAG = MessageProcessor.class.getSimpleName(); private Map<Integer, JK2019MessageBase> mMsgTimeoutMap = new ConcurrentHashMap<>(); private Gson gson; private MessageProcessor(){ messageEscaper = new MessageEscaper(); gson = new Gson(); } private static class MessageProcessorInstance{ @@ -52,8 +65,22 @@ MyLog.i(PlatFormConstant.TAG,"转义过后的服务消息:"+ BytesUtils.bytesToHexString(datas)); JK2019MessageBase jk2019MessageBase = MessageManager.get(BytesUtils.bytesToHexString(datas)); if (jk2019MessageBase instanceof JKMessage8100){ if (((JKMessage8100) jk2019MessageBase).result == JKMessage8100.SUCCESS){ Log.i(TAG, "receiveMsg: "+jk2019MessageBase.messageId); SPUtils.put(app.getAppContext(),SPUtils.DES_HEX_PWD,((JKMessage8100) jk2019MessageBase).des); } }else if (jk2019MessageBase instanceof JKMessage8001){ JKMessage8001 jkMessage8001 = (JKMessage8001) jk2019MessageBase; String hexMessageId = jkMessage8001.respMessageId; if (hexMessageId.equalsIgnoreCase("0101")){ MyLog.i(PlatFormConstant.TAG,"鉴权成功"); } }else if (jk2019MessageBase instanceof JKMessage8201){ JKMessage8201 jkMessage8201 = (JKMessage8201) jk2019MessageBase; Data data = new Data.Builder().putString(Constant.STU_INFO_PLATFORM,gson.toJson(jkMessage8201)).build(); OneTimeWorkRequest stuInfoUpdateWork = new OneTimeWorkRequest.Builder(StudentInfoUpdateWork.class) .setInputData(data).build(); WorkManager.getInstance(app.getAppContext()).enqueue(stuInfoUpdateWork); } } @@ -65,6 +92,7 @@ CThreadPoolExecutor.runInBackground(new Runnable() { @Override public void run() { if (IMSClientBootstrap.getInstance().isActive()){ IMSClientBootstrap.getInstance().sendMessage(msg.toBytes()); }else{ app/src/main/java/safeluck/drive/evaluation/platformMessage/JKMessage8201.java
@@ -19,5 +19,5 @@ @Order(4) @Length(0) @Ascii public String url; public String head_url; } app/src/main/java/safeluck/drive/evaluation/util/SPUtils.java
@@ -15,6 +15,7 @@ * 保存在手机里面的文件名 */ public static final String FILE_NAME = "share_data"; public static final String DES_HEX_PWD = "des_pwd"; /** * 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法 app/src/main/res/layout/layout_tcpclient.xml
@@ -103,6 +103,12 @@ android:layout_marginEnd="10dp" android:id="@+id/btn_stop_exam" android:text="结束考试"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="10dp" android:id="@+id/btn_keepalive" android:text="心跳"/> </TableRow> </TableLayout>