use of org.traccar.DeviceSession in project traccar by tananaev.
the class OigoProtocolDecoder method decodeMgMessage.
private Position decodeMgMessage(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) {
// tag
buf.readUnsignedByte();
int flags = buf.getUnsignedByte(buf.readerIndex());
DeviceSession deviceSession;
if (BitUtil.check(flags, 6)) {
// flags
buf.readUnsignedByte();
deviceSession = getDeviceSession(channel, remoteAddress);
} else {
String imei = ChannelBuffers.hexDump(buf.readBytes(8)).substring(1);
deviceSession = getDeviceSession(channel, remoteAddress, imei);
}
if (deviceSession == null) {
return null;
}
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
// imsi
buf.skipBytes(8);
int date = buf.readUnsignedShort();
DateBuilder dateBuilder = new DateBuilder().setDate(2010 + BitUtil.from(date, 12), BitUtil.between(date, 8, 12), BitUtil.to(date, 8)).setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), 0);
position.setValid(true);
position.setLatitude(convertCoordinate(buf.readInt()));
position.setLongitude(convertCoordinate(buf.readInt()));
position.setAltitude(UnitsConverter.metersFromFeet(buf.readShort()));
position.setCourse(buf.readUnsignedShort());
position.setSpeed(UnitsConverter.knotsFromMph(buf.readUnsignedByte()));
position.set(Position.KEY_POWER, buf.readUnsignedByte() * 0.1);
position.set(Position.PREFIX_IO + 1, buf.readUnsignedByte());
dateBuilder.setSecond(buf.readUnsignedByte());
position.setTime(dateBuilder.getDate());
position.set(Position.KEY_RSSI, buf.readUnsignedByte());
int index = buf.readUnsignedByte();
position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte());
position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
position.set(Position.KEY_ODOMETER, (long) (buf.readUnsignedInt() * 1609.34));
if (channel != null && BitUtil.check(flags, 7)) {
ChannelBuffer response = ChannelBuffers.dynamicBuffer();
response.writeByte(MSG_ACKNOWLEDGEMENT);
response.writeByte(index);
response.writeByte(0x00);
channel.write(response, remoteAddress);
}
return position;
}
use of org.traccar.DeviceSession in project traccar by tananaev.
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.DeviceSession in project traccar by tananaev.
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.DeviceSession in project traccar by tananaev.
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;
}
use of org.traccar.DeviceSession in project traccar by tananaev.
the class RecodaProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ChannelBuffer buf = (ChannelBuffer) msg;
int type = buf.readInt();
// length
buf.readUnsignedInt();
if (type != MSG_HEARTBEAT) {
// version
buf.readUnsignedShort();
// index
buf.readUnsignedShort();
}
if (type == MSG_SIGNAL_LINK_REGISTRATION) {
getDeviceSession(channel, remoteAddress, buf.readBytes(12).toString(StandardCharsets.US_ASCII));
} else if (type == MSG_GPS_DATA) {
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
if (deviceSession == null) {
return null;
}
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.setTime(new Date(buf.readLong()));
int flags = buf.readUnsignedByte();
if (BitUtil.check(flags, 0)) {
// declination
buf.readUnsignedShort();
position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort()));
position.setLongitude(buf.readUnsignedByte() + buf.readUnsignedByte() / 60.0);
position.setLatitude(buf.readUnsignedByte() + buf.readUnsignedByte() / 60.0);
position.setLongitude(position.getLongitude() + buf.readUnsignedInt() / 3600.0);
position.setLatitude(position.getLatitude() + buf.readUnsignedInt() / 3600.0);
int status = buf.readUnsignedByte();
position.setValid(BitUtil.check(status, 0));
if (BitUtil.check(status, 1)) {
position.setLongitude(-position.getLongitude());
}
if (!BitUtil.check(status, 2)) {
position.setLatitude(-position.getLatitude());
}
} else {
getLastLocation(position, position.getDeviceTime());
}
return position;
}
return null;
}
Aggregations