use of org.traccar.model.Position in project traccar by traccar.
the class M2cProtocolDecoder method decodePosition.
private Position decodePosition(Channel channel, SocketAddress remoteAddress, String line) {
Parser parser = new Parser(PATTERN, line);
if (!parser.matches()) {
return null;
}
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
if (deviceSession == null) {
return null;
}
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.set(Position.KEY_INDEX, parser.nextInt());
if (parser.next().equals("H")) {
position.set(Position.KEY_ARCHIVE, true);
}
position.set(Position.KEY_EVENT, parser.nextInt());
position.setValid(true);
position.setTime(parser.nextDateTime());
position.setLatitude(parser.nextDouble());
position.setLongitude(parser.nextDouble());
position.setAltitude(parser.nextInt());
position.setCourse(parser.nextInt());
position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble()));
position.set(Position.KEY_SATELLITES, parser.nextInt());
position.set(Position.KEY_ODOMETER, parser.nextLong());
position.set(Position.KEY_INPUT, parser.nextInt());
position.set(Position.KEY_OUTPUT, parser.nextInt());
position.set(Position.KEY_POWER, parser.nextInt() * 0.001);
position.set(Position.KEY_BATTERY, parser.nextInt() * 0.001);
position.set(Position.PREFIX_ADC + 1, parser.nextInt());
position.set(Position.PREFIX_ADC + 2, parser.nextInt());
position.set(Position.PREFIX_TEMP + 1, parser.nextDouble());
return position;
}
use of org.traccar.model.Position in project traccar by traccar.
the class M2mProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ChannelBuffer buf = (ChannelBuffer) msg;
// Remove offset
for (int i = 0; i < buf.readableBytes(); i++) {
int b = buf.getByte(i);
if (b != 0x0b) {
buf.setByte(i, b - 0x20);
}
}
if (firstPacket) {
firstPacket = false;
StringBuilder imei = new StringBuilder();
for (int i = 0; i < 8; i++) {
int b = buf.readByte();
if (i != 0) {
imei.append(b / 10);
}
imei.append(b % 10);
}
getDeviceSession(channel, remoteAddress, imei.toString());
} else {
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
if (deviceSession == null) {
return null;
}
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
DateBuilder dateBuilder = new DateBuilder().setDay(buf.readUnsignedByte() & 0x3f).setMonth(buf.readUnsignedByte() & 0x3f).setYear(buf.readUnsignedByte()).setHour(buf.readUnsignedByte() & 0x3f).setMinute(buf.readUnsignedByte() & 0x7f).setSecond(buf.readUnsignedByte() & 0x7f);
position.setTime(dateBuilder.getDate());
int degrees = buf.readUnsignedByte();
double latitude = buf.readUnsignedByte();
latitude += buf.readUnsignedByte() / 100.0;
latitude += buf.readUnsignedByte() / 10000.0;
latitude /= 60;
latitude += degrees;
int b = buf.readUnsignedByte();
degrees = (b & 0x7f) * 100 + buf.readUnsignedByte();
double longitude = buf.readUnsignedByte();
longitude += buf.readUnsignedByte() / 100.0;
longitude += buf.readUnsignedByte() / 10000.0;
longitude /= 60;
longitude += degrees;
if ((b & 0x80) != 0) {
longitude = -longitude;
}
if ((b & 0x40) != 0) {
latitude = -latitude;
}
position.setValid(true);
position.setLatitude(latitude);
position.setLongitude(longitude);
position.setSpeed(buf.readUnsignedByte());
int satellites = buf.readUnsignedByte();
if (satellites == 0) {
// cell information
return null;
}
position.set(Position.KEY_SATELLITES, satellites);
return position;
}
return null;
}
use of org.traccar.model.Position in project traccar by traccar.
the class PricolProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ChannelBuffer buf = (ChannelBuffer) msg;
// header
buf.readUnsignedByte();
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, buf.readBytes(7).toString(StandardCharsets.US_ASCII));
if (deviceSession == null) {
return null;
}
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.set("eventType", buf.readUnsignedByte());
position.set("packetVersion", buf.readUnsignedByte());
position.set(Position.KEY_STATUS, buf.readUnsignedByte());
position.set(Position.KEY_RSSI, buf.readUnsignedByte());
position.set(Position.KEY_GPS, buf.readUnsignedByte());
position.setTime(new DateBuilder().setDateReverse(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()).setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()).getDate());
position.setValid(true);
double lat = buf.getUnsignedShort(buf.readerIndex()) / 100;
lat += (buf.readUnsignedShort() % 100 * 10000 + buf.readUnsignedShort()) / 600000.0;
position.setLatitude(buf.readUnsignedByte() == 'S' ? -lat : lat);
double lon = buf.getUnsignedMedium(buf.readerIndex()) / 100;
lon += (buf.readUnsignedMedium() % 100 * 10000 + buf.readUnsignedShort()) / 600000.0;
position.setLongitude(buf.readUnsignedByte() == 'W' ? -lon : lon);
position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
position.set(Position.KEY_INPUT, buf.readUnsignedShort());
position.set(Position.KEY_OUTPUT, buf.readUnsignedByte());
position.set("analogAlerts", buf.readUnsignedByte());
position.set("customAlertTypes", buf.readUnsignedShort());
for (int i = 1; i <= 5; i++) {
position.set(Position.PREFIX_ADC + i, buf.readUnsignedShort());
}
position.set(Position.KEY_ODOMETER, buf.readUnsignedMedium());
position.set(Position.KEY_RPM, buf.readUnsignedShort());
if (channel != null) {
channel.write(ChannelBuffers.copiedBuffer("ACK", StandardCharsets.US_ASCII), remoteAddress);
}
return position;
}
use of org.traccar.model.Position in project traccar by traccar.
the class ProgressProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ChannelBuffer buf = (ChannelBuffer) msg;
int type = buf.readUnsignedShort();
// length
buf.readUnsignedShort();
if (type == MSG_IDENT || type == MSG_IDENT_FULL) {
// id
buf.readUnsignedInt();
int length = buf.readUnsignedShort();
buf.skipBytes(length);
length = buf.readUnsignedShort();
buf.skipBytes(length);
length = buf.readUnsignedShort();
String imei = buf.readBytes(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.readUnsignedShort();
}
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.readUnsignedShort();
if (subtype == MSG_ALARM) {
position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
}
if (buf.readUnsignedShort() > buf.readableBytes()) {
lastIndex += 1;
// workaround for device bug
break;
}
lastIndex = buf.readUnsignedInt();
position.set(Position.KEY_INDEX, lastIndex);
} else {
newIndex = buf.readUnsignedInt();
}
position.setTime(new Date(buf.readUnsignedInt() * 1000));
position.setLatitude(buf.readInt() * 180.0 / 0x7FFFFFFF);
position.setLongitude(buf.readInt() * 180.0 / 0x7FFFFFFF);
position.setSpeed(buf.readUnsignedInt() * 0.01);
position.setCourse(buf.readUnsignedShort() * 0.01);
position.setAltitude(buf.readUnsignedShort() * 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.readUnsignedInt());
long extraFlags = buf.readLong();
if (BitUtil.check(extraFlags, 0)) {
int count = buf.readUnsignedShort();
for (int i = 1; i <= count; i++) {
position.set(Position.PREFIX_ADC + i, buf.readUnsignedShort());
}
}
if (BitUtil.check(extraFlags, 1)) {
int size = buf.readUnsignedShort();
position.set("can", buf.toString(buf.readerIndex(), size, StandardCharsets.US_ASCII));
buf.skipBytes(size);
}
if (BitUtil.check(extraFlags, 2)) {
position.set("passenger", ChannelBuffers.hexDump(buf.readBytes(buf.readUnsignedShort())));
}
if (type == MSG_ALARM) {
position.set(Position.KEY_ALARM, true);
byte[] response = { (byte) 0xC9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
channel.write(ChannelBuffers.wrappedBuffer(response));
}
// crc
buf.readUnsignedInt();
positions.add(position);
}
requestArchive(channel);
return positions;
}
return null;
}
use of org.traccar.model.Position in project traccar by traccar.
the class Pt3000ProtocolDecoder 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());
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));
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());
return position;
}
Aggregations