Search in sources :

Example 1 with NetworkMessage

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

the class L100ProtocolDecoder method decodeNormal.

private Object decodeNormal(Channel channel, SocketAddress remoteAddress, String sentence) {
    Parser parser = new Parser(PATTERN, sentence);
    if (!parser.matches()) {
        return null;
    }
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(), parser.nextInt(), parser.nextInt(), parser.nextInt(0));
    position.setValid(parser.next().equals("A"));
    position.setLatitude(parser.nextCoordinate());
    position.setLongitude(parser.nextCoordinate());
    position.setSpeed(parser.nextDouble(0));
    position.setCourse(parser.nextDouble(0));
    dateBuilder.setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt());
    position.setTime(dateBuilder.getDate());
    position.set(Position.KEY_STATUS, parser.next());
    position.set(Position.PREFIX_ADC + 1, parser.next());
    position.set(Position.KEY_ODOMETER, parser.nextDouble());
    position.set(Position.PREFIX_TEMP + 1, parser.nextDouble());
    position.set(Position.KEY_BATTERY, parser.nextDouble());
    int rssi = parser.nextInt();
    if (rssi > 0) {
        position.setNetwork(new Network(CellTower.from(parser.nextInt(), parser.nextInt(), parser.nextHexInt(), parser.nextHexInt(), rssi)));
    }
    if (channel != null) {
        channel.writeAndFlush(new NetworkMessage(String.valueOf((char) 0x01), remoteAddress));
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) Network(org.traccar.model.Network) NetworkMessage(org.traccar.NetworkMessage) Parser(org.traccar.helper.Parser)

Example 2 with NetworkMessage

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

the class L100ProtocolDecoder method decodeObdLocation.

private Object decodeObdLocation(Channel channel, SocketAddress remoteAddress, String sentence) {
    Parser parser = new Parser(PATTERN_OBD_LOCATION, sentence);
    if (!parser.matches()) {
        return null;
    }
    String imei = parser.next();
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }
    int type = parser.nextInt();
    int index = parser.nextInt();
    if (type == 1) {
        if (channel != null) {
            String response = "@" + imei + ",00," + index + ",";
            response += "*" + (char) Checksum.xor(response);
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    position.setTime(parser.nextDateTime(Parser.DateTimeFormat.HMS_DMY));
    position.setValid(parser.next().equals("A"));
    position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM));
    position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM));
    position.setSpeed(parser.nextInt());
    position.setCourse(parser.nextInt());
    position.set(Position.KEY_ODOMETER, parser.nextDouble() * 1000);
    position.set(Position.KEY_BATTERY, parser.nextDouble());
    int rssi = parser.nextInt();
    position.setNetwork(new Network(CellTower.from(parser.nextInt(), parser.nextInt(), parser.nextInt(), parser.nextHexInt(), rssi)));
    position.set(Position.KEY_IGNITION, parser.nextInt() == 1);
    // reserved
    parser.next();
    switch(parser.nextInt()) {
        case 0:
            position.set(Position.KEY_ALARM, Position.ALARM_BRAKING);
            break;
        case 2:
            position.set(Position.KEY_ALARM, Position.ALARM_ACCELERATION);
            break;
        case 1:
            position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
            break;
        default:
            break;
    }
    position.set(Position.KEY_CHARGE, parser.nextInt() == 1);
    if (parser.nextInt() == 1) {
        position.set(Position.KEY_ALARM, Position.ALARM_OVERSPEED);
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Network(org.traccar.model.Network) NetworkMessage(org.traccar.NetworkMessage) Parser(org.traccar.helper.Parser)

Example 3 with NetworkMessage

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

the class MilesmateProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    Parser parser = new Parser(PATTERN, (String) msg);
    if (!parser.matches()) {
        return null;
    }
    if (channel != null) {
        channel.writeAndFlush(new NetworkMessage("+##Received OK\n", remoteAddress));
    }
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    position.set(Position.KEY_BATTERY, parser.nextDouble());
    position.set(Position.PREFIX_ADC + 1, parser.nextDouble());
    DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(), parser.nextInt(), parser.nextInt());
    position.setLatitude(parser.nextCoordinate());
    position.setLongitude(parser.nextCoordinate());
    position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble()));
    dateBuilder.setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt());
    position.setTime(dateBuilder.getDate());
    String flags = parser.next();
    position.set(Position.KEY_IGNITION, flags.charAt(0) == '1');
    position.set(Position.KEY_ALARM, flags.charAt(1) == '1' ? Position.ALARM_SOS : null);
    position.set(Position.KEY_CHARGE, flags.charAt(5) == '1');
    position.set(Position.KEY_ALARM, flags.charAt(7) == '1' ? Position.ALARM_OVERSPEED : null);
    flags = parser.next();
    position.set(Position.KEY_BLOCKED, flags.charAt(0) == '1');
    position.set(Position.KEY_ALARM, flags.charAt(1) == '1' ? Position.ALARM_TOW : null);
    position.setValid(parser.next().equals("A"));
    position.setCourse(parser.nextDouble());
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) NetworkMessage(org.traccar.NetworkMessage) Parser(org.traccar.helper.Parser)

Example 4 with NetworkMessage

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

the class IotmProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    if (msg instanceof MqttConnectMessage) {
        MqttConnectMessage message = (MqttConnectMessage) msg;
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, message.payload().clientIdentifier());
        MqttConnectReturnCode returnCode = deviceSession != null ? MqttConnectReturnCode.CONNECTION_ACCEPTED : MqttConnectReturnCode.CONNECTION_REFUSED_IDENTIFIER_REJECTED;
        MqttMessage response = MqttMessageBuilders.connAck().returnCode(returnCode).build();
        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }
    } else if (msg instanceof MqttSubscribeMessage) {
        MqttSubscribeMessage message = (MqttSubscribeMessage) msg;
        MqttMessage response = MqttMessageBuilders.subAck().packetId((short) message.variableHeader().messageId()).build();
        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }
    } else if (msg instanceof MqttPublishMessage) {
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
        if (deviceSession == null) {
            return null;
        }
        List<Position> positions = new LinkedList<>();
        MqttPublishMessage message = (MqttPublishMessage) msg;
        ByteBuf buf = message.payload();
        // structure version
        buf.readUnsignedByte();
        while (buf.readableBytes() > 1) {
            int type = buf.readUnsignedByte();
            int length = buf.readUnsignedShortLE();
            ByteBuf record = buf.readSlice(length);
            if (type == 1) {
                Position position = new Position(getProtocolName());
                position.setDeviceId(deviceSession.getDeviceId());
                position.setTime(new Date(record.readUnsignedIntLE() * 1000));
                while (record.readableBytes() > 0) {
                    int sensorType = record.readUnsignedByte();
                    int sensorId = record.readUnsignedShortLE();
                    if (sensorType == 14) {
                        position.setValid(true);
                        position.setLatitude(record.readFloatLE());
                        position.setLongitude(record.readFloatLE());
                        position.setSpeed(UnitsConverter.knotsFromKph(record.readUnsignedShortLE()));
                        position.set(Position.KEY_HDOP, record.readUnsignedByte());
                        position.set(Position.KEY_SATELLITES, record.readUnsignedByte());
                        position.setCourse(record.readUnsignedShortLE());
                        position.setAltitude(record.readShortLE());
                    } else {
                        if (sensorType == 3) {
                            continue;
                        }
                        decodeSensor(position, record, sensorType, sensorId);
                    }
                }
                positions.add(position);
            } else if (type == 3) {
                Position position = new Position(getProtocolName());
                position.setDeviceId(deviceSession.getDeviceId());
                getLastLocation(position, new Date(record.readUnsignedIntLE() * 1000));
                // function identifier
                record.readUnsignedByte();
                position.set(Position.KEY_EVENT, record.readUnsignedByte());
                positions.add(position);
            }
        }
        // checksum
        buf.readUnsignedByte();
        MqttMessage response = MqttMessageBuilders.pubAck().packetId((short) message.variableHeader().packetId()).build();
        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }
        return positions.isEmpty() ? null : positions;
    }
    return null;
}
Also used : MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) MqttSubscribeMessage(io.netty.handler.codec.mqtt.MqttSubscribeMessage) DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) ByteBuf(io.netty.buffer.ByteBuf) LinkedList(java.util.LinkedList) Date(java.util.Date) MqttConnectReturnCode(io.netty.handler.codec.mqtt.MqttConnectReturnCode) MqttConnectMessage(io.netty.handler.codec.mqtt.MqttConnectMessage) MqttPublishMessage(io.netty.handler.codec.mqtt.MqttPublishMessage) NetworkMessage(org.traccar.NetworkMessage)

Example 5 with NetworkMessage

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

the class MeitrackProtocolDecoder method requestPhotoPacket.

private void requestPhotoPacket(Channel channel, SocketAddress socketAddress, String imei, String file, int index) {
    if (channel != null) {
        String content = "D00," + file + "," + index;
        int length = 1 + imei.length() + 1 + content.length() + 5;
        String response = String.format("@@O%02d,%s,%s*", length, imei, content);
        response += Checksum.sum(response) + "\r\n";
        channel.writeAndFlush(new NetworkMessage(response, socketAddress));
    }
}
Also used : 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