Search in sources :

Example 46 with Position

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

the class ComputedAttributesTest method testComputedAttributes.

@Test
public void testComputedAttributes() {
    Position position = new Position();
    ComputedAttributesHandler computedAttributesHandler = new ComputedAttributesHandler();
    Date date = new Date();
    position.setTime(date);
    position.setSpeed(42);
    position.setValid(false);
    position.set("adc1", 128);
    position.set("booleanFlag", true);
    position.set("adc2", 100);
    position.set("bitFlag", 7);
    position.set("event", 42);
    position.set("result", "success");
    Attribute attribute = new Attribute();
    attribute.setExpression("adc1");
    assertEquals(128, computedAttributesHandler.computeAttribute(attribute, position));
    attribute.setExpression("!booleanFlag");
    assertEquals(false, computedAttributesHandler.computeAttribute(attribute, position));
    attribute.setExpression("adc2 * 2 + 50");
    assertEquals(250, computedAttributesHandler.computeAttribute(attribute, position));
    attribute.setExpression("(bitFlag & 4) != 0");
    assertEquals(true, computedAttributesHandler.computeAttribute(attribute, position));
    attribute.setExpression("if (event == 42) \"lowBattery\"");
    assertEquals("lowBattery", computedAttributesHandler.computeAttribute(attribute, position));
    attribute.setExpression("speed > 5 && valid");
    assertEquals(false, computedAttributesHandler.computeAttribute(attribute, position));
    attribute.setExpression("fixTime");
    assertEquals(date, computedAttributesHandler.computeAttribute(attribute, position));
    attribute.setExpression("math:pow(adc1, 2)");
    assertEquals(16384.0, computedAttributesHandler.computeAttribute(attribute, position));
    // modification tests
    attribute.setExpression("adc1 = 256");
    computedAttributesHandler.computeAttribute(attribute, position);
    assertEquals(128, position.getInteger("adc1"));
    attribute.setExpression("result = \"fail\"");
    computedAttributesHandler.computeAttribute(attribute, position);
    assertEquals("success", position.getString("result"));
    attribute.setExpression("fixTime = \"2017-10-18 10:00:01\"");
    computedAttributesHandler.computeAttribute(attribute, position);
    assertEquals(date, position.getFixTime());
}
Also used : Position(org.traccar.model.Position) Attribute(org.traccar.model.Attribute) Date(java.util.Date) Test(org.junit.Test)

Example 47 with Position

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

the class ManPowerProtocolDecoder 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.set(Position.KEY_STATUS, parser.next());
    position.setTime(parser.nextDateTime());
    position.setValid(parser.next().equals("A"));
    position.setLatitude(parser.nextCoordinate());
    position.setLongitude(parser.nextCoordinate());
    position.setSpeed(parser.nextDouble(0));
    return position;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) Parser(org.traccar.helper.Parser)

Example 48 with Position

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

the class MeiligaoProtocolDecoder method decodeRetransmission.

private List<Position> decodeRetransmission(ChannelBuffer buf, DeviceSession deviceSession) {
    List<Position> positions = new LinkedList<>();
    int count = buf.readUnsignedByte();
    for (int i = 0; i < count; i++) {
        // alarm
        buf.readUnsignedByte();
        int endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '\\');
        if (endIndex < 0) {
            endIndex = buf.writerIndex() - 4;
        }
        String sentence = buf.readBytes(endIndex - buf.readerIndex()).toString(StandardCharsets.US_ASCII);
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        position = decodeRegular(position, sentence);
        if (position != null) {
            positions.add(position);
        }
        if (buf.readableBytes() > 4) {
            // delimiter
            buf.readUnsignedByte();
        }
    }
    return positions;
}
Also used : Position(org.traccar.model.Position) LinkedList(java.util.LinkedList)

Example 49 with Position

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

the class MeitrackProtocolDecoder method decodeBinaryE.

private List<Position> decodeBinaryE(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) {
    List<Position> positions = new LinkedList<>();
    buf.readerIndex(buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ',') + 1);
    String imei = buf.readBytes(15).toString(StandardCharsets.US_ASCII);
    buf.skipBytes(1 + 3 + 1);
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }
    // remaining cache
    buf.readUnsignedInt();
    int count = buf.readUnsignedShort();
    for (int i = 0; i < count; i++) {
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());
        // length
        buf.readUnsignedShort();
        // index
        buf.readUnsignedShort();
        int paramCount = buf.readUnsignedByte();
        for (int j = 0; j < paramCount; j++) {
            int id = buf.readUnsignedByte();
            switch(id) {
                case 0x01:
                    position.set(Position.KEY_EVENT, buf.readUnsignedByte());
                    break;
                case 0x05:
                    position.setValid(buf.readUnsignedByte() > 0);
                    break;
                case 0x06:
                    position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
                    break;
                case 0x07:
                    position.set(Position.KEY_RSSI, buf.readUnsignedByte());
                    break;
                default:
                    buf.readUnsignedByte();
                    break;
            }
        }
        paramCount = buf.readUnsignedByte();
        for (int j = 0; j < paramCount; j++) {
            int id = buf.readUnsignedByte();
            switch(id) {
                case 0x08:
                    position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort()));
                    break;
                case 0x09:
                    position.setCourse(buf.readUnsignedShort() * 0.1);
                    break;
                case 0x0B:
                    position.setAltitude(buf.readShort());
                    break;
                case 0x19:
                    position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.01);
                    break;
                case 0x1A:
                    position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.01);
                    break;
                default:
                    buf.readUnsignedShort();
                    break;
            }
        }
        paramCount = buf.readUnsignedByte();
        for (int j = 0; j < paramCount; j++) {
            int id = buf.readUnsignedByte();
            switch(id) {
                case 0x02:
                    position.setLatitude(buf.readInt() * 0.000001);
                    break;
                case 0x03:
                    position.setLongitude(buf.readInt() * 0.000001);
                    break;
                case 0x04:
                    // 2000-01-01
                    position.setTime(new Date((946684800 + buf.readUnsignedInt()) * 1000));
                    break;
                case 0x0D:
                    position.set("runtime", buf.readUnsignedInt());
                    break;
                default:
                    buf.readUnsignedInt();
                    break;
            }
        }
        paramCount = buf.readUnsignedByte();
        for (int j = 0; j < paramCount; j++) {
            // id
            buf.readUnsignedByte();
            // value
            buf.skipBytes(buf.readUnsignedByte());
        }
        positions.add(position);
    }
    return positions;
}
Also used : DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) LinkedList(java.util.LinkedList) Date(java.util.Date)

Example 50 with Position

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

the class NavigilProtocolDecoder method parsePositionReport.

private Position parsePositionReport(DeviceSession deviceSession, ChannelBuffer buf, int sequenceNumber, long timestamp) {
    Position position = new Position(getProtocolName());
    position.set(Position.KEY_INDEX, sequenceNumber);
    position.setDeviceId(deviceSession.getDeviceId());
    position.setTime(convertTimestamp(timestamp));
    position.setLatitude(buf.readMedium() * 0.00002);
    position.setLongitude(buf.readMedium() * 0.00002);
    position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
    position.setCourse(buf.readUnsignedByte() * 2);
    short flags = buf.readUnsignedByte();
    position.setValid((flags & 0x80) == 0x80 && (flags & 0x40) == 0x40);
    // reserved
    buf.readUnsignedByte();
    return position;
}
Also used : Position(org.traccar.model.Position)

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