|
@@ -1,650 +0,0 @@
|
|
|
-package com.tidecloud.dataacceptance.codec;
|
|
|
-
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileNotFoundException;
|
|
|
-import java.io.FileOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.OutputStream;
|
|
|
-import java.nio.ByteBuffer;
|
|
|
-import java.nio.channels.FileChannel;
|
|
|
-import java.text.SimpleDateFormat;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
-
|
|
|
-import javax.xml.bind.DatatypeConverter;
|
|
|
-
|
|
|
-import org.apache.commons.lang.ArrayUtils;
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
-
|
|
|
-import com.tidecloud.dataacceptance.common.BCD8421Operater;
|
|
|
-import com.tidecloud.dataacceptance.common.BitOperator;
|
|
|
-import com.tidecloud.dataacceptance.common.Constants;
|
|
|
-import com.tidecloud.dataacceptance.common.NumUtil;
|
|
|
-import com.tidecloud.dataacceptance.entity.CanDevice;
|
|
|
-import com.tidecloud.dataacceptance.entity.LocationInfoUploadMsg;
|
|
|
-import com.tidecloud.dataacceptance.entity.LocationSelfInfoUploadMsg;
|
|
|
-import com.tidecloud.dataacceptance.entity.PackageData;
|
|
|
-import com.tidecloud.dataacceptance.entity.PackageData.MsgHeader;
|
|
|
-import com.tidecloud.dataacceptance.entity.TerminalRegisterMsg;
|
|
|
-import com.tidecloud.dataacceptance.entity.TerminalRegisterMsg.TerminalRegInfo;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- * @author: chudk
|
|
|
- * @date: 2017年11月8日 下午4:09:15
|
|
|
- */
|
|
|
-public class MsgDecoder {
|
|
|
-
|
|
|
- private static final String DATA_PATH = "/home/service/collector_6707/rawdata/";
|
|
|
-
|
|
|
- private static File WRITE_FILE = null;
|
|
|
- private static Boolean ISINIT = true;
|
|
|
- private static final Integer TEN_M = 10485760;
|
|
|
- private static final String PREFIX_NAME = "can.";
|
|
|
-
|
|
|
- private static final Logger log = LoggerFactory.getLogger(MsgDecoder.class);
|
|
|
- private static Map<String, Map<Integer, CanDevice>> canDeviceMap = new HashMap<>();
|
|
|
-
|
|
|
- private BitOperator bitOperator;
|
|
|
- private BCD8421Operater bcd8421Operater;
|
|
|
-
|
|
|
- public MsgDecoder() {
|
|
|
- this.bitOperator = new BitOperator();
|
|
|
- this.bcd8421Operater = new BCD8421Operater();
|
|
|
- }
|
|
|
-
|
|
|
- public PackageData bytes2PackageData(byte[] data) {
|
|
|
- PackageData ret = new PackageData();
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- MsgHeader msgHeader = this.parseMsgHeaderFromBytes(data);
|
|
|
- ret.setMsgHeader(msgHeader);
|
|
|
-
|
|
|
- int msgBodyByteStartIndex = 12;
|
|
|
-
|
|
|
-
|
|
|
- if (msgHeader.isHasSubPackage()) {
|
|
|
- msgBodyByteStartIndex = 16;
|
|
|
- }
|
|
|
-
|
|
|
- byte[] tmp = new byte[msgHeader.getMsgBodyLength()];
|
|
|
- System.arraycopy(data, msgBodyByteStartIndex, tmp, 0, tmp.length);
|
|
|
- ret.setMsgBodyBytes(tmp);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- int checkSumInPkg = data[data.length - 1];
|
|
|
- int calculatedCheckSum = this.bitOperator.getCheckSum4JT808(data, 0, data.length - 1);
|
|
|
- ret.setCheckSum(checkSumInPkg);
|
|
|
- if (checkSumInPkg != calculatedCheckSum) {
|
|
|
- log.warn("检验码不一致,msgid:{},pkg:{},calculated:{}", msgHeader.getMsgId(), checkSumInPkg, calculatedCheckSum);
|
|
|
- }
|
|
|
- return ret;
|
|
|
- }
|
|
|
-
|
|
|
- private MsgHeader parseMsgHeaderFromBytes(byte[] data) {
|
|
|
- MsgHeader msgHeader = new MsgHeader();
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- msgHeader.setMsgId(this.parseIntFromBytes(data, 0, 2));
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- int msgBodyProps = this.parseIntFromBytes(data, 2, 2);
|
|
|
- msgHeader.setMsgBodyPropsField(msgBodyProps);
|
|
|
-
|
|
|
- msgHeader.setMsgBodyLength(msgBodyProps & 0x3ff);
|
|
|
-
|
|
|
- msgHeader.setEncryptionType((msgBodyProps & 0x1c00) >> 10);
|
|
|
-
|
|
|
- msgHeader.setHasSubPackage(((msgBodyProps & 0x2000) >> 13) == 1);
|
|
|
-
|
|
|
- msgHeader.setReservedBit(((msgBodyProps & 0xc000) >> 14) + "");
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- msgHeader.setTerminalPhone(this.parseBcdStringFromBytes(data, 4, 6));
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- msgHeader.setFlowId(this.parseIntFromBytes(data, 10, 2));
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- if (msgHeader.isHasSubPackage()) {
|
|
|
-
|
|
|
- msgHeader.setPackageInfoField(this.parseIntFromBytes(data, 12, 4));
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- msgHeader.setTotalSubPackage(this.parseIntFromBytes(data, 12, 2));
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- msgHeader.setSubPackageSeq(this.parseIntFromBytes(data, 12, 2));
|
|
|
- }
|
|
|
- return msgHeader;
|
|
|
- }
|
|
|
-
|
|
|
- protected String parseStringFromBytes(byte[] data, int startIndex, int lenth) {
|
|
|
- return this.parseStringFromBytes(data, startIndex, lenth, null);
|
|
|
- }
|
|
|
-
|
|
|
- private String parseStringFromBytes(byte[] data, int startIndex, int lenth, String defaultVal) {
|
|
|
- try {
|
|
|
- byte[] tmp = new byte[lenth];
|
|
|
- System.arraycopy(data, startIndex, tmp, 0, lenth);
|
|
|
- return new String(tmp, Constants.GBK_CHARSET);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("解析字符串出错:{}", e.getMessage());
|
|
|
- e.printStackTrace();
|
|
|
- return defaultVal;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private String parseBcdStringFromBytes(byte[] data, int startIndex, int lenth) {
|
|
|
- return this.parseBcdStringFromBytes(data, startIndex, lenth, null);
|
|
|
- }
|
|
|
-
|
|
|
- private String parseBcdStringFromBytes(byte[] data, int startIndex, int lenth, String defaultVal) {
|
|
|
- try {
|
|
|
- byte[] tmp = new byte[lenth];
|
|
|
- System.arraycopy(data, startIndex, tmp, 0, lenth);
|
|
|
- return this.bcd8421Operater.bcd2String(tmp);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("解析BCD(8421码)出错:{}", e.getMessage());
|
|
|
- e.printStackTrace();
|
|
|
- return defaultVal;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private int parseIntFromBytes(byte[] data, int startIndex, int length) {
|
|
|
- return this.parseIntFromBytes(data, startIndex, length, 0);
|
|
|
- }
|
|
|
-
|
|
|
- private int parseIntFromBytes(byte[] data, int startIndex, int length, int defaultVal) {
|
|
|
- try {
|
|
|
-
|
|
|
- final int len = length > 4 ? 4 : length;
|
|
|
- byte[] tmp = new byte[len];
|
|
|
- System.arraycopy(data, startIndex, tmp, 0, len);
|
|
|
- return bitOperator.byteToInteger(tmp);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("解析整数出错:{}", e.getMessage());
|
|
|
- return defaultVal;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public TerminalRegisterMsg toTerminalRegisterMsg(PackageData packageData) {
|
|
|
- TerminalRegisterMsg ret = new TerminalRegisterMsg(packageData);
|
|
|
- byte[] data = ret.getMsgBodyBytes();
|
|
|
-
|
|
|
- TerminalRegInfo body = new TerminalRegInfo();
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- body.setProvinceId(this.parseIntFromBytes(data, 0, 2));
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- body.setCityId(this.parseIntFromBytes(data, 2, 2));
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- body.setManufacturerId(this.parseStringFromBytes(data, 4, 5));
|
|
|
-
|
|
|
-
|
|
|
- body.setTerminalType(this.parseStringFromBytes(data, 9, 20));
|
|
|
-
|
|
|
-
|
|
|
- body.setTerminalId(this.parseStringFromBytes(data, 29, 7));
|
|
|
-
|
|
|
-
|
|
|
- body.setLicensePlateColor(this.parseIntFromBytes(data, 36, 1));
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- ret.setTerminalRegInfo(body);
|
|
|
- return ret;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public LocationSelfInfoUploadMsg toSelfLocationInfoUploadMsg(PackageData packageData) throws Exception{
|
|
|
- LocationSelfInfoUploadMsg ret = new LocationSelfInfoUploadMsg(packageData);
|
|
|
- final byte[] data = ret.getMsgBodyBytes();
|
|
|
- String dataStr = DatatypeConverter.printHexBinary(data);
|
|
|
- log.info("data is [{}]", dataStr);
|
|
|
-
|
|
|
- int subProtocal = this.parseIntFromBytes(data, 0, 3);
|
|
|
-
|
|
|
- int beforeComparssLength = this.parseIntFromBytes(data, 2, 2);
|
|
|
-
|
|
|
- byte[] realData = Arrays.copyOfRange(data, 5, data.length);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- return ret;
|
|
|
- }
|
|
|
-
|
|
|
- private CanDevice setCanDevcieFiled(String deviceId, Integer date, int timeDiff) {
|
|
|
- Integer timeStamp = date - timeDiff;
|
|
|
- Map<Integer, CanDevice> timeDiffCanDeviceMap = canDeviceMap.get(deviceId);
|
|
|
- if (timeDiffCanDeviceMap == null) {
|
|
|
- timeDiffCanDeviceMap = new HashMap<>();
|
|
|
- CanDevice canDevice = new CanDevice();
|
|
|
- timeDiffCanDeviceMap.put(timeStamp, canDevice);
|
|
|
- canDeviceMap.put(deviceId, timeDiffCanDeviceMap);
|
|
|
- canDevice.setDeviceId(deviceId);
|
|
|
- canDevice.setDate(timeStamp);
|
|
|
- return canDevice;
|
|
|
- } else {
|
|
|
- CanDevice canDeviceInMap = timeDiffCanDeviceMap.get(timeStamp);
|
|
|
- if (canDeviceInMap == null) {
|
|
|
- canDeviceInMap = new CanDevice();
|
|
|
- timeDiffCanDeviceMap.put(timeStamp, canDeviceInMap);
|
|
|
- canDeviceInMap.setDeviceId(deviceId);
|
|
|
- canDeviceInMap.setDate(timeStamp);
|
|
|
- }
|
|
|
- return canDeviceInMap;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public LocationInfoUploadMsg toLocationInfoUploadMsg(PackageData packageData) {
|
|
|
- LocationInfoUploadMsg ret = new LocationInfoUploadMsg(packageData);
|
|
|
- final byte[] data = ret.getMsgBodyBytes();
|
|
|
-
|
|
|
- ret.setWarningFlagField(this.parseIntFromBytes(data, 0, 3));
|
|
|
-
|
|
|
- int states = this.parseIntFromBytes(data, 4, 4);
|
|
|
- ret.setStatusField(states);
|
|
|
-
|
|
|
- ret.setLatitude(this.parseIntFromBytes(data, 8, 4));
|
|
|
-
|
|
|
- ret.setLongitude(this.parseIntFromBytes(data, 12, 4));
|
|
|
-
|
|
|
- ret.setElevation(this.parseIntFromBytes(data, 16, 2));
|
|
|
-
|
|
|
- ret.setSpeed(this.parseIntFromBytes(data, 18, 2));
|
|
|
-
|
|
|
- ret.setDirection(this.parseIntFromBytes(data, 20, 2));
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- byte[] tmp = new byte[6];
|
|
|
- System.arraycopy(data, 22, tmp, 0, 6);
|
|
|
- String time = this.parseBcdStringFromBytes(data, 22, 6);
|
|
|
- ret.setTime(time);
|
|
|
-
|
|
|
- byte[] additionalInfo = Arrays.copyOfRange(data, 28, data.length);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- setAdditionalInfo(ret, additionalInfo);
|
|
|
- ret.setDeviceId(packageData.getMsgHeader().getTerminalPhone());
|
|
|
- dataStorage(LocationInfoUploadMsg.buildLocationInfo2Str(ret));
|
|
|
- return ret;
|
|
|
- }
|
|
|
-
|
|
|
- private void setAdditionalInfo(LocationInfoUploadMsg ret, byte[] additionalInfo) {
|
|
|
- while (additionalInfo.length > 0) {
|
|
|
- int type = this.parseIntFromBytes(additionalInfo, 0, 1);
|
|
|
- int length = this.parseIntFromBytes(additionalInfo, 1, 1);
|
|
|
- if (Constants.LocationPackage.TYPE_OF_MILEAGE.equals(type)) {
|
|
|
- ret.setMileage(this.parseIntFromBytes(additionalInfo, 2, length));
|
|
|
- additionalInfo = Arrays.copyOfRange(additionalInfo, length + 2, additionalInfo.length);
|
|
|
- } else if (Constants.LocationPackage.TYPE_OF_OIL.equals(type)) {
|
|
|
- ret.setOil(this.parseIntFromBytes(additionalInfo, 2, length));
|
|
|
- additionalInfo = Arrays.copyOfRange(additionalInfo, length + 2, additionalInfo.length);
|
|
|
- } else if (Constants.LocationPackage.TYPE_OF_SPEED.equals(type)) {
|
|
|
- int speed = this.parseIntFromBytes(additionalInfo, 2, length);
|
|
|
- if (speed>0&&ret.getSpeed()==0)
|
|
|
- ret.setSpeed(this.parseIntFromBytes(additionalInfo, 2, length));
|
|
|
- additionalInfo = Arrays.copyOfRange(additionalInfo, length + 2, additionalInfo.length);
|
|
|
- } else if (Constants.LocationPackage.TYPE_OF_WARN_ID.equals(type)) {
|
|
|
-
|
|
|
- additionalInfo = Arrays.copyOfRange(additionalInfo, length + 2, additionalInfo.length);
|
|
|
- } else if (Constants.LocationPackage.TYPE_OF_RUN_WARN.equals(type)) {
|
|
|
- additionalInfo = Arrays.copyOfRange(additionalInfo, length + 2, additionalInfo.length);
|
|
|
- } else if (Constants.LocationPackage.TYPE_OF_ADDITIONAL_STATE.equals(type)) {
|
|
|
- additionalInfo = Arrays.copyOfRange(additionalInfo, length + 2, additionalInfo.length);
|
|
|
- } else if (Constants.LocationPackage.TYPE_OF_IO_STATE.equals(type)) {
|
|
|
- additionalInfo = Arrays.copyOfRange(additionalInfo, length + 2, additionalInfo.length);
|
|
|
- } else if (Constants.LocationPackage.TYPE_OF_SIMULATION.equals(type)) {
|
|
|
- additionalInfo = Arrays.copyOfRange(additionalInfo, length + 2, additionalInfo.length);
|
|
|
- } else if (Constants.LocationPackage.TYPE_OF_SIGNAL_STRENGTH.equals(type)) {
|
|
|
- additionalInfo = Arrays.copyOfRange(additionalInfo, length + 2, additionalInfo.length);
|
|
|
- } else if (Constants.LocationPackage.TYPE_OF_SATELLITE.equals(type)) {
|
|
|
- additionalInfo = Arrays.copyOfRange(additionalInfo, length + 2, additionalInfo.length);
|
|
|
- } else if (Constants.LocationPackage.TYPE_OF_CUSTOM.equals(type)) {
|
|
|
- byte[] subAdditionalInfo = Arrays.copyOfRange(additionalInfo, 2, length);
|
|
|
- additionalInfo = Arrays.copyOfRange(additionalInfo, length + 2, additionalInfo.length);
|
|
|
- while (subAdditionalInfo.length > 0) {
|
|
|
- int subLength = this.parseIntFromBytes(subAdditionalInfo, 0, 2);
|
|
|
- if (Constants.LocationPackage.LENGTH_OF_OIL.equals(subLength)) {
|
|
|
-
|
|
|
-
|
|
|
- ret.setFuel(getFuel(Arrays.copyOfRange(subAdditionalInfo, 4, 10)));
|
|
|
-
|
|
|
- }
|
|
|
- if (additionalInfo.length >= 2 + subLength) {
|
|
|
- subAdditionalInfo = Arrays.copyOfRange(subAdditionalInfo, 2 + subLength, subAdditionalInfo.length);
|
|
|
- } else {
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- } else if (Constants.LocationPackage.TYPE_OF_CUSTOM_WAGNRUI.equals(type)) {
|
|
|
- byte[] subAdditionalInfo = Arrays.copyOfRange(additionalInfo, 2, length+2);
|
|
|
- additionalInfo = Arrays.copyOfRange(additionalInfo, length + 2, additionalInfo.length);
|
|
|
- while (subAdditionalInfo.length > 0) {
|
|
|
- int subCmd = this.parseIntFromBytes(subAdditionalInfo, 0, 1);
|
|
|
- int subLength = this.parseIntFromBytes(subAdditionalInfo, 1, 1);
|
|
|
- if (Constants.LocationPackage.TYPE_OF_CUSTOM_0XFC_0X01.intValue() == subCmd) {
|
|
|
-
|
|
|
- if (subLength == 0x02) {
|
|
|
- int status = parseIntFromBytes(subAdditionalInfo,2,2);
|
|
|
-
|
|
|
- int oilExceptWarning = (status & 0x01);
|
|
|
- ret.setOilExceptWarning(oilExceptWarning);
|
|
|
-
|
|
|
- int timeoutWaitWarning = (status & 0x02) >> 1;
|
|
|
- ret.setTimeoutWaitWarning(timeoutWaitWarning);
|
|
|
-
|
|
|
- int oilDeviceWarning = (status & 0x04) >> 2;
|
|
|
- ret.setOilDeviceWarning(oilDeviceWarning);
|
|
|
- }
|
|
|
- } else if (Constants.LocationPackage.TYPE_OF_CUSTOM_0XFC_0X02.intValue() == subCmd) {
|
|
|
-
|
|
|
- if (subLength == 0x0C) {
|
|
|
-
|
|
|
- int accumulatedFdjWorkTime = parseIntFromBytes(subAdditionalInfo,2,4);
|
|
|
- ret.setAccumulatedFdjWorkTime(accumulatedFdjWorkTime);
|
|
|
-
|
|
|
- int accumulatedWorkTime = parseIntFromBytes(subAdditionalInfo,6,4);
|
|
|
- ret.setAccumulatedWorkTime(accumulatedWorkTime);
|
|
|
-
|
|
|
- int accumulatedWaitWorkTime = parseIntFromBytes(subAdditionalInfo,10,4);
|
|
|
- ret.setAccumulatedWaitWorkTime(accumulatedWaitWorkTime);
|
|
|
- }
|
|
|
- } else if (Constants.LocationPackage.TYPE_OF_CUSTOM_0XFC_0X03.intValue() == subCmd) {
|
|
|
-
|
|
|
- if (subLength == 0x0C) {
|
|
|
-
|
|
|
- int accumulatedOilConsume = parseIntFromBytes(subAdditionalInfo,2,4);
|
|
|
- ret.setAccumulatedOilConsume(accumulatedOilConsume);
|
|
|
-
|
|
|
- int accumulatedWorkOilConsume = parseIntFromBytes(subAdditionalInfo,6,4);
|
|
|
- ret.setAccumulatedWorkOilConsume(accumulatedWorkOilConsume);
|
|
|
-
|
|
|
- int accumulatedWaitOilConsume= parseIntFromBytes(subAdditionalInfo,10,4);
|
|
|
- ret.setAccumulatedWaitOilConsume(accumulatedWaitOilConsume);
|
|
|
- }
|
|
|
- } else if (Constants.LocationPackage.TYPE_OF_CUSTOM_0XFC_0X04.intValue() == subCmd) {
|
|
|
-
|
|
|
- if (subLength == 0x08) {
|
|
|
-
|
|
|
- int oilConsumeOf100KM = parseIntFromBytes(subAdditionalInfo,2,4);
|
|
|
- ret.setOilConsumeOf100KM(oilConsumeOf100KM);
|
|
|
-
|
|
|
- int oilConsumeOf1Hour = parseIntFromBytes(subAdditionalInfo,6,4);
|
|
|
- ret.setOilConsumeOf1Hour(oilConsumeOf1Hour);
|
|
|
-
|
|
|
- }
|
|
|
- } else if (Constants.LocationPackage.TYPE_OF_CUSTOM_0XFC_0X05.intValue() == subCmd) {
|
|
|
- if (subLength == 0x02) {
|
|
|
-
|
|
|
- int status = parseIntFromBytes(subAdditionalInfo,2,2);
|
|
|
-
|
|
|
-
|
|
|
- int openStatus = (status & 0x01);
|
|
|
- ret.setOpenStatus(openStatus);
|
|
|
-
|
|
|
- int runningStatus = (status & 0x02) >> 1;
|
|
|
- ret.setRunningStatus(runningStatus);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (subAdditionalInfo.length >= 2 + subLength) {
|
|
|
- subAdditionalInfo = Arrays.copyOfRange(subAdditionalInfo, 2 + subLength, subAdditionalInfo.length);
|
|
|
- } else {
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- }else {
|
|
|
- additionalInfo = Arrays.copyOfRange(additionalInfo, length + 2, additionalInfo.length);
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private String getFuel(byte[] copyOfRange) {
|
|
|
- String printHexBinary = DatatypeConverter.printHexBinary(copyOfRange);
|
|
|
- System.out.println(printHexBinary);
|
|
|
- String fuel = "";
|
|
|
- for (byte b : copyOfRange) {
|
|
|
- if (Constants.LocationPackage.POINT.equals(b)) {
|
|
|
- fuel += ".";
|
|
|
- }else {
|
|
|
- fuel += b & 0xf;
|
|
|
- }
|
|
|
- }
|
|
|
- System.out.println(fuel);
|
|
|
- return fuel;
|
|
|
- }
|
|
|
-
|
|
|
- public static void main(String[] args) {
|
|
|
- for (int i = 0; i < 1000; i++) {
|
|
|
- LocationInfoUploadMsg infoUploadMsg = new LocationInfoUploadMsg();
|
|
|
- infoUploadMsg.setDeviceId("14142184169");
|
|
|
- infoUploadMsg.setTime("180119160845");
|
|
|
- infoUploadMsg.setLatitude(34000000);
|
|
|
- infoUploadMsg.setLongitude(12000000);
|
|
|
- infoUploadMsg.setSpeed(11);
|
|
|
- infoUploadMsg.setMileage(133);
|
|
|
- infoUploadMsg.setOil(14);
|
|
|
- infoUploadMsg.setFuel("13");
|
|
|
- dataStorage(LocationInfoUploadMsg.buildLocationInfo2Str(infoUploadMsg));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private static FileChannel fc = null;
|
|
|
-
|
|
|
- private static synchronized FileChannel getFileChannel(File file) {
|
|
|
- if (fc == null) {
|
|
|
- try {
|
|
|
- FileOutputStream fos = new FileOutputStream(file, true);
|
|
|
- fc = fos.getChannel();
|
|
|
- } catch (FileNotFoundException e) {
|
|
|
- log.error(e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
- return fc;
|
|
|
- }
|
|
|
- private static synchronized void dataStorage(String deviceStr) {
|
|
|
- File path = new File(DATA_PATH);
|
|
|
- File[] listFiles = path.listFiles();
|
|
|
- if (ISINIT) {
|
|
|
- for (File sonFile : listFiles) {
|
|
|
- String name = sonFile.getName();
|
|
|
- if (name != null && name.startsWith(PREFIX_NAME)) {
|
|
|
- long len = sonFile.length();
|
|
|
- if (len < TEN_M) {
|
|
|
- WRITE_FILE = sonFile;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- ISINIT = false;
|
|
|
- }
|
|
|
- if (WRITE_FILE == null || WRITE_FILE.length() > TEN_M) {
|
|
|
- String fileName = DATA_PATH + PREFIX_NAME + new SimpleDateFormat("yyMMddHHmmss").format(new Date());
|
|
|
- WRITE_FILE = new File(fileName);
|
|
|
- writeDevice2File(WRITE_FILE, deviceStr);
|
|
|
- log.info("touch new file :" + deviceStr + "timestamp:" + new SimpleDateFormat("yyMMddHHmmss").format(new Date()));
|
|
|
- }else{
|
|
|
- writeDevice2File(WRITE_FILE, deviceStr);
|
|
|
- log.info("write data into file : " + deviceStr);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public static void writeDevice2File(File file, String deviceStr){
|
|
|
- FileOutputStream fos = null;
|
|
|
- try {
|
|
|
- fos = new FileOutputStream(file, true);
|
|
|
- Integer length = deviceStr.getBytes().length;
|
|
|
- byte[] lengthBytes = NumUtil.int2bytes(length);
|
|
|
- byte[] deviceBytes = deviceStr.getBytes();
|
|
|
- byte[] dataBytes = ArrayUtils.addAll(lengthBytes, deviceBytes);
|
|
|
-
|
|
|
- FileChannel fc = fos.getChannel();
|
|
|
-
|
|
|
- ByteBuffer bbf = ByteBuffer.wrap(dataBytes);
|
|
|
-
|
|
|
- fc.write(bbf) ;
|
|
|
- fc.close();
|
|
|
- fos.flush();
|
|
|
- } catch (IOException e) {
|
|
|
- log.error(e.getMessage());
|
|
|
- } finally {
|
|
|
- if (fos != null) {
|
|
|
- try {
|
|
|
- fos.close();
|
|
|
- } catch (IOException e) {
|
|
|
- log.error(e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|