use of org.traccar.NetworkMessage in project traccar by tananaev.
the class ProgressProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
int type = buf.readUnsignedShortLE();
// length
buf.readUnsignedShortLE();
if (type == MSG_IDENT || type == MSG_IDENT_FULL) {
// id
buf.readUnsignedIntLE();
int length = buf.readUnsignedShortLE();
buf.skipBytes(length);
length = buf.readUnsignedShortLE();
buf.skipBytes(length);
length = buf.readUnsignedShortLE();
String imei = buf.readSlice(length).toString(StandardCharsets.US_ASCII);
getDeviceSession(channel, remoteAddress, imei);
} else if (type == MSG_POINT || type == MSG_ALARM || type == MSG_LOGMSG) {
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
if (deviceSession == null) {
return null;
}
List<Position> positions = new LinkedList<>();
int recordCount = 1;
if (type == MSG_LOGMSG) {
recordCount = buf.readUnsignedShortLE();
}
for (int j = 0; j < recordCount; j++) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
if (type == MSG_LOGMSG) {
position.set(Position.KEY_ARCHIVE, true);
int subtype = buf.readUnsignedShortLE();
if (subtype == MSG_ALARM) {
position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
}
if (buf.readUnsignedShortLE() > buf.readableBytes()) {
lastIndex += 1;
// workaround for device bug
break;
}
lastIndex = buf.readUnsignedIntLE();
position.set(Position.KEY_INDEX, lastIndex);
} else {
newIndex = buf.readUnsignedIntLE();
}
position.setTime(new Date(buf.readUnsignedIntLE() * 1000));
position.setLatitude(buf.readIntLE() * 180.0 / 0x7FFFFFFF);
position.setLongitude(buf.readIntLE() * 180.0 / 0x7FFFFFFF);
position.setSpeed(buf.readUnsignedIntLE() * 0.01);
position.setCourse(buf.readUnsignedShortLE() * 0.01);
position.setAltitude(buf.readUnsignedShortLE() * 0.01);
int satellites = buf.readUnsignedByte();
position.setValid(satellites >= 3);
position.set(Position.KEY_SATELLITES, satellites);
position.set(Position.KEY_RSSI, buf.readUnsignedByte());
position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE());
long extraFlags = buf.readLongLE();
if (BitUtil.check(extraFlags, 0)) {
int count = buf.readUnsignedShortLE();
for (int i = 1; i <= count; i++) {
position.set(Position.PREFIX_ADC + i, buf.readUnsignedShortLE());
}
}
if (BitUtil.check(extraFlags, 1)) {
int size = buf.readUnsignedShortLE();
position.set("can", buf.toString(buf.readerIndex(), size, StandardCharsets.US_ASCII));
buf.skipBytes(size);
}
if (BitUtil.check(extraFlags, 2)) {
position.set("passenger", ByteBufUtil.hexDump(buf.readSlice(buf.readUnsignedShortLE())));
}
if (type == MSG_ALARM) {
position.set(Position.KEY_ALARM, true);
byte[] response = { (byte) 0xC9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
channel.writeAndFlush(new NetworkMessage(Unpooled.wrappedBuffer(response), remoteAddress));
}
// crc
buf.readUnsignedIntLE();
positions.add(position);
}
requestArchive(channel);
return positions;
}
return null;
}
use of org.traccar.NetworkMessage in project traccar by tananaev.
the class PstProtocolDecoder method sendResponse.
private void sendResponse(Channel channel, SocketAddress remoteAddress, long id, int version, long index, int type) {
if (channel != null) {
ByteBuf response = Unpooled.buffer();
response.writeInt((int) id);
response.writeByte(version);
response.writeInt((int) index);
response.writeByte(MSG_ACK);
response.writeByte(type);
response.writeShort(Checksum.crc16(Checksum.CRC16_XMODEM, response.nioBuffer()));
channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
}
}
use of org.traccar.NetworkMessage in project traccar by tananaev.
the class Pt502ProtocolDecoder method requestPhotoFragment.
private void requestPhotoFragment(Channel channel) {
if (channel != null) {
int offset = photo.writerIndex();
int size = Math.min(photo.writableBytes(), MAX_CHUNK_SIZE);
channel.writeAndFlush(new NetworkMessage("#PHD" + offset + "," + size + "\r\n", channel.remoteAddress()));
}
}
use of org.traccar.NetworkMessage in project traccar by tananaev.
the class RstProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
Parser parser = new Parser(PATTERN, (String) msg);
if (!parser.matches()) {
return null;
}
// archive
parser.next();
String model = parser.next();
String firmware = parser.next();
String serial = parser.next();
int index = parser.nextInt();
int type = parser.nextInt();
if (channel != null) {
String response = "RST;A;" + model + ";" + firmware + ";" + serial + ";" + index + ";6;FIM;";
channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
}
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, serial);
if (deviceSession == null) {
return null;
}
if (parser.hasNext()) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.setDeviceTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS));
position.setFixTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS));
position.setLatitude(parser.nextDouble());
position.setLongitude(parser.nextDouble());
position.setSpeed(UnitsConverter.knotsFromKph(parser.nextInt()));
position.setCourse(parser.nextInt());
position.setAltitude(parser.nextInt());
position.setValid(parser.nextInt() > 0);
position.set(Position.KEY_SATELLITES, parser.nextInt());
position.set(Position.KEY_HDOP, parser.nextInt());
position.set(Position.PREFIX_IN + 1, parser.nextHexInt());
position.set(Position.PREFIX_IN + 2, parser.nextHexInt());
position.set(Position.PREFIX_IN + 3, parser.nextHexInt());
position.set(Position.PREFIX_OUT + 1, parser.nextHexInt());
position.set(Position.PREFIX_OUT + 2, parser.nextHexInt());
position.set(Position.KEY_POWER, parser.nextDouble());
position.set(Position.KEY_BATTERY, parser.nextDouble());
position.set(Position.KEY_ODOMETER, parser.nextInt());
position.set(Position.KEY_RSSI, parser.nextInt());
position.set(Position.PREFIX_TEMP + 1, (int) parser.nextHexInt().byteValue());
int status = (parser.nextHexInt() << 8) + parser.nextHexInt();
position.set(Position.KEY_IGNITION, BitUtil.check(status, 7));
position.set(Position.KEY_STATUS, status);
return position;
} else {
return null;
}
}
use of org.traccar.NetworkMessage in project traccar by tananaev.
the class RuptelaProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
// data length
buf.readUnsignedShort();
String imei = String.format("%015d", buf.readLong());
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
if (deviceSession == null) {
return null;
}
int type = buf.readUnsignedByte();
if (type == MSG_RECORDS || type == MSG_EXTENDED_RECORDS) {
List<Position> positions = new LinkedList<>();
// records left
buf.readUnsignedByte();
int count = buf.readUnsignedByte();
for (int i = 0; i < count; i++) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.setTime(new Date(buf.readUnsignedInt() * 1000));
// timestamp extension
buf.readUnsignedByte();
if (type == MSG_EXTENDED_RECORDS) {
// record extension
buf.readUnsignedByte();
}
// priority (reserved)
buf.readUnsignedByte();
position.setValid(true);
position.setLongitude(buf.readInt() / 10000000.0);
position.setLatitude(buf.readInt() / 10000000.0);
position.setAltitude(buf.readUnsignedShort() / 10.0);
position.setCourse(buf.readUnsignedShort() / 100.0);
position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort()));
position.set(Position.KEY_HDOP, buf.readUnsignedByte() / 10.0);
if (type == MSG_EXTENDED_RECORDS) {
position.set(Position.KEY_EVENT, buf.readUnsignedShort());
} else {
position.set(Position.KEY_EVENT, buf.readUnsignedByte());
}
// Read 1 byte data
int valueCount = buf.readUnsignedByte();
for (int j = 0; j < valueCount; j++) {
int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte();
decodeParameter(position, id, buf, 1);
}
// Read 2 byte data
valueCount = buf.readUnsignedByte();
for (int j = 0; j < valueCount; j++) {
int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte();
decodeParameter(position, id, buf, 2);
}
// Read 4 byte data
valueCount = buf.readUnsignedByte();
for (int j = 0; j < valueCount; j++) {
int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte();
decodeParameter(position, id, buf, 4);
}
// Read 8 byte data
valueCount = buf.readUnsignedByte();
for (int j = 0; j < valueCount; j++) {
int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte();
decodeParameter(position, id, buf, 8);
}
Long driverIdPart1 = (Long) position.getAttributes().remove(Position.PREFIX_IO + 126);
Long driverIdPart2 = (Long) position.getAttributes().remove(Position.PREFIX_IO + 127);
if (driverIdPart1 != null && driverIdPart2 != null) {
ByteBuf driverId = Unpooled.copyLong(driverIdPart1, driverIdPart2);
position.set(Position.KEY_DRIVER_UNIQUE_ID, driverId.toString(StandardCharsets.US_ASCII));
driverId.release();
}
positions.add(position);
}
if (channel != null) {
channel.writeAndFlush(new NetworkMessage(Unpooled.wrappedBuffer(DataConverter.parseHex("0002640113bc")), remoteAddress));
}
return positions;
} else if (type == MSG_DTCS) {
List<Position> positions = new LinkedList<>();
int count = buf.readUnsignedByte();
for (int i = 0; i < count; i++) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
// reserved
buf.readUnsignedByte();
position.setTime(new Date(buf.readUnsignedInt() * 1000));
position.setValid(true);
position.setLongitude(buf.readInt() / 10000000.0);
position.setLatitude(buf.readInt() / 10000000.0);
if (buf.readUnsignedByte() == 2) {
position.set(Position.KEY_ARCHIVE, true);
}
position.set(Position.KEY_DTCS, buf.readSlice(5).toString(StandardCharsets.US_ASCII));
positions.add(position);
}
if (channel != null) {
channel.writeAndFlush(new NetworkMessage(Unpooled.wrappedBuffer(DataConverter.parseHex("00026d01c4a4")), remoteAddress));
}
return positions;
} else if (type == MSG_FILES) {
int subtype = buf.readUnsignedByte();
int source = buf.readUnsignedByte();
if (subtype == 2) {
ByteBuf filename = buf.readSlice(8);
int total = buf.readUnsignedShort();
int current = buf.readUnsignedShort();
if (photo == null) {
photo = Unpooled.buffer();
}
photo.writeBytes(buf.readSlice(buf.readableBytes() - 2));
if (current < total - 1) {
ByteBuf content = Unpooled.buffer();
content.writeByte(subtype);
content.writeByte(source);
content.writeBytes(filename);
content.writeShort(current + 1);
ByteBuf response = RuptelaProtocolEncoder.encodeContent(type, content);
content.release();
if (channel != null) {
channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
}
} else {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
getLastLocation(position, null);
position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(imei, photo, "jpg"));
photo.release();
photo = null;
return position;
}
}
return null;
} else {
return decodeCommandResponse(deviceSession, type, buf);
}
}
Aggregations