Merge branch 'master' of https://gitee.com/endian11/DriveJudge
| | |
| | | import android.os.AsyncTask; |
| | | import android.os.Bundle; |
| | | |
| | | import android.text.TextUtils; |
| | | import android.util.Log; |
| | | import android.widget.Toast; |
| | | |
| | |
| | | import com.anyun.exam.lib.AYSdk; |
| | | import com.anyun.exam.lib.MyLog; |
| | | import com.anyun.exam.lib.util.ByteUtil; |
| | | import com.anyun.im_lib.listener.IMSConnectStatusCallback; |
| | | import com.google.gson.Gson; |
| | | import com.safeluck.aykj.utils.BytesUtils; |
| | | |
| | | import org.json.JSONException; |
| | | import org.json.JSONObject; |
| | |
| | | import java.util.List; |
| | | import java.util.concurrent.ArrayBlockingQueue; |
| | | |
| | | import safeluck.drive.evaluation.im.IMSClientBootstrap; |
| | | import safeluck.drive.evaluation.im.MessageProcessor; |
| | | import safeluck.drive.evaluation.platformMessage.JKMessage0100; |
| | | import safeluck.drive.evaluation.platformMessage.JKMessage0101; |
| | | import safeluck.drive.evaluation.util.CThreadPoolExecutor; |
| | | import safeluck.drive.evaluation.util.FileUtil; |
| | | import safeluck.drive.evaluation.util.PermissionManager; |
| | | import safeluck.drive.evaluation.util.SPUtils; |
| | | import safeluck.drive.evaluation.viewmodels.MainViewModel; |
| | | import safeluck.drive.evaluation.viewmodels.RTKConnAndLogin; |
| | | import safeluck.drive.evaluation.viewmodels.RTKConnAndLoginViewModel; |
| | | |
| | | public class MainActivity extends SupportActivity { |
| | | public class MainActivity extends SupportActivity implements IMSConnectStatusCallback { |
| | | |
| | | private static final int PERMISSIONS_REQUEST_CODE = 1001; |
| | | private String TAG = MainActivity.class.getCanonicalName(); |
| | |
| | | String[] PERMISSIONS = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}; |
| | | RTKConfigViewModel rtkConfigViewModel; |
| | | private boolean onlySendOnceRTKConfig = true; |
| | | |
| | | |
| | | private ICEventListener icEventListener = new ICEventListener() { |
| | | @Override |
| | |
| | | //全屏 |
| | | |
| | | setContentView(R.layout.activity_main); |
| | | // TODO |
| | | // 进行tcp连接 |
| | | String userId = "100002"; |
| | | String token = "token_" + userId; |
| | | String hosts = "[{\"host\":\"192.168.31.77\", \"port\":12125}]"; |
| | | IMSClientBootstrap.getInstance().init(userId,token,hosts,1,this); |
| | | |
| | | WokViewModel wokViewModel = ViewModelProviders.of(this).get(WokViewModel.class); |
| | | wokViewModel.getStudents().observe(this, new Observer<List<Student>>() { |
| | |
| | | return false; |
| | | } |
| | | |
| | | @Override |
| | | public void onConnecting() { |
| | | MyLog.i(TAG,"TCP onConnecting"); |
| | | } |
| | | |
| | | @Override |
| | | public void onConnected() { |
| | | MyLog.i(TAG,"TCP连接成功"); |
| | | //TODO 判断SPUtils 是否保存有des密码;如果没有则进行注册JKMessage0100,如果有密码则进行鉴权JKMessage0101 |
| | | String hexPwd = (String) SPUtils.get(this,SPUtils.DES_HEX_PWD,""); |
| | | MessageProcessor.getInstance().addBeatHeart(10); |
| | | if (TextUtils.isEmpty(hexPwd)){ |
| | | |
| | | JKMessage0100 jkRegisterMessage = new JKMessage0100(); |
| | | jkRegisterMessage.proviceid = 23; |
| | | jkRegisterMessage.cityid = 1; |
| | | jkRegisterMessage.imei = "460123874561"; |
| | | jkRegisterMessage.model = "123"; |
| | | jkRegisterMessage.sn = "0314200100000004"; |
| | | |
| | | Log.i(TAG, "onClick: msg len "+jkRegisterMessage.getMessageLen()+" "+ jkRegisterMessage.props.value); |
| | | byte[] str = jkRegisterMessage.toBytes(); |
| | | MessageProcessor.getInstance().sendMessage(jkRegisterMessage); |
| | | Log.i(TAG, "onClick: "+ BytesUtils.bytesToHexString(str)+" ============"); |
| | | }else{ |
| | | JKMessage0101 jkMessage0101 = new JKMessage0101(); |
| | | jkMessage0101.des =hexPwd; |
| | | jkMessage0101.timestamp = (int) System.currentTimeMillis(); |
| | | MessageProcessor.getInstance().sendMessage(jkMessage0101); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onConnectFailed() { |
| | | MyLog.i(TAG,"TCP连接失败"); |
| | | } |
| | | |
| | | } |
| | |
| | | import com.anyun.exam.lib.MyLog; |
| | | import com.anyun.exam.lib.crash.CrashHandler; |
| | | import com.anyun.im_lib.ExecutorServiceFactory; |
| | | import com.anyun.im_lib.listener.IMSConnectStatusCallback; |
| | | import com.facebook.stetho.Stetho; |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.JsonArray; |
| | | import com.google.gson.JsonObject; |
| | | import com.google.gson.JsonParser; |
| | | import com.safeluck.aykj.utils.BytesUtils; |
| | | |
| | | |
| | | import org.json.JSONException; |
| | | import org.json.JSONObject; |
| | | |
| | | import java.util.Date; |
| | | import java.util.Random; |
| | | |
| | | import safeluck.drive.evaluation.DB.WorkRoomDataBase; |
| | |
| | | import safeluck.drive.evaluation.DB.rtktb.RTKWorkRepository; |
| | | import safeluck.drive.evaluation.cEventCenter.CEvent; |
| | | import safeluck.drive.evaluation.cEventCenter.CEventCenter; |
| | | import safeluck.drive.evaluation.im.IMSClientBootstrap; |
| | | import safeluck.drive.evaluation.im.IMSConnectStatusListener; |
| | | import safeluck.drive.evaluation.im.MessageProcessor; |
| | | import safeluck.drive.evaluation.platformMessage.JKMessage0100; |
| | | import safeluck.drive.evaluation.platformMessage.JKMessage0101; |
| | | import safeluck.drive.evaluation.platformMessage.JKMessage0203; |
| | | import safeluck.drive.evaluation.util.FileUtil; |
| | | import safeluck.drive.evaluation.util.SPUtils; |
| | | import safeluck.drive.evaluation.util.SystemUtil; |
| | | |
| | | /** |
| | |
| | | * 邮箱:632393724@qq.com |
| | | * All Rights Saved! Chongqing AnYun Tech co. LTD |
| | | */ |
| | | public class app extends Application implements IAYExamListener { |
| | | public class app extends Application implements IAYExamListener { |
| | | |
| | | private static final String TAG = "app"; |
| | | private Gson gson; |
| | |
| | | } |
| | | }).start(); |
| | | |
| | | //TODO 进行tcp连接 |
| | | |
| | | //TODO 判断SPUtils 是否保存有des密码;如果没有则进行注册JKMessage0100,如果有密码则进行鉴权JKMessage0101 |
| | | |
| | | } |
| | | |
| | | } |
| | |
| | | MyLog.e(TAG, "OnTerminate()"); |
| | | } |
| | | |
| | | private String lastStr; |
| | | @Override |
| | | public void callBackMsg(final int cmd, String json) { |
| | | MyLog.d(TAG, String.format("收到命令[%d],Json内容为%s", cmd, json)); |
| | | String strConent = String.format("收到命令[%d],Json内容为%s", cmd, json); |
| | | if (strConent.equalsIgnoreCase(lastStr)){ |
| | | |
| | | }else{ |
| | | |
| | | MyLog.d(TAG,strConent ); |
| | | lastStr = strConent; |
| | | } |
| | | switch (cmd) { |
| | | case Constant.NDK_START: |
| | | sendVechileInfo(); |
| | |
| | | case Constant.JUDGE_INFO: |
| | | |
| | | if(!TextUtils.isEmpty(json)){ |
| | | JKMessage0203 jkMessage0203 = new JKMessage0203(); |
| | | jkMessage0203.phone = "0314200100000004"; |
| | | jkMessage0203.timeBCD = new Date(); |
| | | jkMessage0203.fail_item_id = 4; |
| | | jkMessage0203.fail_score = 10; |
| | | jkMessage0203.fail_reason = "发动机启动后,不及时松开启动开关"; |
| | | jkMessage0203.ID = "123456789"; |
| | | jkMessage0203.exam_id = 123456; |
| | | MessageProcessor.getInstance().sendMessage(jkMessage0203); |
| | | JsonArray jsonArray = JsonParser.parseString(json).getAsJsonArray(); |
| | | for (int i = 0; i < jsonArray.size(); i++) { |
| | | JsonObject jsonObject = jsonArray.get(i).getAsJsonObject(); |
| | |
| | | }).start(); |
| | | |
| | | } |
| | | |
| | | |
| | | } |
| | |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | import me.yokeyword.fragmentation.SupportFragment; |
| | |
| | | import safeluck.drive.evaluation.cEventCenter.ICEventListener; |
| | | import safeluck.drive.evaluation.customview.ArrowView; |
| | | import safeluck.drive.evaluation.customview.HouseView; |
| | | import safeluck.drive.evaluation.im.MessageProcessor; |
| | | import safeluck.drive.evaluation.platformMessage.JKMessage0201; |
| | | import safeluck.drive.evaluation.platformMessage.JKMessage0202; |
| | | import safeluck.drive.evaluation.platformMessage.JKMessage0204; |
| | | |
| | | /** |
| | | * 联网训练UI |
| | |
| | | sendExamJson(1); |
| | | iv_head.getDrawable().setLevel(1); |
| | | //TODO 发送JKMessage0202 给平台,开始考试 |
| | | JKMessage0202 jkMessage0202 = new JKMessage0202(); |
| | | jkMessage0202.phone = "0314200100000004"; |
| | | jkMessage0202.timeBCD = new Date(); |
| | | jkMessage0202.curr_exam = 1; |
| | | jkMessage0202.ID = "123456789"; |
| | | jkMessage0202.exam_id = 123456; |
| | | MessageProcessor.getInstance().sendMessage(jkMessage0202); |
| | | break; |
| | | case R.id.tv_stop: |
| | | Toast.makeText(_mActivity, "结束考试", Toast.LENGTH_SHORT).show(); |
| | |
| | | case R.id.profile: |
| | | MyLog.i(TAG,"签到,获取身份证物理卡号"); |
| | | //TODO 获得物理卡号 发送JKMessage0201给平台,获取姓名、身份证、head_url |
| | | JKMessage0201 jkMessage0201 = new JKMessage0201(); |
| | | jkMessage0201.currentExam = 1; |
| | | jkMessage0201.id = "123456789"; |
| | | MessageProcessor.getInstance().sendMessage(jkMessage0201); |
| | | break; |
| | | } |
| | | } |
| | |
| | | WorkManager.getInstance(_mActivity).enqueue(examStatusInitWork); |
| | | sendExamJson(0); |
| | | //TODO 发送JKMessage0204给平台 结束考试 |
| | | JKMessage0204 jkMessage0204 = new JKMessage0204(); |
| | | jkMessage0204.phone = "0314200100000004"; |
| | | jkMessage0204.timeBCD = new Date(); |
| | | jkMessage0204.score = 10; |
| | | jkMessage0204.stop = JKMessage0204.SUCC_STOP; |
| | | jkMessage0204.ID = "123456789"; |
| | | jkMessage0204.exam_id = 123456; |
| | | MessageProcessor.getInstance().sendMessage(jkMessage0204); |
| | | } |
| | | |
| | | private ICEventListener icEventListener = new ICEventListener() { |
| | |
| | | import androidx.lifecycle.Observer; |
| | | |
| | | import com.anyun.exam.lib.util.Speaker; |
| | | import com.anyun.im_lib.listener.IMSConnectStatusCallback; |
| | | import com.anyun.im_lib.util.ByteUtil; |
| | | import com.google.android.material.textfield.TextInputEditText; |
| | | import com.safeluck.aykj.utils.BytesUtils; |
| | |
| | | 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); |
| | | IMSClientBootstrap.getInstance().init(userId, token, hosts, 1, new IMSConnectStatusCallback() { |
| | | @Override |
| | | public void onConnecting() { |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void onConnected() { |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void onConnectFailed() { |
| | | |
| | | } |
| | | }); |
| | | break; |
| | | case R.id.btn_send: |
| | | // if (connectThread != null){ |
| | |
| | | "{\"utc\":\"20070101062157.00\",\"sat_num\":0,\"qf\":0,\"latitude\":0.0,\"longitude\":0.0,\"altitude\":-6378154.162,\"speed\":0.0}"; |
| | | jkMessage0206.length = jkMessage0206.json.length(); |
| | | AttachInfo attachInfo = new AttachInfo(); |
| | | attachInfo.attach_message_id = 0x40; |
| | | attachInfo.attach_message_length = 18; |
| | | attachInfo.attach_data="12345678901"; |
| | | attachInfo.attach_message_id = Integer.parseInt(port.getText().toString().trim()); |
| | | attachInfo.attach_message_length = 4; |
| | | Log.i(TAG, "attachInfo.attach_message_id ="+attachInfo.attach_message_id ); |
| | | if (attachInfo.attach_message_id == 0x41){ |
| | | Log.i(TAG, "onClick: 41"); |
| | | attachInfo.attach_data="00000000"; |
| | | }else{ |
| | | |
| | | //18个字节 ascii |
| | | attachInfo.attach_data="303030303030303030303030303030303030"; |
| | | } |
| | | |
| | | jkMessage0206.attachInfo = attachInfo; |
| | | Log.i(TAG, "位置上报="+jkMessage0206.toString()); |
| | | |
| | | MessageProcessor.getInstance().sendMessage(jkMessage0206); |
| | | break; |
| | | case R.id.btn_stop_exam: |
| | | JKMessage0204 jkMessage0204 = new JKMessage0204(); |
| | |
| | | @Override |
| | | public void run() { |
| | | JKMessage0002 jkMessage0002 = new JKMessage0002(); |
| | | jkMessage0002.checksum = 0x7e; |
| | | // jkMessage0002.checksum = 0x7e; |
| | | MessageProcessor.getInstance().sendMessage(jkMessage0002); |
| | | Log.i(TAG, "run: 5s一次"); |
| | | } |
| | |
| | | import android.util.Log; |
| | | |
| | | import com.anyun.im_lib.interf.IMSClientInteface; |
| | | import com.anyun.im_lib.listener.IMSConnectStatusCallback; |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.JsonArray; |
| | | import com.google.gson.JsonObject; |
| | |
| | | * @param hosts |
| | | * @param appStatus |
| | | */ |
| | | public synchronized void init(String userId,String token,String hosts,int appStatus){ |
| | | public synchronized void init(String userId, String token, String hosts, int appStatus, IMSConnectStatusCallback imsConnectStatusListener){ |
| | | if (!isActive){ |
| | | Vector<String> serverUrlList = convertHosts(hosts); |
| | | if (serverUrlList == null || serverUrlList.size() ==0){ |
| | |
| | | //初始化IMSClientInteface |
| | | imsClient = IMSClientFactory.getIMSClient(); |
| | | updateAppStatus(appStatus); |
| | | imsClient.init(serverUrlList,new IMSEventListener(userId,token),new IMSConnectStatusListener()); |
| | | imsClient.init(serverUrlList,new IMSEventListener(userId,token),imsConnectStatusListener); |
| | | } |
| | | |
| | | } |
| | |
| | | imsClient.sendMsg(message); |
| | | } |
| | | } |
| | | |
| | | public void addHeartbeat(int seconds){ |
| | | if (isActive){ |
| | | imsClient.addHeartbeatHandler(seconds); |
| | | } |
| | | } |
| | | } |
| | |
| | | |
| | | import com.anyun.im_lib.listener.OnEventListener; |
| | | |
| | | import safeluck.drive.evaluation.platformMessage.JKMessage0002; |
| | | import safeluck.drive.evaluation.platformMessage.utils.MessageEscaper; |
| | | |
| | | /** |
| | | * MyApplication2 |
| | | * Created by lzw on 2019/12/12. 16:12:40 |
| | |
| | | |
| | | private String userId; |
| | | private String token; |
| | | |
| | | private MessageEscaper messageEscaper; |
| | | public IMSEventListener(String userId, String token) { |
| | | this.userId = userId; |
| | | this.token = token; |
| | | messageEscaper = new MessageEscaper(); |
| | | } |
| | | |
| | | @Override |
| | |
| | | public byte[] getRegisterMessage() { |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public byte[] getHearbeatMsg() { |
| | | JKMessage0002 jkMessage0002 = new JKMessage0002(); |
| | | return messageEscaper.escape(jkMessage0002.toBytes()); |
| | | } |
| | | } |
| | |
| | | public interface IMessageProcessor { |
| | | void receiveMsg(byte[] message); |
| | | void sendMessage(JK2019MessageBase msg); |
| | | |
| | | void addBeatHeart(int seconds); |
| | | } |
| | |
| | | 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.BytesUtils; |
| | |
| | | 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; |
| | |
| | | if (((JKMessage8100) jk2019MessageBase).result == JKMessage8100.SUCCESS){ |
| | | |
| | | SPUtils.put(app.getAppContext(),SPUtils.DES_HEX_PWD,((JKMessage8100) jk2019MessageBase).des); |
| | | JKMessage0101 jkMessage0101 = new JKMessage0101(); |
| | | jkMessage0101.des =((JKMessage8100) jk2019MessageBase).des; |
| | | jkMessage0101.timestamp = (int) System.currentTimeMillis(); |
| | | } |
| | | }else if (jk2019MessageBase instanceof JKMessage8001){ |
| | | JKMessage8001 jkMessage8001 = (JKMessage8001) jk2019MessageBase; |
| | |
| | | } |
| | | }); |
| | | } |
| | | |
| | | @Override |
| | | public void addBeatHeart(int seconds) { |
| | | if (IMSClientBootstrap.getInstance().isActive()){ |
| | | IMSClientBootstrap.getInstance().addHeartbeat(seconds); |
| | | } |
| | | } |
| | | } |
| | |
| | | @Int8 |
| | | public int attach_message_length; |
| | | @Order(3) |
| | | @Length(lengthField="attach_message_length") |
| | | @Length(lengthField = "attach_message_length") |
| | | @Hex |
| | | public String attach_data; |
| | | } |
| | |
| | | |
| | | @Order(12) |
| | | @Message |
| | | @Length(20) |
| | | @Length(0) |
| | | public AttachInfo attachInfo; |
| | | |
| | | |
| | | |
| | | /** |
| | | * 报警 |
| | | * DWORD |
| | |
| | | android:layout_width="0dp" |
| | | android:layout_height="wrap_content" |
| | | android:id="@+id/tv_rtk_model" |
| | | android:visibility="gone" |
| | | android:text="@string/rtk_config_model" |
| | | android:layout_weight="1"/> |
| | | <TextView |
| | |
| | | package com.anyun.im_lib; |
| | | |
| | | import com.anyun.im_lib.netty.NettyTcpClient; |
| | | import com.anyun.im_lib.util.MyLog; |
| | | import com.safeluck.aykj.utils.BytesUtils; |
| | | |
| | | import io.netty.channel.ChannelHandlerContext; |
| | | import io.netty.channel.ChannelInboundHandlerAdapter; |
| | | import io.netty.handler.timeout.IdleState; |
| | | import io.netty.handler.timeout.IdleStateEvent; |
| | | |
| | | /** |
| | | * MyApplication2 |
| | |
| | | * All Rights Saved! Chongqing AnYun Tech co. LTD |
| | | */ |
| | | public class HeartbeatHandler extends ChannelInboundHandlerAdapter { |
| | | private NettyTcpClient imsClient; |
| | | public HeartbeatHandler(NettyTcpClient imsClient) { |
| | | this.imsClient = imsClient; |
| | | } |
| | | private HeartbeatTask heartbeatTask; |
| | | private class HeartbeatTask implements Runnable { |
| | | |
| | | private ChannelHandlerContext ctx; |
| | | |
| | | public HeartbeatTask(ChannelHandlerContext ctx) { |
| | | this.ctx = ctx; |
| | | } |
| | | |
| | | @Override |
| | | public void run() { |
| | | if (ctx.channel().isActive()) { |
| | | byte []heartbeatMsg = imsClient.getHeartbeatMsg(); |
| | | if (heartbeatMsg == null) { |
| | | return; |
| | | } |
| | | MyLog.i("发送心跳消息,message=" + BytesUtils.bytesToHexString(heartbeatMsg) + "当前心跳间隔为:" +imsClient.getHeartbeatInterval() + "ms\n"); |
| | | imsClient.sendMsg(heartbeatMsg, false); |
| | | } |
| | | } |
| | | } |
| | | @Override |
| | | public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { |
| | | super.userEventTriggered(ctx, evt); |
| | | if (evt instanceof IdleStateEvent){ |
| | | IdleState state = ((IdleStateEvent)evt).state(); |
| | | switch (state){ |
| | | case READER_IDLE: |
| | | // 规定时间内没收到服务端心跳包响应,进行重连操作 |
| | | break; |
| | | case WRITER_IDLE: |
| | | // 规定时间内没向服务端发送心跳包,即发送一个心跳包 |
| | | if (heartbeatTask == null) { |
| | | heartbeatTask = new HeartbeatTask(ctx); |
| | | } |
| | | imsClient.getLoopGroup().execWorkTask(heartbeatTask); |
| | | break; |
| | | |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | public static final int CONNECT_STATE_FAILURE = -1; |
| | | public static final int DEFAULT_RECONNECT_BASE_DELAY_TIME = 3 * 1000; |
| | | public static final int DEFAULT_CONNECT_TIMEOUT = 10 * 1000; |
| | | public static final int DEFAULT_HEARTBEAT_INTERVAL_FOREGROUND = 3 * 1000; |
| | | public static final int DEFAULT_HEARTBEAT_INTERVAL_FOREGROUND = 10 * 1000; |
| | | public static final int DEFAULT_HEARTBEAT_INTERVAL_BACKGROUND = 30 * 1000; |
| | | |
| | | /*** 应用在前台标识***/ |
| | |
| | | |
| | | //获取注册消息 |
| | | byte[] getRegisterMessage(); |
| | | |
| | | byte[] getHeartbeatMsg(); |
| | | |
| | | void addHeartbeatHandler(int seconds); |
| | | } |
| | |
| | | int getReConnectInterval(); |
| | | //注册消息 |
| | | byte[] getRegisterMessage(); |
| | | |
| | | byte[] getHearbeatMsg(); |
| | | } |
| | |
| | | |
| | | import java.nio.ByteBuffer; |
| | | import java.util.Vector; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.function.ToDoubleBiFunction; |
| | | |
| | | import io.netty.bootstrap.Bootstrap; |
| | |
| | | // TODO: 2019/12/12 根据MSG_ID 来加入 超时, 暂不写 |
| | | |
| | | if (channel == null){ |
| | | Log.i(TAG, "sendMsg fail,channel为空"+msg); |
| | | MyLog.i("PlatformMessage", "sendMsg fail,channel为空"+msg); |
| | | } |
| | | try { |
| | | MyLog.i("PlatformMessage", "sendMsg: "+ BytesUtils.bytesToHexString(msg)); |
| | |
| | | byteBuf.writeBytes(msg); |
| | | channel.writeAndFlush(byteBuf); |
| | | } catch (Exception e) { |
| | | Log.i(TAG, "发送消息失败,reason="+e.getMessage()+"\t"+msg); |
| | | Log.i("PlatformMessage", "发送消息失败,reason="+e.getMessage()+"\t"+msg); |
| | | } |
| | | } |
| | | |
| | |
| | | return mOnEventListener.getRegisterMessage(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public byte[] getHeartbeatMsg() { |
| | | if (mOnEventListener != null){ |
| | | return mOnEventListener.getHearbeatMsg(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 增加心跳, |
| | | * @param heartBeatInterval 秒 |
| | | */ |
| | | @Override |
| | | public void addHeartbeatHandler(int heartBeatInterval){ |
| | | this.heartBeatInterval = heartBeatInterval*1000; |
| | | _addHeartbeatHandler(); |
| | | } |
| | | |
| | | public int getHeartbeatInterval() { |
| | | return this.heartBeatInterval; |
| | | } |
| | | |
| | | public ExecutorServiceFactory getLoopGroup() { |
| | | return loopGroup; |
| | | } |
| | | |
| | | |
| | |
| | | } |
| | | |
| | | |
| | | private void _addHeartbeatHandler(){ |
| | | MyLog.i("add HeartbeatHandler"); |
| | | if (channel == null || !channel.isActive() || channel.pipeline()==null){ |
| | | MyLog.i("PlatformMessage","添加心跳handler失败"); |
| | | return; |
| | | } |
| | | try { |
| | | if(channel.pipeline().get(IdleStateHandler.class.getSimpleName()) != null){ |
| | | MyLog.i("PlatformMessage","之前存在的读写超时handler,先移除掉,再重新添加"); |
| | | channel.pipeline().remove(IdleStateHandler.class.getSimpleName()); |
| | | } |
| | | // 3次心跳没响应,代表连接已断开 |
| | | //设定IdleStateHandler心跳检测每四秒进行一次写检测,如果四秒内write()方法未被调用则触发一次userEventTrigger()方法,实现客户端每四秒向服务端发送一次消息; |
| | | channel.pipeline().addFirst(IdleStateHandler.class.getSimpleName(), new IdleStateHandler( |
| | | 0, heartBeatInterval, 0, TimeUnit.MILLISECONDS)); |
| | | |
| | | // 重新添加HeartbeatHandler |
| | | if (channel.pipeline().get(HeartbeatHandler.class.getSimpleName()) != null) { |
| | | channel.pipeline().remove(HeartbeatHandler.class.getSimpleName()); |
| | | } |
| | | if (channel.pipeline().get(TCPReadHandler.class.getSimpleName()) != null) { |
| | | MyLog.i("之前存在的先移除掉,再重新添加HeartbeatHandler"); |
| | | channel.pipeline().addBefore(TCPReadHandler.class.getSimpleName(), HeartbeatHandler.class.getSimpleName(), |
| | | new HeartbeatHandler(this)); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | MyLog.i("添加心跳消息管理handler失败,reason:" + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | } |
| | |
| | | try { |
| | | Object value = fieldDefine.field.get(this); |
| | | String hex = fieldDefine.coder.encode(value); |
| | | // Log.i("PlatformMessage", "FieldName: "+fieldDefine.field.getName()+" hex:"+hex); |
| | | if (hex == null) { |
| | | fieldDefine.relativeLengthField.set(this, 0); |
| | | } else { |
| | |
| | | hex = this.getPaddingLeftString(hex,fieldDefine.getFieldLen()*2, BytesUtils.toHexString(fieldDefine.length.paddingByte())); |
| | | } |
| | | } |
| | | // System.out.println(hex); |
| | | // Log.i("PlatformMessage value", "FieldName: "+fieldDefine.field.getName()+" hex:"+hex); |
| | | sb.append(hex); |
| | | } catch (IllegalAccessException e) { |
| | | this.buildException(e.getMessage()); |