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(); // jkMessage0101.des =((JKMessage8100) jk2019MessageBase).des; 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); 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")){ MyLog.i(PlatFormConstant.TAG,"鉴权成功"); ExamPlatformData.getInstance().setExamplatformStatus(ExamPlatformData.DEV_LOGIN); }else{ MyLog.i(PlatFormConstant.TAG,"收到了鉴权消息,但鉴权不成功"); ExamPlatformData.getInstance().setExamplatformStatus(ExamPlatformData.DEV_NOT_LOGIN); } }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); } } @Override public void sendMessage(final JK2019MessageBase msg) { CThreadPoolExecutor.runInBackground(new Runnable() { @Override public void run() { if ( ExamPlatformData.getInstance().getExamplatformStatus() == ExamPlatformData.DEV_LOGIN){ 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 ((msgid.equalsIgnoreCase("0100")) || (msgid.equalsIgnoreCase("0101"))){ if (IMSClientBootstrap.getInstance().isActive()){ IMSClientBootstrap.getInstance().sendMessage(messageEscaper.escape(msg.toBytes())); }else{ Log.e(TAG, "run: 发送消息失败,未初始化连接NettyTcp"); } }else{ MyLog.i(PlatFormConstant.TAG,"登录未成功,不能发送消息="+msgid); } } } }); } @Override public void addBeatHeart(int seconds) { if (IMSClientBootstrap.getInstance().isActive()){ IMSClientBootstrap.getInstance().addHeartbeat(seconds); } } }