Search in sources :

Example 36 with NetworkMessage

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

the class TytanProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ByteBuf buf = (ByteBuf) msg;
    // protocol
    buf.readUnsignedByte();
    // length
    buf.readUnsignedShort();
    int index = buf.readUnsignedByte() >> 3;
    if (channel != null) {
        ByteBuf response = Unpooled.copiedBuffer("^" + index, StandardCharsets.US_ASCII);
        channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
    }
    String id = String.valueOf(buf.readUnsignedInt());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
    if (deviceSession == null) {
        return null;
    }
    List<Position> positions = new LinkedList<>();
    while (buf.readableBytes() > 2) {
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        int end = buf.readerIndex() + buf.readUnsignedByte();
        position.setTime(new Date(buf.readUnsignedInt() * 1000));
        int flags = buf.readUnsignedByte();
        position.set(Position.KEY_SATELLITES, BitUtil.from(flags, 2));
        position.setValid(BitUtil.to(flags, 2) > 0);
        // Latitude
        double lat = buf.readUnsignedMedium();
        lat = lat * -180 / 16777216 + 90;
        position.setLatitude(lat);
        // Longitude
        double lon = buf.readUnsignedMedium();
        lon = lon * 360 / 16777216 - 180;
        position.setLongitude(lon);
        // Status
        flags = buf.readUnsignedByte();
        position.set(Position.KEY_IGNITION, BitUtil.check(flags, 0));
        position.set(Position.KEY_RSSI, BitUtil.between(flags, 2, 5));
        position.setCourse((BitUtil.from(flags, 5) * 45 + 180) % 360);
        // Speed
        int speed = buf.readUnsignedByte();
        if (speed < 250) {
            position.setSpeed(UnitsConverter.knotsFromKph(speed));
        }
        decodeExtraData(position, buf, end);
        positions.add(position);
    }
    return positions;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage) LinkedList(java.util.LinkedList) Date(java.util.Date)

Example 37 with NetworkMessage

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

the class TaipProtocolDecoder method decodeAttributes.

private Position decodeAttributes(Channel channel, SocketAddress remoteAddress, Position position, String[] attributes) {
    String uniqueId = null;
    DeviceSession deviceSession = null;
    String messageIndex = null;
    if (attributes != null) {
        for (String attribute : attributes) {
            int index = attribute.indexOf('=');
            if (index != -1) {
                String key = attribute.substring(0, index).toLowerCase();
                String value = attribute.substring(index + 1);
                switch(key) {
                    case "id":
                        uniqueId = value;
                        deviceSession = getDeviceSession(channel, remoteAddress, value);
                        if (deviceSession != null) {
                            position.setDeviceId(deviceSession.getDeviceId());
                        }
                        break;
                    case "io":
                        position.set(Position.KEY_IGNITION, BitUtil.check(value.charAt(0) - '0', 0));
                        position.set(Position.KEY_CHARGE, BitUtil.check(value.charAt(0) - '0', 1));
                        position.set(Position.KEY_OUTPUT, value.charAt(1) - '0');
                        position.set(Position.KEY_INPUT, value.charAt(2) - '0');
                        break;
                    case "ix":
                        position.set(Position.PREFIX_IO + 1, value);
                        break;
                    case "ad":
                        position.set(Position.PREFIX_ADC + 1, Integer.parseInt(value));
                        break;
                    case "sv":
                        position.set(Position.KEY_SATELLITES, Integer.parseInt(value));
                        break;
                    case "bl":
                        position.set(Position.KEY_BATTERY, Integer.parseInt(value) * 0.001);
                        break;
                    case "vo":
                        position.set(Position.KEY_ODOMETER, Long.parseLong(value));
                        break;
                    default:
                        position.set(key, value);
                        break;
                }
            } else if (attribute.startsWith("#")) {
                messageIndex = attribute;
            }
        }
    }
    if (deviceSession != null) {
        if (channel != null) {
            if (messageIndex != null) {
                String response;
                if (messageIndex.startsWith("#IP")) {
                    response = ">SAK;ID=" + uniqueId + ";" + messageIndex + "<";
                } else {
                    response = ">ACK;ID=" + uniqueId + ";" + messageIndex + ";*";
                    response += String.format("%02X", Checksum.xor(response)) + "<";
                }
                channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
            } else {
                channel.writeAndFlush(new NetworkMessage(uniqueId, remoteAddress));
            }
        }
        return position;
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) NetworkMessage(org.traccar.NetworkMessage)

Example 38 with NetworkMessage

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

the class ThinkPowerProtocolDecoder method sendResponse.

private void sendResponse(Channel channel, int type, int index, ByteBuf content) {
    if (channel != null) {
        ByteBuf response = Unpooled.buffer();
        response.writeByte(type);
        response.writeByte(index);
        if (content != null) {
            response.writeShort(content.readableBytes());
            response.writeBytes(content);
            content.release();
        } else {
            response.writeShort(0);
        }
        response.writeShort(Checksum.crc16(Checksum.CRC16_CCITT_FALSE, response.nioBuffer()));
        channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Example 39 with NetworkMessage

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

the class ThinkRaceProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ByteBuf buf = (ByteBuf) msg;
    // header
    buf.skipBytes(2);
    ByteBuf id = buf.readSlice(12);
    // separator
    buf.readUnsignedByte();
    int type = buf.readUnsignedByte();
    // length
    buf.readUnsignedShort();
    if (type == MSG_LOGIN) {
        // 0x00 - heartbeat
        int command = buf.readUnsignedByte();
        if (command == 0x01) {
            String imei = buf.toString(buf.readerIndex(), 15, StandardCharsets.US_ASCII);
            DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
            if (deviceSession != null && channel != null) {
                ByteBuf response = Unpooled.buffer();
                // header
                response.writeByte(0x48);
                // header
                response.writeByte(0x52);
                response.writeBytes(id);
                // separator
                response.writeByte(0x2c);
                response.writeByte(type);
                // length
                response.writeShort(0x0002);
                response.writeShort(0x8000);
                // checksum
                response.writeShort(0x0000);
                channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
            }
        }
    } else if (type == MSG_GPS) {
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
        if (deviceSession == null) {
            return null;
        }
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        position.setTime(new Date(buf.readUnsignedInt() * 1000));
        int flags = buf.readUnsignedByte();
        position.setValid(true);
        position.setLatitude(convertCoordinate(buf.readUnsignedInt(), !BitUtil.check(flags, 0)));
        position.setLongitude(convertCoordinate(buf.readUnsignedInt(), !BitUtil.check(flags, 1)));
        position.setSpeed(buf.readUnsignedByte());
        position.setCourse(buf.readUnsignedByte());
        position.setNetwork(new Network(CellTower.fromLacCid(buf.readUnsignedShort(), buf.readUnsignedShort())));
        return position;
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Network(org.traccar.model.Network) ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage) Date(java.util.Date)

Example 40 with NetworkMessage

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

the class TopinProtocolDecoder method sendResponse.

private void sendResponse(Channel channel, int length, int type, ByteBuf content) {
    if (channel != null) {
        ByteBuf response = Unpooled.buffer();
        response.writeShort(0x7878);
        response.writeByte(length);
        response.writeByte(type);
        response.writeBytes(content);
        response.writeByte('\r');
        response.writeByte('\n');
        content.release();
        channel.writeAndFlush(new NetworkMessage(response, channel.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