Search in sources :

Example 1 with Timestamped

use of au.gov.amsa.ais.Timestamped in project risky by amsa-code.

the class ShipStaticDataCreator method writeStaticDataToFileWithTimestamps.

public static Observable<Timestamped<AisShipStatic>> writeStaticDataToFileWithTimestamps(List<File> files, File outputFile, Scheduler scheduler) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    Func0<PrintStream> resourceFactory = Checked.f0(() -> new PrintStream(outputFile));
    Func1<PrintStream, Observable<Timestamped<AisShipStatic>>> observableFactory = out -> Observable.from(files).buffer(Math.max(1, files.size() / Runtime.getRuntime().availableProcessors() - 1)).flatMap(list -> // 
    Observable.from(list).lift(// 
    Logging.<File>logger().showValue().showMemory().log()).concatMap(// 
    file -> timestampedShipStatics(file))).groupBy(// 
    m -> m.message().getMmsi()).flatMap(// 
    g -> collect(g).subscribeOn(scheduler)).compose(Transformers.doOnFirst(x -> {
        out.println("# MMSI, Time, IMO, AisClass, AisShipType, MaxPresentStaticDraughtMetres, DimAMetres, DimBMetres, DimCMetres, DimDMetres, LengthMetres, WidthMetres, Name");
        out.println("# columns are tab delimited");
        out.println("# -1 = not present");
    })).filter(// 
    set -> set.size() <= 10).flatMapIterable(// 
    set -> set).doOnNext(k -> {
        AisShipStatic m = k.message();
        out.format("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", m.getMmsi(), sdf.format(new Date(k.time())), getImo(m).orElse(-1), m instanceof AisShipStaticA ? "A" : "B", m.getShipType(), getMaximumPresentStaticDraughtMetres(m).orElse(-1F), m.getDimensionA().orElse(-1), m.getDimensionB().orElse(-1), m.getDimensionC().orElse(-1), m.getDimensionD().orElse(-1), AisShipStaticUtil.lengthMetres(m).orElse(-1), AisShipStaticUtil.widthMetres(m).orElse(-1), prepareName(m.getName()));
        out.flush();
    });
    Action1<PrintStream> disposeAction = out -> out.close();
    return Observable.using(resourceFactory, observableFactory, disposeAction);
}
Also used : Date(java.util.Date) Logging(com.github.davidmoten.rx.slf4j.Logging) AisShipStatic(au.gov.amsa.ais.message.AisShipStatic) SimpleDateFormat(java.text.SimpleDateFormat) Preconditions(com.github.davidmoten.guavamini.Preconditions) Action1(rx.functions.Action1) TreeSet(java.util.TreeSet) Observable(rx.Observable) Func0(rx.functions.Func0) Func1(rx.functions.Func1) AisShipStaticUtil(au.gov.amsa.ais.message.AisShipStaticUtil) Streams(au.gov.amsa.ais.rx.Streams) Schedulers(rx.schedulers.Schedulers) TimestampedAndLine(au.gov.amsa.ais.rx.Streams.TimestampedAndLine) PrintStream(java.io.PrintStream) Transformers(com.github.davidmoten.rx.Transformers) Iterator(java.util.Iterator) TimeZone(java.util.TimeZone) Checked(com.github.davidmoten.rx.Checked) AisMessage(au.gov.amsa.ais.AisMessage) Scheduler(rx.Scheduler) File(java.io.File) List(java.util.List) GroupedObservable(rx.observables.GroupedObservable) Optional(java.util.Optional) AisShipStaticA(au.gov.amsa.ais.message.AisShipStaticA) Timestamped(au.gov.amsa.ais.Timestamped) PrintStream(java.io.PrintStream) AisShipStatic(au.gov.amsa.ais.message.AisShipStatic) AisShipStaticA(au.gov.amsa.ais.message.AisShipStaticA) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File) Observable(rx.Observable) GroupedObservable(rx.observables.GroupedObservable) Date(java.util.Date)

Example 2 with Timestamped

use of au.gov.amsa.ais.Timestamped in project risky by amsa-code.

the class StreamsTest method stringRepresentationOfTimestampedAndLineContainsMessageInCaseOfSuccess.

@Test
public void stringRepresentationOfTimestampedAndLineContainsMessageInCaseOfSuccess() {
    final Timestamped<AisMessage> aisMessage = new Timestamped<>(new AisMessageOther(4242, "source", 0), 0);
    final TimestampedAndLine<AisMessage> message = new TimestampedAndLine<>(Optional.of(aisMessage), "test", null);
    final String representation = message.toString();
    assertNotNull(representation);
    assertTrue("String representation should contain message ID, but got: " + representation, representation.contains("4242"));
}
Also used : Timestamped(au.gov.amsa.ais.Timestamped) TimestampedAndLine(au.gov.amsa.ais.rx.Streams.TimestampedAndLine) AisMessage(au.gov.amsa.ais.AisMessage) AisMessageOther(au.gov.amsa.ais.message.AisMessageOther) Test(org.junit.Test)

Example 3 with Timestamped

use of au.gov.amsa.ais.Timestamped in project risky by amsa-code.

the class OperatorCraftProperty method call.

@Override
public Subscriber<? super Timestamped<? extends AisMessage>> call(final Subscriber<? super CraftProperty> child) {
    return new Subscriber<Timestamped<? extends AisMessage>>(child) {

        @Override
        public void onCompleted() {
            if (!isUnsubscribed())
                child.onCompleted();
        }

        @Override
        public void onError(Throwable e) {
            if (!isUnsubscribed())
                child.onError(e);
        }

        @SuppressWarnings("unchecked")
        @Override
        public void onNext(Timestamped<? extends AisMessage> m) {
            if (m.message() instanceof AisShipStaticA) {
                handleShipStatic((Timestamped<AisShipStaticA>) m, child);
            } else if (m.message() instanceof AisPositionBExtended)
                handleAisPositionBExtended((Timestamped<AisPositionBExtended>) m, child);
        }

        private void handleShipStatic(Timestamped<AisShipStaticA> m, Subscriber<? super CraftProperty> child) {
            handleProperty(child, m, CraftPropertyName.CALLSIGN, m.message().getCallsign());
            handleProperty(child, m, CraftPropertyName.DESTINATION, m.message().getDestination());
            handleProperty(child, m, CraftPropertyName.DIMENSION_A, m.message().getDimensionA());
            handleProperty(child, m, CraftPropertyName.DIMENSION_B, m.message().getDimensionB());
            handleProperty(child, m, CraftPropertyName.DIMENSION_C, m.message().getDimensionC());
            handleProperty(child, m, CraftPropertyName.DIMENSION_D, m.message().getDimensionD());
            handleProperty(child, m, CraftPropertyName.IMO_NUMBER, m.message().getImo());
            handleProperty(child, m, CraftPropertyName.LENGTH_METRES, m.message().getLengthMetres());
            handleProperty(child, m, CraftPropertyName.DRAUGHT_METRES, m.message().getMaximumPresentStaticDraughtMetres());
            handleProperty(child, m, CraftPropertyName.NAME, m.message().getName());
            handleProperty(child, m, CraftPropertyName.SHIP_TYPE, m.message().getShipType());
            handleProperty(child, m, CraftPropertyName.WIDTH_METRES, m.message().getWidthMetres());
        }

        private void handleAisPositionBExtended(Timestamped<AisPositionBExtended> m, Subscriber<? super CraftProperty> child) {
            handleProperty(child, m, CraftPropertyName.DIMENSION_A, m.message().getDimensionA());
            handleProperty(child, m, CraftPropertyName.DIMENSION_B, m.message().getDimensionB());
            handleProperty(child, m, CraftPropertyName.DIMENSION_C, m.message().getDimensionC());
            handleProperty(child, m, CraftPropertyName.DIMENSION_D, m.message().getDimensionD());
            handleProperty(child, m, CraftPropertyName.LENGTH_METRES, m.message().getLengthMetres());
            handleProperty(child, m, CraftPropertyName.NAME, m.message().getName());
            handleProperty(child, m, CraftPropertyName.SHIP_TYPE, m.message().getShipType());
            handleProperty(child, m, CraftPropertyName.WIDTH_METRES, m.message().getWidthMetres());
        }

        private <R extends AisMessage & HasMmsi> void handleProperty(Subscriber<? super CraftProperty> child, Timestamped<R> m, CraftPropertyName name, Object value) {
            if (!isUnsubscribed() && value != null)
                child.onNext(new CraftProperty(new Mmsi(m.message().getMmsi()), name, value.toString(), m.time()));
        }
    };
}
Also used : HasMmsi(au.gov.amsa.ais.HasMmsi) Mmsi(au.gov.amsa.ais.rx.Mmsi) Subscriber(rx.Subscriber) Timestamped(au.gov.amsa.ais.Timestamped) CraftPropertyName(au.gov.amsa.ais.rx.CraftPropertyName) AisShipStaticA(au.gov.amsa.ais.message.AisShipStaticA) AisMessage(au.gov.amsa.ais.AisMessage) AisPositionBExtended(au.gov.amsa.ais.message.AisPositionBExtended) CraftProperty(au.gov.amsa.ais.rx.CraftProperty) HasMmsi(au.gov.amsa.ais.HasMmsi)

Aggregations

AisMessage (au.gov.amsa.ais.AisMessage)3 Timestamped (au.gov.amsa.ais.Timestamped)3 AisShipStaticA (au.gov.amsa.ais.message.AisShipStaticA)2 TimestampedAndLine (au.gov.amsa.ais.rx.Streams.TimestampedAndLine)2 HasMmsi (au.gov.amsa.ais.HasMmsi)1 AisMessageOther (au.gov.amsa.ais.message.AisMessageOther)1 AisPositionBExtended (au.gov.amsa.ais.message.AisPositionBExtended)1 AisShipStatic (au.gov.amsa.ais.message.AisShipStatic)1 AisShipStaticUtil (au.gov.amsa.ais.message.AisShipStaticUtil)1 CraftProperty (au.gov.amsa.ais.rx.CraftProperty)1 CraftPropertyName (au.gov.amsa.ais.rx.CraftPropertyName)1 Mmsi (au.gov.amsa.ais.rx.Mmsi)1 Streams (au.gov.amsa.ais.rx.Streams)1 Preconditions (com.github.davidmoten.guavamini.Preconditions)1 Checked (com.github.davidmoten.rx.Checked)1 Transformers (com.github.davidmoten.rx.Transformers)1 Logging (com.github.davidmoten.rx.slf4j.Logging)1 File (java.io.File)1 PrintStream (java.io.PrintStream)1 SimpleDateFormat (java.text.SimpleDateFormat)1