Search in sources :

Example 71 with NetworkMessage

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

the class MeiligaoProtocolDecoder method sendResponse.

private static void sendResponse(Channel channel, SocketAddress remoteAddress, ByteBuf id, int type, ByteBuf msg) {
    if (channel != null) {
        ByteBuf buf = Unpooled.buffer(2 + 2 + id.readableBytes() + 2 + msg.readableBytes() + 2 + 2);
        buf.writeByte('@');
        buf.writeByte('@');
        buf.writeShort(buf.capacity());
        buf.writeBytes(id);
        buf.writeShort(type);
        buf.writeBytes(msg);
        msg.release();
        buf.writeShort(Checksum.crc16(Checksum.CRC16_CCITT_FALSE, buf.nioBuffer()));
        buf.writeByte('\r');
        buf.writeByte('\n');
        channel.writeAndFlush(new NetworkMessage(buf, remoteAddress));
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Example 72 with NetworkMessage

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

the class MeitrackProtocolDecoder method decodeBinaryC.

private List<Position> decodeBinaryC(Channel channel, SocketAddress remoteAddress, ByteBuf buf) {
    List<Position> positions = new LinkedList<>();
    String flag = buf.toString(2, 1, StandardCharsets.US_ASCII);
    int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ',');
    String imei = buf.toString(index + 1, 15, StandardCharsets.US_ASCII);
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }
    buf.skipBytes(index + 1 + 15 + 1 + 3 + 1 + 2 + 2 + 4);
    while (buf.readableBytes() >= 0x34) {
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        position.set(Position.KEY_EVENT, buf.readUnsignedByte());
        position.setLatitude(buf.readIntLE() * 0.000001);
        position.setLongitude(buf.readIntLE() * 0.000001);
        // 946684800 = 2000-01-01
        position.setTime(new Date((946684800 + buf.readUnsignedIntLE()) * 1000));
        position.setValid(buf.readUnsignedByte() == 1);
        position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
        int rssi = buf.readUnsignedByte();
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShortLE()));
        position.setCourse(buf.readUnsignedShortLE());
        position.set(Position.KEY_HDOP, buf.readUnsignedShortLE() * 0.1);
        position.setAltitude(buf.readUnsignedShortLE());
        position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE());
        position.set("runtime", buf.readUnsignedIntLE());
        position.setNetwork(new Network(CellTower.from(buf.readUnsignedShortLE(), buf.readUnsignedShortLE(), buf.readUnsignedShortLE(), buf.readUnsignedShortLE(), rssi)));
        position.set(Position.KEY_STATUS, buf.readUnsignedShortLE());
        position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShortLE());
        position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE() * 0.01);
        position.set(Position.KEY_POWER, buf.readUnsignedShortLE());
        // geo-fence
        buf.readUnsignedIntLE();
        positions.add(position);
    }
    if (channel != null) {
        StringBuilder command = new StringBuilder("@@");
        command.append(flag).append(27 + positions.size() / 10).append(",");
        command.append(imei).append(",CCC,").append(positions.size()).append("*");
        int checksum = 0;
        for (int i = 0; i < command.length(); i += 1) {
            checksum += command.charAt(i);
        }
        command.append(String.format("%02x", checksum & 0xff).toUpperCase());
        command.append("\r\n");
        // delete processed data
        channel.writeAndFlush(new NetworkMessage(command.toString(), remoteAddress));
    }
    return positions;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Network(org.traccar.model.Network) NetworkMessage(org.traccar.NetworkMessage) LinkedList(java.util.LinkedList) Date(java.util.Date)

Example 73 with NetworkMessage

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

the class Mta6ProtocolDecoder method sendResponse.

private void sendResponse(Channel channel, short packetId, short packetCount) {
    ByteBuf begin = Unpooled.copiedBuffer("#ACK#", StandardCharsets.US_ASCII);
    ByteBuf end = Unpooled.buffer(3);
    end.writeByte(packetId);
    end.writeByte(packetCount);
    end.writeByte(0);
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(begin, end));
    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) ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Example 74 with NetworkMessage

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

the class MtxProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    if (channel != null) {
        channel.writeAndFlush(new NetworkMessage("#ACK", remoteAddress));
    }
    Parser parser = new Parser(PATTERN, (String) msg);
    if (!parser.matches()) {
        return null;
    }
    Position position = new Position(getProtocolName());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());
    position.setTime(parser.nextDateTime());
    position.setValid(true);
    position.setLatitude(parser.nextDouble(0));
    position.setLongitude(parser.nextDouble(0));
    position.setSpeed(parser.nextDouble(0));
    position.setCourse(parser.nextDouble(0));
    position.set(Position.KEY_ODOMETER, parser.nextDouble(0) * 1000);
    position.set(Position.KEY_INPUT, parser.next());
    position.set(Position.KEY_OUTPUT, parser.next());
    position.set(Position.PREFIX_ADC + 1, parser.next());
    position.set(Position.PREFIX_ADC + 2, 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 75 with NetworkMessage

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

the class Jt600ProtocolDecoder method decodeU01.

private Position decodeU01(String sentence, Channel channel, SocketAddress remoteAddress) {
    Parser parser = new Parser(PATTERN_U01, sentence);
    if (!parser.matches()) {
        return null;
    }
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    String type = parser.next();
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS));
    position.setValid(parser.next().equals("T"));
    position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM));
    position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM));
    position.setSpeed(UnitsConverter.knotsFromMph(parser.nextDouble(0)));
    position.setCourse(parser.nextDouble(0));
    position.set(Position.KEY_SATELLITES, parser.nextInt(0));
    position.set(Position.KEY_BATTERY_LEVEL, parser.nextInt(0));
    position.set(Position.KEY_STATUS, parser.nextBinInt(0));
    CellTower cellTower = CellTower.fromCidLac(parser.nextInt(0), parser.nextInt(0));
    cellTower.setSignalStrength(parser.nextInt(0));
    position.setNetwork(new Network(cellTower));
    position.set(Position.KEY_ODOMETER, parser.nextLong(0) * 1000);
    position.set(Position.KEY_INDEX, parser.nextInt(0));
    if (channel != null) {
        if (type.equals("U01") || type.equals("U02") || type.equals("U03")) {
            channel.writeAndFlush(new NetworkMessage("(S39)", remoteAddress));
        } else if (type.equals("U06")) {
            channel.writeAndFlush(new NetworkMessage("(S20)", remoteAddress));
        }
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) CellTower(org.traccar.model.CellTower) 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