lizhanwei
2020-03-16 6e512f99ae6d56f52cd79ff7ea7245a431be4dbb
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
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<Integer, JK2019MessageBase> 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);
        }
    }
}