Search in sources :

Example 21 with NetworkMessage

use of org.traccar.NetworkMessage in project traccar by tananaev.

the class Gt02ProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ByteBuf buf = (ByteBuf) msg;
    // header
    buf.skipBytes(2);
    // size
    buf.readByte();
    Position position = new Position(getProtocolName());
    // Zero for location messages
    int power = buf.readUnsignedByte();
    int gsm = buf.readUnsignedByte();
    String imei = ByteBufUtil.hexDump(buf.readSlice(8)).substring(1);
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());
    position.set(Position.KEY_INDEX, buf.readUnsignedShort());
    int type = buf.readUnsignedByte();
    if (type == MSG_HEARTBEAT) {
        getLastLocation(position, null);
        position.set(Position.KEY_POWER, power);
        position.set(Position.KEY_RSSI, gsm);
        if (channel != null) {
            byte[] response = { 0x54, 0x68, 0x1A, 0x0D, 0x0A };
            channel.writeAndFlush(new NetworkMessage(Unpooled.wrappedBuffer(response), remoteAddress));
        }
    } else if (type == MSG_DATA) {
        DateBuilder dateBuilder = new DateBuilder().setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()).setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
        position.setTime(dateBuilder.getDate());
        double latitude = buf.readUnsignedInt() / (60.0 * 30000.0);
        double longitude = buf.readUnsignedInt() / (60.0 * 30000.0);
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
        position.setCourse(buf.readUnsignedShort());
        // reserved
        buf.skipBytes(3);
        long flags = buf.readUnsignedInt();
        position.setValid(BitUtil.check(flags, 0));
        if (!BitUtil.check(flags, 1)) {
            latitude = -latitude;
        }
        if (!BitUtil.check(flags, 2)) {
            longitude = -longitude;
        }
        position.setLatitude(latitude);
        position.setLongitude(longitude);
    } else if (type == MSG_RESPONSE) {
        getLastLocation(position, null);
        position.set(Position.KEY_RESULT, buf.readSlice(buf.readUnsignedByte()).toString(StandardCharsets.US_ASCII));
    } else {
        return null;
    }
    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 22 with NetworkMessage

use of org.traccar.NetworkMessage in project traccar by tananaev.

the class Gps103ProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    if (sentence.contains("imei:") && sentence.length() <= 30) {
        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage("LOAD", remoteAddress));
            Matcher matcher = Pattern.compile("imei:(\\d+),").matcher(sentence);
            if (matcher.find()) {
                getDeviceSession(channel, remoteAddress, matcher.group(1));
            }
        }
        return null;
    }
    if (!sentence.isEmpty() && Character.isDigit(sentence.charAt(0))) {
        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage("ON", remoteAddress));
        }
        int start = sentence.indexOf("imei:");
        if (start >= 0) {
            sentence = sentence.substring(start);
        } else {
            return null;
        }
    }
    if (sentence.startsWith("vr", 21)) {
        return decodePhoto(channel, remoteAddress, sentence);
    } else if (sentence.substring(21, 21 + 3).contains("OBD")) {
        return decodeObd(channel, remoteAddress, sentence);
    } else if (sentence.endsWith("*")) {
        return decodeAlternative(channel, remoteAddress, sentence);
    } else {
        return decodeRegular(channel, remoteAddress, sentence);
    }
}
Also used : Matcher(java.util.regex.Matcher) NetworkMessage(org.traccar.NetworkMessage)

Example 23 with NetworkMessage

use of org.traccar.NetworkMessage in project traccar by tananaev.

the class NavisProtocolDecoder method sendNtcbReply.

private void sendNtcbReply(Channel channel, ByteBuf data) {
    if (channel != null) {
        ByteBuf header = Unpooled.buffer(16);
        header.writeCharSequence(prefix, StandardCharsets.US_ASCII);
        header.writeIntLE((int) deviceUniqueId);
        header.writeIntLE((int) serverId);
        header.writeShortLE(data.readableBytes());
        header.writeByte(Checksum.xor(data.nioBuffer()));
        header.writeByte(Checksum.xor(header.nioBuffer()));
        channel.writeAndFlush(new NetworkMessage(Unpooled.wrappedBuffer(header, data), channel.remoteAddress()));
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Example 24 with NetworkMessage

use of org.traccar.NetworkMessage in project traccar by tananaev.

the class NoranProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ByteBuf buf = (ByteBuf) msg;
    // length
    buf.readUnsignedShortLE();
    int type = buf.readUnsignedShortLE();
    if (type == MSG_SHAKE_HAND && channel != null) {
        ByteBuf response = Unpooled.buffer(13);
        response.writeCharSequence("\r\n*KW", StandardCharsets.US_ASCII);
        response.writeByte(0);
        response.writeShortLE(response.capacity());
        response.writeShortLE(MSG_SHAKE_HAND_RESPONSE);
        // status
        response.writeByte(1);
        response.writeCharSequence("\r\n", StandardCharsets.US_ASCII);
        channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
    } else if (type == MSG_UPLOAD_POSITION || type == MSG_UPLOAD_POSITION_NEW || type == MSG_CONTROL_RESPONSE || type == MSG_ALARM) {
        boolean newFormat = false;
        if (type == MSG_UPLOAD_POSITION && buf.readableBytes() == 48 || type == MSG_ALARM && buf.readableBytes() == 48 || type == MSG_CONTROL_RESPONSE && buf.readableBytes() == 57) {
            newFormat = true;
        }
        Position position = new Position(getProtocolName());
        if (type == MSG_CONTROL_RESPONSE) {
            // GIS ip
            buf.readUnsignedIntLE();
            // GIS port
            buf.readUnsignedIntLE();
        }
        position.setValid(BitUtil.check(buf.readUnsignedByte(), 0));
        short alarm = buf.readUnsignedByte();
        switch(alarm) {
            case 1:
                position.set(Position.KEY_ALARM, Position.ALARM_SOS);
                break;
            case 2:
                position.set(Position.KEY_ALARM, Position.ALARM_OVERSPEED);
                break;
            case 3:
                position.set(Position.KEY_ALARM, Position.ALARM_GEOFENCE_EXIT);
                break;
            case 9:
                position.set(Position.KEY_ALARM, Position.ALARM_POWER_OFF);
                break;
            default:
                break;
        }
        if (newFormat) {
            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedIntLE()));
            position.setCourse(buf.readFloatLE());
        } else {
            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
            position.setCourse(buf.readUnsignedShortLE());
        }
        position.setLongitude(buf.readFloatLE());
        position.setLatitude(buf.readFloatLE());
        if (!newFormat) {
            long timeValue = buf.readUnsignedIntLE();
            DateBuilder dateBuilder = new DateBuilder().setYear((int) BitUtil.from(timeValue, 26)).setMonth((int) BitUtil.between(timeValue, 22, 26)).setDay((int) BitUtil.between(timeValue, 17, 22)).setHour((int) BitUtil.between(timeValue, 12, 17)).setMinute((int) BitUtil.between(timeValue, 6, 12)).setSecond((int) BitUtil.to(timeValue, 6));
            position.setTime(dateBuilder.getDate());
        }
        ByteBuf rawId;
        if (newFormat) {
            rawId = buf.readSlice(12);
        } else {
            rawId = buf.readSlice(11);
        }
        String id = rawId.toString(StandardCharsets.US_ASCII).replaceAll("[^\\p{Print}]", "");
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
        if (deviceSession == null) {
            return null;
        }
        position.setDeviceId(deviceSession.getDeviceId());
        if (newFormat) {
            DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
            position.setTime(dateFormat.parse(buf.readSlice(17).toString(StandardCharsets.US_ASCII)));
            buf.readByte();
        }
        if (!newFormat) {
            position.set(Position.PREFIX_IO + 1, buf.readUnsignedByte());
            position.set(Position.KEY_FUEL_LEVEL, buf.readUnsignedByte());
        } else if (type == MSG_UPLOAD_POSITION_NEW) {
            position.set(Position.PREFIX_TEMP + 1, buf.readShortLE());
            position.set(Position.KEY_ODOMETER, buf.readFloatLE());
        }
        return position;
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage) SimpleDateFormat(java.text.SimpleDateFormat)

Example 25 with NetworkMessage

use of org.traccar.NetworkMessage in project traccar by tananaev.

the class OigoProtocolDecoder method decodeMgMessage.

private Position decodeMgMessage(Channel channel, SocketAddress remoteAddress, ByteBuf buf) {
    // tag
    buf.readUnsignedByte();
    int flags = buf.getUnsignedByte(buf.readerIndex());
    DeviceSession deviceSession;
    if (BitUtil.check(flags, 6)) {
        // flags
        buf.readUnsignedByte();
        deviceSession = getDeviceSession(channel, remoteAddress);
    } else {
        String imei = ByteBufUtil.hexDump(buf.readSlice(8)).substring(1);
        deviceSession = getDeviceSession(channel, remoteAddress, imei);
    }
    if (deviceSession == null) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    // imsi
    buf.skipBytes(8);
    int date = buf.readUnsignedShort();
    DateBuilder dateBuilder = new DateBuilder().setDate(2010 + BitUtil.from(date, 12), BitUtil.between(date, 8, 12), BitUtil.to(date, 8)).setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), 0);
    position.setValid(true);
    position.setLatitude(convertCoordinate(buf.readInt()));
    position.setLongitude(convertCoordinate(buf.readInt()));
    position.setAltitude(UnitsConverter.metersFromFeet(buf.readShort()));
    position.setCourse(buf.readUnsignedShort());
    position.setSpeed(UnitsConverter.knotsFromMph(buf.readUnsignedByte()));
    position.set(Position.KEY_POWER, buf.readUnsignedByte() * 0.1);
    position.set(Position.PREFIX_IO + 1, buf.readUnsignedByte());
    dateBuilder.setSecond(buf.readUnsignedByte());
    position.setTime(dateBuilder.getDate());
    position.set(Position.KEY_RSSI, buf.readUnsignedByte());
    int index = buf.readUnsignedByte();
    position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte());
    position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
    position.set(Position.KEY_ODOMETER, (long) (buf.readUnsignedInt() * 1609.34));
    if (channel != null && BitUtil.check(flags, 7)) {
        ByteBuf response = Unpooled.buffer();
        response.writeByte(MSG_ACKNOWLEDGEMENT);
        response.writeByte(index);
        response.writeByte(0x00);
        channel.writeAndFlush(new NetworkMessage(response, 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)

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