use of org.traccar.NetworkMessage in project traccar by tananaev.
the class Gt02ProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
// header
buf.skipBytes(2);
// size
buf.readByte();
Position position = new Position(getProtocolName());
// Zero for location messages
int power = buf.readUnsignedByte();
int gsm = buf.readUnsignedByte();
String imei = ByteBufUtil.hexDump(buf.readSlice(8)).substring(1);
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
if (deviceSession == null) {
return null;
}
position.setDeviceId(deviceSession.getDeviceId());
position.set(Position.KEY_INDEX, buf.readUnsignedShort());
int type = buf.readUnsignedByte();
if (type == MSG_HEARTBEAT) {
getLastLocation(position, null);
position.set(Position.KEY_POWER, power);
position.set(Position.KEY_RSSI, gsm);
if (channel != null) {
byte[] response = { 0x54, 0x68, 0x1A, 0x0D, 0x0A };
channel.writeAndFlush(new NetworkMessage(Unpooled.wrappedBuffer(response), remoteAddress));
}
} else if (type == MSG_DATA) {
DateBuilder dateBuilder = new DateBuilder().setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()).setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
position.setTime(dateBuilder.getDate());
double latitude = buf.readUnsignedInt() / (60.0 * 30000.0);
double longitude = buf.readUnsignedInt() / (60.0 * 30000.0);
position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
position.setCourse(buf.readUnsignedShort());
// reserved
buf.skipBytes(3);
long flags = buf.readUnsignedInt();
position.setValid(BitUtil.check(flags, 0));
if (!BitUtil.check(flags, 1)) {
latitude = -latitude;
}
if (!BitUtil.check(flags, 2)) {
longitude = -longitude;
}
position.setLatitude(latitude);
position.setLongitude(longitude);
} else if (type == MSG_RESPONSE) {
getLastLocation(position, null);
position.set(Position.KEY_RESULT, buf.readSlice(buf.readUnsignedByte()).toString(StandardCharsets.US_ASCII));
} else {
return null;
}
return position;
}
use of org.traccar.NetworkMessage 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;
if (sentence.contains("imei:") && sentence.length() <= 30) {
if (channel != null) {
channel.writeAndFlush(new NetworkMessage("LOAD", remoteAddress));
Matcher matcher = Pattern.compile("imei:(\\d+),").matcher(sentence);
if (matcher.find()) {
getDeviceSession(channel, remoteAddress, matcher.group(1));
}
}
return null;
}
if (!sentence.isEmpty() && Character.isDigit(sentence.charAt(0))) {
if (channel != null) {
channel.writeAndFlush(new NetworkMessage("ON", remoteAddress));
}
int start = sentence.indexOf("imei:");
if (start >= 0) {
sentence = sentence.substring(start);
} else {
return null;
}
}
if (sentence.startsWith("vr", 21)) {
return decodePhoto(channel, remoteAddress, sentence);
} else if (sentence.substring(21, 21 + 3).contains("OBD")) {
return decodeObd(channel, remoteAddress, sentence);
} else if (sentence.endsWith("*")) {
return decodeAlternative(channel, remoteAddress, sentence);
} else {
return decodeRegular(channel, remoteAddress, sentence);
}
}
use of org.traccar.NetworkMessage in project traccar by tananaev.
the class NavisProtocolDecoder method sendNtcbReply.
private void sendNtcbReply(Channel channel, ByteBuf data) {
if (channel != null) {
ByteBuf header = Unpooled.buffer(16);
header.writeCharSequence(prefix, StandardCharsets.US_ASCII);
header.writeIntLE((int) deviceUniqueId);
header.writeIntLE((int) serverId);
header.writeShortLE(data.readableBytes());
header.writeByte(Checksum.xor(data.nioBuffer()));
header.writeByte(Checksum.xor(header.nioBuffer()));
channel.writeAndFlush(new NetworkMessage(Unpooled.wrappedBuffer(header, data), channel.remoteAddress()));
}
}
use of org.traccar.NetworkMessage in project traccar by tananaev.
the class NoranProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
// length
buf.readUnsignedShortLE();
int type = buf.readUnsignedShortLE();
if (type == MSG_SHAKE_HAND && channel != null) {
ByteBuf response = Unpooled.buffer(13);
response.writeCharSequence("\r\n*KW", StandardCharsets.US_ASCII);
response.writeByte(0);
response.writeShortLE(response.capacity());
response.writeShortLE(MSG_SHAKE_HAND_RESPONSE);
// status
response.writeByte(1);
response.writeCharSequence("\r\n", StandardCharsets.US_ASCII);
channel.writeAndFlush(new NetworkMessage(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.readUnsignedIntLE();
// GIS port
buf.readUnsignedIntLE();
}
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.readUnsignedIntLE()));
position.setCourse(buf.readFloatLE());
} else {
position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
position.setCourse(buf.readUnsignedShortLE());
}
position.setLongitude(buf.readFloatLE());
position.setLatitude(buf.readFloatLE());
if (!newFormat) {
long timeValue = buf.readUnsignedIntLE();
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());
}
ByteBuf rawId;
if (newFormat) {
rawId = buf.readSlice(12);
} else {
rawId = buf.readSlice(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.readSlice(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.readShortLE());
position.set(Position.KEY_ODOMETER, buf.readFloatLE());
}
return position;
}
return null;
}
use of org.traccar.NetworkMessage in project traccar by tananaev.
the class OigoProtocolDecoder method decodeMgMessage.
private Position decodeMgMessage(Channel channel, SocketAddress remoteAddress, ByteBuf 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 = ByteBufUtil.hexDump(buf.readSlice(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)) {
ByteBuf response = Unpooled.buffer();
response.writeByte(MSG_ACKNOWLEDGEMENT);
response.writeByte(index);
response.writeByte(0x00);
channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
}
return position;
}
Aggregations