Search in sources :

Example 81 with Position

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

the class IgnitionEventHandler method analyzePosition.

@Override
protected Map<Event, Position> analyzePosition(Position position) {
    Device device = Context.getIdentityManager().getById(position.getDeviceId());
    if (device == null || !Context.getIdentityManager().isLatestPosition(position)) {
        return null;
    }
    Map<Event, Position> result = null;
    if (position.getAttributes().containsKey(Position.KEY_IGNITION)) {
        boolean ignition = position.getBoolean(Position.KEY_IGNITION);
        Position lastPosition = Context.getIdentityManager().getLastPosition(position.getDeviceId());
        if (lastPosition != null && lastPosition.getAttributes().containsKey(Position.KEY_IGNITION)) {
            boolean oldIgnition = lastPosition.getBoolean(Position.KEY_IGNITION);
            if (ignition && !oldIgnition) {
                result = Collections.singletonMap(new Event(Event.TYPE_IGNITION_ON, position.getDeviceId(), position.getId()), position);
            } else if (!ignition && oldIgnition) {
                result = Collections.singletonMap(new Event(Event.TYPE_IGNITION_OFF, position.getDeviceId(), position.getId()), position);
            }
        }
    }
    return result;
}
Also used : Position(org.traccar.model.Position) Device(org.traccar.model.Device) Event(org.traccar.model.Event)

Example 82 with Position

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

the class Xt013ProtocolDecoder 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.setTime(parser.nextDateTime());
    position.setLatitude(parser.nextDouble(0));
    position.setLongitude(parser.nextDouble(0));
    position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0)));
    position.setCourse(parser.nextDouble(0));
    position.setAltitude(parser.nextDouble(0));
    position.setValid(parser.next().equals("F"));
    position.set(Position.KEY_SATELLITES, parser.nextInt());
    position.set(Position.KEY_RSSI, parser.nextDouble());
    position.set(Position.KEY_BATTERY, parser.nextDouble(0));
    position.set(Position.KEY_CHARGE, parser.next().equals("1"));
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Parser(org.traccar.helper.Parser)

Example 83 with Position

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

the class Xt2400ProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    ChannelBuffer buf = (ChannelBuffer) msg;
    byte[] format = null;
    if (formats.size() > 1) {
        format = formats.get(buf.getUnsignedByte(buf.readerIndex()));
    } else if (!formats.isEmpty()) {
        format = formats.values().iterator().next();
    }
    if (format == null) {
        return null;
    }
    Position position = new Position(getProtocolName());
    for (byte tag : format) {
        switch(tag) {
            case 0x03:
                DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(buf.readUnsignedInt()));
                if (deviceSession == null) {
                    return null;
                }
                position.setDeviceId(deviceSession.getDeviceId());
                break;
            case 0x04:
                position.set(Position.KEY_EVENT, buf.readUnsignedByte());
                break;
            case 0x05:
                position.set(Position.KEY_INDEX, buf.readUnsignedShort());
                break;
            case 0x06:
                position.setTime(new Date(buf.readUnsignedInt() * 1000));
                break;
            case 0x07:
                position.setLatitude(buf.readInt() * 0.000001);
                break;
            case 0x08:
                position.setLongitude(buf.readInt() * 0.000001);
                break;
            case 0x09:
                position.setAltitude(buf.readShort() * 0.1);
                break;
            case 0x0a:
                position.setCourse(buf.readShort() * 0.1);
                break;
            case 0x0b:
                position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
                break;
            case 0x10:
                position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedInt());
                break;
            case 0x12:
                position.set(Position.KEY_HDOP, buf.readUnsignedByte() * 0.1);
                break;
            case 0x13:
                position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
                break;
            case 0x14:
                position.set(Position.KEY_RSSI, buf.readShort());
                break;
            case 0x16:
                position.set(Position.KEY_BATTERY, buf.readUnsignedByte() * 0.1);
                break;
            case 0x17:
                position.set(Position.KEY_POWER, buf.readUnsignedByte() * 0.1);
                break;
            case 0x57:
                position.set(Position.KEY_OBD_SPEED, UnitsConverter.knotsFromKph(buf.readUnsignedShort()));
                break;
            case 0x65:
                position.set(Position.KEY_VIN, buf.readBytes(17).toString(StandardCharsets.US_ASCII));
                break;
            case 0x73:
                position.set(Position.KEY_VERSION_FW, buf.readBytes(16).toString(StandardCharsets.US_ASCII).trim());
                break;
            default:
                buf.skipBytes(getTagLength(tag));
                break;
        }
    }
    if (position.getLatitude() != 0 && position.getLongitude() != 0) {
        position.setValid(true);
    } else {
        getLastLocation(position, position.getDeviceTime());
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Date(java.util.Date) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 84 with Position

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

the class YwtProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    String sentence = (String) msg;
    // Synchronization
    if (sentence.startsWith("%SN") && channel != null) {
        int start = sentence.indexOf(':');
        int end = start;
        for (int i = 0; i < 4; i++) {
            end = sentence.indexOf(',', end + 1);
        }
        if (end == -1) {
            end = sentence.length();
        }
        channel.write("%AT+SN=" + sentence.substring(start, end));
        return null;
    }
    Parser parser = new Parser(PATTERN, sentence);
    if (!parser.matches()) {
        return null;
    }
    Position position = new Position(getProtocolName());
    String type = parser.next();
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
    if (deviceSession == null) {
        return null;
    }
    position.setDeviceId(deviceSession.getDeviceId());
    position.setTime(parser.nextDateTime());
    position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG));
    position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG));
    position.setAltitude(parser.nextDouble(0));
    position.setSpeed(parser.nextDouble(0));
    position.setCourse(parser.nextDouble(0));
    int satellites = parser.nextInt(0);
    position.setValid(satellites >= 3);
    position.set(Position.KEY_SATELLITES, satellites);
    String reportId = parser.next();
    position.set(Position.KEY_STATUS, parser.next());
    // Send response
    if ((type.equals("KP") || type.equals("EP")) && channel != null) {
        channel.write("%AT+" + type + "=" + reportId + "\r\n");
    }
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Parser(org.traccar.helper.Parser)

Example 85 with Position

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

the class ReportUtils method calculateStop.

private static StopReport calculateStop(ArrayList<Position> positions, int startIndex, int endIndex) {
    Position startStop = positions.get(startIndex);
    Position endStop = positions.get(endIndex);
    StopReport stop = new StopReport();
    long deviceId = startStop.getDeviceId();
    stop.setDeviceId(deviceId);
    stop.setDeviceName(Context.getIdentityManager().getById(deviceId).getName());
    stop.setPositionId(startStop.getId());
    stop.setLatitude(startStop.getLatitude());
    stop.setLongitude(startStop.getLongitude());
    stop.setStartTime(startStop.getFixTime());
    String address = startStop.getAddress();
    if (address == null && Context.getGeocoder() != null && Context.getConfig().getBoolean("geocoder.onRequest")) {
        address = Context.getGeocoder().getAddress(stop.getLatitude(), stop.getLongitude(), null);
    }
    stop.setAddress(address);
    stop.setEndTime(endStop.getFixTime());
    long stopDuration = endStop.getFixTime().getTime() - startStop.getFixTime().getTime();
    stop.setDuration(stopDuration);
    stop.setSpentFuel(calculateFuel(startStop, endStop));
    long engineHours = 0;
    for (int i = startIndex + 1; i <= endIndex; i++) {
        if (positions.get(i).getBoolean(Position.KEY_IGNITION) && positions.get(i - 1).getBoolean(Position.KEY_IGNITION)) {
            engineHours += positions.get(i).getFixTime().getTime() - positions.get(i - 1).getFixTime().getTime();
        }
    }
    stop.setEngineHours(engineHours);
    return stop;
}
Also used : Position(org.traccar.model.Position) StopReport(org.traccar.reports.model.StopReport)

Aggregations

Position (org.traccar.model.Position)831 DeviceSession (org.traccar.DeviceSession)505 Parser (org.traccar.helper.Parser)319 DateBuilder (org.traccar.helper.DateBuilder)179 Date (java.util.Date)154 Network (org.traccar.model.Network)143 LinkedList (java.util.LinkedList)112 ByteBuf (io.netty.buffer.ByteBuf)105 Event (org.traccar.model.Event)75 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)69 NetworkMessage (org.traccar.NetworkMessage)67 Test (org.junit.Test)56 WifiAccessPoint (org.traccar.model.WifiAccessPoint)54 BaseTest (org.traccar.BaseTest)40 SimpleDateFormat (java.text.SimpleDateFormat)38 Device (org.traccar.model.Device)29 DateFormat (java.text.DateFormat)27 DeviceState (org.traccar.model.DeviceState)27 List (java.util.List)25 TripsConfig (org.traccar.reports.model.TripsConfig)25