Search in sources :

Example 11 with NetworkMessage

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

the class FlexCommProtocolDecoder 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;
    }
    Position position = new Position(getProtocolName());
    position.set(Position.KEY_STATUS, parser.nextInt());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());
    position.setTime(parser.nextDateTime());
    position.setValid(parser.next().equals("1"));
    position.setLatitude(parseSignedValue(parser, 6));
    position.setLongitude(parseSignedValue(parser, 6));
    position.setAltitude(parseSignedValue(parser, 0));
    position.setSpeed(UnitsConverter.knotsFromKph(parser.nextInt()));
    position.setCourse(parser.nextDouble(0));
    position.set(Position.KEY_SATELLITES_VISIBLE, parser.nextInt());
    position.set(Position.KEY_SATELLITES, parser.nextInt());
    position.set(Position.KEY_RSSI, parser.nextInt());
    position.setNetwork(new Network(CellTower.from(parser.nextInt(), parser.nextInt(), parser.nextHexInt(), parser.nextHexInt())));
    for (int i = 1; i <= 3; i++) {
        position.set(Position.PREFIX_IN + i, parser.nextInt());
    }
    for (int i = 1; i <= 2; i++) {
        position.set(Position.PREFIX_OUT + i, parser.nextInt());
    }
    position.set(Position.KEY_FUEL_LEVEL, parser.nextInt());
    position.set(Position.PREFIX_TEMP + 1, parseSignedValue(parser, 0));
    position.set(Position.KEY_BATTERY_LEVEL, parser.nextInt());
    position.set(Position.KEY_POWER, parser.nextInt() * 0.1);
    if (channel != null) {
        channel.writeAndFlush(new NetworkMessage("{01}", remoteAddress));
    }
    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 12 with NetworkMessage

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

the class FlexibleReportProtocolDecoder method sendResponse.

private void sendResponse(Channel channel, SocketAddress remoteAddress, int index) {
    if (channel != null) {
        ByteBuf response = Unpooled.buffer();
        // header
        response.writeByte(0x7E);
        // length
        response.writeShort(2);
        response.writeByte(0xE0);
        response.writeByte(BitUtil.check(index, 0) ? 0x4F : 0x0F);
        channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Example 13 with NetworkMessage

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

the class GlobalSatProtocolDecoder method decodeOriginal.

private Position decodeOriginal(Channel channel, SocketAddress remoteAddress, String sentence) {
    if (channel != null) {
        channel.writeAndFlush(new NetworkMessage("ACK\r", remoteAddress));
    }
    String format;
    if (sentence.startsWith("GSr") || sentence.startsWith("GSb")) {
        format = format0;
    } else if (sentence.startsWith("GSh")) {
        format = format1;
    } else {
        return null;
    }
    // Check that message contains required parameters
    if (!format.contains("B") || !format.contains("S") || !(format.contains("1") || format.contains("2") || format.contains("3")) || !(format.contains("6") || format.contains("7") || format.contains("8"))) {
        return null;
    }
    if (format.contains("*")) {
        format = format.substring(0, format.indexOf('*'));
        sentence = sentence.substring(0, sentence.indexOf('*'));
    }
    String[] values = sentence.split(",");
    Position position = new Position(getProtocolName());
    CellTower cellTower = new CellTower();
    for (int formatIndex = 0, valueIndex = 1; formatIndex < format.length() && valueIndex < values.length; formatIndex++) {
        String value = values[valueIndex].replace("\"", "");
        switch(format.charAt(formatIndex)) {
            case 'S':
                DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, value);
                if (deviceSession == null) {
                    return null;
                }
                position.setDeviceId(deviceSession.getDeviceId());
                break;
            case 'A':
                if (value.isEmpty()) {
                    position.setValid(false);
                } else {
                    position.setValid(Integer.parseInt(value) != 1);
                }
                break;
            case 'B':
                DateBuilder dateBuilder = new DateBuilder().setDay(Integer.parseInt(value.substring(0, 2))).setMonth(Integer.parseInt(value.substring(2, 4))).setYear(Integer.parseInt(value.substring(4)));
                value = values[++valueIndex];
                dateBuilder.setHour(Integer.parseInt(value.substring(0, 2))).setMinute(Integer.parseInt(value.substring(2, 4))).setSecond(Integer.parseInt(value.substring(4)));
                position.setTime(dateBuilder.getDate());
                break;
            case 'C':
                valueIndex += 1;
                break;
            case '1':
                double longitude = Double.parseDouble(value.substring(1));
                if (value.charAt(0) == 'W') {
                    longitude = -longitude;
                }
                position.setLongitude(longitude);
                break;
            case '2':
                longitude = Double.parseDouble(value.substring(4)) / 60;
                longitude += Integer.parseInt(value.substring(1, 4));
                if (value.charAt(0) == 'W') {
                    longitude = -longitude;
                }
                position.setLongitude(longitude);
                break;
            case '3':
                position.setLongitude(Double.parseDouble(value) * 0.000001);
                break;
            case '6':
                double latitude = Double.parseDouble(value.substring(1));
                if (value.charAt(0) == 'S') {
                    latitude = -latitude;
                }
                position.setLatitude(latitude);
                break;
            case '7':
                latitude = Double.parseDouble(value.substring(3)) / 60;
                latitude += Integer.parseInt(value.substring(1, 3));
                if (value.charAt(0) == 'S') {
                    latitude = -latitude;
                }
                position.setLatitude(latitude);
                break;
            case '8':
                position.setLatitude(Double.parseDouble(value) * 0.000001);
                break;
            case 'G':
                position.setAltitude(Double.parseDouble(value));
                break;
            case 'H':
                position.setSpeed(Double.parseDouble(value));
                break;
            case 'I':
                position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble(value)));
                break;
            case 'J':
                position.setSpeed(UnitsConverter.knotsFromMph(Double.parseDouble(value)));
                break;
            case 'K':
                position.setCourse(Double.parseDouble(value));
                break;
            case 'L':
                position.set(Position.KEY_SATELLITES, Integer.parseInt(value));
                break;
            case 'P':
                if (value.length() == 4) {
                    position.set(Position.KEY_ALARM, decodeAlarm(Integer.parseInt(value, 16)));
                }
                break;
            case 'Z':
                if (!value.isEmpty()) {
                    position.set("geofence", value);
                }
                break;
            case 'Y':
                int io = Integer.parseInt(value, 16);
                position.set(Position.PREFIX_IN + 1, BitUtil.check(io, 1));
                position.set(Position.KEY_MOTION, BitUtil.check(io, 7));
                position.set(Position.PREFIX_OUT + 1, BitUtil.check(io, 9));
                position.set(Position.KEY_IGNITION, BitUtil.check(io, 13));
                position.set(Position.KEY_CHARGE, BitUtil.check(io, 15));
                break;
            case 'c':
                cellTower.setSignalStrength(Integer.parseInt(value));
                break;
            case 'm':
                position.set(Position.KEY_POWER, decodeVoltage(value));
                break;
            case 'n':
            case 'N':
                position.set(Position.KEY_BATTERY, decodeVoltage(value));
                break;
            case 't':
                cellTower.setMobileCountryCode(Integer.parseInt(value));
                break;
            case 'u':
                cellTower.setMobileNetworkCode(Integer.parseInt(value));
                break;
            case 'v':
                cellTower.setLocationAreaCode(Integer.parseInt(value, 16));
                break;
            case 'w':
                cellTower.setCellId(Long.parseLong(value, 16));
                break;
            default:
                // Unsupported
                break;
        }
        valueIndex += 1;
    }
    if (cellTower.getCellId() != null) {
        position.setNetwork(new Network(cellTower));
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) CellTower(org.traccar.model.CellTower) Network(org.traccar.model.Network) NetworkMessage(org.traccar.NetworkMessage)

Example 14 with NetworkMessage

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

the class GatorProtocolDecoder method sendResponse.

private void sendResponse(Channel channel, SocketAddress remoteAddress, int type, int checksum) {
    if (channel != null) {
        ByteBuf response = Unpooled.buffer();
        // header
        response.writeShort(0x2424);
        response.writeByte(MSG_HEARTBEAT);
        // length
        response.writeShort(5);
        response.writeByte(checksum);
        response.writeByte(type);
        // subtype
        response.writeByte(0);
        response.writeByte(Checksum.xor(response.nioBuffer(2, response.writerIndex())));
        response.writeByte(0x0D);
        channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) NetworkMessage(org.traccar.NetworkMessage)

Example 15 with NetworkMessage

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

the class Gl100ProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    if (sentence.contains("AT+GTHBD=") && channel != null) {
        String response = "+RESP:GTHBD,GPRS ACTIVE,";
        response += sentence.substring(9, sentence.lastIndexOf(','));
        response += '\0';
        // heartbeat response
        channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
    }
    Parser parser = new Parser(PATTERN, sentence);
    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.nextInt(0) == 0);
    position.setSpeed(parser.nextDouble(0));
    position.setCourse(parser.nextDouble(0));
    position.setAltitude(parser.nextDouble(0));
    position.setLongitude(parser.nextDouble(0));
    position.setLatitude(parser.nextDouble(0));
    position.setTime(parser.nextDateTime());
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) NetworkMessage(org.traccar.NetworkMessage) Parser(org.traccar.helper.Parser)

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