Search in sources :

Example 91 with NetworkMessage

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

the class TotemProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    Pattern pattern = PATTERN3;
    if (sentence.contains("$Cloud")) {
        pattern = PATTERN_OBD;
    } else if (sentence.charAt(2) == '0') {
        pattern = PATTERN4;
    } else if (sentence.contains("$GPRMC")) {
        pattern = PATTERN1;
    } else {
        int index = sentence.indexOf('|');
        if (index != -1 && sentence.indexOf('|', index + 1) != -1) {
            pattern = PATTERN2;
        }
    }
    Parser parser = new Parser(pattern, sentence);
    if (!parser.matches()) {
        return null;
    }
    Position position = new Position(getProtocolName());
    if (pattern == PATTERN4) {
        position.set(Position.KEY_ALARM, decodeAlarm4(parser.nextHexInt()));
    }
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());
    boolean result;
    if (pattern == PATTERN1 || pattern == PATTERN2) {
        result = decode12(position, parser, pattern);
    } else if (pattern == PATTERN3) {
        result = decode3(position, parser);
    } else if (pattern == PATTERN4) {
        result = decode4(position, parser);
    } else {
        result = decodeObd(position, parser);
    }
    if (channel != null) {
        if (pattern == PATTERN4) {
            String response = "$$0014AA" + sentence.substring(sentence.length() - 6, sentence.length() - 2);
            response += String.format("%02X", Checksum.xor(response)).toUpperCase();
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        } else {
            channel.writeAndFlush(new NetworkMessage("ACK OK\r\n", remoteAddress));
        }
    }
    return result ? position : null;
}
Also used : Pattern(java.util.regex.Pattern) DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) NetworkMessage(org.traccar.NetworkMessage) Parser(org.traccar.helper.Parser)

Example 92 with NetworkMessage

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

the class Tr20ProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    Parser parser = new Parser(PATTERN_PING, (String) msg);
    if (parser.matches()) {
        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage("&&" + parser.next() + "\r\n", // keep-alive response
            remoteAddress));
        }
        return null;
    }
    parser = new Parser(PATTERN_DATA, (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.setValid(parser.next().equals("A"));
    position.setTime(parser.nextDateTime());
    position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN));
    position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN));
    position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble()));
    position.setCourse(parser.nextDouble());
    position.set(Position.PREFIX_TEMP + 1, parser.nextInt());
    position.set(Position.KEY_STATUS, parser.nextHexLong());
    position.set(Position.KEY_EVENT, parser.nextInt());
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) NetworkMessage(org.traccar.NetworkMessage) Parser(org.traccar.helper.Parser)

Example 93 with NetworkMessage

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

the class TramigoProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ByteBuf buf = (ByteBuf) msg;
    int protocol = buf.readUnsignedByte();
    boolean legacy = protocol == 0x80;
    // version id
    buf.readUnsignedByte();
    int index = legacy ? buf.readUnsignedShort() : buf.readUnsignedShortLE();
    int type = legacy ? buf.readUnsignedShort() : buf.readUnsignedShortLE();
    // length
    buf.readUnsignedShort();
    // mask
    buf.readUnsignedShort();
    // checksum
    buf.readUnsignedShort();
    long id = legacy ? buf.readUnsignedInt() : buf.readUnsignedIntLE();
    // time
    buf.readUnsignedInt();
    Position position = new Position(getProtocolName());
    position.set(Position.KEY_INDEX, index);
    position.setValid(true);
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(id));
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());
    if (protocol == 0x01 && (type == MSG_COMPACT || type == MSG_FULL)) {
        // need to send ack?
        // report trigger
        buf.readUnsignedShortLE();
        // state flag
        buf.readUnsignedShortLE();
        position.setLatitude(buf.readUnsignedIntLE() * 0.0000001);
        position.setLongitude(buf.readUnsignedIntLE() * 0.0000001);
        position.set(Position.KEY_RSSI, buf.readUnsignedShortLE());
        position.set(Position.KEY_SATELLITES, buf.readUnsignedShortLE());
        position.set(Position.KEY_SATELLITES_VISIBLE, buf.readUnsignedShortLE());
        position.set("gpsAntennaStatus", buf.readUnsignedShortLE());
        position.setSpeed(buf.readUnsignedShortLE() * 0.194384);
        position.setCourse(buf.readUnsignedShortLE());
        position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE());
        position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE());
        position.set(Position.KEY_CHARGE, buf.readUnsignedShortLE());
        position.setTime(new Date(buf.readUnsignedIntLE() * 1000));
        return position;
    } else if (legacy) {
        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage(Unpooled.copiedBuffer("gprs,ack," + index, StandardCharsets.US_ASCII), remoteAddress));
        }
        String sentence = buf.toString(StandardCharsets.US_ASCII);
        Pattern pattern = Pattern.compile("(-?\\d+\\.\\d+), (-?\\d+\\.\\d+)");
        Matcher matcher = pattern.matcher(sentence);
        if (!matcher.find()) {
            return null;
        }
        position.setLatitude(Double.parseDouble(matcher.group(1)));
        position.setLongitude(Double.parseDouble(matcher.group(2)));
        pattern = Pattern.compile("([NSWE]{1,2}) with speed (\\d+) km/h");
        matcher = pattern.matcher(sentence);
        if (matcher.find()) {
            for (int i = 0; i < DIRECTIONS.length; i++) {
                if (matcher.group(1).equals(DIRECTIONS[i])) {
                    position.setCourse(i * 45.0);
                    break;
                }
            }
            position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble(matcher.group(2))));
        }
        pattern = Pattern.compile("(\\d{1,2}:\\d{2}(:\\d{2})? \\w{3} \\d{1,2})");
        matcher = pattern.matcher(sentence);
        if (!matcher.find()) {
            return null;
        }
        DateFormat dateFormat = new SimpleDateFormat(matcher.group(2) != null ? "HH:mm:ss MMM d yyyy" : "HH:mm MMM d yyyy", Locale.ENGLISH);
        position.setTime(DateUtil.correctYear(dateFormat.parse(matcher.group(1) + " " + Calendar.getInstance().get(Calendar.YEAR))));
        if (sentence.contains("Ignition on detected")) {
            position.set(Position.KEY_IGNITION, true);
        } else if (sentence.contains("Ignition off detected")) {
            position.set(Position.KEY_IGNITION, false);
        }
        return position;
    }
    return null;
}
Also used : Pattern(java.util.regex.Pattern) DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Matcher(java.util.regex.Matcher) ByteBuf(io.netty.buffer.ByteBuf) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) NetworkMessage(org.traccar.NetworkMessage) SimpleDateFormat(java.text.SimpleDateFormat)

Example 94 with NetworkMessage

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

the class TzoneProtocolDecoder method sendResponse.

private void sendResponse(Channel channel, SocketAddress remoteAddress, int index) {
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    String ack = String.format("@ACK,%d#", index);
    String time = String.format("@UTC time:%s", dateFormat.format(new Date()));
    ByteBuf response = Unpooled.copiedBuffer(ack + time, StandardCharsets.US_ASCII);
    if (channel != null) {
        channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
    }
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 95 with NetworkMessage

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

the class TeltonikaProtocolDecoder method parseIdentification.

private void parseIdentification(Channel channel, SocketAddress remoteAddress, ByteBuf buf) {
    int length = buf.readUnsignedShort();
    String imei = buf.toString(buf.readerIndex(), length, StandardCharsets.US_ASCII);
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (channel != null) {
        ByteBuf response = Unpooled.buffer(1);
        if (deviceSession != null) {
            response.writeByte(1);
        } else {
            response.writeByte(0);
        }
        channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
    }
}
Also used : DeviceSession(org.traccar.DeviceSession) 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