use of org.traccar.DeviceSession in project traccar by tananaev.
the class Pt502ProtocolDecoder 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;
}
Position position = new Position(getProtocolName());
String type = parser.next();
if (type.startsWith("PHO") && channel != null) {
photo = new byte[Integer.parseInt(type.substring(3))];
channel.write("#PHD0," + Math.min(photo.length, MAX_CHUNK_SIZE) + "\r\n");
}
position.set(Position.KEY_ALARM, decodeAlarm(type));
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
if (deviceSession == null) {
return null;
}
position.setDeviceId(deviceSession.getDeviceId());
DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
position.setValid(parser.next().equals("A"));
position.setLatitude(parser.nextCoordinate());
position.setLongitude(parser.nextCoordinate());
position.setSpeed(parser.nextDouble(0));
position.setCourse(parser.nextDouble(0));
dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
position.setTime(dateBuilder.getDate());
position.set(Position.KEY_INPUT, parser.next());
position.set(Position.KEY_OUTPUT, parser.next());
if (parser.hasNext()) {
String[] values = parser.next().split(",");
for (int i = 0; i < values.length; i++) {
position.set(Position.PREFIX_ADC + (i + 1), Integer.parseInt(values[i], 16));
}
}
position.set(Position.KEY_ODOMETER, parser.nextInt(0));
position.set(Position.KEY_DRIVER_UNIQUE_ID, parser.next());
if (parser.hasNext()) {
int value = parser.nextHexInt(0);
position.set(Position.KEY_BATTERY, value >> 8);
position.set(Position.KEY_RSSI, (value >> 4) & 0xf);
position.set(Position.KEY_SATELLITES, value & 0xf);
}
return position;
}
use of org.traccar.DeviceSession in project traccar by tananaev.
the class RuptelaProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ChannelBuffer buf = (ChannelBuffer) 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 cnt = buf.readUnsignedByte();
for (int j = 0; j < cnt; j++) {
int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte();
decodeParameter(position, id, buf, 1);
}
// Read 2 byte data
cnt = buf.readUnsignedByte();
for (int j = 0; j < cnt; j++) {
int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte();
decodeParameter(position, id, buf, 2);
}
// Read 4 byte data
cnt = buf.readUnsignedByte();
for (int j = 0; j < cnt; j++) {
int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte();
decodeParameter(position, id, buf, 4);
}
// Read 8 byte data
cnt = buf.readUnsignedByte();
for (int j = 0; j < cnt; j++) {
int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte();
decodeParameter(position, id, buf, 8);
}
positions.add(position);
}
if (channel != null) {
channel.write(ChannelBuffers.wrappedBuffer(DataConverter.parseHex("0002640113bc")));
}
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.readBytes(5).toString(StandardCharsets.US_ASCII));
positions.add(position);
}
if (channel != null) {
channel.write(ChannelBuffers.wrappedBuffer(DataConverter.parseHex("00026d01c4a4")));
}
return positions;
} else {
return decodeCommandResponse(deviceSession, type, buf);
}
}
use of org.traccar.DeviceSession in project traccar by tananaev.
the class TytanProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ChannelBuffer buf = (ChannelBuffer) msg;
// protocol
buf.readUnsignedByte();
// length
buf.readUnsignedShort();
int index = buf.readUnsignedByte() >> 3;
if (channel != null) {
ChannelBuffer response = ChannelBuffers.copiedBuffer("^" + index, StandardCharsets.US_ASCII);
channel.write(response, remoteAddress);
}
String id = String.valueOf(buf.readUnsignedInt());
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
if (deviceSession == null) {
return null;
}
List<Position> positions = new LinkedList<>();
while (buf.readableBytes() > 2) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
int end = buf.readerIndex() + buf.readUnsignedByte();
position.setTime(new Date(buf.readUnsignedInt() * 1000));
int flags = buf.readUnsignedByte();
position.set(Position.KEY_SATELLITES, BitUtil.from(flags, 2));
position.setValid(BitUtil.to(flags, 2) > 0);
// Latitude
double lat = buf.readUnsignedMedium();
lat = lat * -180 / 16777216 + 90;
position.setLatitude(lat);
// Longitude
double lon = buf.readUnsignedMedium();
lon = lon * 360 / 16777216 - 180;
position.setLongitude(lon);
// Status
flags = buf.readUnsignedByte();
position.set(Position.KEY_IGNITION, BitUtil.check(flags, 0));
position.set(Position.KEY_RSSI, BitUtil.between(flags, 2, 5));
position.setCourse((BitUtil.from(flags, 5) * 45 + 180) % 360);
// Speed
int speed = buf.readUnsignedByte();
if (speed < 250) {
position.setSpeed(UnitsConverter.knotsFromKph(speed));
}
decodeExtraData(position, buf, end);
positions.add(position);
}
return positions;
}
use of org.traccar.DeviceSession in project traccar by tananaev.
the class WialonProtocolDecoder method decodePosition.
private Position decodePosition(Channel channel, SocketAddress remoteAddress, String substring) {
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
if (deviceSession == null) {
return null;
}
Parser parser = new Parser(PATTERN, substring);
if (!parser.matches()) {
return null;
}
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS));
position.setLatitude(parser.nextCoordinate());
position.setLongitude(parser.nextCoordinate());
position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0)));
position.setCourse(parser.nextDouble(0));
position.setAltitude(parser.nextDouble(0));
if (parser.hasNext()) {
int satellites = parser.nextInt(0);
position.setValid(satellites >= 3);
position.set(Position.KEY_SATELLITES, satellites);
}
position.set(Position.KEY_HDOP, parser.nextDouble());
position.set(Position.KEY_INPUT, parser.next());
position.set(Position.KEY_OUTPUT, parser.next());
if (parser.hasNext()) {
String[] values = parser.next().split(",");
for (int i = 0; i < values.length; i++) {
position.set(Position.PREFIX_ADC + (i + 1), values[i]);
}
}
position.set(Position.KEY_DRIVER_UNIQUE_ID, parser.next());
if (parser.hasNext()) {
String[] values = parser.next().split(",");
for (String param : values) {
Matcher paramParser = Pattern.compile("(.*):[1-3]:(.*)").matcher(param);
if (paramParser.matches()) {
try {
position.set(paramParser.group(1).toLowerCase(), Double.parseDouble(paramParser.group(2)));
} catch (NumberFormatException e) {
position.set(paramParser.group(1).toLowerCase(), paramParser.group(2));
}
}
}
}
return position;
}
use of org.traccar.DeviceSession in project traccar by tananaev.
the class WondexProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ChannelBuffer buf = (ChannelBuffer) msg;
if (buf.getUnsignedByte(0) == 0xD0) {
long deviceId = ((Long.reverseBytes(buf.getLong(0))) >> 32) & 0xFFFFFFFFL;
getDeviceSession(channel, remoteAddress, String.valueOf(deviceId));
return null;
} else if (buf.toString(StandardCharsets.US_ASCII).startsWith("$OK:") || buf.toString(StandardCharsets.US_ASCII).startsWith("$ERR:") || buf.toString(StandardCharsets.US_ASCII).startsWith("$MSG:")) {
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
getLastLocation(position, new Date());
position.set(Position.KEY_RESULT, buf.toString(StandardCharsets.US_ASCII));
return position;
} else {
Parser parser = new Parser(PATTERN, buf.toString(StandardCharsets.US_ASCII));
if (!parser.matches()) {
return null;
}
Position position = new Position(getProtocolName());
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
if (deviceSession == null) {
return null;
}
position.setDeviceId(deviceSession.getDeviceId());
position.setTime(parser.nextDateTime());
position.setLongitude(parser.nextDouble(0));
position.setLatitude(parser.nextDouble(0));
position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0)));
position.setCourse(parser.nextDouble(0));
position.setAltitude(parser.nextDouble(0));
int satellites = parser.nextInt(0);
position.setValid(satellites != 0);
position.set(Position.KEY_SATELLITES, satellites);
position.set(Position.KEY_EVENT, parser.next());
position.set(Position.KEY_BATTERY, parser.nextDouble());
if (parser.hasNext()) {
position.set(Position.KEY_ODOMETER, parser.nextDouble(0) * 1000);
}
position.set(Position.KEY_INPUT, parser.next());
position.set(Position.PREFIX_ADC + 1, parser.next());
position.set(Position.PREFIX_ADC + 2, parser.next());
position.set(Position.KEY_OUTPUT, parser.next());
return position;
}
}
Aggregations