use of org.traccar.NetworkMessage in project traccar by tananaev.
the class H02ProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
String marker = buf.toString(0, 1, StandardCharsets.US_ASCII);
switch(marker) {
case "*":
String sentence = buf.toString(StandardCharsets.US_ASCII).trim();
int typeStart = sentence.indexOf(',', sentence.indexOf(',') + 1) + 1;
int typeEnd = sentence.indexOf(',', typeStart);
if (typeEnd < 0) {
typeEnd = sentence.indexOf('#', typeStart);
}
if (typeEnd > 0) {
String type = sentence.substring(typeStart, typeEnd);
switch(type) {
case "V0":
case "HTBT":
if (channel != null) {
String response = sentence.substring(0, typeEnd) + "#";
channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
}
return decodeHeartbeat(sentence, channel, remoteAddress);
case "NBR":
return decodeLbs(sentence, channel, remoteAddress);
case "LINK":
return decodeLink(sentence, channel, remoteAddress);
case "V3":
return decodeV3(sentence, channel, remoteAddress);
case "VP1":
return decodeVp1(sentence, channel, remoteAddress);
default:
return decodeText(sentence, channel, remoteAddress);
}
} else {
return null;
}
case "$":
return decodeBinary(buf, channel, remoteAddress);
case "X":
default:
return null;
}
}
use of org.traccar.NetworkMessage in project traccar by tananaev.
the class HuabaoProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
if (buf.getByte(buf.readerIndex()) == '(') {
String sentence = buf.toString(StandardCharsets.US_ASCII);
if (sentence.contains("BASE,2")) {
DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String response = sentence.replace("TIME", dateFormat.format(new Date()));
if (channel != null) {
channel.writeAndFlush(new NetworkMessage(Unpooled.copiedBuffer(response, StandardCharsets.US_ASCII), remoteAddress));
}
return null;
} else {
return decodeResult(channel, remoteAddress, sentence);
}
}
// start marker
buf.readUnsignedByte();
int type = buf.readUnsignedShort();
int attribute = buf.readUnsignedShort();
// phone number
ByteBuf id = buf.readSlice(6);
int index;
if (type == MSG_LOCATION_REPORT_2 || type == MSG_LOCATION_REPORT_BLIND) {
index = buf.readUnsignedByte();
} else {
index = buf.readUnsignedShort();
}
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, ByteBufUtil.hexDump(id));
if (deviceSession == null) {
return null;
}
if (deviceSession.getTimeZone() == null) {
deviceSession.setTimeZone(getTimeZone(deviceSession.getDeviceId(), "GMT+8"));
}
if (type == MSG_TERMINAL_REGISTER) {
if (channel != null) {
ByteBuf response = Unpooled.buffer();
response.writeShort(index);
response.writeByte(RESULT_SUCCESS);
response.writeBytes(ByteBufUtil.hexDump(id).getBytes(StandardCharsets.US_ASCII));
channel.writeAndFlush(new NetworkMessage(formatMessage(MSG_TERMINAL_REGISTER_RESPONSE, id, false, response), remoteAddress));
}
} else if (type == MSG_TERMINAL_AUTH || type == MSG_HEARTBEAT || type == MSG_PHOTO) {
sendGeneralResponse(channel, remoteAddress, id, type, index);
} else if (type == MSG_LOCATION_REPORT) {
sendGeneralResponse(channel, remoteAddress, id, type, index);
return decodeLocation(deviceSession, buf);
} else if (type == MSG_LOCATION_REPORT_2 || type == MSG_LOCATION_REPORT_BLIND) {
if (BitUtil.check(attribute, 15)) {
sendGeneralResponse2(channel, remoteAddress, id, type);
}
return decodeLocation2(deviceSession, buf, type);
} else if (type == MSG_LOCATION_BATCH) {
sendGeneralResponse(channel, remoteAddress, id, type, index);
return decodeLocationBatch(deviceSession, buf);
} else if (type == MSG_TIME_SYNC_REQUEST) {
if (channel != null) {
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
ByteBuf response = Unpooled.buffer();
response.writeShort(calendar.get(Calendar.YEAR));
response.writeByte(calendar.get(Calendar.MONTH) + 1);
response.writeByte(calendar.get(Calendar.DAY_OF_MONTH));
response.writeByte(calendar.get(Calendar.HOUR_OF_DAY));
response.writeByte(calendar.get(Calendar.MINUTE));
response.writeByte(calendar.get(Calendar.SECOND));
channel.writeAndFlush(new NetworkMessage(formatMessage(MSG_TERMINAL_REGISTER_RESPONSE, id, false, response), remoteAddress));
}
} else if (type == MSG_ACCELERATION) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
getLastLocation(position, null);
StringBuilder data = new StringBuilder("[");
while (buf.readableBytes() > 2) {
// time
buf.skipBytes(6);
if (data.length() > 1) {
data.append(",");
}
data.append("[");
data.append(readSignedWord(buf));
data.append(",");
data.append(readSignedWord(buf));
data.append(",");
data.append(readSignedWord(buf));
data.append("]");
}
data.append("]");
position.set(Position.KEY_G_SENSOR, data.toString());
return position;
}
return null;
}
use of org.traccar.NetworkMessage in project traccar by tananaev.
the class HuabaoProtocolDecoder method sendGeneralResponse2.
private void sendGeneralResponse2(Channel channel, SocketAddress remoteAddress, ByteBuf id, int type) {
if (channel != null) {
ByteBuf response = Unpooled.buffer();
response.writeShort(type);
response.writeByte(RESULT_SUCCESS);
channel.writeAndFlush(new NetworkMessage(formatMessage(MSG_GENERAL_RESPONSE_2, id, true, response), remoteAddress));
}
}
use of org.traccar.NetworkMessage in project traccar by tananaev.
the class GranitProtocolDecoder method sendResponseCurrent.
private static void sendResponseCurrent(Channel channel, int deviceId, long time) {
ByteBuf response = Unpooled.buffer();
response.writeBytes("BB+UGRC~".getBytes(StandardCharsets.US_ASCII));
// length
response.writeShortLE(6);
response.writeInt((int) time);
response.writeShortLE(deviceId);
appendChecksum(response, 16);
channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
}
use of org.traccar.NetworkMessage in project traccar by tananaev.
the class Gs100ProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
String header = buf.readCharSequence(2, StandardCharsets.US_ASCII).toString();
if (header.equals("GL")) {
buf.skipBytes(1);
String imei = buf.readCharSequence(buf.readUnsignedByte(), StandardCharsets.US_ASCII).toString();
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
if (channel != null && deviceSession != null) {
ByteBuf response = Unpooled.copiedBuffer("GS100", StandardCharsets.US_ASCII);
channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
}
return null;
} else {
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
if (deviceSession == null) {
return null;
}
List<Position> positions = new LinkedList<>();
int count = buf.readUnsignedByte();
for (int i = 0; i < count; i++) {
int endIndex = buf.readUnsignedByte() + buf.readerIndex();
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
int status = buf.readUnsignedMedium();
position.set(Position.KEY_STATUS, status);
if (BitUtil.check(status, 8 + 8 + 7)) {
DateBuilder dateBuilder = new DateBuilder().setHour(BcdUtil.readInteger(buf, 2)).setMinute(BcdUtil.readInteger(buf, 2)).setSecond(BcdUtil.readInteger(buf, 2)).setDay(BcdUtil.readInteger(buf, 2)).setMonth(BcdUtil.readInteger(buf, 2)).setYear(BcdUtil.readInteger(buf, 2));
position.setTime(dateBuilder.getDate());
position.setValid(true);
String coordinates = ByteBufUtil.hexDump(buf.readSlice(9));
position.setLongitude(Integer.parseInt(coordinates.substring(0, 3)) + Integer.parseInt(coordinates.substring(3, 9)) * 0.0001 / 60);
position.setLatitude(Integer.parseInt(coordinates.substring(10, 12)) + Integer.parseInt(coordinates.substring(12, 18)) * 0.0001 / 60);
int flags = Integer.parseInt(coordinates.substring(9, 10), 16);
if (!BitUtil.check(flags, 3)) {
position.setLongitude(-position.getLongitude());
}
if (!BitUtil.check(flags, 2)) {
position.setLatitude(-position.getLatitude());
}
String other = ByteBufUtil.hexDump(buf.readSlice(4));
position.setSpeed(UnitsConverter.knotsFromKph(Integer.parseInt(other.substring(0, 5)) * 0.01));
position.setCourse(Integer.parseInt(other.substring(5, 8)));
} else {
getLastLocation(position, null);
}
positions.add(position);
buf.readerIndex(endIndex);
}
if (channel != null) {
ByteBuf response = Unpooled.buffer();
response.writeCharSequence("GS100", StandardCharsets.US_ASCII);
response.writeByte(count);
channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
}
return positions.isEmpty() ? null : positions;
}
}
Aggregations