Search in sources :

Example 86 with NetworkMessage

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

the class EgtsProtocolDecoder method sendResponse.

private void sendResponse(Channel channel, int packetType, int index, int serviceType, int type, ByteBuf content) {
    if (channel != null) {
        ByteBuf data = Unpooled.buffer();
        data.writeByte(type);
        data.writeShortLE(content.readableBytes());
        data.writeBytes(content);
        content.release();
        ByteBuf record = Unpooled.buffer();
        if (packetType == PT_RESPONSE) {
            record.writeShortLE(index);
            // success
            record.writeByte(0);
        }
        record.writeShortLE(data.readableBytes());
        record.writeShortLE(0);
        // flags (possibly 1 << 6)
        record.writeByte(0);
        record.writeByte(serviceType);
        record.writeByte(serviceType);
        record.writeBytes(data);
        data.release();
        int recordChecksum = Checksum.crc16(Checksum.CRC16_CCITT_FALSE, record.nioBuffer());
        ByteBuf response = Unpooled.buffer();
        // protocol version
        response.writeByte(1);
        // security key id
        response.writeByte(0);
        // flags
        response.writeByte(0);
        // header length
        response.writeByte(5 + 2 + 2 + 2);
        // encoding
        response.writeByte(0);
        response.writeShortLE(record.readableBytes());
        response.writeShortLE(packetId++);
        response.writeByte(packetType);
        response.writeByte(Checksum.crc8(Checksum.CRC8_EGTS, response.nioBuffer()));
        response.writeBytes(record);
        record.release();
        response.writeShortLE(recordChecksum);
        channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Example 87 with NetworkMessage

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

the class EskyProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    Parser parser = new Parser(PATTERN, sentence);
    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.set(Position.KEY_SATELLITES, parser.nextInt());
    position.setValid(true);
    position.setTime(parser.nextDateTime());
    position.setLatitude(parser.nextDouble());
    position.setLongitude(parser.nextDouble());
    position.setSpeed(UnitsConverter.knotsFromMps(parser.nextDouble()));
    position.setCourse(parser.nextDouble());
    if (parser.hasNext(3)) {
        int input = parser.nextHexInt();
        position.set(Position.KEY_IGNITION, !BitUtil.check(input, 0));
        position.set(Position.PREFIX_IN + 1, !BitUtil.check(input, 1));
        position.set(Position.PREFIX_IN + 2, !BitUtil.check(input, 2));
        position.set(Position.KEY_EVENT, parser.nextInt());
        position.set(Position.KEY_ODOMETER, parser.nextInt());
    }
    position.set(Position.PREFIX_ADC + 1, parser.nextInt());
    position.set(Position.KEY_BATTERY, parser.nextInt() * 0.01);
    int index = sentence.lastIndexOf('+');
    if (index > 0 && channel instanceof DatagramChannel) {
        channel.writeAndFlush(new NetworkMessage("ACK," + sentence.substring(index + 1) + "#", remoteAddress));
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DatagramChannel(io.netty.channel.socket.DatagramChannel) NetworkMessage(org.traccar.NetworkMessage) Parser(org.traccar.helper.Parser)

Example 88 with NetworkMessage

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

the class FifotrackProtocolDecoder method requestPhoto.

private void requestPhoto(Channel channel, SocketAddress socketAddress, String imei, String file) {
    if (channel != null) {
        String content = "1,D06," + file + "," + photo.writerIndex() + "," + Math.min(1024, photo.writableBytes());
        int length = 1 + imei.length() + 1 + content.length();
        String response = String.format("##%02d,%s,%s*", length, imei, content);
        response += Checksum.sum(response) + "\r\n";
        channel.writeAndFlush(new NetworkMessage(response, socketAddress));
    }
}
Also used : NetworkMessage(org.traccar.NetworkMessage) WifiAccessPoint(org.traccar.model.WifiAccessPoint)

Example 89 with NetworkMessage

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

the class DwayProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    if (sentence.equals("AA55,HB")) {
        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage("55AA,HB,OK\r\n", remoteAddress));
        }
        return null;
    }
    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.setValid(true);
    position.setTime(parser.nextDateTime());
    position.setLatitude(parser.nextDouble());
    position.setLongitude(parser.nextDouble());
    position.setAltitude(parser.nextDouble(0));
    position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0)));
    position.setCourse(parser.nextDouble(0));
    position.set(Position.KEY_INPUT, parser.nextBinInt());
    position.set(Position.KEY_OUTPUT, parser.nextBinInt());
    position.set(Position.KEY_BATTERY, parser.nextInt() * 0.001);
    position.set(Position.PREFIX_ADC + 1, parser.nextInt() * 0.001);
    position.set(Position.PREFIX_ADC + 2, parser.nextInt() * 0.001);
    position.set(Position.KEY_DRIVER_UNIQUE_ID, parser.next());
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) NetworkMessage(org.traccar.NetworkMessage) Parser(org.traccar.helper.Parser)

Example 90 with NetworkMessage

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

the class EasyTrackProtocolDecoder method decodeCell.

private Position decodeCell(Channel channel, SocketAddress remoteAddress, String sentence) {
    Parser parser = new Parser(PATTERN_CELL, sentence);
    if (!parser.matches()) {
        return null;
    }
    String imei = parser.next();
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    getLastLocation(position, null);
    if (channel != null && parser.nextInt() > 0) {
        String response = String.format("*ET,%s,JZ,undefined#", imei);
        channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
    }
    int cid = parser.nextInt();
    int lac = parser.nextInt();
    int mcc = parser.nextInt();
    int mnc = parser.nextInt();
    position.setNetwork(new Network(CellTower.from(mcc, mnc, lac, cid)));
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Network(org.traccar.model.Network) NetworkMessage(org.traccar.NetworkMessage) 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