Search in sources :

Example 66 with DeviceSession

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

the class HuaShengProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    // start marker
    buf.skipBytes(1);
    // flag
    buf.readUnsignedByte();
    // reserved
    buf.readUnsignedByte();
    // length
    buf.readUnsignedShort();
    int type = buf.readUnsignedShort();
    // checksum
    buf.readUnsignedShort();
    int index = buf.readInt();
    if (type == MSG_LOGIN) {
        while (buf.readableBytes() > 4) {
            int subtype = buf.readUnsignedShort();
            int length = buf.readUnsignedShort() - 4;
            if (subtype == 0x0003) {
                String imei = buf.readBytes(length).toString(StandardCharsets.US_ASCII);
                DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
                if (deviceSession != null && channel != null) {
                    ChannelBuffer content = ChannelBuffers.dynamicBuffer();
                    // success
                    content.writeByte(0);
                    sendResponse(channel, MSG_LOGIN_RSP, index, content);
                }
            } else {
                buf.skipBytes(length);
            }
        }
    } else if (type == MSG_HSO_REQ) {
        sendResponse(channel, MSG_HSO_RSP, index, null);
    } else if (type == MSG_POSITION) {
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
        if (deviceSession == null) {
            return null;
        }
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        int status = buf.readUnsignedShort();
        position.setValid(BitUtil.check(status, 15));
        position.set(Position.KEY_STATUS, status);
        position.set(Position.KEY_IGNITION, BitUtil.check(status, 14));
        position.set(Position.KEY_EVENT, buf.readUnsignedShort());
        String time = buf.readBytes(12).toString(StandardCharsets.US_ASCII);
        DateBuilder dateBuilder = new DateBuilder().setYear(Integer.parseInt(time.substring(0, 2))).setMonth(Integer.parseInt(time.substring(2, 4))).setDay(Integer.parseInt(time.substring(4, 6))).setHour(Integer.parseInt(time.substring(6, 8))).setMinute(Integer.parseInt(time.substring(8, 10))).setSecond(Integer.parseInt(time.substring(10, 12)));
        position.setTime(dateBuilder.getDate());
        position.setLongitude(buf.readInt() * 0.00001);
        position.setLatitude(buf.readInt() * 0.00001);
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort()));
        position.setCourse(buf.readUnsignedShort());
        position.setAltitude(buf.readUnsignedShort());
        position.set(Position.KEY_ODOMETER, buf.readUnsignedShort() * 1000);
        while (buf.readableBytes() > 4) {
            // subtype
            buf.readUnsignedShort();
            int length = buf.readUnsignedShort() - 4;
            buf.skipBytes(length);
        }
        sendResponse(channel, MSG_POSITION_RSP, index, null);
        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)

Example 67 with DeviceSession

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

the class Gt02ProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    // header
    buf.skipBytes(2);
    // size
    buf.readByte();
    Position position = new Position(getProtocolName());
    // Zero for location messages
    int power = buf.readUnsignedByte();
    int gsm = buf.readUnsignedByte();
    String imei = ChannelBuffers.hexDump(buf.readBytes(8)).substring(1);
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());
    position.set(Position.KEY_INDEX, buf.readUnsignedShort());
    int type = buf.readUnsignedByte();
    if (type == MSG_HEARTBEAT) {
        getLastLocation(position, null);
        position.set(Position.KEY_POWER, power);
        position.set(Position.KEY_RSSI, gsm);
        if (channel != null) {
            byte[] response = { 0x54, 0x68, 0x1A, 0x0D, 0x0A };
            channel.write(ChannelBuffers.wrappedBuffer(response));
        }
    } else if (type == MSG_DATA) {
        DateBuilder dateBuilder = new DateBuilder().setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()).setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
        position.setTime(dateBuilder.getDate());
        double latitude = buf.readUnsignedInt() / (60.0 * 30000.0);
        double longitude = buf.readUnsignedInt() / (60.0 * 30000.0);
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
        position.setCourse(buf.readUnsignedShort());
        // reserved
        buf.skipBytes(3);
        long flags = buf.readUnsignedInt();
        position.setValid(BitUtil.check(flags, 0));
        if (!BitUtil.check(flags, 1)) {
            latitude = -latitude;
        }
        if (!BitUtil.check(flags, 2)) {
            longitude = -longitude;
        }
        position.setLatitude(latitude);
        position.setLongitude(longitude);
    } else if (type == MSG_RESPONSE) {
        getLastLocation(position, null);
        position.set(Position.KEY_RESULT, buf.readBytes(buf.readUnsignedByte()).toString(StandardCharsets.US_ASCII));
    } else {
        return null;
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 68 with DeviceSession

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

the class HuabaoProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    // start marker
    buf.readUnsignedByte();
    int type = buf.readUnsignedShort();
    // body length
    buf.readUnsignedShort();
    // phone number
    ChannelBuffer id = buf.readBytes(6);
    int index = buf.readUnsignedShort();
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, ChannelBuffers.hexDump(id));
    if (deviceSession == null) {
        return null;
    }
    if (type == MSG_TERMINAL_REGISTER) {
        if (channel != null) {
            ChannelBuffer response = ChannelBuffers.dynamicBuffer();
            response.writeShort(index);
            response.writeByte(RESULT_SUCCESS);
            response.writeBytes("authentication".getBytes(StandardCharsets.US_ASCII));
            channel.write(formatMessage(MSG_TERMINAL_REGISTER_RESPONSE, id, response), remoteAddress);
        }
    } else if (type == MSG_TERMINAL_AUTH) {
        sendGeneralResponse(channel, remoteAddress, id, type, index);
    } else if (type == MSG_LOCATION_REPORT) {
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        position.set(Position.KEY_ALARM, decodeAlarm(buf.readUnsignedInt()));
        int flags = buf.readInt();
        position.set(Position.KEY_IGNITION, BitUtil.check(flags, 0));
        position.setValid(BitUtil.check(flags, 1));
        double lat = buf.readUnsignedInt() * 0.000001;
        double lon = buf.readUnsignedInt() * 0.000001;
        if (BitUtil.check(flags, 2)) {
            position.setLatitude(-lat);
        } else {
            position.setLatitude(lat);
        }
        if (BitUtil.check(flags, 3)) {
            position.setLongitude(-lon);
        } else {
            position.setLongitude(lon);
        }
        position.setAltitude(buf.readShort());
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort() * 0.1));
        position.setCourse(buf.readUnsignedShort());
        DateBuilder dateBuilder = new DateBuilder(TimeZone.getTimeZone("GMT+8")).setYear(BcdUtil.readInteger(buf, 2)).setMonth(BcdUtil.readInteger(buf, 2)).setDay(BcdUtil.readInteger(buf, 2)).setHour(BcdUtil.readInteger(buf, 2)).setMinute(BcdUtil.readInteger(buf, 2)).setSecond(BcdUtil.readInteger(buf, 2));
        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)

Example 69 with DeviceSession

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

the class Jt600ProtocolDecoder method decodeBinary.

private List<Position> decodeBinary(ChannelBuffer buf, Channel channel, SocketAddress remoteAddress) {
    List<Position> positions = new LinkedList<>();
    // header
    buf.readByte();
    boolean longFormat = buf.getUnsignedByte(buf.readerIndex()) == 0x75;
    String id = String.valueOf(Long.parseLong(ChannelBuffers.hexDump(buf.readBytes(5))));
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
    if (deviceSession == null) {
        return null;
    }
    int protocolVersion = 0;
    if (longFormat) {
        protocolVersion = buf.readUnsignedByte();
    }
    int version = BitUtil.from(buf.readUnsignedByte(), 4);
    // length
    buf.readUnsignedShort();
    while (buf.readableBytes() > 1) {
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        DateBuilder dateBuilder = new DateBuilder().setDay(BcdUtil.readInteger(buf, 2)).setMonth(BcdUtil.readInteger(buf, 2)).setYear(BcdUtil.readInteger(buf, 2)).setHour(BcdUtil.readInteger(buf, 2)).setMinute(BcdUtil.readInteger(buf, 2)).setSecond(BcdUtil.readInteger(buf, 2));
        position.setTime(dateBuilder.getDate());
        double latitude = convertCoordinate(BcdUtil.readInteger(buf, 8));
        double longitude = convertCoordinate(BcdUtil.readInteger(buf, 9));
        byte flags = buf.readByte();
        position.setValid((flags & 0x1) == 0x1);
        if ((flags & 0x2) == 0) {
            latitude = -latitude;
        }
        position.setLatitude(latitude);
        if ((flags & 0x4) == 0) {
            longitude = -longitude;
        }
        position.setLongitude(longitude);
        position.setSpeed(BcdUtil.readInteger(buf, 2));
        position.setCourse(buf.readUnsignedByte() * 2.0);
        if (longFormat) {
            position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 1000);
            position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
            // vehicle id combined
            buf.readUnsignedInt();
            position.set(Position.KEY_STATUS, buf.readUnsignedShort());
            int battery = buf.readUnsignedByte();
            if (battery == 0xff) {
                position.set(Position.KEY_CHARGE, true);
            } else {
                position.set(Position.KEY_BATTERY_LEVEL, battery);
            }
            CellTower cellTower = CellTower.fromCidLac(buf.readUnsignedShort(), buf.readUnsignedShort());
            cellTower.setSignalStrength((int) buf.readUnsignedByte());
            position.setNetwork(new Network(cellTower));
            if (protocolVersion == 0x17) {
                // geofence id
                buf.readUnsignedByte();
                // reserved
                buf.skipBytes(3);
            }
        } else if (version == 1) {
            position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
            position.set(Position.KEY_POWER, buf.readUnsignedByte());
            // other flags and sensors
            buf.readByte();
            position.setAltitude(buf.readUnsignedShort());
            int cid = buf.readUnsignedShort();
            int lac = buf.readUnsignedShort();
            int rssi = buf.readUnsignedByte();
            if (cid != 0 && lac != 0) {
                CellTower cellTower = CellTower.fromCidLac(cid, lac);
                cellTower.setSignalStrength(rssi);
                position.setNetwork(new Network(cellTower));
            } else {
                position.set(Position.KEY_RSSI, rssi);
            }
        } else if (version == 2) {
            int fuel = buf.readUnsignedByte() << 8;
            position.set(Position.KEY_STATUS, buf.readUnsignedInt());
            position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 1000);
            fuel += buf.readUnsignedByte();
            position.set(Position.KEY_FUEL_LEVEL, fuel);
        } else if (version == 3) {
            BitBuffer bitBuffer = new BitBuffer(buf);
            position.set("fuel1", bitBuffer.readUnsigned(12));
            position.set("fuel2", bitBuffer.readUnsigned(12));
            position.set("fuel3", bitBuffer.readUnsigned(12));
            position.set(Position.KEY_ODOMETER, bitBuffer.readUnsigned(20) * 1000);
            int status = bitBuffer.readUnsigned(24);
            position.set(Position.KEY_IGNITION, BitUtil.check(status, 0));
            position.set(Position.KEY_STATUS, status);
        }
        positions.add(position);
    }
    // index
    buf.readUnsignedByte();
    return positions;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) CellTower(org.traccar.model.CellTower) LinkedList(java.util.LinkedList) Network(org.traccar.model.Network) BitBuffer(org.traccar.helper.BitBuffer)

Example 70 with DeviceSession

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

the class HunterProProtocolDecoder 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());
    DateBuilder dateBuilder = new DateBuilder();
    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());
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) 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