diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2019-05-20 07:04:15 +0300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2019-05-20 07:04:15 +0300 |
commit | d395f5b42fd261265ca52b1674f17883b8c251c9 (patch) | |
tree | dc973d7bd798ade657e44fed60c93d094e9d89a2 | |
parent | 9f6ab9f7934cec76247e1a3e0b0ae4a0f099a5d9 (diff) | |
download | traccar-server-d395f5b42fd261265ca52b1674f17883b8c251c9.tar.gz traccar-server-d395f5b42fd261265ca52b1674f17883b8c251c9.tar.bz2 traccar-server-d395f5b42fd261265ca52b1674f17883b8c251c9.zip |
Refactor Sinocastel decoder
-rw-r--r-- | src/main/java/org/traccar/protocol/CastelProtocolDecoder.java | 227 | ||||
-rw-r--r-- | src/test/java/org/traccar/protocol/CastelProtocolDecoderTest.java | 6 |
2 files changed, 119 insertions, 114 deletions
diff --git a/src/main/java/org/traccar/protocol/CastelProtocolDecoder.java b/src/main/java/org/traccar/protocol/CastelProtocolDecoder.java index 0541adf6f..1f67b6a77 100644 --- a/src/main/java/org/traccar/protocol/CastelProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/CastelProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2019 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -84,6 +84,7 @@ public class CastelProtocolDecoder extends BaseProtocolDecoder { public static final short MSG_SC_HEARTBEAT_RESPONSE = (short) 0x9003; public static final short MSG_SC_GPS = 0x4001; public static final short MSG_SC_PID_DATA = 0x4002; + public static final short MSG_SC_G_SENSOR = 0x4003; public static final short MSG_SC_SUPPORTED_PID = 0x4004; public static final short MSG_SC_OBD_DATA = 0x4005; public static final short MSG_SC_DTCS_PASSENGER = 0x4006; @@ -290,152 +291,150 @@ public class CastelProtocolDecoder extends BaseProtocolDecoder { Channel channel, SocketAddress remoteAddress, ByteBuf buf, int version, ByteBuf id, short type, DeviceSession deviceSession) { - if (type == MSG_SC_HEARTBEAT) { - - sendResponse(channel, remoteAddress, version, id, MSG_SC_HEARTBEAT_RESPONSE, null); - - } else if (type == MSG_SC_LOGIN || type == MSG_SC_LOGOUT || type == MSG_SC_GPS - || type == MSG_SC_ALARM || type == MSG_SC_CURRENT_LOCATION || type == MSG_SC_FUEL) { - - if (type == MSG_SC_LOGIN) { - ByteBuf response = Unpooled.buffer(10); - response.writeIntLE(0xFFFFFFFF); - response.writeShortLE(0); - response.writeIntLE((int) (System.currentTimeMillis() / 1000)); - sendResponse(channel, remoteAddress, version, id, MSG_SC_LOGIN_RESPONSE, response); - } - - if (type == MSG_SC_GPS) { - buf.readUnsignedByte(); // historical - } else if (type == MSG_SC_ALARM) { - buf.readUnsignedIntLE(); // alarm - } else if (type == MSG_SC_CURRENT_LOCATION) { - buf.readUnsignedShortLE(); - } - - buf.readUnsignedIntLE(); // ACC ON time - buf.readUnsignedIntLE(); // UTC time - long odometer = buf.readUnsignedIntLE(); - long tripOdometer = buf.readUnsignedIntLE(); - long fuelConsumption = buf.readUnsignedIntLE(); - buf.readUnsignedShortLE(); // current fuel consumption - long status = buf.readUnsignedIntLE(); - buf.skipBytes(8); - - int count = buf.readUnsignedByte(); + Position position; + + switch (type) { + + case MSG_SC_HEARTBEAT: + sendResponse(channel, remoteAddress, version, id, MSG_SC_HEARTBEAT_RESPONSE, null); + return null; + + case MSG_SC_LOGIN: + case MSG_SC_LOGOUT: + case MSG_SC_GPS: + case MSG_SC_ALARM: + case MSG_SC_CURRENT_LOCATION: + case MSG_SC_FUEL: + if (type == MSG_SC_LOGIN) { + ByteBuf response = Unpooled.buffer(10); + response.writeIntLE(0xFFFFFFFF); + response.writeShortLE(0); + response.writeIntLE((int) (System.currentTimeMillis() / 1000)); + sendResponse(channel, remoteAddress, version, id, MSG_SC_LOGIN_RESPONSE, response); + } - List<Position> positions = new LinkedList<>(); + if (type == MSG_SC_GPS) { + buf.readUnsignedByte(); // historical + } else if (type == MSG_SC_ALARM) { + buf.readUnsignedIntLE(); // alarm + } else if (type == MSG_SC_CURRENT_LOCATION) { + buf.readUnsignedShortLE(); + } - for (int i = 0; i < count; i++) { - Position position = readPosition(deviceSession, buf); - position.set(Position.KEY_ODOMETER, odometer); - position.set(Position.KEY_ODOMETER_TRIP, tripOdometer); - position.set(Position.KEY_FUEL_CONSUMPTION, fuelConsumption); - position.set(Position.KEY_STATUS, status); - positions.add(position); - } + buf.readUnsignedIntLE(); // ACC ON time + buf.readUnsignedIntLE(); // UTC time + long odometer = buf.readUnsignedIntLE(); + long tripOdometer = buf.readUnsignedIntLE(); + long fuelConsumption = buf.readUnsignedIntLE(); + buf.readUnsignedShortLE(); // current fuel consumption + long status = buf.readUnsignedIntLE(); + buf.skipBytes(8); + + int count = buf.readUnsignedByte(); + + List<Position> positions = new LinkedList<>(); + + for (int i = 0; i < count; i++) { + position = readPosition(deviceSession, buf); + position.set(Position.KEY_ODOMETER, odometer); + position.set(Position.KEY_ODOMETER_TRIP, tripOdometer); + position.set(Position.KEY_FUEL_CONSUMPTION, fuelConsumption); + position.set(Position.KEY_STATUS, status); + positions.add(position); + } - if (type == MSG_SC_ALARM) { - int alarmCount = buf.readUnsignedByte(); - for (int i = 0; i < alarmCount; i++) { - if (buf.readUnsignedByte() != 0) { - int alarm = buf.readUnsignedByte(); - for (Position position : positions) { - decodeAlarm(position, alarm); + if (type == MSG_SC_ALARM) { + int alarmCount = buf.readUnsignedByte(); + for (int i = 0; i < alarmCount; i++) { + if (buf.readUnsignedByte() != 0) { + int alarm = buf.readUnsignedByte(); + for (Position p : positions) { + decodeAlarm(p, alarm); + } + buf.readUnsignedShortLE(); // description + buf.readUnsignedShortLE(); // threshold } - buf.readUnsignedShortLE(); // description - buf.readUnsignedShortLE(); // threshold + } + } else if (type == MSG_SC_FUEL) { + for (Position p : positions) { + p.set(Position.PREFIX_ADC + 1, buf.readUnsignedShortLE()); } } - } else if (type == MSG_SC_FUEL) { - for (Position position : positions) { - position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShortLE()); - } - } - - if (!positions.isEmpty()) { - return positions; - } - } else if (type == MSG_SC_GPS_SLEEP) { + return positions.isEmpty() ? null : positions; - buf.readUnsignedIntLE(); // device time + case MSG_SC_GPS_SLEEP: + buf.readUnsignedIntLE(); // device time + return readPosition(deviceSession, buf); - return readPosition(deviceSession, buf); - - } else if (type == MSG_SC_AGPS_REQUEST) { - - return readPosition(deviceSession, buf); + case MSG_SC_AGPS_REQUEST: + return readPosition(deviceSession, buf); - } else if (type == MSG_SC_PID_DATA) { + case MSG_SC_PID_DATA: + position = createPosition(deviceSession); - Position position = createPosition(deviceSession); + decodeStat(position, buf); - decodeStat(position, buf); + buf.readUnsignedShortLE(); // sample rate + decodeObd(position, buf, true); - buf.readUnsignedShortLE(); // sample rate - decodeObd(position, buf, true); + return position; - return position; + case MSG_SC_DTCS_PASSENGER: + position = createPosition(deviceSession); - } else if (type == MSG_SC_DTCS_PASSENGER) { + decodeStat(position, buf); - Position position = createPosition(deviceSession); + buf.readUnsignedByte(); // flag + position.add(ObdDecoder.decodeCodes(ByteBufUtil.hexDump(buf.readSlice(buf.readUnsignedByte())))); - decodeStat(position, buf); + return position; - buf.readUnsignedByte(); // flag - position.add(ObdDecoder.decodeCodes(ByteBufUtil.hexDump(buf.readSlice(buf.readUnsignedByte())))); + case MSG_SC_OBD_DATA: + position = createPosition(deviceSession); - return position; + decodeStat(position, buf); - } else if (type == MSG_SC_OBD_DATA) { - - Position position = createPosition(deviceSession); - - decodeStat(position, buf); - - buf.readUnsignedByte(); // flag - decodeObd(position, buf, false); - - return position; + buf.readUnsignedByte(); // flag + decodeObd(position, buf, false); - } else if (type == MSG_SC_CELL) { + return position; - Position position = createPosition(deviceSession); + case MSG_SC_CELL: + position = createPosition(deviceSession); - decodeStat(position, buf); + decodeStat(position, buf); - position.setNetwork(new Network( - CellTower.fromLacCid(buf.readUnsignedShortLE(), buf.readUnsignedShortLE()))); + position.setNetwork(new Network( + CellTower.fromLacCid(buf.readUnsignedShortLE(), buf.readUnsignedShortLE()))); - return position; + return position; - } else if (type == MSG_SC_QUERY_RESPONSE) { + case MSG_SC_QUERY_RESPONSE: + position = createPosition(deviceSession); - Position position = createPosition(deviceSession); + buf.readUnsignedShortLE(); // index + buf.readUnsignedByte(); // response count + buf.readUnsignedByte(); // response index - buf.readUnsignedShortLE(); // index - buf.readUnsignedByte(); // response count - buf.readUnsignedByte(); // response index + int failureCount = buf.readUnsignedByte(); + for (int i = 0; i < failureCount; i++) { + buf.readUnsignedShortLE(); // tag + } - int failureCount = buf.readUnsignedByte(); - for (int i = 0; i < failureCount; i++) { - buf.readUnsignedShortLE(); // tag - } + int successCount = buf.readUnsignedByte(); + for (int i = 0; i < successCount; i++) { + buf.readUnsignedShortLE(); // tag + position.set(Position.KEY_RESULT, + buf.readSlice(buf.readUnsignedShortLE()).toString(StandardCharsets.US_ASCII)); + } - int successCount = buf.readUnsignedByte(); - for (int i = 0; i < successCount; i++) { - buf.readUnsignedShortLE(); // tag - position.set(Position.KEY_RESULT, - buf.readSlice(buf.readUnsignedShortLE()).toString(StandardCharsets.US_ASCII)); - } + return position; - return position; + default: + return null; } - - return null; } private Object decodeCc( diff --git a/src/test/java/org/traccar/protocol/CastelProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/CastelProtocolDecoderTest.java index 27f503b34..570845c61 100644 --- a/src/test/java/org/traccar/protocol/CastelProtocolDecoderTest.java +++ b/src/test/java/org/traccar/protocol/CastelProtocolDecoderTest.java @@ -11,6 +11,12 @@ public class CastelProtocolDecoderTest extends ProtocolTest { CastelProtocolDecoder decoder = new CastelProtocolDecoder(null); verifyAttributes(decoder, binary( + "404043000432313357503230313830303138323400000000004005f064d95c8365d95c9f2f0100c50200004006000000000000040003440068000000000100f3660d0a")); + + verifyNull(decoder, binary( + "40409c020432313357503230313830303138323400000000004003f064d95c3b66d95c9f2f0100000d0000400600000000000004000345006800000000e80364f0ff3c00f4fff3ff3d00f4fff1ff3d00f4fff7ff3e00f4fffcff3f00f5fffcff3f00f5fffdff3f00f5fffdff3f00f5fffcff3f00f5fffeff3f00f3fffeff3e00f3fffdff3f00f5fffbff3f00f4fffbff3f00f5fffbff4000f5fffdff3e00f4fffdff4000f6fffeff4100f6ffffff3f00f5ffffff3f00f4ffffff3f00f5fffeff4000f5fffeff3f00f4fffcff3e00f5fff6ff3e00f5fff6ff3e00f5fff6ff3f00f6fff4ff3e00f5fff1ff3e00f5fff5ff3e00f5fff6ff3e00f6fff6ff3e00f3ffeeff3c00f1fff2ff3a00e9fff4ff3b00e9fff7ff3d00e5fff7ff3d00e4fff7ff3a00e1ff00003d00e9ff01003f00f4ff03004000f6ff02003f00f8ff00003f00f7ff00003f00fcfffeff4000f8fffeff4100f7ffffff4100f8fffeff3f00f5fffdff4000f6fffdff3b00f6fffdff4000f6fffdff3f00f4fffbff4400f5fffdff4500f3fffcff3d00f7fffcff3f00f5fffbff3f00fafffcff4100f5fffbff3c00f7fffaff3f00f8fffcff3e00f5fffbff3c00f4fffaff3f00f7fffbff4200f8fffcff3c00fefff9ff4000f4fff8ff3d00f3fffaff3d00f7fffbff3f00f7fffbff3c00f8fffaff3f00f6fffaff4100f5ff")); + + verifyAttributes(decoder, binary( "40403a00043231335750323031373030363135360000000000a00200000100000101201100344a474446364545374a4230373632363056ff0d0a")); verifyAttributes(decoder, binary( |