Search in sources :

Example 1 with CellTower

use of org.traccar.model.CellTower in project traccar by tananaev.

the class AutoFonProtocolDecoder method decodePosition.

private Position decodePosition(DeviceSession deviceSession, ChannelBuffer buf, boolean history) {
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    if (!history) {
        // interval
        buf.readUnsignedByte();
        // settings
        buf.skipBytes(8);
    }
    position.set(Position.KEY_STATUS, buf.readUnsignedByte());
    if (!history) {
        buf.readUnsignedShort();
    }
    position.set(Position.KEY_BATTERY, buf.readUnsignedByte());
    // time
    buf.skipBytes(6);
    if (!history) {
        for (int i = 0; i < 2; i++) {
            // time
            buf.skipBytes(5);
            // interval
            buf.readUnsignedShort();
            // mode
            buf.skipBytes(5);
        }
    }
    position.set(Position.PREFIX_TEMP + 1, buf.readByte());
    int rssi = buf.readUnsignedByte();
    CellTower cellTower = CellTower.from(buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort(), rssi);
    position.setNetwork(new Network(cellTower));
    int valid = buf.readUnsignedByte();
    position.setValid((valid & 0xc0) != 0);
    position.set(Position.KEY_SATELLITES, valid & 0x3f);
    DateBuilder dateBuilder = new DateBuilder().setDateReverse(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()).setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
    position.setTime(dateBuilder.getDate());
    position.setLatitude(convertCoordinate(buf.readInt()));
    position.setLongitude(convertCoordinate(buf.readInt()));
    position.setAltitude(buf.readShort());
    position.setSpeed(buf.readUnsignedByte());
    position.setCourse(buf.readUnsignedByte() * 2.0);
    position.set(Position.KEY_HDOP, buf.readUnsignedShort());
    // reserved
    buf.readUnsignedShort();
    // checksum
    buf.readUnsignedByte();
    return position;
}
Also used : Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) CellTower(org.traccar.model.CellTower) Network(org.traccar.model.Network)

Example 2 with CellTower

use of org.traccar.model.CellTower in project traccar by tananaev.

the class TeltonikaProtocolDecoder method decodeLocation.

private void decodeLocation(Position position, ChannelBuffer buf, int codec) {
    int globalMask = 0x0f;
    if (codec == CODEC_GH3000) {
        long time = buf.readUnsignedInt() & 0x3fffffff;
        // 2007-01-01 00:00:00
        time += 1167609600;
        globalMask = buf.readUnsignedByte();
        if (BitUtil.check(globalMask, 0)) {
            position.setTime(new Date(time * 1000));
            int locationMask = buf.readUnsignedByte();
            if (BitUtil.check(locationMask, 0)) {
                position.setLatitude(buf.readFloat());
                position.setLongitude(buf.readFloat());
            }
            if (BitUtil.check(locationMask, 1)) {
                position.setAltitude(buf.readUnsignedShort());
            }
            if (BitUtil.check(locationMask, 2)) {
                position.setCourse(buf.readUnsignedByte() * 360.0 / 256);
            }
            if (BitUtil.check(locationMask, 3)) {
                position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
            }
            if (BitUtil.check(locationMask, 4)) {
                int satellites = buf.readUnsignedByte();
                position.set(Position.KEY_SATELLITES, satellites);
                position.setValid(satellites >= 3);
            }
            if (BitUtil.check(locationMask, 5)) {
                CellTower cellTower = CellTower.fromLacCid(buf.readUnsignedShort(), buf.readUnsignedShort());
                if (BitUtil.check(locationMask, 6)) {
                    cellTower.setSignalStrength((int) buf.readUnsignedByte());
                }
                if (BitUtil.check(locationMask, 7)) {
                    cellTower.setOperator(buf.readUnsignedInt());
                }
                position.setNetwork(new Network(cellTower));
            } else {
                if (BitUtil.check(locationMask, 6)) {
                    position.set(Position.KEY_RSSI, buf.readUnsignedByte());
                }
                if (BitUtil.check(locationMask, 7)) {
                    position.set(Position.KEY_OPERATOR, buf.readUnsignedInt());
                }
            }
        } else {
            getLastLocation(position, new Date(time * 1000));
        }
    } else {
        position.setTime(new Date(buf.readLong()));
        position.set("priority", buf.readUnsignedByte());
        position.setLongitude(buf.readInt() / 10000000.0);
        position.setLatitude(buf.readInt() / 10000000.0);
        position.setAltitude(buf.readShort());
        position.setCourse(buf.readUnsignedShort());
        int satellites = buf.readUnsignedByte();
        position.set(Position.KEY_SATELLITES, satellites);
        position.setValid(satellites != 0);
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort()));
        position.set(Position.KEY_EVENT, buf.readUnsignedByte());
        // total IO data records
        buf.readUnsignedByte();
    }
    // Read 1 byte data
    if (BitUtil.check(globalMask, 1)) {
        int cnt = buf.readUnsignedByte();
        for (int j = 0; j < cnt; j++) {
            decodeParameter(position, buf.readUnsignedByte(), buf, 1, codec);
        }
    }
    // Read 2 byte data
    if (BitUtil.check(globalMask, 2)) {
        int cnt = buf.readUnsignedByte();
        for (int j = 0; j < cnt; j++) {
            decodeParameter(position, buf.readUnsignedByte(), buf, 2, codec);
        }
    }
    // Read 4 byte data
    if (BitUtil.check(globalMask, 3)) {
        int cnt = buf.readUnsignedByte();
        for (int j = 0; j < cnt; j++) {
            decodeParameter(position, buf.readUnsignedByte(), buf, 4, codec);
        }
    }
    // Read 8 byte data
    if (codec == CODEC_FM4X00 || codec == CODEC_16) {
        int cnt = buf.readUnsignedByte();
        for (int j = 0; j < cnt; j++) {
            decodeOtherParameter(position, buf.readUnsignedByte(), buf, 8);
        }
    }
    // Read 16 byte data
    if (extended) {
        int cnt = buf.readUnsignedByte();
        for (int j = 0; j < cnt; j++) {
            position.set(Position.PREFIX_IO + buf.readUnsignedByte(), ChannelBuffers.hexDump(buf.readBytes(16)));
        }
    }
    decodeNetwork(position);
}
Also used : CellTower(org.traccar.model.CellTower) Network(org.traccar.model.Network) Date(java.util.Date)

Example 3 with CellTower

use of org.traccar.model.CellTower 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 4 with CellTower

use of org.traccar.model.CellTower in project traccar by traccar.

the class ProtocolTest method verifyDecodedPosition.

private void verifyDecodedPosition(Object decodedObject, boolean checkLocation, boolean checkAttributes, Position expected) {
    assertNotNull("position is null", decodedObject);
    assertTrue("not a position", decodedObject instanceof Position);
    Position position = (Position) decodedObject;
    if (checkLocation) {
        if (expected != null) {
            if (expected.getFixTime() != null) {
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
                dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
                assertEquals("time", dateFormat.format(expected.getFixTime()), dateFormat.format(position.getFixTime()));
            }
            assertEquals("valid", expected.getValid(), position.getValid());
            assertEquals("latitude", expected.getLatitude(), position.getLatitude(), 0.00001);
            assertEquals("longitude", expected.getLongitude(), position.getLongitude(), 0.00001);
        } else {
            assertNotNull(position.getFixTime());
            assertTrue("year > 1999", position.getFixTime().after(new Date(915148800000L)));
            assertTrue("time < +25 hours", position.getFixTime().getTime() < System.currentTimeMillis() + 25 * 3600000);
            assertTrue("latitude >= -90", position.getLatitude() >= -90);
            assertTrue("latitude <= 90", position.getLatitude() <= 90);
            assertTrue("longitude >= -180", position.getLongitude() >= -180);
            assertTrue("longitude <= 180", position.getLongitude() <= 180);
        }
        assertTrue("altitude >= -12262", position.getAltitude() >= -12262);
        assertTrue("altitude <= 18000", position.getAltitude() <= 18000);
        assertTrue("speed >= 0", position.getSpeed() >= 0);
        assertTrue("speed <= 869", position.getSpeed() <= 869);
        assertTrue("course >= 0", position.getCourse() >= 0);
        assertTrue("course <= 360", position.getCourse() <= 360);
        assertNotNull("protocol is null", position.getProtocol());
    }
    Map<String, Object> attributes = position.getAttributes();
    if (checkAttributes) {
        assertFalse("no attributes", attributes.isEmpty());
    }
    if (attributes.containsKey(Position.KEY_INDEX)) {
        assertTrue(attributes.get(Position.KEY_INDEX) instanceof Number);
    }
    if (attributes.containsKey(Position.KEY_HDOP)) {
        assertTrue(attributes.get(Position.KEY_HDOP) instanceof Number);
    }
    if (attributes.containsKey(Position.KEY_VDOP)) {
        assertTrue(attributes.get(Position.KEY_VDOP) instanceof Number);
    }
    if (attributes.containsKey(Position.KEY_PDOP)) {
        assertTrue(attributes.get(Position.KEY_PDOP) instanceof Number);
    }
    if (attributes.containsKey(Position.KEY_SATELLITES)) {
        assertTrue(attributes.get(Position.KEY_SATELLITES) instanceof Number);
    }
    if (attributes.containsKey(Position.KEY_SATELLITES_VISIBLE)) {
        assertTrue(attributes.get(Position.KEY_SATELLITES_VISIBLE) instanceof Number);
    }
    if (attributes.containsKey(Position.KEY_RSSI)) {
        assertTrue(attributes.get(Position.KEY_RSSI) instanceof Number);
    }
    if (attributes.containsKey(Position.KEY_ODOMETER)) {
        assertTrue(attributes.get(Position.KEY_ODOMETER) instanceof Number);
    }
    if (attributes.containsKey(Position.KEY_RPM)) {
        assertTrue(attributes.get(Position.KEY_RPM) instanceof Number);
    }
    if (attributes.containsKey(Position.KEY_FUEL_LEVEL)) {
        assertTrue(attributes.get(Position.KEY_FUEL_LEVEL) instanceof Number);
    }
    if (attributes.containsKey(Position.KEY_POWER)) {
        assertTrue(attributes.get(Position.KEY_POWER) instanceof Number);
    }
    if (attributes.containsKey(Position.KEY_BATTERY)) {
        assertTrue(attributes.get(Position.KEY_BATTERY) instanceof Number);
    }
    if (attributes.containsKey(Position.KEY_BATTERY_LEVEL)) {
        int batteryLevel = ((Number) attributes.get(Position.KEY_BATTERY_LEVEL)).intValue();
        assertTrue(batteryLevel <= 100 && batteryLevel >= 0);
    }
    if (attributes.containsKey(Position.KEY_CHARGE)) {
        assertTrue(attributes.get(Position.KEY_CHARGE) instanceof Boolean);
    }
    if (attributes.containsKey(Position.KEY_IGNITION)) {
        assertTrue(attributes.get(Position.KEY_IGNITION) instanceof Boolean);
    }
    if (attributes.containsKey(Position.KEY_MOTION)) {
        assertTrue(attributes.get(Position.KEY_MOTION) instanceof Boolean);
    }
    if (attributes.containsKey(Position.KEY_ARCHIVE)) {
        assertTrue(attributes.get(Position.KEY_ARCHIVE) instanceof Boolean);
    }
    if (attributes.containsKey(Position.KEY_DRIVER_UNIQUE_ID)) {
        assertTrue(attributes.get(Position.KEY_DRIVER_UNIQUE_ID) instanceof String);
    }
    if (attributes.containsKey(Position.KEY_STEPS)) {
        assertTrue(attributes.get(Position.KEY_STEPS) instanceof Number);
    }
    if (attributes.containsKey(Position.KEY_ROAMING)) {
        assertTrue(attributes.get(Position.KEY_ROAMING) instanceof Boolean);
    }
    if (position.getNetwork() != null && position.getNetwork().getCellTowers() != null) {
        for (CellTower cellTower : position.getNetwork().getCellTowers()) {
            checkInteger(cellTower.getMobileCountryCode(), 0, 999);
            checkInteger(cellTower.getMobileNetworkCode(), 0, 999);
            checkInteger(cellTower.getLocationAreaCode(), 1, 65535);
            checkInteger(cellTower.getCellId(), 0, 268435455);
        }
    }
}
Also used : Position(org.traccar.model.Position) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) CellTower(org.traccar.model.CellTower) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 5 with CellTower

use of org.traccar.model.CellTower in project traccar by traccar.

the class Jt600ProtocolDecoder method decodeU01.

private Position decodeU01(String sentence, Channel channel, SocketAddress remoteAddress) {
    Parser parser = new Parser(PATTERN_U01, sentence);
    if (!parser.matches()) {
        return null;
    }
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    String type = parser.next();
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS));
    position.setValid(parser.next().equals("T"));
    position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM));
    position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM));
    position.setSpeed(UnitsConverter.knotsFromMph(parser.nextDouble(0)));
    position.setCourse(parser.nextDouble(0));
    position.set(Position.KEY_SATELLITES, parser.nextInt(0));
    position.set(Position.KEY_BATTERY_LEVEL, parser.nextInt(0));
    position.set(Position.KEY_STATUS, parser.nextBinInt(0));
    CellTower cellTower = CellTower.fromCidLac(parser.nextInt(0), parser.nextInt(0));
    cellTower.setSignalStrength(parser.nextInt(0));
    position.setNetwork(new Network(cellTower));
    position.set(Position.KEY_ODOMETER, parser.nextLong(0) * 1000);
    position.set(Position.KEY_INDEX, parser.nextInt(0));
    if (channel != null) {
        if (type.equals("U01") || type.equals("U02") || type.equals("U03")) {
            channel.write("(S39)");
        } else if (type.equals("U06")) {
            channel.write("(S20)");
        }
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) CellTower(org.traccar.model.CellTower) Network(org.traccar.model.Network) Parser(org.traccar.helper.Parser)

Aggregations

CellTower (org.traccar.model.CellTower)29 Network (org.traccar.model.Network)25 Position (org.traccar.model.Position)16 DeviceSession (org.traccar.DeviceSession)10 Date (java.util.Date)7 DateBuilder (org.traccar.helper.DateBuilder)6 Parser (org.traccar.helper.Parser)4 DateFormat (java.text.DateFormat)3 SimpleDateFormat (java.text.SimpleDateFormat)3 LinkedList (java.util.LinkedList)3 JsonObject (javax.json.JsonObject)3 NetworkMessage (org.traccar.NetworkMessage)3 BitBuffer (org.traccar.helper.BitBuffer)3 AsyncCompletionHandler (com.ning.http.client.AsyncCompletionHandler)1 Response (com.ning.http.client.Response)1 ByteBuf (io.netty.buffer.ByteBuf)1 StringReader (java.io.StringReader)1 JsonReader (javax.json.JsonReader)1 WifiAccessPoint (org.traccar.model.WifiAccessPoint)1