use of org.traccar.NetworkMessage in project traccar by tananaev.
the class CastelProtocolDecoder method sendResponse.
private void sendResponse(Channel channel, SocketAddress remoteAddress, ByteBuf id, short type) {
if (channel != null) {
int length = 2 + 2 + id.readableBytes() + 2 + 4 + 8 + 2 + 2;
ByteBuf response = Unpooled.buffer(length);
response.writeByte('@');
response.writeByte('@');
response.writeShortLE(length);
response.writeBytes(id);
response.writeShort(type);
response.writeIntLE(0);
for (int i = 0; i < 8; i++) {
response.writeByte(0xff);
}
response.writeShortLE(Checksum.crc16(Checksum.CRC16_X25, response.nioBuffer(0, response.writerIndex())));
response.writeByte(0x0D);
response.writeByte(0x0A);
channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
}
}
use of org.traccar.NetworkMessage in project traccar by tananaev.
the class BlueProtocolDecoder method sendResponse.
private void sendResponse(Channel channel, int deviceIndex) {
if (channel != null) {
ByteBuf response = Unpooled.buffer();
response.writeByte(0xaa);
response.writeShort(2 + 1 + 1 + 6 + 1);
// version
response.writeByte(0x86);
response.writeByte(0);
// data length
response.writeByte(6);
// type
response.writeByte(0xa4);
// server index
response.writeByte(0);
response.writeByte(deviceIndex);
response.writeByte(0);
response.writeByte(0);
response.writeByte(Checksum.xor(response.nioBuffer(1, response.writerIndex() - 1)));
channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
}
}
use of org.traccar.NetworkMessage in project traccar by tananaev.
the class C2stekProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
String sentence = (String) msg;
if (sentence.contains("$20$") && channel != null) {
channel.writeAndFlush(new NetworkMessage(sentence, remoteAddress));
}
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.setTime(parser.nextDateTime());
position.setValid(parser.nextInt() > 0);
position.setLatitude(parser.nextDouble());
position.setLongitude(parser.nextDouble());
position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble()));
position.setCourse(parser.nextDouble());
position.setAltitude(parser.nextDouble());
position.set(Position.KEY_BATTERY, parser.nextInt() * 0.001);
position.set(Position.KEY_ALARM, decodeAlarm(parser.nextHexInt()));
if (parser.hasNext()) {
position.set(Position.KEY_ARMED, parser.nextInt() > 0);
}
position.set(Position.KEY_DOOR, parser.nextInt() > 0);
position.set(Position.KEY_IGNITION, parser.nextInt() > 0);
return position;
}
use of org.traccar.NetworkMessage in project traccar by tananaev.
the class AutoTrackProtocolDecoder method decodeTelemetry.
private Position decodeTelemetry(Channel channel, SocketAddress remoteAddress, DeviceSession deviceSession, ByteBuf buf) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
// seconds since 2002
position.setTime(new Date(1009843200000L + buf.readUnsignedIntLE() * 1000));
position.setLatitude(buf.readIntLE() * 0.0000001);
position.setLongitude(buf.readIntLE() * 0.0000001);
position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE());
position.set(Position.KEY_FUEL_USED, buf.readUnsignedIntLE());
position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShortLE()));
// max speed
buf.readUnsignedShortLE();
position.set(Position.KEY_INPUT, buf.readUnsignedShortLE());
// di 3 count
buf.readUnsignedIntLE();
// di 4 count
buf.readUnsignedIntLE();
for (int i = 0; i < 5; i++) {
position.set(Position.PREFIX_ADC + (i + 1), buf.readUnsignedShortLE());
}
position.setCourse(buf.readUnsignedShortLE());
position.set(Position.KEY_STATUS, buf.readUnsignedShortLE());
position.set(Position.KEY_EVENT, buf.readUnsignedShortLE());
position.set(Position.KEY_DRIVER_UNIQUE_ID, buf.readLongLE());
int index = buf.readUnsignedShortLE();
// checksum
buf.readUnsignedShortLE();
if (channel != null) {
ByteBuf response = Unpooled.buffer();
// sync
response.writeInt(0xF1F1F1F1);
response.writeByte(MSG_TELEMETRY_CONFIRM);
// length
response.writeShortLE(2);
response.writeShortLE(index);
response.writeShort(Checksum.crc16(Checksum.CRC16_XMODEM, response.nioBuffer()));
channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
}
return position;
}
use of org.traccar.NetworkMessage in project traccar by tananaev.
the class AutoTrackProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
// sync
buf.skipBytes(4);
int type = buf.readUnsignedByte();
// length
buf.readUnsignedShortLE();
switch(type) {
case MSG_LOGIN_REQUEST:
String imei = ByteBufUtil.hexDump(buf.readSlice(8));
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
if (deviceSession == null) {
return null;
}
int fuelConst = buf.readUnsignedShortLE();
int tripConst = buf.readUnsignedShortLE();
if (channel != null) {
ByteBuf response = Unpooled.buffer();
// sync
response.writeInt(0xF1F1F1F1);
response.writeByte(MSG_LOGIN_CONFIRM);
// length
response.writeShortLE(12);
response.writeBytes(ByteBufUtil.decodeHexDump(imei));
response.writeShortLE(fuelConst);
response.writeShortLE(tripConst);
response.writeShort(Checksum.crc16(Checksum.CRC16_XMODEM, response.nioBuffer()));
channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
}
return null;
case MSG_TELEMETRY_1:
case MSG_TELEMETRY_2:
case MSG_TELEMETRY_3:
deviceSession = getDeviceSession(channel, remoteAddress);
if (deviceSession == null) {
return null;
}
return decodeTelemetry(channel, remoteAddress, deviceSession, buf);
default:
return null;
}
}
Aggregations