use of org.traccar.NetworkMessage in project traccar by tananaev.
the class PricolProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
// header
buf.readUnsignedByte();
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, buf.readSlice(7).toString(StandardCharsets.US_ASCII));
if (deviceSession == null) {
return null;
}
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.set("eventType", buf.readUnsignedByte());
position.set("packetVersion", buf.readUnsignedByte());
position.set(Position.KEY_STATUS, buf.readUnsignedByte());
position.set(Position.KEY_RSSI, buf.readUnsignedByte());
position.set(Position.KEY_GPS, buf.readUnsignedByte());
position.setTime(new DateBuilder().setDateReverse(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()).setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()).getDate());
position.setValid(true);
double lat = buf.getUnsignedShort(buf.readerIndex()) / 100;
lat += (buf.readUnsignedShort() % 100 * 10000 + buf.readUnsignedShort()) / 600000.0;
position.setLatitude(buf.readUnsignedByte() == 'S' ? -lat : lat);
double lon = buf.getUnsignedMedium(buf.readerIndex()) / 100;
lon += (buf.readUnsignedMedium() % 100 * 10000 + buf.readUnsignedShort()) / 600000.0;
position.setLongitude(buf.readUnsignedByte() == 'W' ? -lon : lon);
position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
position.set(Position.KEY_INPUT, buf.readUnsignedShort());
position.set(Position.KEY_OUTPUT, buf.readUnsignedByte());
position.set("analogAlerts", buf.readUnsignedByte());
position.set("customAlertTypes", buf.readUnsignedShort());
for (int i = 1; i <= 5; i++) {
position.set(Position.PREFIX_ADC + i, buf.readUnsignedShort());
}
position.set(Position.KEY_ODOMETER, buf.readUnsignedMedium());
position.set(Position.KEY_RPM, buf.readUnsignedShort());
if (channel != null) {
channel.writeAndFlush(new NetworkMessage(Unpooled.copiedBuffer("ACK", StandardCharsets.US_ASCII), remoteAddress));
}
return position;
}
use of org.traccar.NetworkMessage in project traccar by tananaev.
the class SviasProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
if (channel != null) {
channel.writeAndFlush(new NetworkMessage("@", 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(Parser.DateTimeFormat.DMY_HMS));
position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN_MIN));
position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN_MIN));
position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble() * 0.01));
position.setCourse(parser.nextDouble() * 0.01);
position.set(Position.KEY_ODOMETER, parser.nextInt() * 100);
int input = parser.nextInt();
int output = parser.nextInt();
position.set(Position.KEY_ALARM, BitUtil.check(input, 0) ? Position.ALARM_SOS : null);
position.set(Position.KEY_IGNITION, BitUtil.check(input, 4));
position.setValid(BitUtil.check(output, 0));
position.set(Position.KEY_POWER, parser.nextInt() * 0.001);
position.set(Position.KEY_BATTERY_LEVEL, parser.nextInt());
position.set(Position.KEY_RSSI, parser.nextInt());
return position;
}
use of org.traccar.NetworkMessage in project traccar by tananaev.
the class ArnaviBinaryProtocolDecoder method sendResponse.
private void sendResponse(Channel channel, byte version, int index) {
if (channel != null) {
ByteBuf response = Unpooled.buffer();
response.writeByte(0x7b);
if (version == HEADER_VERSION_1) {
response.writeByte(0x00);
response.writeByte((byte) index);
} else if (version == HEADER_VERSION_2) {
response.writeByte(0x04);
response.writeByte(0x00);
ByteBuffer time = ByteBuffer.allocate(4).putInt((int) (System.currentTimeMillis() / 1000));
((Buffer) time).position(0);
response.writeByte(Checksum.modulo256(time.slice()));
response.writeBytes(time);
}
response.writeByte(0x7d);
channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
}
}
use of org.traccar.NetworkMessage in project traccar by tananaev.
the class AstraProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
if (channel != null) {
channel.writeAndFlush(new NetworkMessage(Unpooled.wrappedBuffer(new byte[] { 0x06 }), remoteAddress));
}
// protocol
buf.readUnsignedByte();
// length
buf.readUnsignedShort();
String imei = String.format("%08d", buf.readUnsignedInt()) + String.format("%07d", buf.readUnsignedMedium());
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
if (deviceSession == null) {
return null;
}
List<Position> positions = new LinkedList<>();
while (buf.readableBytes() > 2) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
// index
buf.readUnsignedByte();
position.setValid(true);
position.setLatitude(buf.readInt() * 0.000001);
position.setLongitude(buf.readInt() * 0.000001);
DateBuilder dateBuilder = new DateBuilder().setDate(1980, 1, 6).addMillis(buf.readUnsignedInt() * 1000L);
position.setTime(dateBuilder.getDate());
position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte() * 2));
position.setCourse(buf.readUnsignedByte() * 2);
int reason = buf.readUnsignedMedium();
position.set(Position.KEY_EVENT, reason);
int status = buf.readUnsignedShort();
position.set(Position.KEY_STATUS, status);
position.set(Position.PREFIX_IO + 1, buf.readUnsignedByte());
position.set(Position.PREFIX_ADC + 1, buf.readUnsignedByte());
position.set(Position.KEY_BATTERY, buf.readUnsignedByte());
position.set(Position.KEY_POWER, buf.readUnsignedByte());
// max journey speed
buf.readUnsignedByte();
// accelerometer
buf.skipBytes(6);
position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedShort());
// journey idle time
buf.readUnsignedShort();
position.setAltitude(buf.readUnsignedByte() * 20);
int quality = buf.readUnsignedByte();
position.set(Position.KEY_SATELLITES, quality & 0xf);
position.set(Position.KEY_RSSI, quality >> 4);
// geofence events
buf.readUnsignedByte();
if (BitUtil.check(status, 8)) {
position.set(Position.KEY_DRIVER_UNIQUE_ID, buf.readSlice(7).toString(StandardCharsets.US_ASCII));
position.set(Position.KEY_ODOMETER, buf.readUnsignedMedium() * 1000);
position.set(Position.KEY_HOURS, UnitsConverter.msFromHours(buf.readUnsignedShort()));
}
if (BitUtil.check(status, 6)) {
LOGGER.warn("Extension data is not supported");
return position;
}
positions.add(position);
}
return positions;
}
use of org.traccar.NetworkMessage in project traccar by tananaev.
the class AnytrekProtocolDecoder method sendResponse.
private void sendResponse(Channel channel, SocketAddress remoteAddress, int type) {
if (channel != null) {
ByteBuf response = Unpooled.buffer();
response.writeShort(0x7878);
// length
response.writeShortLE(1 + 1 + 2 + 1 + 2);
response.writeByte(type);
// error
response.writeByte(0);
// report interval
response.writeShortLE(0);
// clear alarm
response.writeByte(0);
// checksum
response.writeShortLE(0);
response.writeByte('\r');
response.writeByte('\n');
channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
}
}
Aggregations