use of org.traccar.DeviceSession in project traccar by tananaev.
the class OrionProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ChannelBuffer buf = (ChannelBuffer) msg;
// header
buf.skipBytes(2);
int type = buf.readUnsignedByte() & 0x0f;
if (type == MSG_USERLOG) {
int header = buf.readUnsignedByte();
if ((header & 0x40) != 0) {
sendResponse(channel, buf);
}
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(buf.readUnsignedInt()));
if (deviceSession == null) {
return null;
}
List<Position> positions = new LinkedList<>();
for (int i = 0; i < (header & 0x0f); i++) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.set(Position.KEY_EVENT, buf.readUnsignedByte());
// length
buf.readUnsignedByte();
position.set(Position.KEY_FLAGS, buf.readUnsignedShort());
position.setLatitude(convertCoordinate(buf.readInt()));
position.setLongitude(convertCoordinate(buf.readInt()));
position.setAltitude(buf.readShort() / 10.0);
position.setCourse(buf.readUnsignedShort());
position.setSpeed(buf.readUnsignedShort() * 0.0539957);
DateBuilder dateBuilder = new DateBuilder().setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()).setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
position.setTime(dateBuilder.getDate());
int satellites = buf.readUnsignedByte();
position.setValid(satellites >= 3);
position.set(Position.KEY_SATELLITES, satellites);
positions.add(position);
}
return positions;
}
return null;
}
use of org.traccar.DeviceSession in project traccar by tananaev.
the class PretraceProtocolDecoder 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;
}
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
if (deviceSession == null) {
return null;
}
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.setValid(parser.next().equals("A"));
position.setTime(parser.nextDateTime());
position.setLatitude(parser.nextCoordinate());
position.setLongitude(parser.nextCoordinate());
position.setSpeed(UnitsConverter.knotsFromKph(parser.nextInt(0)));
position.setCourse(parser.nextInt(0));
position.setAltitude(parser.nextHexInt(0));
position.set(Position.KEY_ODOMETER, parser.nextHexInt(0));
position.set(Position.KEY_SATELLITES, parser.nextHexInt(0));
position.set(Position.KEY_HDOP, parser.nextInt(0));
position.set(Position.KEY_RSSI, parser.nextInt(0));
return position;
}
use of org.traccar.DeviceSession in project traccar by tananaev.
the class NavisProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ChannelBuffer buf = (ChannelBuffer) msg;
prefix = buf.toString(buf.readerIndex(), 4, StandardCharsets.US_ASCII);
// prefix @NTC by default
buf.skipBytes(prefix.length());
serverId = buf.readUnsignedInt();
deviceUniqueId = buf.readUnsignedInt();
int length = buf.readUnsignedShort();
// header and data XOR checksum
buf.skipBytes(2);
if (length == 0) {
// keep alive message
return null;
}
String type = buf.toString(buf.readerIndex(), 3, StandardCharsets.US_ASCII);
buf.skipBytes(type.length());
if (type.equals("*>S")) {
return processHandshake(channel, remoteAddress, buf);
} else {
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
if (deviceSession != null) {
if (type.equals("*>T")) {
return processSingle(deviceSession, channel, buf);
} else if (type.equals("*>A")) {
return processArray(deviceSession, channel, buf);
}
}
}
return null;
}
use of org.traccar.DeviceSession in project traccar by tananaev.
the class NoranProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ChannelBuffer buf = (ChannelBuffer) msg;
// length
buf.readUnsignedShort();
int type = buf.readUnsignedShort();
if (type == MSG_SHAKE_HAND && channel != null) {
ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 13);
response.writeBytes(ChannelBuffers.copiedBuffer(ByteOrder.LITTLE_ENDIAN, "\r\n*KW", StandardCharsets.US_ASCII));
response.writeByte(0);
response.writeShort(response.capacity());
response.writeShort(MSG_SHAKE_HAND_RESPONSE);
// status
response.writeByte(1);
response.writeBytes(ChannelBuffers.copiedBuffer(ByteOrder.LITTLE_ENDIAN, "\r\n", StandardCharsets.US_ASCII));
channel.write(response, remoteAddress);
} else if (type == MSG_UPLOAD_POSITION || type == MSG_UPLOAD_POSITION_NEW || type == MSG_CONTROL_RESPONSE || type == MSG_ALARM) {
boolean newFormat = false;
if (type == MSG_UPLOAD_POSITION && buf.readableBytes() == 48 || type == MSG_ALARM && buf.readableBytes() == 48 || type == MSG_CONTROL_RESPONSE && buf.readableBytes() == 57) {
newFormat = true;
}
Position position = new Position(getProtocolName());
if (type == MSG_CONTROL_RESPONSE) {
// GIS ip
buf.readUnsignedInt();
// GIS port
buf.readUnsignedInt();
}
position.setValid(BitUtil.check(buf.readUnsignedByte(), 0));
short alarm = buf.readUnsignedByte();
switch(alarm) {
case 1:
position.set(Position.KEY_ALARM, Position.ALARM_SOS);
break;
case 2:
position.set(Position.KEY_ALARM, Position.ALARM_OVERSPEED);
break;
case 3:
position.set(Position.KEY_ALARM, Position.ALARM_GEOFENCE_EXIT);
break;
case 9:
position.set(Position.KEY_ALARM, Position.ALARM_POWER_OFF);
break;
default:
break;
}
if (newFormat) {
position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedInt()));
position.setCourse(buf.readFloat());
} else {
position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
position.setCourse(buf.readUnsignedShort());
}
position.setLongitude(buf.readFloat());
position.setLatitude(buf.readFloat());
if (!newFormat) {
long timeValue = buf.readUnsignedInt();
DateBuilder dateBuilder = new DateBuilder().setYear((int) BitUtil.from(timeValue, 26)).setMonth((int) BitUtil.between(timeValue, 22, 26)).setDay((int) BitUtil.between(timeValue, 17, 22)).setHour((int) BitUtil.between(timeValue, 12, 17)).setMinute((int) BitUtil.between(timeValue, 6, 12)).setSecond((int) BitUtil.to(timeValue, 6));
position.setTime(dateBuilder.getDate());
}
ChannelBuffer rawId;
if (newFormat) {
rawId = buf.readBytes(12);
} else {
rawId = buf.readBytes(11);
}
String id = rawId.toString(StandardCharsets.US_ASCII).replaceAll("[^\\p{Print}]", "");
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
if (deviceSession == null) {
return null;
}
position.setDeviceId(deviceSession.getDeviceId());
if (newFormat) {
DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
position.setTime(dateFormat.parse(buf.readBytes(17).toString(StandardCharsets.US_ASCII)));
buf.readByte();
}
if (!newFormat) {
position.set(Position.PREFIX_IO + 1, buf.readUnsignedByte());
position.set(Position.KEY_FUEL_LEVEL, buf.readUnsignedByte());
} else if (type == MSG_UPLOAD_POSITION_NEW) {
position.set(Position.PREFIX_TEMP + 1, buf.readShort());
position.set(Position.KEY_ODOMETER, buf.readFloat());
}
return position;
}
return null;
}
use of org.traccar.DeviceSession in project traccar by tananaev.
the class ObdDongleProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ChannelBuffer buf = (ChannelBuffer) msg;
// header
buf.skipBytes(2);
int index = buf.readUnsignedShort();
String imei = buf.readBytes(15).toString(StandardCharsets.US_ASCII);
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
if (deviceSession == null) {
return null;
}
int type = buf.readUnsignedByte();
// data length
buf.readUnsignedShort();
if (type == MSG_TYPE_CONNECT) {
ChannelBuffer response = ChannelBuffers.dynamicBuffer();
response.writeByte(1);
response.writeShort(0);
response.writeInt(0);
sendResponse(channel, MSG_TYPE_CONNACK, index, imei, response);
} else if (type == MSG_TYPE_PUBLISH) {
int typeMajor = buf.readUnsignedByte();
int typeMinor = buf.readUnsignedByte();
// event id
buf.readUnsignedByte();
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.setTime(new Date(buf.readUnsignedInt() * 1000));
int flags = buf.readUnsignedByte();
position.setValid(!BitUtil.check(flags, 6));
position.set(Position.KEY_SATELLITES, BitUtil.to(flags, 4));
double longitude = ((BitUtil.to(buf.readUnsignedShort(), 1) << 24) + buf.readUnsignedMedium()) * 0.00001;
position.setLongitude(BitUtil.check(flags, 5) ? longitude : -longitude);
double latitude = buf.readUnsignedMedium() * 0.00001;
position.setLatitude(BitUtil.check(flags, 4) ? latitude : -latitude);
int speedCourse = buf.readUnsignedMedium();
position.setSpeed(UnitsConverter.knotsFromMph(BitUtil.from(speedCourse, 10) * 0.1));
position.setCourse(BitUtil.to(speedCourse, 10));
ChannelBuffer response = ChannelBuffers.dynamicBuffer();
response.writeByte(typeMajor);
response.writeByte(typeMinor);
sendResponse(channel, MSG_TYPE_PUBACK, index, imei, response);
return position;
}
return null;
}
Aggregations