use of org.traccar.DeviceSession in project traccar by tananaev.
the class EgtsProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ChannelBuffer buf = (ChannelBuffer) msg;
buf.skipBytes(buf.getUnsignedByte(buf.readerIndex() + 3));
List<Position> positions = new LinkedList<>();
while (buf.readableBytes() > 2) {
int length = buf.readUnsignedShort();
int index = buf.readUnsignedShort();
int recordFlags = buf.readUnsignedByte();
if (BitUtil.check(recordFlags, 0)) {
// object id
buf.readUnsignedInt();
}
if (BitUtil.check(recordFlags, 1)) {
// event id
buf.readUnsignedInt();
}
if (BitUtil.check(recordFlags, 2)) {
// time
buf.readUnsignedInt();
}
int serviceType = buf.readUnsignedByte();
// recipient service type
buf.readUnsignedByte();
int recordEnd = buf.readerIndex() + length;
Position position = new Position(getProtocolName());
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
if (deviceSession != null) {
position.setDeviceId(deviceSession.getDeviceId());
}
ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0);
response.writeShort(index);
// success
response.writeByte(0);
sendResponse(channel, PT_RESPONSE, index, serviceType, MSG_RECORD_RESPONSE, response);
while (buf.readerIndex() < recordEnd) {
int type = buf.readUnsignedByte();
int end = buf.readUnsignedShort() + buf.readerIndex();
if (type == MSG_TERM_IDENTITY) {
// object id
buf.readUnsignedInt();
int flags = buf.readUnsignedByte();
if (BitUtil.check(flags, 0)) {
// home dispatcher identifier
buf.readUnsignedShort();
}
if (BitUtil.check(flags, 1)) {
getDeviceSession(channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII).trim());
}
if (BitUtil.check(flags, 2)) {
getDeviceSession(channel, remoteAddress, buf.readBytes(16).toString(StandardCharsets.US_ASCII).trim());
}
if (BitUtil.check(flags, 3)) {
// language identifier
buf.skipBytes(3);
}
if (BitUtil.check(flags, 5)) {
// network identifier
buf.skipBytes(3);
}
if (BitUtil.check(flags, 6)) {
// buffer size
buf.readUnsignedShort();
}
if (BitUtil.check(flags, 7)) {
getDeviceSession(channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII).trim());
}
response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0);
// success
response.writeByte(0);
sendResponse(channel, PT_APPDATA, index, serviceType, MSG_RESULT_CODE, response);
} else if (type == MSG_POS_DATA) {
// since 2010-01-01
position.setTime(new Date((buf.readUnsignedInt() + 1262304000) * 1000));
position.setLatitude(buf.readUnsignedInt() * 90.0 / 0xFFFFFFFFL);
position.setLongitude(buf.readUnsignedInt() * 180.0 / 0xFFFFFFFFL);
int flags = buf.readUnsignedByte();
position.setValid(BitUtil.check(flags, 0));
if (BitUtil.check(flags, 5)) {
position.setLatitude(-position.getLatitude());
}
if (BitUtil.check(flags, 6)) {
position.setLongitude(-position.getLongitude());
}
int speed = buf.readUnsignedShort();
position.setSpeed(UnitsConverter.knotsFromKph(BitUtil.to(speed, 14) * 0.1));
position.setCourse(buf.readUnsignedByte() + (BitUtil.check(speed, 15) ? 0x100 : 0));
position.set(Position.KEY_ODOMETER, buf.readUnsignedMedium() * 100);
position.set(Position.KEY_INPUT, buf.readUnsignedByte());
position.set(Position.KEY_EVENT, buf.readUnsignedByte());
if (BitUtil.check(flags, 7)) {
position.setAltitude(buf.readMedium());
}
} else if (type == MSG_EXT_POS_DATA) {
int flags = buf.readUnsignedByte();
if (BitUtil.check(flags, 0)) {
position.set(Position.KEY_VDOP, buf.readUnsignedShort());
}
if (BitUtil.check(flags, 1)) {
position.set(Position.KEY_HDOP, buf.readUnsignedShort());
}
if (BitUtil.check(flags, 2)) {
position.set(Position.KEY_PDOP, buf.readUnsignedShort());
}
if (BitUtil.check(flags, 3)) {
position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
}
} else if (type == MSG_AD_SENSORS_DATA) {
// inputs flags
buf.readUnsignedByte();
position.set(Position.KEY_OUTPUT, buf.readUnsignedByte());
// adc flags
buf.readUnsignedByte();
}
buf.readerIndex(end);
}
if (serviceType == SERVICE_TELEDATA && deviceSession != null) {
positions.add(position);
}
}
return positions.isEmpty() ? null : positions;
}
use of org.traccar.DeviceSession in project traccar by tananaev.
the class GalileoProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ChannelBuffer buf = (ChannelBuffer) msg;
// header
buf.readUnsignedByte();
int length = (buf.readUnsignedShort() & 0x7fff) + 3;
List<Position> positions = new LinkedList<>();
Set<Integer> tags = new HashSet<>();
boolean hasLocation = false;
DeviceSession deviceSession = null;
Position position = new Position(getProtocolName());
while (buf.readerIndex() < length) {
int tag = buf.readUnsignedByte();
if (tags.contains(tag)) {
if (hasLocation && position.getFixTime() != null) {
positions.add(position);
}
tags.clear();
hasLocation = false;
// new position starts
position = new Position(getProtocolName());
}
tags.add(tag);
if (tag == 0x03) {
deviceSession = getDeviceSession(channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII));
} else if (tag == 0x30) {
hasLocation = true;
position.setValid((buf.readUnsignedByte() & 0xf0) == 0x00);
position.setLatitude(buf.readInt() / 1000000.0);
position.setLongitude(buf.readInt() / 1000000.0);
} else {
decodeTag(position, buf, tag);
}
}
if (deviceSession == null) {
deviceSession = getDeviceSession(channel, remoteAddress);
if (deviceSession == null) {
return null;
}
}
if (hasLocation && position.getFixTime() != null) {
positions.add(position);
} else if (position.getAttributes().containsKey(Position.KEY_RESULT)) {
position.setDeviceId(deviceSession.getDeviceId());
getLastLocation(position, null);
positions.add(position);
}
sendReply(channel, buf.readUnsignedShort());
for (Position p : positions) {
p.setDeviceId(deviceSession.getDeviceId());
}
return positions.isEmpty() ? null : positions;
}
use of org.traccar.DeviceSession in project traccar by tananaev.
the class Gl200BinaryProtocolDecoder method decodeLocation.
private List<Position> decodeLocation(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) {
List<Position> positions = new LinkedList<>();
int type = buf.readUnsignedByte();
// mask
buf.readUnsignedInt();
// length
buf.readUnsignedShort();
// device type
buf.readUnsignedByte();
// protocol version
buf.readUnsignedShort();
// firmware version
buf.readUnsignedShort();
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.format("%015d", buf.readLong()));
if (deviceSession == null) {
return null;
}
int battery = buf.readUnsignedByte();
int power = buf.readUnsignedShort();
if (type == MSG_RSP_GEO) {
// reserved
buf.readUnsignedByte();
// reserved
buf.readUnsignedByte();
}
// motion status
buf.readUnsignedByte();
int satellites = buf.readUnsignedByte();
if (type != MSG_RSP_COMPRESSED) {
// index
buf.readUnsignedByte();
}
if (type == MSG_RSP_LCB) {
// phone length
buf.readUnsignedByte();
for (int b = buf.readUnsignedByte(); ; b = buf.readUnsignedByte()) {
if ((b & 0xf) == 0xf || (b & 0xf0) == 0xf0) {
break;
}
}
}
if (type == MSG_RSP_COMPRESSED) {
int count = buf.readUnsignedShort();
BitBuffer bits;
int speed = 0;
int heading = 0;
int latitude = 0;
int longitude = 0;
long time = 0;
for (int i = 0; i < count; i++) {
if (time > 0) {
time += 1;
}
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
switch(BitUtil.from(buf.getUnsignedByte(buf.readerIndex()), 8 - 2)) {
case 1:
bits = new BitBuffer(buf.readBytes(3));
// point attribute
bits.readUnsigned(2);
// fix type
bits.readUnsigned(1);
speed = bits.readUnsigned(12);
heading = bits.readUnsigned(9);
longitude = buf.readInt();
latitude = buf.readInt();
if (time == 0) {
time = buf.readUnsignedInt();
}
break;
case 2:
bits = new BitBuffer(buf.readBytes(5));
// point attribute
bits.readUnsigned(2);
// fix type
bits.readUnsigned(1);
speed += bits.readSigned(7);
heading += bits.readSigned(7);
longitude += bits.readSigned(12);
latitude += bits.readSigned(11);
break;
default:
// invalid or same
buf.readUnsignedByte();
continue;
}
position.setValid(true);
position.setTime(new Date(time * 1000));
position.setSpeed(UnitsConverter.knotsFromKph(speed * 0.1));
position.setCourse(heading);
position.setLongitude(longitude * 0.000001);
position.setLatitude(latitude * 0.000001);
positions.add(position);
}
} else {
int count = buf.readUnsignedByte();
for (int i = 0; i < count; i++) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.set(Position.KEY_BATTERY_LEVEL, battery);
position.set(Position.KEY_POWER, power);
position.set(Position.KEY_SATELLITES, satellites);
int hdop = buf.readUnsignedByte();
position.setValid(hdop > 0);
position.set(Position.KEY_HDOP, hdop);
position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedMedium() * 0.1));
position.setCourse(buf.readUnsignedShort());
position.setAltitude(buf.readShort());
position.setLongitude(buf.readInt() * 0.000001);
position.setLatitude(buf.readInt() * 0.000001);
position.setTime(decodeTime(buf));
position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort())));
// reserved
buf.readUnsignedByte();
positions.add(position);
}
}
return positions;
}
use of org.traccar.DeviceSession in project traccar by tananaev.
the class Gl200BinaryProtocolDecoder method decodeInformation.
private Position decodeInformation(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) {
Position position = new Position(getProtocolName());
int type = buf.readUnsignedByte();
// mask
buf.readUnsignedInt();
// length
buf.readUnsignedShort();
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.format("%015d", buf.readLong()));
if (deviceSession == null) {
return null;
}
position.setDeviceId(deviceSession.getDeviceId());
// device type
buf.readUnsignedByte();
// protocol version
buf.readUnsignedShort();
position.set(Position.KEY_VERSION_FW, String.valueOf(buf.readUnsignedShort()));
if (type == MSG_INF_VER) {
// hardware version
buf.readUnsignedShort();
// mcu version
buf.readUnsignedShort();
// reserved
buf.readUnsignedShort();
}
// motion status
buf.readUnsignedByte();
// reserved
buf.readUnsignedByte();
position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
// mode
buf.readUnsignedByte();
// last fix time
buf.skipBytes(7);
// reserved
buf.readUnsignedByte();
buf.readUnsignedByte();
// response report mask
buf.readUnsignedShort();
// ign interval
buf.readUnsignedShort();
// igf interval
buf.readUnsignedShort();
// reserved
buf.readUnsignedInt();
// reserved
buf.readUnsignedByte();
if (type == MSG_INF_BAT) {
position.set(Position.KEY_CHARGE, buf.readUnsignedByte() != 0);
position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.001);
position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.001);
position.set(Position.KEY_BATTERY_LEVEL, buf.readUnsignedByte());
}
// iccid
buf.skipBytes(10);
if (type == MSG_INF_CSQ) {
position.set(Position.KEY_RSSI, buf.readUnsignedByte());
buf.readUnsignedByte();
}
// time zone flags
buf.readUnsignedByte();
// time zone offset
buf.readUnsignedShort();
if (type == MSG_INF_GIR) {
// gir trigger
buf.readUnsignedByte();
// cell number
buf.readUnsignedByte();
position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort())));
// ta
buf.readUnsignedByte();
// rx level
buf.readUnsignedByte();
}
getLastLocation(position, decodeTime(buf));
return position;
}
use of org.traccar.DeviceSession in project traccar by tananaev.
the class Gps103ProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
String sentence = (String) msg;
// Send response #1
if (sentence.contains("##")) {
if (channel != null) {
channel.write("LOAD", remoteAddress);
Parser handshakeParser = new Parser(PATTERN_HANDSHAKE, sentence);
if (handshakeParser.matches()) {
getDeviceSession(channel, remoteAddress, handshakeParser.next());
}
}
return null;
}
// Send response #2
if (!sentence.isEmpty() && Character.isDigit(sentence.charAt(0))) {
if (channel != null) {
channel.write("ON", remoteAddress);
}
int start = sentence.indexOf("imei:");
if (start >= 0) {
sentence = sentence.substring(start);
} else {
return null;
}
}
Position position = new Position(getProtocolName());
Parser parser = new Parser(PATTERN_NETWORK, sentence);
if (parser.matches()) {
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
if (deviceSession == null) {
return null;
}
position.setDeviceId(deviceSession.getDeviceId());
getLastLocation(position, null);
position.setNetwork(new Network(CellTower.fromLacCid(parser.nextHexInt(0), parser.nextHexInt(0))));
return position;
}
parser = new Parser(PATTERN_OBD, sentence);
if (parser.matches()) {
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
if (deviceSession == null) {
return null;
}
position.setDeviceId(deviceSession.getDeviceId());
getLastLocation(position, parser.nextDateTime());
position.set(Position.KEY_ODOMETER, parser.nextInt(0));
// instant fuel consumption
parser.nextDouble(0);
position.set(Position.KEY_FUEL_CONSUMPTION, parser.nextDouble(0));
position.set(Position.KEY_HOURS, parser.nextInt());
position.set(Position.KEY_OBD_SPEED, parser.nextInt(0));
position.set(Position.KEY_ENGINE_LOAD, parser.next());
position.set(Position.KEY_COOLANT_TEMP, parser.nextInt());
position.set(Position.KEY_THROTTLE, parser.next());
position.set(Position.KEY_RPM, parser.nextInt(0));
position.set(Position.KEY_BATTERY, parser.nextDouble(0));
position.set(Position.KEY_DTCS, parser.next().replace(',', ' ').trim());
return position;
}
parser = new Parser(PATTERN, sentence);
if (!parser.matches()) {
return null;
}
String imei = parser.next();
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
if (deviceSession == null) {
return null;
}
position.setDeviceId(deviceSession.getDeviceId());
String alarm = parser.next();
position.set(Position.KEY_ALARM, decodeAlarm(alarm));
if (alarm.equals("help me")) {
if (channel != null) {
channel.write("**,imei:" + imei + ",E;", remoteAddress);
}
} else if (alarm.equals("acc on")) {
position.set(Position.KEY_IGNITION, true);
} else if (alarm.equals("acc off")) {
position.set(Position.KEY_IGNITION, false);
} else if (alarm.startsWith("T:")) {
position.set(Position.PREFIX_TEMP + 1, alarm.substring(2));
} else if (alarm.startsWith("oil ")) {
position.set("oil", alarm.substring(4));
} else if (!position.getAttributes().containsKey(Position.KEY_ALARM) && !alarm.equals("tracker")) {
position.set(Position.KEY_EVENT, alarm);
}
DateBuilder dateBuilder = new DateBuilder().setDate(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
int localHours = parser.nextInt(0);
int localMinutes = parser.nextInt(0);
String rfid = parser.next();
if (alarm.equals("rfid")) {
position.set(Position.KEY_DRIVER_UNIQUE_ID, rfid);
}
String utcHours = parser.next();
String utcMinutes = parser.next();
dateBuilder.setTime(localHours, localMinutes, parser.nextInt(0));
// Timezone calculation
if (utcHours != null && utcMinutes != null) {
int deltaMinutes = (localHours - Integer.parseInt(utcHours)) * 60;
deltaMinutes += localMinutes - Integer.parseInt(utcMinutes);
if (deltaMinutes <= -12 * 60) {
deltaMinutes += 24 * 60;
} else if (deltaMinutes > 12 * 60) {
deltaMinutes -= 24 * 60;
}
dateBuilder.addMinute(-deltaMinutes);
}
position.setTime(dateBuilder.getDate());
position.setValid(parser.next().equals("A"));
position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN_HEM));
position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN_HEM));
position.setSpeed(parser.nextDouble(0));
position.setCourse(parser.nextDouble(0));
position.setAltitude(parser.nextDouble(0));
for (int i = 1; i <= 5; i++) {
position.set(Position.PREFIX_IO + i, parser.next());
}
return position;
}
Aggregations