use of org.traccar.NetworkMessage in project traccar by tananaev.
the class Jt600ProtocolDecoder method decodeBinary.
private List<Position> decodeBinary(ByteBuf buf, Channel channel, SocketAddress remoteAddress) {
List<Position> positions = new LinkedList<>();
// header
buf.readByte();
boolean longFormat = isLongFormat(buf, buf.readerIndex());
String id = String.valueOf(Long.parseLong(ByteBufUtil.hexDump(buf.readSlice(5))));
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
if (deviceSession == null) {
return null;
}
int protocolVersion = 0;
if (longFormat) {
protocolVersion = buf.readUnsignedByte();
}
int version = BitUtil.from(buf.readUnsignedByte(), 4);
// length
buf.readUnsignedShort();
boolean responseRequired = false;
while (buf.readableBytes() > 1) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
decodeBinaryLocation(buf, position);
if (longFormat) {
position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 1000);
position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
// vehicle id combined
buf.readUnsignedInt();
int status = buf.readUnsignedShort();
position.set(Position.KEY_ALARM, BitUtil.check(status, 1) ? Position.ALARM_GEOFENCE_ENTER : null);
position.set(Position.KEY_ALARM, BitUtil.check(status, 2) ? Position.ALARM_GEOFENCE_EXIT : null);
position.set(Position.KEY_ALARM, BitUtil.check(status, 3) ? Position.ALARM_POWER_CUT : null);
position.set(Position.KEY_ALARM, BitUtil.check(status, 4) ? Position.ALARM_VIBRATION : null);
if (BitUtil.check(status, 5)) {
responseRequired = true;
}
position.set(Position.KEY_BLOCKED, BitUtil.check(status, 7));
position.set(Position.KEY_ALARM, BitUtil.check(status, 8 + 3) ? Position.ALARM_LOW_BATTERY : null);
position.set(Position.KEY_ALARM, BitUtil.check(status, 8 + 6) ? Position.ALARM_FAULT : null);
position.set(Position.KEY_STATUS, status);
int battery = buf.readUnsignedByte();
if (battery == 0xff) {
position.set(Position.KEY_CHARGE, true);
} else {
position.set(Position.KEY_BATTERY_LEVEL, battery);
}
CellTower cellTower = CellTower.fromCidLac(buf.readUnsignedShort(), buf.readUnsignedShort());
cellTower.setSignalStrength((int) buf.readUnsignedByte());
position.setNetwork(new Network(cellTower));
if (protocolVersion == 0x17 || protocolVersion == 0x19) {
// geofence id
buf.readUnsignedByte();
// reserved
buf.skipBytes(3);
buf.skipBytes(buf.readableBytes() - 1);
}
} else if (version == 1) {
position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
position.set(Position.KEY_POWER, buf.readUnsignedByte());
// other flags and sensors
buf.readByte();
position.setAltitude(buf.readUnsignedShort());
int cid = buf.readUnsignedShort();
int lac = buf.readUnsignedShort();
int rssi = buf.readUnsignedByte();
if (cid != 0 && lac != 0) {
CellTower cellTower = CellTower.fromCidLac(cid, lac);
cellTower.setSignalStrength(rssi);
position.setNetwork(new Network(cellTower));
} else {
position.set(Position.KEY_RSSI, rssi);
}
} else if (version == 2) {
int fuel = buf.readUnsignedByte() << 8;
decodeStatus(position, buf);
position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 1000);
fuel += buf.readUnsignedByte();
position.set(Position.KEY_FUEL_LEVEL, fuel);
} else if (version == 3) {
BitBuffer bitBuffer = new BitBuffer(buf);
position.set("fuel1", bitBuffer.readUnsigned(12));
position.set("fuel2", bitBuffer.readUnsigned(12));
position.set("fuel3", bitBuffer.readUnsigned(12));
position.set(Position.KEY_ODOMETER, bitBuffer.readUnsigned(20) * 1000);
int status = bitBuffer.readUnsigned(24);
position.set(Position.KEY_IGNITION, BitUtil.check(status, 0));
position.set(Position.KEY_STATUS, status);
}
positions.add(position);
}
int index = buf.readUnsignedByte();
if (channel != null && responseRequired) {
if (protocolVersion < 0x19) {
channel.writeAndFlush(new NetworkMessage("(P35)", remoteAddress));
} else {
channel.writeAndFlush(new NetworkMessage("(P69,0," + index + ")", remoteAddress));
}
}
return positions;
}
use of org.traccar.NetworkMessage in project traccar by tananaev.
the class MobilogixProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
String sentence = ((String) msg).trim();
String type = sentence.substring(21, sentence.indexOf(',', 21));
if (channel != null) {
String time = sentence.substring(1, 20);
String response;
if (type.equals("T1")) {
response = String.format("[%s,S1,1]", time);
} else {
response = String.format("[%s,S%s]", time, type.substring(1));
}
channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
}
Parser parser = new Parser(PATTERN, sentence);
if (!parser.matches()) {
return null;
}
Position position = new Position(getProtocolName());
position.setDeviceTime(parser.nextDateTime());
if (parser.nextInt() == 0) {
position.set(Position.KEY_ARCHIVE, true);
}
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
if (deviceSession == null) {
return null;
}
position.setDeviceId(deviceSession.getDeviceId());
position.set(Position.KEY_TYPE, type);
position.set(Position.KEY_ALARM, decodeAlarm(type));
int status = parser.nextHexInt();
position.set(Position.KEY_IGNITION, BitUtil.check(status, 2));
position.set(Position.KEY_MOTION, BitUtil.check(status, 3));
position.set(Position.KEY_STATUS, status);
position.set(Position.KEY_BATTERY, parser.nextDouble());
if (parser.hasNext(7)) {
position.set(Position.KEY_SATELLITES, parser.nextInt());
position.set(Position.KEY_RSSI, 6 * parser.nextInt() - 111);
position.setValid(parser.nextInt() > 0);
position.setFixTime(position.getDeviceTime());
position.setLatitude(parser.nextDouble());
position.setLongitude(parser.nextDouble());
position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble()));
position.setCourse(parser.nextDouble());
} else {
getLastLocation(position, position.getDeviceTime());
}
return position;
}
use of org.traccar.NetworkMessage in project traccar by tananaev.
the class NavisProtocolDecoder method sendFlexReply.
private void sendFlexReply(Channel channel, ByteBuf data) {
if (channel != null) {
ByteBuf cs = Unpooled.buffer(1);
cs.writeByte(Checksum.crc8(new Algorithm(8, 0x31, 0xFF, false, false, 0x00), data.nioBuffer()));
channel.writeAndFlush(new NetworkMessage(Unpooled.wrappedBuffer(data, cs), channel.remoteAddress()));
}
}
use of org.traccar.NetworkMessage in project traccar by tananaev.
the class NavtelecomProtocolDecoder method sendResponse.
private void sendResponse(Channel channel, SocketAddress remoteAddress, int receiver, int sender, ByteBuf content) {
if (channel != null) {
ByteBuf response = Unpooled.buffer();
response.writeCharSequence("@NTC", StandardCharsets.US_ASCII);
response.writeIntLE(sender);
response.writeIntLE(receiver);
response.writeShortLE(content.readableBytes());
response.writeByte(Checksum.xor(content.nioBuffer()));
response.writeByte(Checksum.xor(response.nioBuffer()));
response.writeBytes(content);
content.release();
channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
}
}
use of org.traccar.NetworkMessage in project traccar by tananaev.
the class NavtelecomProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
if (buf.getByte(buf.readerIndex()) == '@') {
// preamble
buf.skipBytes(4);
int receiver = buf.readIntLE();
int sender = buf.readIntLE();
int length = buf.readUnsignedShortLE();
// data checksum
buf.readUnsignedByte();
// header checksum
buf.readUnsignedByte();
String type = buf.toString(buf.readerIndex(), 6, StandardCharsets.US_ASCII);
if (type.startsWith("*>S")) {
String sentence = buf.readCharSequence(length, StandardCharsets.US_ASCII).toString();
getDeviceSession(channel, remoteAddress, sentence.substring(4));
ByteBuf payload = Unpooled.copiedBuffer("*<S", StandardCharsets.US_ASCII);
sendResponse(channel, remoteAddress, receiver, sender, payload);
} else if (type.startsWith("*>FLEX")) {
buf.skipBytes(6);
ByteBuf payload = Unpooled.buffer();
payload.writeCharSequence("*<FLEX", StandardCharsets.US_ASCII);
// protocol
payload.writeByte(buf.readUnsignedByte());
// protocol version
payload.writeByte(buf.readUnsignedByte());
// struct version
payload.writeByte(buf.readUnsignedByte());
int bitCount = buf.readUnsignedByte();
bits = new BitSet((bitCount + 7) / 8);
int currentByte = 0;
for (int i = 0; i < bitCount; i++) {
if (i % 8 == 0) {
currentByte = buf.readUnsignedByte();
}
bits.set(i, BitUtil.check(currentByte, 7 - i % 8));
}
sendResponse(channel, remoteAddress, receiver, sender, payload);
}
} else {
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
if (deviceSession == null) {
return null;
}
String type = buf.readCharSequence(2, StandardCharsets.US_ASCII).toString();
if (type.equals("~A")) {
int count = buf.readUnsignedByte();
List<Position> positions = new LinkedList<>();
for (int i = 0; i < count; i++) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
for (int j = 0; j < bits.length(); j++) {
if (bits.get(j)) {
switch(j + 1) {
case 1:
position.set(Position.KEY_INDEX, buf.readUnsignedIntLE());
break;
case 2:
position.set(Position.KEY_EVENT, buf.readUnsignedShortLE());
break;
case 3:
position.setDeviceTime(new Date(buf.readUnsignedIntLE() * 1000));
break;
case 9:
position.setValid(true);
position.setFixTime(new Date(buf.readUnsignedIntLE() * 1000));
break;
case 10:
position.setLatitude(buf.readIntLE() * 0.0001 / 60);
break;
case 11:
position.setLongitude(buf.readIntLE() * 0.0001 / 60);
break;
case 12:
position.setAltitude(buf.readIntLE() * 0.1);
break;
case 13:
position.setSpeed(UnitsConverter.knotsFromKph(buf.readFloatLE()));
break;
default:
buf.skipBytes(getItemLength(j + 1));
break;
}
}
}
if (position.getFixTime() == null) {
getLastLocation(position, position.getDeviceTime());
}
positions.add(position);
}
if (channel != null) {
ByteBuf response = Unpooled.buffer();
response.writeCharSequence(type, StandardCharsets.US_ASCII);
response.writeByte(count);
response.writeByte(Checksum.crc8(Checksum.CRC8_ROHC, response.nioBuffer()));
channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
}
return positions;
}
}
return null;
}
Aggregations