Search in sources :

Example 41 with DeviceSession

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

the class ThinkRaceProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    // header
    buf.skipBytes(2);
    ChannelBuffer id = buf.readBytes(12);
    // separator
    buf.readUnsignedByte();
    int type = buf.readUnsignedByte();
    // length
    buf.readUnsignedShort();
    if (type == MSG_LOGIN) {
        // 0x00 - heartbeat
        int command = buf.readUnsignedByte();
        if (command == 0x01) {
            String imei = buf.toString(buf.readerIndex(), 15, StandardCharsets.US_ASCII);
            DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
            if (deviceSession != null && channel != null) {
                ChannelBuffer response = ChannelBuffers.dynamicBuffer();
                // header
                response.writeByte(0x48);
                // header
                response.writeByte(0x52);
                response.writeBytes(id);
                // separator
                response.writeByte(0x2c);
                response.writeByte(type);
                // length
                response.writeShort(0x0002);
                response.writeShort(0x8000);
                // checksum
                response.writeShort(0x0000);
                channel.write(response);
            }
        }
    } else if (type == MSG_GPS) {
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
        if (deviceSession == null) {
            return null;
        }
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        position.setTime(new Date(buf.readUnsignedInt() * 1000));
        int flags = buf.readUnsignedByte();
        position.setValid(true);
        position.setLatitude(convertCoordinate(buf.readUnsignedInt(), !BitUtil.check(flags, 0)));
        position.setLongitude(convertCoordinate(buf.readUnsignedInt(), !BitUtil.check(flags, 1)));
        position.setSpeed(buf.readUnsignedByte());
        position.setCourse(buf.readUnsignedByte());
        position.setNetwork(new Network(CellTower.fromLacCid(buf.readUnsignedShort(), buf.readUnsignedShort())));
        return position;
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Network(org.traccar.model.Network) Date(java.util.Date) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 42 with DeviceSession

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

the class Tk102ProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    // header
    buf.skipBytes(1);
    int type = buf.readUnsignedByte();
    ChannelBuffer dataSequence = buf.readBytes(10);
    int length = buf.readUnsignedByte();
    if (type == MSG_LOGIN_REQUEST || type == MSG_LOGIN_REQUEST_2) {
        ChannelBuffer data = buf.readBytes(length);
        String id;
        if (type == MSG_LOGIN_REQUEST) {
            id = data.toString(StandardCharsets.US_ASCII);
        } else {
            id = data.copy(1, 15).toString(StandardCharsets.US_ASCII);
        }
        if (getDeviceSession(channel, remoteAddress, id) != null) {
            ChannelBuffer response = ChannelBuffers.dynamicBuffer();
            response.writeByte(MODE_GPRS);
            response.writeBytes(data);
            sendResponse(channel, MSG_LOGIN_RESPONSE, dataSequence, response);
        }
    } else if (type == MSG_HEARTBEAT_REQUEST) {
        sendResponse(channel, MSG_HEARTBEAT_RESPONSE, dataSequence, buf.readBytes(length));
    } else {
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
        if (deviceSession == null) {
            return null;
        }
        Parser parser = new Parser(PATTERN, buf.readBytes(length).toString(StandardCharsets.US_ASCII));
        if (!parser.matches()) {
            return null;
        }
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
        position.setValid(parser.next().equals("A"));
        position.setLatitude(parser.nextCoordinate());
        position.setLongitude(parser.nextCoordinate());
        position.setSpeed(parser.nextDouble(0));
        dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
        position.setTime(dateBuilder.getDate());
        return position;
    }
    return null;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Parser(org.traccar.helper.Parser)

Example 43 with DeviceSession

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

the class Tlt2hProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    sentence = sentence.trim();
    String header = sentence.substring(0, sentence.indexOf('\r'));
    Parser parser = new Parser(PATTERN_HEADER, header);
    if (!parser.matches()) {
        return null;
    }
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    String status = parser.next();
    String[] messages = sentence.substring(sentence.indexOf('\n') + 1).split("\r\n");
    List<Position> positions = new LinkedList<>();
    for (String message : messages) {
        parser = new Parser(PATTERN_POSITION, message);
        if (parser.matches()) {
            Position position = new Position(getProtocolName());
            position.setDeviceId(deviceSession.getDeviceId());
            // base station info
            parser.next();
            DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(0), parser.nextInt(0), 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(0), parser.nextInt(0), parser.nextInt(0));
            position.setTime(dateBuilder.getDate());
            decodeStatus(position, status);
            positions.add(position);
        }
    }
    return positions;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) LinkedList(java.util.LinkedList) Parser(org.traccar.helper.Parser)

Example 44 with DeviceSession

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

the class Tr900ProtocolDecoder 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());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());
    position.setValid(parser.nextInt(0) == 1);
    position.setTime(parser.nextDateTime());
    position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN));
    position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN));
    position.setSpeed(parser.nextDouble(0));
    position.setCourse(parser.nextDouble(0));
    position.set(Position.KEY_RSSI, parser.nextDouble());
    position.set(Position.KEY_EVENT, parser.nextInt(0));
    position.set(Position.PREFIX_ADC + 1, parser.nextInt(0));
    position.set(Position.KEY_BATTERY, parser.nextInt(0));
    position.set(Position.KEY_INPUT, parser.next());
    position.set(Position.KEY_STATUS, parser.next());
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Parser(org.traccar.helper.Parser)

Example 45 with DeviceSession

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

the class TrakMateProtocolDecoder method decodePer.

private Object decodePer(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    Parser parser = new Parser(PATTERN_PER, (String) msg);
    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());
    // seq
    parser.next();
    position.setLatitude(parser.nextDouble(0));
    position.setLongitude(parser.nextDouble(0));
    position.setTime(parser.nextDateTime(Parser.DateTimeFormat.HMS_DMY));
    position.setSpeed(parser.nextDouble(0));
    position.setCourse(parser.nextDouble(0));
    position.set(Position.KEY_IGNITION, parser.nextInt(0) == 1);
    position.set("dop1", parser.next());
    position.set("dop2", parser.next());
    position.set(Position.KEY_INPUT, parser.next());
    position.set(Position.KEY_BATTERY, parser.nextDouble(0));
    position.set(Position.KEY_POWER, parser.nextDouble());
    position.set(Position.KEY_ODOMETER, parser.nextDouble(0));
    position.set("pulseOdometer", parser.next());
    position.set(Position.KEY_STATUS, parser.nextInt(0));
    position.setValid(parser.nextInt(0) != 0);
    position.set(Position.KEY_ARCHIVE, parser.nextInt(0) == 1);
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Parser(org.traccar.helper.Parser)

Aggregations

DeviceSession (org.traccar.DeviceSession)197 Position (org.traccar.model.Position)188 Parser (org.traccar.helper.Parser)110 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)64 DateBuilder (org.traccar.helper.DateBuilder)63 Date (java.util.Date)37 Network (org.traccar.model.Network)37 LinkedList (java.util.LinkedList)30 SimpleDateFormat (java.text.SimpleDateFormat)10 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)10 List (java.util.List)9 WifiAccessPoint (org.traccar.model.WifiAccessPoint)9 DateFormat (java.text.DateFormat)8 Pattern (java.util.regex.Pattern)6 StringReader (java.io.StringReader)4 JsonObject (javax.json.JsonObject)4 QueryStringDecoder (org.jboss.netty.handler.codec.http.QueryStringDecoder)4 Matcher (java.util.regex.Matcher)3 Calendar (java.util.Calendar)2 HashSet (java.util.HashSet)2