Search in sources :

Example 16 with NetworkMessage

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

the class H02ProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ByteBuf buf = (ByteBuf) msg;
    String marker = buf.toString(0, 1, StandardCharsets.US_ASCII);
    switch(marker) {
        case "*":
            String sentence = buf.toString(StandardCharsets.US_ASCII).trim();
            int typeStart = sentence.indexOf(',', sentence.indexOf(',') + 1) + 1;
            int typeEnd = sentence.indexOf(',', typeStart);
            if (typeEnd < 0) {
                typeEnd = sentence.indexOf('#', typeStart);
            }
            if (typeEnd > 0) {
                String type = sentence.substring(typeStart, typeEnd);
                switch(type) {
                    case "V0":
                    case "HTBT":
                        if (channel != null) {
                            String response = sentence.substring(0, typeEnd) + "#";
                            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
                        }
                        return decodeHeartbeat(sentence, channel, remoteAddress);
                    case "NBR":
                        return decodeLbs(sentence, channel, remoteAddress);
                    case "LINK":
                        return decodeLink(sentence, channel, remoteAddress);
                    case "V3":
                        return decodeV3(sentence, channel, remoteAddress);
                    case "VP1":
                        return decodeVp1(sentence, channel, remoteAddress);
                    default:
                        return decodeText(sentence, channel, remoteAddress);
                }
            } else {
                return null;
            }
        case "$":
            return decodeBinary(buf, channel, remoteAddress);
        case "X":
        default:
            return null;
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Example 17 with NetworkMessage

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

the class HuabaoProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ByteBuf buf = (ByteBuf) msg;
    if (buf.getByte(buf.readerIndex()) == '(') {
        String sentence = buf.toString(StandardCharsets.US_ASCII);
        if (sentence.contains("BASE,2")) {
            DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
            String response = sentence.replace("TIME", dateFormat.format(new Date()));
            if (channel != null) {
                channel.writeAndFlush(new NetworkMessage(Unpooled.copiedBuffer(response, StandardCharsets.US_ASCII), remoteAddress));
            }
            return null;
        } else {
            return decodeResult(channel, remoteAddress, sentence);
        }
    }
    // start marker
    buf.readUnsignedByte();
    int type = buf.readUnsignedShort();
    int attribute = buf.readUnsignedShort();
    // phone number
    ByteBuf id = buf.readSlice(6);
    int index;
    if (type == MSG_LOCATION_REPORT_2 || type == MSG_LOCATION_REPORT_BLIND) {
        index = buf.readUnsignedByte();
    } else {
        index = buf.readUnsignedShort();
    }
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, ByteBufUtil.hexDump(id));
    if (deviceSession == null) {
        return null;
    }
    if (deviceSession.getTimeZone() == null) {
        deviceSession.setTimeZone(getTimeZone(deviceSession.getDeviceId(), "GMT+8"));
    }
    if (type == MSG_TERMINAL_REGISTER) {
        if (channel != null) {
            ByteBuf response = Unpooled.buffer();
            response.writeShort(index);
            response.writeByte(RESULT_SUCCESS);
            response.writeBytes(ByteBufUtil.hexDump(id).getBytes(StandardCharsets.US_ASCII));
            channel.writeAndFlush(new NetworkMessage(formatMessage(MSG_TERMINAL_REGISTER_RESPONSE, id, false, response), remoteAddress));
        }
    } else if (type == MSG_TERMINAL_AUTH || type == MSG_HEARTBEAT || type == MSG_PHOTO) {
        sendGeneralResponse(channel, remoteAddress, id, type, index);
    } else if (type == MSG_LOCATION_REPORT) {
        sendGeneralResponse(channel, remoteAddress, id, type, index);
        return decodeLocation(deviceSession, buf);
    } else if (type == MSG_LOCATION_REPORT_2 || type == MSG_LOCATION_REPORT_BLIND) {
        if (BitUtil.check(attribute, 15)) {
            sendGeneralResponse2(channel, remoteAddress, id, type);
        }
        return decodeLocation2(deviceSession, buf, type);
    } else if (type == MSG_LOCATION_BATCH) {
        sendGeneralResponse(channel, remoteAddress, id, type, index);
        return decodeLocationBatch(deviceSession, buf);
    } else if (type == MSG_TIME_SYNC_REQUEST) {
        if (channel != null) {
            Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
            ByteBuf response = Unpooled.buffer();
            response.writeShort(calendar.get(Calendar.YEAR));
            response.writeByte(calendar.get(Calendar.MONTH) + 1);
            response.writeByte(calendar.get(Calendar.DAY_OF_MONTH));
            response.writeByte(calendar.get(Calendar.HOUR_OF_DAY));
            response.writeByte(calendar.get(Calendar.MINUTE));
            response.writeByte(calendar.get(Calendar.SECOND));
            channel.writeAndFlush(new NetworkMessage(formatMessage(MSG_TERMINAL_REGISTER_RESPONSE, id, false, response), remoteAddress));
        }
    } else if (type == MSG_ACCELERATION) {
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        getLastLocation(position, null);
        StringBuilder data = new StringBuilder("[");
        while (buf.readableBytes() > 2) {
            // time
            buf.skipBytes(6);
            if (data.length() > 1) {
                data.append(",");
            }
            data.append("[");
            data.append(readSignedWord(buf));
            data.append(",");
            data.append(readSignedWord(buf));
            data.append(",");
            data.append(readSignedWord(buf));
            data.append("]");
        }
        data.append("]");
        position.set(Position.KEY_G_SENSOR, data.toString());
        return position;
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Calendar(java.util.Calendar) ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 18 with NetworkMessage

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

the class HuabaoProtocolDecoder method sendGeneralResponse2.

private void sendGeneralResponse2(Channel channel, SocketAddress remoteAddress, ByteBuf id, int type) {
    if (channel != null) {
        ByteBuf response = Unpooled.buffer();
        response.writeShort(type);
        response.writeByte(RESULT_SUCCESS);
        channel.writeAndFlush(new NetworkMessage(formatMessage(MSG_GENERAL_RESPONSE_2, id, true, response), remoteAddress));
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Example 19 with NetworkMessage

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

the class GranitProtocolDecoder method sendResponseCurrent.

private static void sendResponseCurrent(Channel channel, int deviceId, long time) {
    ByteBuf response = Unpooled.buffer();
    response.writeBytes("BB+UGRC~".getBytes(StandardCharsets.US_ASCII));
    // length
    response.writeShortLE(6);
    response.writeInt((int) time);
    response.writeShortLE(deviceId);
    appendChecksum(response, 16);
    channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Example 20 with NetworkMessage

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

the class Gs100ProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ByteBuf buf = (ByteBuf) msg;
    String header = buf.readCharSequence(2, StandardCharsets.US_ASCII).toString();
    if (header.equals("GL")) {
        buf.skipBytes(1);
        String imei = buf.readCharSequence(buf.readUnsignedByte(), StandardCharsets.US_ASCII).toString();
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
        if (channel != null && deviceSession != null) {
            ByteBuf response = Unpooled.copiedBuffer("GS100", StandardCharsets.US_ASCII);
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }
        return null;
    } else {
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
        if (deviceSession == null) {
            return null;
        }
        List<Position> positions = new LinkedList<>();
        int count = buf.readUnsignedByte();
        for (int i = 0; i < count; i++) {
            int endIndex = buf.readUnsignedByte() + buf.readerIndex();
            Position position = new Position(getProtocolName());
            position.setDeviceId(deviceSession.getDeviceId());
            int status = buf.readUnsignedMedium();
            position.set(Position.KEY_STATUS, status);
            if (BitUtil.check(status, 8 + 8 + 7)) {
                DateBuilder dateBuilder = new DateBuilder().setHour(BcdUtil.readInteger(buf, 2)).setMinute(BcdUtil.readInteger(buf, 2)).setSecond(BcdUtil.readInteger(buf, 2)).setDay(BcdUtil.readInteger(buf, 2)).setMonth(BcdUtil.readInteger(buf, 2)).setYear(BcdUtil.readInteger(buf, 2));
                position.setTime(dateBuilder.getDate());
                position.setValid(true);
                String coordinates = ByteBufUtil.hexDump(buf.readSlice(9));
                position.setLongitude(Integer.parseInt(coordinates.substring(0, 3)) + Integer.parseInt(coordinates.substring(3, 9)) * 0.0001 / 60);
                position.setLatitude(Integer.parseInt(coordinates.substring(10, 12)) + Integer.parseInt(coordinates.substring(12, 18)) * 0.0001 / 60);
                int flags = Integer.parseInt(coordinates.substring(9, 10), 16);
                if (!BitUtil.check(flags, 3)) {
                    position.setLongitude(-position.getLongitude());
                }
                if (!BitUtil.check(flags, 2)) {
                    position.setLatitude(-position.getLatitude());
                }
                String other = ByteBufUtil.hexDump(buf.readSlice(4));
                position.setSpeed(UnitsConverter.knotsFromKph(Integer.parseInt(other.substring(0, 5)) * 0.01));
                position.setCourse(Integer.parseInt(other.substring(5, 8)));
            } else {
                getLastLocation(position, null);
            }
            positions.add(position);
            buf.readerIndex(endIndex);
        }
        if (channel != null) {
            ByteBuf response = Unpooled.buffer();
            response.writeCharSequence("GS100", StandardCharsets.US_ASCII);
            response.writeByte(count);
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }
        return positions.isEmpty() ? null : positions;
    }
}
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) LinkedList(java.util.LinkedList)

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