Search in sources :

Example 31 with NetworkMessage

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

the class Mta6ProtocolDecoder method sendContinue.

private void sendContinue(Channel channel) {
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE);
    channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) NetworkMessage(org.traccar.NetworkMessage)

Example 32 with NetworkMessage

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

the class NavigilProtocolDecoder method sendAcknowledgment.

private void sendAcknowledgment(Channel channel, int sequenceNumber) {
    ByteBuf data = Unpooled.buffer(4);
    data.writeShortLE(sequenceNumber);
    // OK
    data.writeShortLE(0);
    ByteBuf header = Unpooled.buffer(20);
    header.writeByte(1);
    header.writeByte(0);
    header.writeShortLE(senderSequenceNumber++);
    header.writeShortLE(MSG_ACKNOWLEDGEMENT);
    header.writeShortLE(header.capacity() + data.capacity());
    header.writeShortLE(0);
    header.writeShortLE(Checksum.crc16(Checksum.CRC16_CCITT_FALSE, data.nioBuffer()));
    header.writeIntLE(0);
    header.writeIntLE((int) (System.currentTimeMillis() / 1000) + LEAP_SECONDS_DELTA);
    if (channel != null) {
        channel.writeAndFlush(new NetworkMessage(Unpooled.wrappedBuffer(header, data), channel.remoteAddress()));
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Example 33 with NetworkMessage

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

the class NiotProtocolDecoder method sendResponse.

private void sendResponse(Channel channel, SocketAddress remoteAddress, int type, int checksum) {
    if (channel != null) {
        ByteBuf response = Unpooled.buffer();
        // header
        response.writeShort(0x5858);
        response.writeByte(MSG_RESPONSE);
        // length
        response.writeShort(5);
        response.writeByte(checksum);
        response.writeByte(type);
        // subtype
        response.writeByte(0);
        response.writeByte(Checksum.xor(response.nioBuffer(2, response.writerIndex())));
        response.writeByte(0x0D);
        channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Example 34 with NetworkMessage

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

the class Tk103ProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    if (channel != null) {
        String id = sentence.substring(1, 13);
        String type = sentence.substring(13, 17);
        if (type.equals("BP00")) {
            channel.writeAndFlush(new NetworkMessage("(" + id + "AP01HSO)", remoteAddress));
            return null;
        } else if (type.equals("BP05")) {
            channel.writeAndFlush(new NetworkMessage("(" + id + "AP05)", remoteAddress));
        }
    }
    if (sentence.contains("ZC20")) {
        return decodeBattery(channel, remoteAddress, sentence);
    } else if (sentence.contains("BZ00")) {
        return decodeNetwork(channel, remoteAddress, sentence);
    } else if (sentence.contains("ZC03")) {
        return decodeCommandResult(channel, remoteAddress, sentence);
    } else if (sentence.contains("DW5")) {
        return decodeLbsWifi(channel, remoteAddress, sentence);
    } else if (sentence.contains("BV00")) {
        return decodeVin(channel, remoteAddress, sentence);
    }
    Parser parser = new Parser(PATTERN, sentence);
    if (!parser.matches()) {
        return null;
    }
    String id = null;
    boolean alternative = false;
    if (parser.hasNext()) {
        id = parser.next();
    }
    if (parser.hasNext()) {
        id = parser.next();
        alternative = true;
    }
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
    if (deviceSession == null) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    String type = parser.next();
    String data = null;
    if (parser.hasNext()) {
        data = parser.next();
    }
    if (parser.hasNext()) {
        data = parser.next();
    }
    decodeType(position, type, data);
    DateBuilder dateBuilder = new DateBuilder();
    if (alternative) {
        dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
    } else {
        dateBuilder.setDate(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
    }
    position.setValid(parser.next().equals("A"));
    position.setLatitude(parser.nextCoordinate());
    position.setLongitude(parser.nextCoordinate());
    position.setSpeed(convertSpeed(parser.nextDouble(0), "kmh"));
    dateBuilder.setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
    position.setTime(dateBuilder.getDate());
    if (parser.hasNext()) {
        position.setCourse(parser.nextDouble());
    }
    if (parser.hasNext()) {
        position.setCourse(parser.nextDouble());
    }
    if (parser.hasNext(7)) {
        position.set(Position.KEY_CHARGE, parser.nextInt() == 0);
        position.set(Position.KEY_IGNITION, parser.nextInt() == 1);
        int mask1 = parser.nextHexInt();
        position.set(Position.PREFIX_IN + 2, BitUtil.check(mask1, 0) ? 1 : 0);
        position.set("panic", BitUtil.check(mask1, 1) ? 1 : 0);
        position.set(Position.PREFIX_OUT + 2, BitUtil.check(mask1, 2) ? 1 : 0);
        if (decodeLow || BitUtil.check(mask1, 3)) {
            position.set(Position.KEY_BLOCKED, BitUtil.check(mask1, 3) ? 1 : 0);
        }
        int mask2 = parser.nextHexInt();
        for (int i = 0; i < 3; i++) {
            if (decodeLow || BitUtil.check(mask2, i)) {
                position.set("hs" + (3 - i), BitUtil.check(mask2, i) ? 1 : 0);
            }
        }
        if (decodeLow || BitUtil.check(mask2, 3)) {
            position.set(Position.KEY_DOOR, BitUtil.check(mask2, 3) ? 1 : 0);
        }
        int mask3 = parser.nextHexInt();
        for (int i = 1; i <= 3; i++) {
            if (decodeLow || BitUtil.check(mask3, i)) {
                position.set("ls" + (3 - i + 1), BitUtil.check(mask3, i) ? 1 : 0);
            }
        }
        position.set(Position.KEY_FUEL_LEVEL, parser.nextHexInt());
        position.set(Position.KEY_ODOMETER, parser.nextLong(16, 0));
    }
    if (parser.hasNext()) {
        position.setCourse(parser.nextDouble());
    }
    if (parser.hasNext()) {
        position.set(Position.PREFIX_TEMP + 1, parser.nextDouble(0));
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) NetworkMessage(org.traccar.NetworkMessage) WifiAccessPoint(org.traccar.model.WifiAccessPoint) Parser(org.traccar.helper.Parser)

Example 35 with NetworkMessage

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

the class TrvProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    String id = sentence.startsWith("TRV") ? sentence.substring(0, 3) : sentence.substring(0, 2);
    String type = sentence.substring(id.length(), id.length() + 4);
    if (channel != null) {
        String responseHeader = id + (char) (type.charAt(0) + 1) + type.substring(1);
        if (type.equals("AP00") && id.equals("IW")) {
            String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
            channel.writeAndFlush(new NetworkMessage(responseHeader + "," + time + ",0#", remoteAddress));
        } else if (type.equals("AP14")) {
            channel.writeAndFlush(new NetworkMessage(responseHeader + ",0.000,0.000#", remoteAddress));
        } else {
            channel.writeAndFlush(new NetworkMessage(responseHeader + "#", remoteAddress));
        }
    }
    if (type.equals("AP00")) {
        getDeviceSession(channel, remoteAddress, sentence.substring(id.length() + type.length()));
        return null;
    }
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
    if (deviceSession == null) {
        return null;
    }
    if (type.equals("CP01")) {
        Parser parser = new Parser(PATTERN_HEATRBEAT, sentence);
        if (!parser.matches()) {
            return null;
        }
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        getLastLocation(position, null);
        decodeCommon(position, parser);
        if (parser.hasNext(3)) {
            position.set(Position.KEY_BLOCKED, decodeOptionalValue(parser, 2));
            position.set(Position.KEY_CHARGE, decodeOptionalValue(parser, 1));
            position.set(Position.KEY_MOTION, decodeOptionalValue(parser, 1));
        }
        return position;
    } else if (type.equals("AP01") || type.equals("AP10") || type.equals("YP03") || type.equals("YP14")) {
        Parser parser = new Parser(PATTERN, sentence);
        if (!parser.matches()) {
            return null;
        }
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        DateBuilder dateBuilder = new DateBuilder().setDate(parser.nextInt(), parser.nextInt(), parser.nextInt());
        position.setValid(parser.next().equals("A"));
        position.setLatitude(parser.nextCoordinate());
        position.setLongitude(parser.nextCoordinate());
        position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble()));
        dateBuilder.setTime(parser.nextInt(), parser.nextInt(), parser.nextInt());
        position.setTime(dateBuilder.getDate());
        position.setCourse(parser.nextDouble());
        decodeCommon(position, parser);
        position.setNetwork(new Network(CellTower.from(parser.nextInt(), parser.nextInt(), parser.nextInt(), parser.nextInt())));
        return position;
    } else if (type.equals("AP02")) {
        Parser parser = new Parser(PATTERN_LBS, sentence);
        if (!parser.matches()) {
            return null;
        }
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        getLastLocation(position, null);
        int mcc = parser.nextInt();
        int mnc = parser.nextInt();
        Network network = new Network();
        for (String cell : parser.next().split(",")) {
            if (!cell.isEmpty()) {
                String[] values = cell.split("\\|");
                network.addCellTower(CellTower.from(mcc, mnc, Integer.parseInt(values[0]), Integer.parseInt(values[1]), Integer.parseInt(values[2])));
            }
        }
        for (String wifi : parser.next().split("&")) {
            if (!wifi.isEmpty()) {
                String[] values = wifi.split("\\|");
                network.addWifiAccessPoint(WifiAccessPoint.from(values[1], Integer.parseInt(values[2])));
            }
        }
        position.setNetwork(network);
        return position;
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) Network(org.traccar.model.Network) NetworkMessage(org.traccar.NetworkMessage) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) WifiAccessPoint(org.traccar.model.WifiAccessPoint) Parser(org.traccar.helper.Parser)

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