package com.ld.igds.protocol.gps.analysis; import com.ld.igds.io.notify.NotifyGpsMacService; import com.ld.igds.oa.dto.GpsData; import com.ld.igds.protocol.gps.Gps808ServerEngine; import com.ld.igds.protocol.utl.GpsUtils; import com.ld.igds.util.BytesUtil; import com.ld.io.api.InvokeResult; import com.ld.io.api.IoSession; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** * GPS-808协议鉴权 */ @Slf4j @Component(AnalysisAuthInfo.BEAN_ID) public class AnalysisAuthInfo { public static final String BEAN_ID = "gps.analysisAuthInfo"; @Autowired private NotifyGpsMacService notifyGpsMacService; /** * 7E 01 02 00 06 01 38 20 03 63 16 01 82 01 38 20 03 63 16 86 7E (21) * 7E * 0102 消息id * 0006 消息体属性 * 01 38 20 03 63 16 手机卡号 * 01 82 消息流水号 * 01 38 20 03 63 16 鉴权码 * 86 检验码 * 7E * 鉴权通过则回复通用应答 * @param ip * @param port * @param message */ public void analysisAuthInfo(IoSession ioSession, String ip, Integer port, String message) { //01 02 00 06 01 38 20 03 63 16 01 82 01 38 20 03 63 16 86 String sn = message.substring(24, 36); String tempPhone = message.substring(8, 20); GpsData data = new GpsData(); data.setPhone(tempPhone); data.setIp(ip); data.setPort(port); data.setSn(sn); boolean flag = notifyGpsMacService.authSn(data); if(flag){ //鉴权通过,将此次鉴权的ip和port信息更新到缓存和数据库 notifyGpsMacService.registerAndAuthNotify(data); //鉴权应答 authResponse(ip, port, message); }else { //不通过则直接断开连接 log.debug("鉴权不通过"); ioSession.destroy(); } } /** * 鉴权应答(通用应答) * * @param ip * @param port * @param message */ private static void authResponse(String ip, Integer port, String message) { //01 02 00 06 01 38 20 03 63 16 01 82 01 38 20 03 63 16 86 //应答:7E 80 01 00 05 01 38 20 03 63 16 01 AE 01 AE 01 02 00 E4 7E String msg = ""; //消息头信息(消息ID+消息体属性+手机卡号+鉴权消息流水号) msg += GpsUtils.MSG_22; msg += "0005"; msg += message.substring(8, 20); msg += message.substring(20, 24); //消息体信息(注册消息流水号+鉴权消息ID+消息成功应答) msg += message.substring(20, 24); msg += message.substring(0, 4); msg += "00"; //获取校验码 String checkCode = GpsUtils.checkCode(msg); msg += checkCode; //对消息整体转义:7D-->7D01,7E-->7D02 msg = GpsUtils.escapeCode1(msg); //首尾添加标识符 msg = GpsUtils.addIdentifier(msg); //鉴权应答 byte[] mess = BytesUtil.hexStrToBytes(msg); InvokeResult result = Gps808ServerEngine.push(ip, port, mess); log.debug("鉴权通过,发送应答结果:{}", result.toString()); } }