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.RemoteGpsMacServiceImpl; 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 lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** * 注册解析及应答 */ @Slf4j @Component(AnalysisRegisterInfo.BEAN_ID) public class AnalysisRegisterInfo { public static final String BEAN_ID = "gps.analysisRegisterInfo"; @Autowired private NotifyGpsMacService notifyGpsMacService; @Autowired private RemoteGpsMacServiceImpl remoteGpsMacService; /** * 7E 01 00 00 31 01 38 20 03 63 16 00 02 00 00 00 00 00 00 00 00 00 46 32 30 30 28 46 4C 59 2D 44 72 53 70 29 2D 56 32 2E 39 33 30 30 33 36 33 31 36 31 31 33 2E 31 30 36 2E 39 33 2E 35 30 3A 7E(64) * -------消息头------- * 0100 消息ID * 0031 消息体属性(转10进制为49,即消息体长度为49) * 013820036316 手机号 (不进行解析,方便直接使用) * 0002 消息流水号 * -------消息体------- * 0000000000000000004632303028464C592D44725370 * 292D56322E3933303033363331363131332E3130362E * 39332E3530 * 3A 校验码 * * 注册消息解析 * * @param ip * @param port * @param message */ public void analysisRegisterInfo(String ip, Integer port, String message) { //01000031 013820036316 00020000000000000000004632303028464C592D44725370292D56322E3933303033363331363131332E3130362E39332E35303A GpsData data = new GpsData(); //获取手机卡号(即SN) String tempPhone = message.substring(8, 20); data.setPhone(tempPhone); data.setIp(ip); data.setPort(port); data.setName("GPS设备(SN:"+tempPhone+")"); //生成鉴权码,直接使用手机卡号作为鉴权码(不进行解析) data.setSn(tempPhone); data.setProtocol(remoteGpsMacService.getProtocol()); //注册信息更新到数据库和缓存 notifyGpsMacService.registerAndAuthNotify(data); //注册应答 registerResponse(ip, port, message); } /** * 注册应答回复鉴权码 * 注意<发送消息时:消息封装---计算并填充检验码---转义> * @param ip * @param port * @param message */ private static void registerResponse(String ip, Integer port, String message) { //0100 0031 013820036316 0002 0000000000000000004632303028464C592D44725370292D56322E3933303033363331363131332E3130362E39332E35303A //应答:7E 81 00 00 09 01 38 20 03 63 16 00 02 00 02 00 01 38 20 03 63 16 88 7E String msg = ""; //消息头信息(消息ID+消息体属性+手机卡号+注册消息流水号) msg += GpsUtils.MSG_11; msg += "0009"; msg += message.substring(8, 20); msg += message.substring(20, 24); //消息体信息(注册消息流水号+消息成功应答+鉴权码) msg += message.substring(20, 24); msg += "00"; msg += message.substring(8, 20); //获取校验码 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()); } }