Search in sources :

Example 11 with Sentence

use of net.sf.marineapi.nmea.sentence.Sentence in project marine-api by ktuukkan.

the class AbstractSentenceListener method sentenceRead.

/**
	 * <p>Resolves the type of each received sentence parser and passes it to
	 * <code>sentenceRead(T)</code> if the type matches the expected type
	 * <code>T</code>.</p>
	 * 
	 * <p>This method may be overridden, but be sure to call
	 * <code>super.sentencerRead(SentenceEvent)</code> before or after your
	 * additional event handling. However, for listeners that need to handle all
	 * incoming sentences, it's recommended to directly implement the 
	 * {@link net.sf.marineapi.nmea.event.SentenceListener} interface.</p>
	 *
	 * @see net.sf.marineapi.nmea.event.SentenceListener#sentenceRead(net.sf.marineapi.nmea.event.SentenceEvent)
	 */
@SuppressWarnings("unchecked")
public void sentenceRead(SentenceEvent event) {
    Sentence sentence = event.getSentence();
    Class<?>[] interfaces = sentence.getClass().getInterfaces();
    if (Arrays.asList(interfaces).contains(this.expectedType)) {
        sentenceRead((T) sentence);
    }
}
Also used : Sentence(net.sf.marineapi.nmea.sentence.Sentence)

Example 12 with Sentence

use of net.sf.marineapi.nmea.sentence.Sentence in project marine-api by ktuukkan.

the class AbstractDataReader method run.

/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Runnable#run()
	 */
public void run() {
    ActivityMonitor monitor = new ActivityMonitor(parent);
    SentenceFactory factory = SentenceFactory.getInstance();
    while (isRunning) {
        try {
            String data = read();
            if (data == null) {
                Thread.sleep(SLEEP_TIME);
            } else if (SentenceValidator.isValid(data)) {
                monitor.refresh();
                Sentence s = factory.createParser(data);
                parent.fireSentenceEvent(s);
            } else if (!SentenceValidator.isSentence(data)) {
                parent.fireDataEvent(data);
            }
        } catch (Exception e) {
            parent.handleException("Data read failed", e);
            try {
                Thread.sleep(SLEEP_TIME);
            } catch (InterruptedException interruptException) {
            }
        } finally {
            monitor.tick();
        }
    }
    monitor.reset();
    parent.fireReadingStopped();
}
Also used : SentenceFactory(net.sf.marineapi.nmea.parser.SentenceFactory) Sentence(net.sf.marineapi.nmea.sentence.Sentence)

Example 13 with Sentence

use of net.sf.marineapi.nmea.sentence.Sentence in project marine-api by ktuukkan.

the class SentenceReaderTest method testFireSentenceEventWithExpectedType.

@Test
public void testFireSentenceEventWithExpectedType() {
    assertNull(sentence);
    SentenceFactory sf = SentenceFactory.getInstance();
    Sentence s = sf.createParser(GGATest.EXAMPLE);
    reader.fireSentenceEvent(s);
    assertEquals(s, sentence);
}
Also used : SentenceFactory(net.sf.marineapi.nmea.parser.SentenceFactory) Sentence(net.sf.marineapi.nmea.sentence.Sentence) GGATest(net.sf.marineapi.nmea.parser.GGATest) Test(org.junit.Test) BODTest(net.sf.marineapi.nmea.parser.BODTest)

Example 14 with Sentence

use of net.sf.marineapi.nmea.sentence.Sentence in project marine-api by ktuukkan.

the class PositionProvider method createProviderEvent.

/*
	 * (non-Javadoc)
	 * @see net.sf.marineapi.provider.AbstractProvider#createProviderEvent()
	 */
@Override
protected PositionEvent createProviderEvent() {
    Position p = null;
    Double sog = null;
    Double cog = null;
    Date d = null;
    Time t = null;
    FaaMode mode = null;
    GpsFixQuality fix = null;
    for (Sentence s : getSentences()) {
        if (s instanceof RMCSentence) {
            RMCSentence rmc = (RMCSentence) s;
            sog = rmc.getSpeed();
            try {
                cog = rmc.getCourse();
            } catch (DataNotAvailableException e) {
            // If we are not moving, cource can be undefined. Leave null in that case.
            }
            d = rmc.getDate();
            t = rmc.getTime();
            if (p == null) {
                p = rmc.getPosition();
                if (rmc.getFieldCount() > 11) {
                    mode = rmc.getMode();
                }
            }
        } else if (s instanceof VTGSentence) {
            VTGSentence vtg = (VTGSentence) s;
            sog = vtg.getSpeedKnots();
            try {
                cog = vtg.getTrueCourse();
            } catch (DataNotAvailableException e) {
            // If we are not moving, cource can be undefined. Leave null in that case.
            }
        } else if (s instanceof GGASentence) {
            // Using GGA as primary position source as it contains both
            // position and altitude
            GGASentence gga = (GGASentence) s;
            p = gga.getPosition();
            fix = gga.getFixQuality();
            // Some receivers do not provide RMC message
            if (t == null) {
                t = gga.getTime();
            }
        } else if (s instanceof GLLSentence && p == null) {
            GLLSentence gll = (GLLSentence) s;
            p = gll.getPosition();
        }
    }
    // Ag-Star reciever does not provide RMC sentence. So we have to guess what date it is
    if (d == null) {
        d = new Date();
    }
    return new PositionEvent(this, p, sog, cog, d, t, mode, fix);
}
Also used : GLLSentence(net.sf.marineapi.nmea.sentence.GLLSentence) Position(net.sf.marineapi.nmea.util.Position) Time(net.sf.marineapi.nmea.util.Time) VTGSentence(net.sf.marineapi.nmea.sentence.VTGSentence) Date(net.sf.marineapi.nmea.util.Date) GGASentence(net.sf.marineapi.nmea.sentence.GGASentence) PositionEvent(net.sf.marineapi.provider.event.PositionEvent) RMCSentence(net.sf.marineapi.nmea.sentence.RMCSentence) DataNotAvailableException(net.sf.marineapi.nmea.parser.DataNotAvailableException) FaaMode(net.sf.marineapi.nmea.util.FaaMode) GpsFixQuality(net.sf.marineapi.nmea.util.GpsFixQuality) VTGSentence(net.sf.marineapi.nmea.sentence.VTGSentence) GLLSentence(net.sf.marineapi.nmea.sentence.GLLSentence) GGASentence(net.sf.marineapi.nmea.sentence.GGASentence) RMCSentence(net.sf.marineapi.nmea.sentence.RMCSentence) Sentence(net.sf.marineapi.nmea.sentence.Sentence)

Example 15 with Sentence

use of net.sf.marineapi.nmea.sentence.Sentence in project marine-api by ktuukkan.

the class SatelliteInfoProvider method isReady.

/*
	 * (non-Javadoc)
	 * @see net.sf.marineapi.provider.AbstractProvider#isReady()
	 */
@Override
protected boolean isReady() {
    boolean hasFirstGSV = false;
    boolean hasLastGSV = false;
    boolean hasAllGSV = false;
    int count = 0;
    for (Sentence s : getSentences()) {
        if ("GSV".equals(s.getSentenceId())) {
            GSVSentence gsv = (GSVSentence) s;
            if (!hasFirstGSV) {
                hasFirstGSV = gsv.isFirst();
            }
            if (!hasLastGSV) {
                hasLastGSV = gsv.isLast();
            }
            hasAllGSV = (gsv.getSentenceCount() == ++count);
        }
    }
    return hasOne("GSA") && hasAllGSV && hasFirstGSV && hasLastGSV;
}
Also used : GSVSentence(net.sf.marineapi.nmea.sentence.GSVSentence) GSASentence(net.sf.marineapi.nmea.sentence.GSASentence) GSVSentence(net.sf.marineapi.nmea.sentence.GSVSentence) Sentence(net.sf.marineapi.nmea.sentence.Sentence)

Aggregations

Sentence (net.sf.marineapi.nmea.sentence.Sentence)27 Test (org.junit.Test)20 FOOSentence (net.sf.marineapi.test.util.FOOSentence)10 BODSentence (net.sf.marineapi.nmea.sentence.BODSentence)8 GGATest (net.sf.marineapi.nmea.parser.GGATest)7 SentenceEvent (net.sf.marineapi.nmea.event.SentenceEvent)6 SentenceFactory (net.sf.marineapi.nmea.parser.SentenceFactory)6 BODTest (net.sf.marineapi.nmea.parser.BODTest)4 GLLTest (net.sf.marineapi.nmea.parser.GLLTest)3 HDGTest (net.sf.marineapi.nmea.parser.HDGTest)3 HDMTest (net.sf.marineapi.nmea.parser.HDMTest)3 HDTTest (net.sf.marineapi.nmea.parser.HDTTest)3 RMCTest (net.sf.marineapi.nmea.parser.RMCTest)3 FOOParser (net.sf.marineapi.test.util.FOOParser)3 GGASentence (net.sf.marineapi.nmea.sentence.GGASentence)2 GLLSentence (net.sf.marineapi.nmea.sentence.GLLSentence)2 GSASentence (net.sf.marineapi.nmea.sentence.GSASentence)2 GSVSentence (net.sf.marineapi.nmea.sentence.GSVSentence)2 RMCSentence (net.sf.marineapi.nmea.sentence.RMCSentence)2 SentenceId (net.sf.marineapi.nmea.sentence.SentenceId)2