Search in sources :

Example 66 with NetworkMessage

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;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Example 67 with NetworkMessage

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;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) NetworkMessage(org.traccar.NetworkMessage) Parser(org.traccar.helper.Parser)

Example 68 with NetworkMessage

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()));
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage) ByteBuffer(java.nio.ByteBuffer)

Example 69 with NetworkMessage

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;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage) LinkedList(java.util.LinkedList)

Example 70 with NetworkMessage

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));
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Aggregations

NetworkMessage (org.traccar.NetworkMessage)146 ByteBuf (io.netty.buffer.ByteBuf)93 DeviceSession (org.traccar.DeviceSession)67 Position (org.traccar.model.Position)67 Parser (org.traccar.helper.Parser)34 Date (java.util.Date)27 DateBuilder (org.traccar.helper.DateBuilder)20 LinkedList (java.util.LinkedList)18 Network (org.traccar.model.Network)16 SimpleDateFormat (java.text.SimpleDateFormat)9 WifiAccessPoint (org.traccar.model.WifiAccessPoint)8 DateFormat (java.text.DateFormat)6 List (java.util.List)5 DatagramChannel (io.netty.channel.socket.DatagramChannel)4 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)4 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)4 Matcher (java.util.regex.Matcher)3 Pattern (java.util.regex.Pattern)3 CellTower (org.traccar.model.CellTower)3 DatagramPacket (io.netty.channel.socket.DatagramPacket)2