Search in sources :

Example 51 with NetworkMessage

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

the class YwtProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    // Synchronization
    if (sentence.startsWith("%SN") && channel != null) {
        int start = sentence.indexOf(':');
        int end = start;
        for (int i = 0; i < 4; i++) {
            end = sentence.indexOf(',', end + 1);
        }
        if (end == -1) {
            end = sentence.length();
        }
        channel.writeAndFlush(new NetworkMessage("%AT+SN=" + sentence.substring(start, end), remoteAddress));
        return null;
    }
    Parser parser = new Parser(PATTERN, sentence);
    if (!parser.matches()) {
        return null;
    }
    Position position = new Position(getProtocolName());
    String type = parser.next();
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());
    position.setTime(parser.nextDateTime());
    position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG));
    position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG));
    position.setAltitude(parser.nextDouble(0));
    position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble()));
    position.setCourse(parser.nextDouble());
    int satellites = parser.nextInt();
    position.setValid(satellites != 0);
    position.set(Position.KEY_SATELLITES, satellites);
    String reportId = parser.next();
    position.set(Position.KEY_STATUS, parser.next());
    // Send response
    if ((type.equals("KP") || type.equals("EP")) && channel != null) {
        channel.writeAndFlush(new NetworkMessage("%AT+" + type + "=" + reportId + "\r\n", remoteAddress));
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) NetworkMessage(org.traccar.NetworkMessage) Parser(org.traccar.helper.Parser)

Example 52 with NetworkMessage

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

the class UproProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ByteBuf buf = (ByteBuf) msg;
    if (buf.getByte(buf.readerIndex()) != '*') {
        return null;
    }
    int headerIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '&');
    if (headerIndex < 0) {
        headerIndex = buf.writerIndex();
    }
    String header = buf.readSlice(headerIndex - buf.readerIndex()).toString(StandardCharsets.US_ASCII);
    Parser parser = new Parser(PATTERN_HEADER, header);
    if (!parser.matches()) {
        return null;
    }
    String head = parser.next();
    boolean reply = parser.next().equals("1");
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    Network network = new Network();
    String type = parser.next();
    String subtype = parser.next();
    if (reply && channel != null) {
        channel.writeAndFlush(new NetworkMessage("*" + head + "Y" + type + subtype + "#", remoteAddress));
    }
    while (buf.readableBytes() > 1) {
        // skip delimiter
        buf.readByte();
        byte dataType = buf.readByte();
        int delimiterIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '&');
        if (delimiterIndex < 0) {
            delimiterIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '#');
            if (delimiterIndex < 0) {
                delimiterIndex = buf.writerIndex();
            }
        }
        ByteBuf data = buf.readSlice(delimiterIndex - buf.readerIndex());
        int mcc = 0, mnc = 0, count;
        String stringValue;
        switch(dataType) {
            case 'A':
                decodeLocation(position, data.toString(StandardCharsets.US_ASCII));
                break;
            case 'B':
                position.set(Position.KEY_STATUS, data.toString(StandardCharsets.US_ASCII));
                break;
            case 'C':
                long odometer = 0;
                while (data.isReadable()) {
                    odometer <<= 4;
                    odometer += data.readByte() - (byte) '0';
                }
                position.set(Position.KEY_ODOMETER, odometer * 2 * 1852 / 3600);
                break;
            case 'F':
                position.setSpeed(Integer.parseInt(data.readSlice(4).toString(StandardCharsets.US_ASCII)) * 0.1);
                break;
            case 'G':
                position.setAltitude(Integer.parseInt(data.readSlice(6).toString(StandardCharsets.US_ASCII)) * 0.1);
                break;
            case 'I':
                stringValue = data.toString(StandardCharsets.US_ASCII);
                count = Integer.parseInt(stringValue.substring(0, 1));
                if (stringValue.length() == 6 + count * 10) {
                    mcc = Integer.parseInt(stringValue.substring(1, 4));
                    mnc = Integer.parseInt(stringValue.substring(4, 6));
                    for (int i = 0; i < count; i++) {
                        int offset = 6 + i * 10;
                        network.addCellTower(CellTower.from(mcc, mnc, Integer.parseInt(stringValue.substring(offset, offset + 4), 16), Integer.parseInt(stringValue.substring(offset + 4, offset + 8), 16), Integer.parseInt(stringValue.substring(offset + 8, offset + 10))));
                    }
                }
                break;
            case 'J':
                if (data.readableBytes() == 6) {
                    char index = (char) data.readUnsignedByte();
                    int status = data.readUnsignedByte();
                    double value = Integer.parseInt(data.readSlice(4).toString(StandardCharsets.US_ASCII)) * 0.1;
                    if (BitUtil.check(status, 0)) {
                        value = -value;
                    }
                    position.set(Position.PREFIX_TEMP + index, value);
                }
                break;
            case 'K':
                position.set("statusExtended", data.toString(StandardCharsets.US_ASCII));
                break;
            case 'M':
                if (data.readableBytes() == 3) {
                    position.set(Position.KEY_BATTERY_LEVEL, Integer.parseInt(data.readSlice(3).toString(StandardCharsets.US_ASCII)) * 0.1);
                } else if (data.readableBytes() == 4) {
                    char index = (char) data.readUnsignedByte();
                    // status
                    data.readUnsignedByte();
                    position.set("humidity" + index, Integer.parseInt(data.readSlice(2).toString(StandardCharsets.US_ASCII)));
                }
                break;
            case 'N':
                position.set(Position.KEY_RSSI, Integer.parseInt(data.readSlice(2).toString(StandardCharsets.US_ASCII)));
                break;
            case 'O':
                position.set(Position.KEY_SATELLITES, Integer.parseInt(data.readSlice(2).toString(StandardCharsets.US_ASCII)));
                break;
            case 'P':
                if (data.readableBytes() >= 16) {
                    position.setNetwork(new Network(CellTower.from(Integer.parseInt(data.readSlice(4).toString(StandardCharsets.US_ASCII)), Integer.parseInt(data.readSlice(4).toString(StandardCharsets.US_ASCII)), Integer.parseInt(data.readSlice(4).toString(StandardCharsets.US_ASCII), 16), Integer.parseInt(data.readSlice(4).toString(StandardCharsets.US_ASCII), 16))));
                }
                break;
            case 'Q':
                if (!head.startsWith("HQ")) {
                    position.set("obdPid", ByteBufUtil.hexDump(data));
                }
                break;
            case 'R':
                if (head.startsWith("HQ")) {
                    position.set(Position.KEY_RSSI, Integer.parseInt(data.readSlice(2).toString(StandardCharsets.US_ASCII)));
                    position.set(Position.KEY_SATELLITES, Integer.parseInt(data.readSlice(2).toString(StandardCharsets.US_ASCII)));
                } else {
                    position.set("odbTravel", ByteBufUtil.hexDump(data));
                }
                break;
            case 'S':
                position.set("obdTraffic", ByteBufUtil.hexDump(data));
                break;
            case 'T':
                if (data.readableBytes() == 2) {
                    position.set(Position.KEY_BATTERY_LEVEL, Integer.parseInt(data.toString(StandardCharsets.US_ASCII)));
                }
                break;
            case 'V':
                position.set(Position.KEY_POWER, Integer.parseInt(data.readSlice(4).toString(StandardCharsets.US_ASCII)) * 0.1);
                break;
            case 'W':
                position.set(Position.KEY_ALARM, decodeAlarm(Integer.parseInt(data.readSlice(2).toString(StandardCharsets.US_ASCII))));
                break;
            case 'X':
                String[] cells = data.toString(StandardCharsets.US_ASCII).split(";");
                if (!cells[0].startsWith("(")) {
                    for (int i = 0; i < cells.length; i++) {
                        String[] values = cells[i].split(",");
                        int index = 0;
                        if (i == 0) {
                            mcc = Integer.parseInt(values[index++]);
                            mnc = Integer.parseInt(values[index++]);
                        }
                        network.addCellTower(CellTower.from(mcc, mnc, Integer.parseInt(values[index++]), Integer.parseInt(values[index++]), Integer.parseInt(values[index])));
                    }
                    position.setNetwork(network);
                }
                break;
            case 'Y':
                stringValue = data.toString(StandardCharsets.US_ASCII);
                count = Integer.parseInt(stringValue.substring(0, 1));
                if (stringValue.length() == 6 + count * 14) {
                    mcc = Integer.parseInt(stringValue.substring(1, 4));
                    mnc = Integer.parseInt(stringValue.substring(4, 6));
                    for (int i = 0; i < count; i++) {
                        int offset = 6 + i * 14;
                        network.addCellTower(CellTower.from(mcc, mnc, Integer.parseInt(stringValue.substring(offset, offset + 4), 16), Long.parseLong(stringValue.substring(offset + 4, offset + 12), 16), Integer.parseInt(stringValue.substring(offset + 12, offset + 14))));
                    }
                } else {
                    position.set(Position.KEY_POWER, Integer.parseInt(data.readSlice(5).toString(StandardCharsets.US_ASCII)) * 0.001);
                }
                break;
            case 'b':
                if (data.readableBytes() > 3) {
                    position.set("serial", data.toString(StandardCharsets.US_ASCII).substring(3));
                }
                break;
            default:
                break;
        }
    }
    if (network.getCellTowers() != null || network.getWifiAccessPoints() != null) {
        position.setNetwork(network);
    }
    if (position.getLatitude() == 0 || position.getLongitude() == 0) {
        if (position.getAttributes().isEmpty()) {
            return null;
        }
        getLastLocation(position, position.getDeviceTime());
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) ByteBuf(io.netty.buffer.ByteBuf) Parser(org.traccar.helper.Parser) Network(org.traccar.model.Network) NetworkMessage(org.traccar.NetworkMessage)

Example 53 with NetworkMessage

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

the class WialonProtocolDecoder method sendResponse.

private void sendResponse(Channel channel, SocketAddress remoteAddress, String type, Integer number) {
    if (channel != null) {
        StringBuilder response = new StringBuilder("#A");
        response.append(type);
        response.append("#");
        if (number != null) {
            response.append(number);
        }
        response.append("\r\n");
        channel.writeAndFlush(new NetworkMessage(response.toString(), remoteAddress));
    }
}
Also used : NetworkMessage(org.traccar.NetworkMessage)

Example 54 with NetworkMessage

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

the class XirgoProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    if (channel instanceof NioDatagramChannel) {
        Matcher matcher = Pattern.compile("\\$\\$\\d+,(\\d+),.*,(\\d+)##").matcher(sentence);
        if (matcher.matches()) {
            String response = "!UDP_ACK," + matcher.group(1) + "," + matcher.group(2);
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }
    }
    if (form != null) {
        return decodeCustom(channel, remoteAddress, sentence);
    } else {
        return decodeFixed(channel, remoteAddress, sentence);
    }
}
Also used : Matcher(java.util.regex.Matcher) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) NetworkMessage(org.traccar.NetworkMessage)

Example 55 with NetworkMessage

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

the class NetworkMessageHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    if (ctx.channel() instanceof DatagramChannel) {
        DatagramPacket packet = (DatagramPacket) msg;
        ctx.fireChannelRead(new NetworkMessage(packet.content(), packet.sender()));
    } else if (msg instanceof ByteBuf) {
        ByteBuf buffer = (ByteBuf) msg;
        ctx.fireChannelRead(new NetworkMessage(buffer, ctx.channel().remoteAddress()));
    }
}
Also used : DatagramPacket(io.netty.channel.socket.DatagramPacket) DatagramChannel(io.netty.channel.socket.DatagramChannel) NetworkMessage(org.traccar.NetworkMessage) ByteBuf(io.netty.buffer.ByteBuf)

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