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.interf.IMSClientInteface; import com.anyun.im_lib.util.ByteUtil; import com.google.gson.Gson; import com.safeluck.aykj.utils.ArrayUtils; 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.bean.ExamPlatformData; import safeluck.drive.evaluation.platformMessage.JK2019MessageBase; import safeluck.drive.evaluation.platformMessage.JKMessage0001; import safeluck.drive.evaluation.platformMessage.JKMessage0100; import safeluck.drive.evaluation.platformMessage.JKMessage0101; 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; import safeluck.drive.evaluation.util.CThreadPoolExecutor; import safeluck.drive.evaluation.util.SPUtils; import safeluck.drive.evaluation.util.Utils; /** * MyApplication2 * Created by lzw on 2019/12/12. 16:14:33 * 邮箱:632393724@qq.com * All Rights Saved! Chongqing AnYun Tech co. LTD */ public class MessageProcessor implements IMessageProcessor { private static final String TAG = MessageProcessor.class.getSimpleName(); private Map mMsgTimeoutMap = new ConcurrentHashMap<>(); private Gson gson; private MessageProcessor(){ messageEscaper = new MessageEscaper(); gson = new Gson(); } private static class MessageProcessorInstance{ private static final IMessageProcessor INSTANCE = new MessageProcessor(); } MessageEscaper messageEscaper; public static IMessageProcessor getInstance(){ return MessageProcessorInstance.INSTANCE; } @Override public void receiveMsg(byte[] message) { //反转义 byte[] datas=messageEscaper.unescape(message); MyLog.i(PlatFormConstant.TAG,"服务消息:"+ BytesUtils.bytesToHexString(datas)); JK2019MessageBase jk2019MessageBase = MessageManager.get(BytesUtils.bytesToHexString(datas)); if (jk2019MessageBase instanceof JKMessage8100){ if (((JKMessage8100) jk2019MessageBase).result == JKMessage8100.SUCCESS){ MyLog.i(PlatFormConstant.TAG,"注册成功"); String des = ((JKMessage8100) jk2019MessageBase).des; SPUtils.put(app.getAppContext(),SPUtils.DES_HEX_PWD,des); JKMessage0101 jkMessage0101 = new JKMessage0101(); long time =System.currentTimeMillis()/1000; int unsignedInt = Utils.parseUnsignedInt(String.valueOf(time),10); MyLog.i(String.format("总秒数(long)=%d,装换成unsigned int= %d",time,unsignedInt)); jkMessage0101.timestamp = unsignedInt; byte[] miwen =Utils.encrypt(ByteUtil.intGetBytes(unsignedInt),des); MyLog.i(PlatFormConstant.TAG,"鉴权密文="+BytesUtils.bytesToHexString(miwen)+" time="+unsignedInt); jkMessage0101.des = BytesUtils.bytesToHexString(miwen); MyLog.i(PlatFormConstant.TAG,"发送鉴权消息"); sendMessage(jkMessage0101); ExamPlatformData.getInstance().setExamplatformStatus(ExamPlatformData.DEV_REGISTERED); }else{ ExamPlatformData.getInstance().setExamplatformStatus(ExamPlatformData.DEV_REGISTERED); } }else if (jk2019MessageBase instanceof JKMessage8001){ JKMessage8001 jkMessage8001 = (JKMessage8001) jk2019MessageBase; String hexMessageId = jkMessage8001.respMessageId; if (hexMessageId.equalsIgnoreCase("0101")){ if (jkMessage8001.result == JKMessage8001.SUCCESS){ MyLog.i(PlatFormConstant.TAG,"鉴权成功"); ExamPlatformData.getInstance().setExamplatformStatus(ExamPlatformData.DEV_LOGIN); }else{ MyLog.i(PlatFormConstant.TAG,"鉴权不成功:"+jkMessage8001.result); ExamPlatformData.getInstance().setExamplatformStatus(ExamPlatformData.DEV_NOT_LOGIN); } }else{ MyLog.i(PlatFormConstant.TAG,String.format("收到了对%s消息的应答",jkMessage8001.respMessageId)); } }else if (jk2019MessageBase instanceof JKMessage8201){ JKMessage8201 jkMessage8201 = (JKMessage8201) jk2019MessageBase; JKMessage0001 jkMessage0001 = new JKMessage0001(); jkMessage0001.respMessageId = "8201"; jkMessage0001.result = JKMessage0001.SUCCESS; jkMessage0001.respNo = jkMessage8201.messageNo; sendMessage(jkMessage0001); 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); } } @Override public void sendMessage(final JK2019MessageBase msg) { CThreadPoolExecutor.runInBackground(new Runnable() { @Override public void run() { if ( ExamPlatformData.getInstance().getExamplatformStatus() == ExamPlatformData.DEV_LOGIN){ byte[] msgidbytes= ArrayUtils.subArray(msg.toBytes(),2,2); String msgid = BytesUtils.bytesToHexString(msgidbytes); if (canSend(msgid)){ if (IMSClientBootstrap.getInstance().isActive()){ IMSClientBootstrap.getInstance().sendMessage(messageEscaper.escape(msg.toBytes())); }else{ Log.e(TAG, "run: 发送消息失败,未初始化连接NettyTcp"); } } }else{ // JK2019MessageBase jk2019MessageBase = MessageManager.get(msg.toString()); byte[] msgidbytes= ArrayUtils.subArray(msg.toBytes(),2,2); String msgid = BytesUtils.bytesToHexString(msgidbytes); Log.i(TAG, "run: msgid="+msgid); if (canSend(msgid)){ if ((msgid.equalsIgnoreCase("0100")) || (msgid.equalsIgnoreCase("0101"))){ MyLog.i(PlatFormConstant.TAG,"注册/鉴权消息直接发送"); if (IMSClientBootstrap.getInstance().isActive()){ IMSClientBootstrap.getInstance().sendMessage(messageEscaper.escape(msg.toBytes())); }else{ MyLog.i(PlatFormConstant.TAG, "发送消息失败,未初始化连接NettyTcp"); } }else{ MyLog.i(PlatFormConstant.TAG,"登录未成功,不能发送消息="+msgid); } } } } }); } @Override public void addBeatHeart(int seconds) { if (IMSClientBootstrap.getInstance().isActive()){ IMSClientBootstrap.getInstance().addHeartbeat(seconds); } } private boolean canSend(String msgId){ if (ExamPlatformData.getInstance().getTrainingMode()==ExamPlatformData.TRAINING_MODE){ //训练模式下 只有几条可以发送 MyLog.i(PlatFormConstant.TAG,"训练模式,msgid="+msgId); switch (msgId){ case "0206": case "0100": case "0101": return true; default: return false; } }else{ //考试模式下 和什么模式都不是状态 全部都可以发送 return true; } } }