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);
}
}
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();
}
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);
}
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);
}
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;
}
Aggregations